Skip to content
Myr

Set foreground color without affecting background

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

Close #712

authored by Junegunn Choi · committed by Junegunn Choi · parent cfdb00b · Browse files · 6 files+73 added−76 removed
This commit

State

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

Diff stats

6 files+73 added−76 removed

  • other:+73 added−76 removed

6 files+73 added−76 removed

modified:src/curses/curses.go+60 added-57 removed
111 unmodified lines
112)113114// Pallete115const (116 ColNormal = iota116 _ = iota117 ColNormal118 ColPrompt119 ColMatch120 ColCurrent121 ColCurrentMatch
12 unmodified lines
134 colUndefined = -2135)136137type ColorTheme struct {137 UseDefault bool138 Fg int16139 Bg int16140 DarkBg int16141 Prompt int16
25 unmodified lines
167168var (169 _buf []byte170 _in *os.File171 _color bool171 _color func(int, Attr) C.int172 _colorFn func(int, Attr) C.int173 _colorMap map[int]int174 _prevDownTime time.Time175 _clickY []int176 _screen *C.SCREEN177 Default16 *ColorTheme178 Dark256 *ColorTheme179 Light256 *ColorTheme179 FG int180 CurrentFG int181 BG int182 DarkBG int180)181182type Window struct {183 win *C.WINDOW
4 unmodified lines
188}189190func NewWindow(top int, left int, width int, height int, border bool) *Window {191 win := C.newwin(C.int(height), C.int(width), C.int(top), C.int(left))192 if _color {193 C.wbkgd(win, C.chtype(C.COLOR_PAIR(ColNormal)))194 }195 if border {196 attr := _color(ColBorder, 0)196 attr := _colorFn(ColBorder, 0)197 C.wattron(win, attr)198 C.box(win, 0, 0)199 C.wattroff(win, attr)200 }201202 return &Window{203 win: win,204 Top: top,205 Left: left,
3 unmodified lines
209}210211func EmptyTheme() *ColorTheme {212 return &ColorTheme{212 UseDefault: true,213 Fg: colUndefined,214 Bg: colUndefined,215 DarkBg: colUndefined,216 Prompt: colUndefined,
12 unmodified lines
229 _prevDownTime = time.Unix(0, 0)230 _clickY = []int{}231 _colorMap = make(map[int]int)232 Default16 = &ColorTheme{233 UseDefault: true,234 Fg: 15,235 Bg: 0,233 Fg: colDefault,234 Bg: colDefault,235 DarkBg: C.COLOR_BLACK,236 Prompt: C.COLOR_BLUE,237 Match: C.COLOR_GREEN,238 Current: C.COLOR_YELLOW,
4 unmodified lines
243 Selected: C.COLOR_MAGENTA,244 Header: C.COLOR_CYAN,245 Border: C.COLOR_BLACK}246 Dark256 = &ColorTheme{248 UseDefault: true,249 Fg: 15,250 Bg: 0,247 Fg: colDefault,248 Bg: colDefault,249 DarkBg: 236,250 Prompt: 110,251 Match: 108,252 Current: 254,
4 unmodified lines
257 Selected: 168,258 Header: 109,259 Border: 59}260 Light256 = &ColorTheme{263 UseDefault: true,264 Fg: 15,265 Bg: 0,261 Fg: colDefault,262 Bg: colDefault,263 DarkBg: 251,264 Prompt: 25,265 Match: 66,266 Current: 237,
7 unmodified lines
274}275276func attrColored(pair int, a Attr) C.int {277 var attr C.int281 if pair > ColNormal {278 if pair > 0 {279 attr = C.COLOR_PAIR(C.int(pair))280 }281 return attr | C.int(a)282}
57 unmodified lines
340 }341 C.noecho()342 C.raw() // stty dsusp undef343347 if theme != nil {344 _color = theme != nil345 if _color {346 C.start_color()347 var baseTheme *ColorTheme348 if C.tigetnum(C.CString("colors")) >= 256 {349 baseTheme = Dark256350 } else {351 baseTheme = Default16352 }353 initPairs(baseTheme, theme, black)354 C.bkgd(C.chtype(C.COLOR_PAIR(ColNormal)))356 _color = attrColored355 _colorFn = attrColored356 } else {358 _color = attrMono357 _colorFn = attrMono358 }359}360362func override(a int16, b int16) C.short {363 if b == colUndefined {364 return C.short(a)361func override(baseTheme *ColorTheme, theme *ColorTheme) {362 o := func(a int16, b int16) int16 {363 if b == colUndefined {364 return a365 }366 return b367 }366 return C.short(b)368 theme.Fg = o(baseTheme.Fg, theme.Fg)369 theme.Bg = o(baseTheme.Bg, theme.Bg)370 theme.DarkBg = o(baseTheme.DarkBg, theme.DarkBg)371 theme.Prompt = o(baseTheme.Prompt, theme.Prompt)372 theme.Match = o(baseTheme.Match, theme.Match)373 theme.Current = o(baseTheme.Current, theme.Current)374 theme.CurrentMatch = o(baseTheme.CurrentMatch, theme.CurrentMatch)375 theme.Spinner = o(baseTheme.Spinner, theme.Spinner)376 theme.Info = o(baseTheme.Info, theme.Info)377 theme.Cursor = o(baseTheme.Cursor, theme.Cursor)378 theme.Selected = o(baseTheme.Selected, theme.Selected)379 theme.Header = o(baseTheme.Header, theme.Header)380 theme.Border = o(baseTheme.Border, theme.Border)381}382383func initPairs(baseTheme *ColorTheme, theme *ColorTheme, black bool) {370 fg := override(baseTheme.Fg, theme.Fg)371 bg := override(baseTheme.Bg, theme.Bg)384 if black {373 bg = C.COLOR_BLACK374 } else if theme.UseDefault {375 fg = colDefault376 bg = colDefault377 C.use_default_colors()385 theme.Bg = C.COLOR_BLACK386 }379 if theme.UseDefault {380 FG = colDefault381 BG = colDefault382 } else {383 FG = int(fg)384 BG = int(bg)385 C.assume_default_colors(C.int(override(baseTheme.Fg, theme.Fg)), C.int(bg))386 }387 // Updates theme388 override(baseTheme, theme)389388 currentFG := override(baseTheme.Current, theme.Current)389 darkBG := override(baseTheme.DarkBg, theme.DarkBg)390 CurrentFG = int(currentFG)391 DarkBG = int(darkBG)392 C.init_pair(ColPrompt, override(baseTheme.Prompt, theme.Prompt), bg)393 C.init_pair(ColMatch, override(baseTheme.Match, theme.Match), bg)394 C.init_pair(ColCurrent, currentFG, darkBG)395 C.init_pair(ColCurrentMatch, override(baseTheme.CurrentMatch, theme.CurrentMatch), darkBG)396 C.init_pair(ColSpinner, override(baseTheme.Spinner, theme.Spinner), bg)397 C.init_pair(ColInfo, override(baseTheme.Info, theme.Info), bg)398 C.init_pair(ColCursor, override(baseTheme.Cursor, theme.Cursor), darkBG)399 C.init_pair(ColSelected, override(baseTheme.Selected, theme.Selected), darkBG)400 C.init_pair(ColHeader, override(baseTheme.Header, theme.Header), bg)401 C.init_pair(ColBorder, override(baseTheme.Border, theme.Border), bg)390 C.assume_default_colors(C.int(theme.Fg), C.int(theme.Bg))391 initPair := func(group C.short, fg int16, bg int16) {392 C.init_pair(group, C.short(fg), C.short(bg))393 }394 initPair(ColNormal, theme.Fg, theme.Bg)395 initPair(ColPrompt, theme.Prompt, theme.Bg)396 initPair(ColMatch, theme.Match, theme.Bg)397 initPair(ColCurrent, theme.Current, theme.DarkBg)398 initPair(ColCurrentMatch, theme.CurrentMatch, theme.DarkBg)399 initPair(ColSpinner, theme.Spinner, theme.Bg)400 initPair(ColInfo, theme.Info, theme.Bg)401 initPair(ColCursor, theme.Cursor, theme.DarkBg)402 initPair(ColSelected, theme.Selected, theme.DarkBg)403 initPair(ColHeader, theme.Header, theme.Bg)404 initPair(ColBorder, theme.Border, theme.Bg)405}406407func Close() {408 C.endwin()
249 unmodified lines
658 }, text)))659}660661func (w *Window) CPrint(pair int, a Attr, text string) {659 attr := _color(pair, a)662 attr := _colorFn(pair, a)663 C.wattron(w.win, attr)664 w.Print(text)665 C.wattroff(w.win, attr)666}
18 unmodified lines
685 return C.waddstr(w.win, C.CString(str)) == C.OK686}687688func (w *Window) CFill(str string, fg int, bg int, a Attr) bool {686 attr := _color(PairFor(fg, bg), a)689 attr := _colorFn(PairFor(fg, bg), a)690 C.wattron(w.win, attr)691 ret := w.Fill(str)692 C.wattroff(w.win, attr)693 return ret
21 unmodified lines
modified:src/options_test.go+2 added-8 removed
297 unmodified lines
298 t.Errorf("colors should not be equivalent")299 }300 customized.Fg = curses.Dark256.Fg301 customized.Bg = curses.Dark256.Bg302 if *curses.Dark256 == *customized {303 t.Errorf("colors should now be equivalent")302 if *curses.Dark256 != *customized {303 t.Errorf("colors should now be equivalent: %v, %v", curses.Dark256, customized)304 }305306 customized = parseTheme(theme, "fg:231,dark,bg:232")307 if customized.Fg != curses.Dark256.Fg || customized.Bg == curses.Dark256.Bg {308 t.Errorf("color not customized")309 }310 if customized.UseDefault {311 t.Errorf("not using default colors")312 }313 if !curses.Dark256.UseDefault {314 t.Errorf("using default colors")315 }310}311312func TestParseNilTheme(t *testing.T) {313 var theme *curses.ColorTheme
87 unmodified lines
modified:src/options.go-2 removed
497 unmodified lines
498 ansi := int16(ansi32)499 switch pair[0] {500 case "fg":501 theme.Fg = ansi502 theme.UseDefault = theme.UseDefault && ansi < 0502 case "bg":503 theme.Bg = ansi505 theme.UseDefault = theme.UseDefault && ansi < 0504 case "fg+":505 theme.Current = ansi506 case "bg+":507 theme.DarkBg = ansi
612 unmodified lines
modified:src/result_test.go+1 added-1 removed
101 unmodified lines
102 ansiOffset{[2]int32{30, 32}, ansiState{3, 7, 0}},103 ansiOffset{[2]int32{33, 40}, ansiState{4, 8, curses.Bold}}}}}104 // [{[0 5] 9 false} {[5 15] 99 false} {[15 20] 9 false} {[22 25] 10 true} {[25 35] 99 false} {[35 40] 11 true}]105106 colors := item.colorOffsets(offsets, 99, 0, true)106 colors := item.colorOffsets(offsets, curses.Dark256, 99, 0, true)107 assert := func(idx int, b int32, e int32, c int, bold bool) {108 var attr curses.Attr109 if bold {110 attr = curses.Bold
13 unmodified lines
modified:src/result.go+6 added-6 removed
90 unmodified lines
91func minRank() rank {92 return rank{index: 0, points: [4]uint16{math.MaxUint16, 0, 0, 0}}93}9495func (result *Result) colorOffsets(matchOffsets []Offset, color int, attr curses.Attr, current bool) []colorOffset {95func (result *Result) colorOffsets(matchOffsets []Offset, theme *curses.ColorTheme, color int, attr curses.Attr, current bool) []colorOffset {96 itemColors := result.item.Colors()9798 // No ANSI code, or --color=no99 if len(itemColors) == 0 {100 var offsets []colorOffset101 for _, off := range matchOffsets {101102 offsets = append(offsets, colorOffset{offset: [2]int32{off[0], off[1]}, color: color, attr: attr})103 }104 return offsets105 }
42 unmodified lines
148 ansi := itemColors[curr-1]149 fg := ansi.color.fg150 if fg == -1 {151 if current {152 fg = curses.CurrentFG152 fg = int(theme.Current)153 } else {154 fg = curses.FG154 fg = int(theme.Fg)155 }156 }157 bg := ansi.color.bg158 if bg == -1 {159 if current {160 bg = curses.DarkBG160 bg = int(theme.DarkBg)161 } else {162 bg = curses.BG162 bg = int(theme.Bg)163 }164 }165 colors = append(colors, colorOffset{166 offset: [2]int32{int32(start), int32(idx)},
75 unmodified lines
modified:src/terminal.go+4 added-2 removed
88 unmodified lines
89 initFunc func()90 suppress bool91 startChan chan bool92 slab *util.Slab93 theme *C.ColorTheme94}9596type selectedItem struct {97 at time.Time
197 unmodified lines
295 eventBox: eventBox,296 mutex: sync.Mutex{},297 suppress: true,298 slab: util.MakeSlab(slab16Size, slab32Size),299 theme: opts.Theme,300 startChan: make(chan bool, 1),301 initFunc: func() {302 C.Init(opts.Theme, opts.Black, opts.Mouse)303 }}
334 unmodified lines
638 t.window.CPrint(C.ColSelected, C.Bold, ">")639 } else {640 t.window.Print(" ")641 }640 t.printHighlighted(result, 0, 0, C.ColMatch, false)642 t.printHighlighted(result, 0, C.ColNormal, C.ColMatch, false)643 }644}645646func trimRight(runes []rune, width int) ([]rune, int) {
72 unmodified lines
719 for _, offset := range charOffsets {720 maxe = util.Max(maxe, int(offset[1]))721 }722721 offsets := result.colorOffsets(charOffsets, col2, attr, current)723 offsets := result.colorOffsets(charOffsets, t.theme, col2, attr, current)724 maxWidth := t.window.Width - 3725 maxe = util.Constrain(maxe+util.Min(maxWidth/2-2, t.hscrollOff), 0, len(text))726 if overflow(text, maxWidth) {727 if t.hscroll {
828 unmodified lines