Skip to content
Myr

Color customization (#245)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Junegunn Choi · committed by Junegunn Choi · parent 446e822 · Browse files · 5 files+257 added−90 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+257 added−90 removed

  • other:+257 added−90 removed

5 files+257 added−90 removed

modified:man/man1/fzf.1+32 added-15 removed
89 unmodified lines
90.TP91.B "--no-mouse"92Disable mouse93.TP94.B "--color=COL"95Color scheme: [dark|light|16|bw]96.br97(default: dark on 256-color terminal, otherwise 16)98.br99.R ""100.br101.BR dark " Color scheme for dark 256-color terminal"102.br103.BR light " Color scheme for light 256-color terminal"104.br105.BR 16 " Color scheme for 16-color terminal"106.br107.BR bw " No colors"108.br94.BI "--color=" "[BASE_SCHEME][,COLOR:ANSI]"95Color configuration. The name of the base color scheme is followed by custom96color mappings. Ansi color code of -1 denotes terminal default97foreground/background color.9899.RS100e.g. \fBfzf --color=bg+:24\fR101 \fBfzf --color=light,fg:232,bg:255,bg+:116,info:27\fR102.RE103104.RS105.B BASE SCHEME:106 (default: dark on 256-color terminal, otherwise 16)107108 \fBdark \fRColor scheme for dark 256-color terminal109 \fBlight \fRColor scheme for light 256-color terminal110 \fB16 \fRColor scheme for 16-color terminal111 \fBbw \fRNo colors112113.B COLOR:114 \fBfg \fRText115 \fBbg \fRBackground116 \fBhl \fRHighlighted substrings117 \fBfg+ \fRText (current line)118 \fBbg+ \fRBackground (current line)119 \fBhl+ \fRHighlighted substrings (current line)120 \fBinfo \fRInfo121 \fBprompt \fRPrompt122 \fBpointer \fRPointer to the current line123 \fBmarker \fRMulti-select marker124 \fBspinner \fRStreaming input indicator125.RE126.TP127.B "--black"128Use black background129.TP
162 unmodified lines
modified:src/curses/curses.go+76 added-49 removed
104 unmodified lines
105 doubleClickDuration = 500 * time.Millisecond106)107108type ColorTheme struct {109 darkBg C.short110 prompt C.short111 match C.short112 current C.short113 currentMatch C.short114 spinner C.short115 info C.short116 cursor C.short117 selected C.short109 UseDefault bool110 Fg int16111 Bg int16112 DarkBg int16113 Prompt int16114 Match int16115 Current int16116 CurrentMatch int16117 Spinner int16118 Info int16119 Cursor int16120 Selected int16121}122123type Event struct {124 Type int
19 unmodified lines
144 _clickY []int145 Default16 *ColorTheme146 Dark256 *ColorTheme147 Light256 *ColorTheme145 DarkBG C.short148 FG int149 CurrentFG int150 BG int151 DarkBG int152)153154func init() {155 _prevDownTime = time.Unix(0, 0)156 _clickY = []int{}157 _colorMap = make(map[int]int)158 Default16 = &ColorTheme{159 UseDefault: true,160 Fg: 15,161 Bg: 0,153 darkBg: C.COLOR_BLACK,154 prompt: C.COLOR_BLUE,155 match: C.COLOR_GREEN,156 current: C.COLOR_YELLOW,157 currentMatch: C.COLOR_GREEN,158 spinner: C.COLOR_GREEN,159 info: C.COLOR_WHITE,160 cursor: C.COLOR_RED,161 selected: C.COLOR_MAGENTA}162 DarkBg: C.COLOR_BLACK,163 Prompt: C.COLOR_BLUE,164 Match: C.COLOR_GREEN,165 Current: C.COLOR_YELLOW,166 CurrentMatch: C.COLOR_GREEN,167 Spinner: C.COLOR_GREEN,168 Info: C.COLOR_WHITE,169 Cursor: C.COLOR_RED,170 Selected: C.COLOR_MAGENTA}171 Dark256 = &ColorTheme{172 UseDefault: true,173 Fg: 15,174 Bg: 0,163 darkBg: 236,164 prompt: 110,165 match: 108,166 current: 254,167 currentMatch: 151,168 spinner: 148,169 info: 144,170 cursor: 161,171 selected: 168}175 DarkBg: 236,176 Prompt: 110,177 Match: 108,178 Current: 254,179 CurrentMatch: 151,180 Spinner: 148,181 Info: 144,182 Cursor: 161,183 Selected: 168}184 Light256 = &ColorTheme{185 UseDefault: true,186 Fg: 15,187 Bg: 0,173 darkBg: 251,174 prompt: 25,175 match: 66,176 current: 237,177 currentMatch: 23,178 spinner: 65,179 info: 101,180 cursor: 161,181 selected: 168}188 DarkBg: 251,189 Prompt: 25,190 Match: 66,191 Current: 237,192 CurrentMatch: 23,193 Spinner: 65,194 Info: 101,195 Cursor: 161,196 Selected: 168}197}198199func attrColored(pair int, bold bool) C.int {200 var attr C.int
81 unmodified lines
282 }283}284285func initPairs(theme *ColorTheme, black bool) {271 var bg C.short286 fg := C.short(theme.Fg)287 bg := C.short(theme.Bg)288 if black {289 bg = C.COLOR_BLACK274 } else {275 C.use_default_colors()290 } else if theme.UseDefault {291 fg = -1292 bg = -1293 C.use_default_colors()294 }295 if theme.UseDefault {296 FG = -1297 BG = -1298 } else {299 FG = int(fg)300 BG = int(bg)301 C.assume_default_colors(C.int(theme.Fg), C.int(bg))302 }303279 DarkBG = theme.darkBg280 C.init_pair(ColPrompt, theme.prompt, bg)281 C.init_pair(ColMatch, theme.match, bg)282 C.init_pair(ColCurrent, theme.current, DarkBG)283 C.init_pair(ColCurrentMatch, theme.currentMatch, DarkBG)284 C.init_pair(ColSpinner, theme.spinner, bg)285 C.init_pair(ColInfo, theme.info, bg)286 C.init_pair(ColCursor, theme.cursor, DarkBG)287 C.init_pair(ColSelected, theme.selected, DarkBG)304 CurrentFG = int(theme.Current)305 DarkBG = int(theme.DarkBg)306 darkBG := C.short(DarkBG)307 C.init_pair(ColPrompt, C.short(theme.Prompt), bg)308 C.init_pair(ColMatch, C.short(theme.Match), bg)309 C.init_pair(ColCurrent, C.short(theme.Current), darkBG)310 C.init_pair(ColCurrentMatch, C.short(theme.CurrentMatch), darkBG)311 C.init_pair(ColSpinner, C.short(theme.Spinner), bg)312 C.init_pair(ColInfo, C.short(theme.Info), bg)313 C.init_pair(ColCursor, C.short(theme.Cursor), darkBG)314 C.init_pair(ColSelected, C.short(theme.Selected), darkBG)315}316317func Close() {318 C.endwin()
228 unmodified lines
modified:src/item.go+15 added-3 removed
141 unmodified lines
142 offsets = append(offsets, colorOffset{143 offset: Offset{int32(start), int32(idx)}, color: color, bold: bold})144 } else {145 ansi := item.colors[curr-1]146 fg := ansi.color.fg147 if fg == -1 {148 if current {149 fg = curses.CurrentFG150 } else {151 fg = curses.FG152 }153 }154 bg := ansi.color.bg147 if current && bg == -1 {148 bg = int(curses.DarkBG)155 if bg == -1 {156 if current {157 bg = curses.DarkBG158 } else {159 bg = curses.BG160 }161 }162 offsets = append(offsets, colorOffset{163 offset: Offset{int32(start), int32(idx)},152 color: curses.PairFor(ansi.color.fg, bg),164 color: curses.PairFor(fg, bg),165 bold: ansi.color.bold || bold})166 }167 }168 }
79 unmodified lines
modified:src/options_test.go+46 added
153 unmodified lines
154 t.Errorf("toggleSort set")155 }156 check(actAbort, keymap[curses.F1])157}158159func TestColorSpec(t *testing.T) {160 theme := curses.Dark256161 dark := parseTheme(theme, "dark")162 if *dark != *theme {163 t.Errorf("colors should be equivalent")164 }165 if dark == theme {166 t.Errorf("point should not be equivalent")167 }168169 light := parseTheme(theme, "dark,light")170 if *light == *theme {171 t.Errorf("should not be equivalent")172 }173 if *light != *curses.Light256 {174 t.Errorf("colors should be equivalent")175 }176 if light == theme {177 t.Errorf("point should not be equivalent")178 }179180 customized := parseTheme(theme, "fg:231,bg:232")181 if customized.Fg != 231 || customized.Bg != 232 {182 t.Errorf("color not customized")183 }184 if *curses.Dark256 == *customized {185 t.Errorf("colors should not be equivalent")186 }187 customized.Fg = curses.Dark256.Fg188 customized.Bg = curses.Dark256.Bg189 if *curses.Dark256 == *customized {190 t.Errorf("colors should now be equivalent")191 }192193 customized = parseTheme(theme, "fg:231,dark,bg:232")194 if customized.Fg != curses.Dark256.Fg || customized.Bg == curses.Dark256.Bg {195 t.Errorf("color not customized")196 }197 if customized.UseDefault {198 t.Errorf("not using default colors")199 }200 if !curses.Dark256.UseDefault {201 t.Errorf("using default colors")202 }203}
modified:src/options.go+88 added-23 removed
2 unmodified lines
3import (4 "fmt"5 "os"6 "regexp"7 "strconv"8 "strings"9 "unicode/utf8"1011 "github.com/junegunn/fzf/src/curses"
23 unmodified lines
35 Interface36 -m, --multi Enable multi-select with tab/shift-tab37 --ansi Enable processing of ANSI color codes38 --no-mouse Disable mouse38 --color=COL Color scheme; [dark|light|16|bw]39 (default: dark on 256-color terminal, otherwise 16)39 --color=COLSPEC Base scheme (dark|light|16|bw) and/or custom colors40 --black Use black background41 --reverse Reverse orientation42 --no-hscroll Disable horizontal scroll43 --inline-info Display finder info inline with the query
76 unmodified lines
120 Sync bool121 Version bool122}123124func defaultOptions() *Options {125 var defaultTheme *curses.ColorTheme124func defaultTheme() *curses.ColorTheme {125 if strings.Contains(os.Getenv("TERM"), "256") {127 defaultTheme = curses.Dark256128 } else {129 defaultTheme = curses.Default16126 return curses.Dark256127 }128 return curses.Default16129}130131func defaultOptions() *Options {132 return &Options{133 Mode: ModeFuzzy,134 Case: CaseSmart,135 Nth: make([]Range, 0),
4 unmodified lines
140 Tiebreak: byLength,141 Multi: false,142 Ansi: false,143 Mouse: true,144 Theme: defaultTheme,144 Theme: defaultTheme(),145 Black: false,146 Reverse: false,147 Hscroll: true,148 InlineInfo: false,
37 unmodified lines
186 }187 return args[*i]188}189190func optionalNextString(args []string, i *int) string {191 if len(args) > *i+1 {192 *i++193 return args[*i]194 }195 return ""196}197198func optionalNumeric(args []string, i *int) int {199 if len(args) > *i+1 {200 if strings.IndexAny(args[*i+1], "0123456789") == 0 {201 *i++
82 unmodified lines
284 }285 return byLength286}287280func parseTheme(str string) *curses.ColorTheme {281 switch strings.ToLower(str) {282 case "dark":283 return curses.Dark256284 case "light":285 return curses.Light256286 case "16":287 return curses.Default16288 case "bw", "no":289 return nil290 default:291 errorExit("invalid color scheme: " + str)288func dupeTheme(theme *curses.ColorTheme) *curses.ColorTheme {289 dupe := *theme290 return &dupe291}292293func parseTheme(defaultTheme *curses.ColorTheme, str string) *curses.ColorTheme {294 theme := dupeTheme(defaultTheme)295 for _, str := range strings.Split(strings.ToLower(str), ",") {296 switch str {297 case "dark":298 theme = dupeTheme(curses.Dark256)299 case "light":300 theme = dupeTheme(curses.Light256)301 case "16":302 theme = dupeTheme(curses.Default16)303 case "bw", "no":304 theme = nil305 default:306 fail := func() {307 errorExit("invalid color specification: " + str)308 }309 // Color is disabled310 if theme == nil {311 errorExit("colors disabled; cannot customize colors")312 }313314 pair := strings.Split(str, ":")315 if len(pair) != 2 {316 fail()317 }318 ansi32, err := strconv.Atoi(pair[1])319 if err != nil || ansi32 < -1 || ansi32 > 255 {320 fail()321 }322 ansi := int16(ansi32)323 switch pair[0] {324 case "fg":325 theme.Fg = ansi326 theme.UseDefault = theme.UseDefault && ansi < 0327 case "bg":328 theme.Bg = ansi329 theme.UseDefault = theme.UseDefault && ansi < 0330 case "fg+":331 theme.Current = ansi332 case "bg+":333 theme.DarkBg = ansi334 case "hl":335 theme.Match = ansi336 case "hl+":337 theme.CurrentMatch = ansi338 case "prompt":339 theme.Prompt = ansi340 case "spinner":341 theme.Spinner = ansi342 case "info":343 theme.Info = ansi344 case "pointer":345 theme.Cursor = ansi346 case "marker":347 theme.Selected = ansi348 default:349 fail()350 }351 }352 }293 return nil353 return theme354}355356func parseKeymap(keymap map[int]actionType, toggleSort bool, str string) (map[int]actionType, bool) {357 for _, pairStr := range strings.Split(str, ",") {
101 unmodified lines
459 opts.Tiebreak = parseTiebreak(nextString(allArgs, &i, "sort criterion required"))460 case "--bind":461 opts.Keymap, opts.ToggleSort = parseKeymap(opts.Keymap, opts.ToggleSort, nextString(allArgs, &i, "bind expression required"))462 case "--color":403 opts.Theme = parseTheme(nextString(allArgs, &i, "color scheme name required"))463 spec := optionalNextString(allArgs, &i)464 if len(spec) == 0 {465 opts.Theme = defaultTheme()466 } else {467 opts.Theme = parseTheme(opts.Theme, spec)468 }469 case "--toggle-sort":470 opts.Keymap = checkToggleSort(opts.Keymap, nextString(allArgs, &i, "key name required"))471 opts.ToggleSort = true472 case "-d", "--delimiter":
88 unmodified lines
561 opts.Expect = parseKeyChords(value, "key names required")562 } else if match, value := optString(arg, "--tiebreak="); match {563 opts.Tiebreak = parseTiebreak(value)564 } else if match, value := optString(arg, "--color="); match {500 opts.Theme = parseTheme(value)565 opts.Theme = parseTheme(opts.Theme, value)566 } else if match, value := optString(arg, "--bind="); match {567 opts.Keymap, opts.ToggleSort = parseKeymap(opts.Keymap, opts.ToggleSort, value)568 } else {569 errorExit("unknown option: " + arg)
28 unmodified lines