Determine 256-color capability using tigetnum("colors")

Close #570
This commit is contained in:
Junegunn Choi 2016-05-11 01:07:06 +09:00
parent 24516bcf4d
commit 88a80e3c2c
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
2 changed files with 56 additions and 30 deletions

View File

@ -109,6 +109,8 @@ const (
const ( const (
doubleClickDuration = 500 * time.Millisecond doubleClickDuration = 500 * time.Millisecond
colDefault = -1
colUndefined = -2
) )
type ColorTheme struct { type ColorTheme struct {
@ -159,6 +161,23 @@ var (
DarkBG int DarkBG int
) )
func EmptyTheme() *ColorTheme {
return &ColorTheme{
UseDefault: true,
Fg: colUndefined,
Bg: colUndefined,
DarkBg: colUndefined,
Prompt: colUndefined,
Match: colUndefined,
Current: colUndefined,
CurrentMatch: colUndefined,
Spinner: colUndefined,
Info: colUndefined,
Cursor: colUndefined,
Selected: colUndefined,
Header: colUndefined}
}
func init() { func init() {
_prevDownTime = time.Unix(0, 0) _prevDownTime = time.Unix(0, 0)
_clickY = []int{} _clickY = []int{}
@ -280,44 +299,58 @@ func Init(theme *ColorTheme, black bool, mouse bool) {
if theme != nil { if theme != nil {
C.start_color() C.start_color()
initPairs(theme, black) var baseTheme *ColorTheme
if C.tigetnum(C.CString("colors")) >= 256 {
baseTheme = Dark256
} else {
baseTheme = Default16
}
initPairs(baseTheme, theme, black)
_color = attrColored _color = attrColored
} else { } else {
_color = attrMono _color = attrMono
} }
} }
func initPairs(theme *ColorTheme, black bool) { func override(a int16, b int16) C.short {
fg := C.short(theme.Fg) if b == colUndefined {
bg := C.short(theme.Bg) return C.short(a)
}
return C.short(b)
}
func initPairs(baseTheme *ColorTheme, theme *ColorTheme, black bool) {
fg := override(baseTheme.Fg, theme.Fg)
bg := override(baseTheme.Bg, theme.Bg)
if black { if black {
bg = C.COLOR_BLACK bg = C.COLOR_BLACK
} else if theme.UseDefault { } else if theme.UseDefault {
fg = -1 fg = colDefault
bg = -1 bg = colDefault
C.use_default_colors() C.use_default_colors()
} }
if theme.UseDefault { if theme.UseDefault {
FG = -1 FG = colDefault
BG = -1 BG = colDefault
} else { } else {
FG = int(fg) FG = int(fg)
BG = int(bg) BG = int(bg)
C.assume_default_colors(C.int(theme.Fg), C.int(bg)) C.assume_default_colors(C.int(override(baseTheme.Fg, theme.Fg)), C.int(bg))
} }
CurrentFG = int(theme.Current) currentFG := override(baseTheme.Current, theme.Current)
DarkBG = int(theme.DarkBg) darkBG := override(baseTheme.DarkBg, theme.DarkBg)
darkBG := C.short(DarkBG) CurrentFG = int(currentFG)
C.init_pair(ColPrompt, C.short(theme.Prompt), bg) DarkBG = int(darkBG)
C.init_pair(ColMatch, C.short(theme.Match), bg) C.init_pair(ColPrompt, override(baseTheme.Prompt, theme.Prompt), bg)
C.init_pair(ColCurrent, C.short(theme.Current), darkBG) C.init_pair(ColMatch, override(baseTheme.Match, theme.Match), bg)
C.init_pair(ColCurrentMatch, C.short(theme.CurrentMatch), darkBG) C.init_pair(ColCurrent, currentFG, darkBG)
C.init_pair(ColSpinner, C.short(theme.Spinner), bg) C.init_pair(ColCurrentMatch, override(baseTheme.CurrentMatch, theme.CurrentMatch), darkBG)
C.init_pair(ColInfo, C.short(theme.Info), bg) C.init_pair(ColSpinner, override(baseTheme.Spinner, theme.Spinner), bg)
C.init_pair(ColCursor, C.short(theme.Cursor), darkBG) C.init_pair(ColInfo, override(baseTheme.Info, theme.Info), bg)
C.init_pair(ColSelected, C.short(theme.Selected), darkBG) C.init_pair(ColCursor, override(baseTheme.Cursor, theme.Cursor), darkBG)
C.init_pair(ColHeader, C.short(theme.Header), bg) C.init_pair(ColSelected, override(baseTheme.Selected, theme.Selected), darkBG)
C.init_pair(ColHeader, override(baseTheme.Header, theme.Header), bg)
} }
func Close() { func Close() {

View File

@ -132,13 +132,6 @@ type Options struct {
Version bool Version bool
} }
func defaultTheme() *curses.ColorTheme {
if strings.Contains(os.Getenv("TERM"), "256") {
return curses.Dark256
}
return curses.Default16
}
func defaultOptions() *Options { func defaultOptions() *Options {
return &Options{ return &Options{
Fuzzy: true, Fuzzy: true,
@ -153,7 +146,7 @@ func defaultOptions() *Options {
Multi: false, Multi: false,
Ansi: false, Ansi: false,
Mouse: true, Mouse: true,
Theme: defaultTheme(), Theme: curses.EmptyTheme(),
Black: false, Black: false,
Reverse: false, Reverse: false,
Cycle: false, Cycle: false,
@ -745,7 +738,7 @@ func parseOptions(opts *Options, allArgs []string) {
case "--color": case "--color":
spec := optionalNextString(allArgs, &i) spec := optionalNextString(allArgs, &i)
if len(spec) == 0 { if len(spec) == 0 {
opts.Theme = defaultTheme() opts.Theme = curses.EmptyTheme()
} else { } else {
opts.Theme = parseTheme(opts.Theme, spec) opts.Theme = parseTheme(opts.Theme, spec)
} }