Determine 256-color capability using tigetnum("colors")
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Close #570
authored by Junegunn Choi · committed by Junegunn Choi · parent 24516bc · Browse files · 2 files·+56 added−30 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+56 added−30 removed
- other:+56 added−30 removed
Provenance
- Commit
88a80e3- Tree
779dd99- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
24516bc
2 files changed+56 added−30 removed
modified:src/curses/curses.go+54 added-21 removed
107 unmodified lines108 )109 110 const (111 doubleClickDuration = 500 * time.Millisecond112+ colDefault = -1113+ colUndefined = -2114 )115 116 type ColorTheme struct {117 UseDefault bool42 unmodified lines160 BG int161 DarkBG int162 )163 164+func EmptyTheme() *ColorTheme {165+ return &ColorTheme{166+ UseDefault: true,167+ Fg: colUndefined,168+ Bg: colUndefined,169+ DarkBg: colUndefined,170+ Prompt: colUndefined,171+ Match: colUndefined,172+ Current: colUndefined,173+ CurrentMatch: colUndefined,174+ Spinner: colUndefined,175+ Info: colUndefined,176+ Cursor: colUndefined,177+ Selected: colUndefined,178+ Header: colUndefined}179+}180+181 func init() {182 _prevDownTime = time.Unix(0, 0)183 _clickY = []int{}184 _colorMap = make(map[int]int)113 unmodified lines298 C.raw() // stty dsusp undef299 300 if theme != nil {301 C.start_color()302+ var baseTheme *ColorTheme303+ if C.tigetnum(C.CString("colors")) >= 256 {304+ baseTheme = Dark256305+ } else {306+ baseTheme = Default16307+ }283- initPairs(theme, black)308+ initPairs(baseTheme, theme, black)309 _color = attrColored310 } else {311 _color = attrMono312 }313 }314 290-func initPairs(theme *ColorTheme, black bool) {291- fg := C.short(theme.Fg)292- bg := C.short(theme.Bg)315+func override(a int16, b int16) C.short {316+ if b == colUndefined {317+ return C.short(a)318+ }319+ return C.short(b)320+}321+322+func initPairs(baseTheme *ColorTheme, theme *ColorTheme, black bool) {323+ fg := override(baseTheme.Fg, theme.Fg)324+ bg := override(baseTheme.Bg, theme.Bg)325 if black {326 bg = C.COLOR_BLACK327 } else if theme.UseDefault {296- fg = -1297- bg = -1328+ fg = colDefault329+ bg = colDefault330 C.use_default_colors()331 }332 if theme.UseDefault {301- FG = -1302- BG = -1333+ FG = colDefault334+ BG = colDefault335 } else {336 FG = int(fg)337 BG = int(bg)306- C.assume_default_colors(C.int(theme.Fg), C.int(bg))338+ C.assume_default_colors(C.int(override(baseTheme.Fg, theme.Fg)), C.int(bg))339 }340 309- CurrentFG = int(theme.Current)310- DarkBG = int(theme.DarkBg)311- darkBG := C.short(DarkBG)312- C.init_pair(ColPrompt, C.short(theme.Prompt), bg)313- C.init_pair(ColMatch, C.short(theme.Match), bg)314- C.init_pair(ColCurrent, C.short(theme.Current), darkBG)315- C.init_pair(ColCurrentMatch, C.short(theme.CurrentMatch), darkBG)316- C.init_pair(ColSpinner, C.short(theme.Spinner), bg)317- C.init_pair(ColInfo, C.short(theme.Info), bg)318- C.init_pair(ColCursor, C.short(theme.Cursor), darkBG)319- C.init_pair(ColSelected, C.short(theme.Selected), darkBG)320- C.init_pair(ColHeader, C.short(theme.Header), bg)341+ currentFG := override(baseTheme.Current, theme.Current)342+ darkBG := override(baseTheme.DarkBg, theme.DarkBg)343+ CurrentFG = int(currentFG)344+ DarkBG = int(darkBG)345+ C.init_pair(ColPrompt, override(baseTheme.Prompt, theme.Prompt), bg)346+ C.init_pair(ColMatch, override(baseTheme.Match, theme.Match), bg)347+ C.init_pair(ColCurrent, currentFG, darkBG)348+ C.init_pair(ColCurrentMatch, override(baseTheme.CurrentMatch, theme.CurrentMatch), darkBG)349+ C.init_pair(ColSpinner, override(baseTheme.Spinner, theme.Spinner), bg)350+ C.init_pair(ColInfo, override(baseTheme.Info, theme.Info), bg)351+ C.init_pair(ColCursor, override(baseTheme.Cursor, theme.Cursor), darkBG)352+ C.init_pair(ColSelected, override(baseTheme.Selected, theme.Selected), darkBG)353+ C.init_pair(ColHeader, override(baseTheme.Header, theme.Header), bg)354 }355 356 func Close() {357 C.endwin()237 unmodified linesmodified:src/options.go+2 added-9 removed
130 unmodified lines131 Tabstop int132 Version bool133 }134 135-func defaultTheme() *curses.ColorTheme {136- if strings.Contains(os.Getenv("TERM"), "256") {137- return curses.Dark256138- }139- return curses.Default16140-}141-135 func defaultOptions() *Options {136 return &Options{137 Fuzzy: true,138 Extended: true,6 unmodified lines145 Criteria: []criterion{byMatchLen, byLength},146 Multi: false,147 Ansi: false,148 Mouse: true,156- Theme: defaultTheme(),149+ Theme: curses.EmptyTheme(),150 Black: false,151 Reverse: false,152 Cycle: false,153 Hscroll: true,583 unmodified lines737 parseKeymap(opts.Keymap, opts.Execmap, nextString(allArgs, &i, "bind expression required"))738 case "--color":739 spec := optionalNextString(allArgs, &i)740 if len(spec) == 0 {748- opts.Theme = defaultTheme()741+ opts.Theme = curses.EmptyTheme()742 } else {743 opts.Theme = parseTheme(opts.Theme, spec)744 }745 case "--toggle-sort":210 unmodified lines