Add `--color=[dark|light|16|bw]` option
Before adoption: this commit came in with the history the project adopted, so no landing records it.
- dark: the current default for 256-color terminal - light: color scheme for 256-color terminal with light background - 16: the default color scheme for 16-color terminal (`+2`) - bw: no colors (`+c`)
authored by Junegunn Choi · committed by Junegunn Choi · parent 2fe1e28 · Browse files · 7 files·+124 added−47 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+124 added−47 removed
- other:+124 added−47 removed
Provenance
- Commit
f66d94c- Tree
bf33ee2- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
2fe1e28
7 files changed+124 added−47 removed
modified:man/man1/fzf.1+15 added-5 removed
89 unmodified lines90 .TP91 .B "--no-mouse"92 Disable mouse93 .TP94-.B "+c, --no-color"95-Disable colors94+.B "--color=COL"95+Color scheme: [dark|light|16|bw]96+.br97+(default: dark on 256-color terminal, otherwise 16)98+.br99+.R ""100+.br101+.BR dark " Dark color scheme for 256-color terminal"102+.br103+.BR light " Light color scheme for 256-color terminal"104+.br105+.BR 16 " Default color scheme for 16-color terminal"106+.br107+.BR bw " No colors"108+.br109 .TP97-.B "+2, --no-256"98-Disable 256-color99-.TP110 .B "--black"111 Use black background112 .TP113 .B "--reverse"117 unmodified linesmodified:src/curses/curses.go+68 added-30 removed
93 unmodified lines94 const (95 doubleClickDuration = 500 * time.Millisecond96 )97 98+type ColorTheme struct {99+ darkBg C.short100+ prompt C.short101+ match C.short102+ current C.short103+ currentMatch C.short104+ spinner C.short105+ info C.short106+ cursor C.short107+ selected C.short108+}109+110 type Event struct {111 Type int112 Char rune113 MouseEvent *MouseEvent14 unmodified lines128 _color func(int, bool) C.int129 _colorMap map[int]int130 _prevDownTime time.Time131 _clickY []int132+ Default16 *ColorTheme133+ Dark256 *ColorTheme134+ Light256 *ColorTheme135 DarkBG C.short136 )137 138 func init() {139 _prevDownTime = time.Unix(0, 0)140 _clickY = []int{}141 _colorMap = make(map[int]int)142+ Default16 = &ColorTheme{143+ darkBg: C.COLOR_BLACK,144+ prompt: C.COLOR_BLUE,145+ match: C.COLOR_GREEN,146+ current: C.COLOR_YELLOW,147+ currentMatch: C.COLOR_GREEN,148+ spinner: C.COLOR_GREEN,149+ info: C.COLOR_WHITE,150+ cursor: C.COLOR_RED,151+ selected: C.COLOR_MAGENTA}152+ Dark256 = &ColorTheme{153+ darkBg: 236,154+ prompt: 110,155+ match: 108,156+ current: 254,157+ currentMatch: 151,158+ spinner: 148,159+ info: 144,160+ cursor: 161,161+ selected: 168}162+ Light256 = &ColorTheme{163+ darkBg: 251,164+ prompt: 25,165+ match: 66,166+ current: 237,167+ currentMatch: 23,168+ spinner: 65,169+ info: 101,170+ cursor: 161,171+ selected: 168}172 }173 174 func attrColored(pair int, bold bool) C.int {175 var attr C.int41 unmodified lines217 }218 return int(b[0])219 }220 176-func Init(color bool, color256 bool, black bool, mouse bool) {221+func Init(theme *ColorTheme, black bool, mouse bool) {222 {223 in, err := os.OpenFile("/dev/tty", syscall.O_RDONLY, 0)224 if err != nil {225 panic("Failed to open /dev/tty")21 unmodified lines247 Close()248 os.Exit(1)249 }()250 206- if color {251+ if theme != nil {252 C.start_color()208- var bg C.short209- if black {210- bg = C.COLOR_BLACK211- } else {212- C.use_default_colors()213- bg = -1214- }215- if color256 {216- DarkBG = 236217- C.init_pair(ColPrompt, 110, bg)218- C.init_pair(ColMatch, 108, bg)219- C.init_pair(ColCurrent, 254, DarkBG)220- C.init_pair(ColCurrentMatch, 151, DarkBG)221- C.init_pair(ColSpinner, 148, bg)222- C.init_pair(ColInfo, 144, bg)223- C.init_pair(ColCursor, 161, DarkBG)224- C.init_pair(ColSelected, 168, DarkBG)225- } else {226- DarkBG = C.COLOR_BLACK227- C.init_pair(ColPrompt, C.COLOR_BLUE, bg)228- C.init_pair(ColMatch, C.COLOR_GREEN, bg)229- C.init_pair(ColCurrent, C.COLOR_YELLOW, DarkBG)230- C.init_pair(ColCurrentMatch, C.COLOR_GREEN, DarkBG)231- C.init_pair(ColSpinner, C.COLOR_GREEN, bg)232- C.init_pair(ColInfo, C.COLOR_WHITE, bg)233- C.init_pair(ColCursor, C.COLOR_RED, DarkBG)234- C.init_pair(ColSelected, C.COLOR_MAGENTA, DarkBG)235- }253+ initPairs(theme, black)254 _color = attrColored255 } else {256 _color = attrMono257 }258 }259 260+func initPairs(theme *ColorTheme, black bool) {261+ var bg C.short262+ if black {263+ bg = C.COLOR_BLACK264+ } else {265+ C.use_default_colors()266+ bg = -1267+ }268+269+ DarkBG = theme.darkBg270+ C.init_pair(ColPrompt, theme.prompt, bg)271+ C.init_pair(ColMatch, theme.match, bg)272+ C.init_pair(ColCurrent, theme.current, DarkBG)273+ C.init_pair(ColCurrentMatch, theme.currentMatch, DarkBG)274+ C.init_pair(ColSpinner, theme.spinner, bg)275+ C.init_pair(ColInfo, theme.info, bg)276+ C.init_pair(ColCursor, theme.cursor, DarkBG)277+ C.init_pair(ColSelected, theme.selected, DarkBG)278+}279+280 func Close() {281 C.endwin()282 C.swapOutput()283 }226 unmodified linesmodified:src/core.go+1 added-1 removed
66 unmodified lines67 // By default, we do nothing68 return data, nil69 }70 if opts.Ansi {71- if opts.Color {71+ if opts.Theme != nil {72 ansiProcessor = func(data *string) (*string, []ansiOffset) {73 return extractColor(data)74 }75 } else {172 unmodified linesmodified:src/options.go+33 added-8 removed
33 unmodified lines34 Interface35 -m, --multi Enable multi-select with tab/shift-tab36 --ansi Enable processing of ANSI color codes37 --no-mouse Disable mouse38- +c, --no-color Disable colors39- +2, --no-256 Disable 256-color38+ --color=COL Color scheme [dark|light|16|bw]39+ (default: dark on 256-color terminal, otherwise 16)40 --black Use black background41 --reverse Reverse orientation42 --no-hscroll Disable horizontal scroll43 --prompt=STR Input prompt (default: '> ')56 unmodified lines100 Tiebreak tiebreak101 Multi bool102 Ansi bool103 Mouse bool104- Color bool105- Color256 bool104+ Theme *curses.ColorTheme105 Black bool106 Reverse bool107 Hscroll bool108 Prompt string8 unmodified lines117 Version bool118 }119 120 func defaultOptions() *Options {121+ var defaultTheme *curses.ColorTheme122+ if strings.Contains(os.Getenv("TERM"), "256") {123+ defaultTheme = curses.Dark256124+ } else {125+ defaultTheme = curses.Default16126+ }127+128 return &Options{129 Mode: ModeFuzzy,130 Case: CaseSmart,131 Nth: make([]Range, 0),4 unmodified lines136 Tiebreak: byLength,137 Multi: false,138 Ansi: false,139 Mouse: true,134- Color: true,135- Color256: strings.Contains(os.Getenv("TERM"), "256"),140+ Theme: defaultTheme,141 Black: false,142 Reverse: false,143 Hscroll: true,144 Prompt: "> ",125 unmodified lines270 }271 return byLength272 }273 274+func parseTheme(str string) *curses.ColorTheme {275+ switch strings.ToLower(str) {276+ case "dark":277+ return curses.Dark256278+ case "light":279+ return curses.Light256280+ case "16":281+ return curses.Default16282+ case "bw", "off", "no", "none":283+ return nil284+ default:285+ errorExit("invalid color scheme: " + str)286+ }287+ return nil288+}289+290 func checkToggleSort(str string) int {291 keys := parseKeyChords(str, "key name required")292 if len(keys) != 1 {293 errorExit("multiple keys specified")21 unmodified lines315 case "--expect":316 opts.Expect = parseKeyChords(nextString(allArgs, &i, "key names required"), "key names required")317 case "--tiebreak":318 opts.Tiebreak = parseTiebreak(nextString(allArgs, &i, "sort criterion required"))319+ case "--color":320+ opts.Theme = parseTheme(nextString(allArgs, &i, "color scheme name required"))321 case "--toggle-sort":322 opts.ToggleSort = checkToggleSort(nextString(allArgs, &i, "key name required"))323 case "-d", "--delimiter":324 opts.Delimiter = delimiterRegexp(nextString(allArgs, &i, "delimiter required"))23 unmodified lines348 opts.Ansi = false349 case "--no-mouse":350 opts.Mouse = false351 case "+c", "--no-color":329- opts.Color = false352+ opts.Theme = nil353 case "+2", "--no-256":331- opts.Color256 = false354+ opts.Theme = curses.Default16355 case "--black":356 opts.Black = true357 case "--no-black":358 opts.Black = false47 unmodified lines406 } else if match, value := optString(arg, "--expect="); match {407 opts.Expect = parseKeyChords(value, "key names required")408 } else if match, value := optString(arg, "--tiebreak="); match {409 opts.Tiebreak = parseTiebreak(value)410+ } else if match, value := optString(arg, "--color="); match {411+ opts.Theme = parseTheme(value)412 } else {413 errorExit("unknown option: " + arg)414 }415 }26 unmodified linesmodified:src/terminal.go+1 added-1 removed
103 unmodified lines104 mutex: sync.Mutex{},105 suppress: true,106 startChan: make(chan bool, 1),107 initFunc: func() {108- C.Init(opts.Color, opts.Color256, opts.Black, opts.Mouse)108+ C.Init(opts.Theme, opts.Black, opts.Mouse)109 }}110 }111 112 // Input returns current query string654 unmodified linesmodified:CHANGELOG.md+4 added
7 unmodified lines8910111213141516171819132 unmodified linesExpand all7 unmodified lines
- Performance optimization- Less aggressive memoization to limit memory usage
### New features
- Added color scheme for light background: `--color=light`
0.9.9-----
### New features132 unmodified linesExpand allmodified:fzf+2 added-2 removed
204 unmodified lines205 argv.shift206 @expect = true207 when /^--expect=(.*)$/208 @expect = true209- when '--toggle-sort', '--tiebreak'209+ when '--toggle-sort', '--tiebreak', '--color'210 argv.shift211 when '--tac', '--no-tac', '--sync', '--no-sync', '--hscroll', '--no-hscroll',212- /^--toggle-sort=(.*)$/, /^--tiebreak=(.*)$/212+ /^--color=(.*)$/, /^--toggle-sort=(.*)$/, /^--tiebreak=(.*)$/213 # XXX214 else215 usage 1, "illegal option: #{o}"216 end1131 unmodified lines