Add support for more ANSI color attributes (#674)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Dim, underline, blink, reverse
authored by Junegunn Choi · committed by Junegunn Choi · parent 1acd2ad · Browse files · 4 files·+62 added−45 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+62 added−45 removed
- other:+62 added−45 removed
Provenance
- Commit
1fc5659- Tree
b974096- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
1acd2ad
4 files changed+62 added−45 removed
modified:src/curses/curses.go+21 added-14 removed
21 unmodified lines22 "time"23 "unicode/utf8"24 )25 26+const (27+ Bold = C.A_BOLD28+ Dim = C.A_DIM29+ Blink = C.A_BLINK30+ Reverse = C.A_REVERSE31+ Underline = C.A_UNDERLINE32+)33+34+type Attr C.int35+36 // Types of user action37 const (38 Rune = iota39 127 unmodified lines167 168 var (169 _buf []byte170 _in *os.File161- _color func(int, bool) C.int171+ _color func(int, Attr) C.int172 _colorMap map[int]int173 _prevDownTime time.Time174 _clickY []int175 _screen *C.SCREEN16 unmodified lines192 193 func NewWindow(top int, left int, width int, height int, border bool) *Window {194 win := C.newwin(C.int(height), C.int(width), C.int(top), C.int(left))195 if border {186- attr := _color(ColBorder, false)196+ attr := _color(ColBorder, 0)197 C.wattron(win, attr)198 C.box(win, 0, 0)199 C.wattroff(win, attr)200 }74 unmodified lines275 Header: 31,276 Border: 145}277 }278 269-func attrColored(pair int, bold bool) C.int {279+func attrColored(pair int, a Attr) C.int {280 var attr C.int281 if pair > ColNormal {282 attr = C.COLOR_PAIR(C.int(pair))283 }274- if bold {275- attr = attr | C.A_BOLD276- }277- return attr284+ return attr | C.int(a)285 }286 280-func attrMono(pair int, bold bool) C.int {287+func attrMono(pair int, a Attr) C.int {288 var attr C.int289 switch pair {290 case ColCurrent:284- if bold {291+ if a&C.A_BOLD == C.A_BOLD {292 attr = C.A_REVERSE293 }294 case ColMatch:295 attr = C.A_UNDERLINE296 case ColCurrentMatch:297 attr = C.A_UNDERLINE | C.A_REVERSE298 }292- if bold {299+ if a&C.A_BOLD == C.A_BOLD {300 attr = attr | C.A_BOLD301 }302 return attr303 }350 unmodified lines654 return r655 }, text)))656 }657 651-func (w *Window) CPrint(pair int, bold bool, text string) {652- attr := _color(pair, bold)658+func (w *Window) CPrint(pair int, a Attr, text string) {659+ attr := _color(pair, a)660 C.wattron(w.win, attr)661 w.Print(text)662 C.wattroff(w.win, attr)663 }17 unmodified lines681 func (w *Window) Fill(str string) bool {682 return C.waddstr(w.win, C.CString(str)) == C.OK683 }684 678-func (w *Window) CFill(str string, fg int, bg int, bold bool) bool {679- attr := _color(PairFor(fg, bg), bold)685+func (w *Window) CFill(str string, fg int, bg int, a Attr) bool {686+ attr := _color(PairFor(fg, bg), a)687 C.wattron(w.win, attr)688 ret := w.Fill(str)689 C.wattroff(w.win, attr)690 return ret21 unmodified linesmodified:src/ansi.go+17 added-7 removed
4 unmodified lines5 "regexp"6 "strconv"7 "strings"8 "unicode/utf8"9+10+ "github.com/junegunn/fzf/src/curses"11 )12 13 type ansiOffset struct {14 offset [2]int322 unmodified lines17 18 type ansiState struct {19 fg int20 bg int19- bold bool21+ attr curses.Attr22 }23 24 func (s *ansiState) colored() bool {23- return s.fg != -1 || s.bg != -1 || s.bold25+ return s.fg != -1 || s.bg != -1 || s.attr > 026 }27 28 func (s *ansiState) equals(t *ansiState) bool {29 if t == nil {30 return !s.colored()31 }30- return s.fg == t.fg && s.bg == t.bg && s.bold == t.bold32+ return s.fg == t.fg && s.bg == t.bg && s.attr == t.attr33 }34 35 var ansiRegex *regexp.Regexp36 58 unmodified lines95 func interpretCode(ansiCode string, prevState *ansiState) *ansiState {96 // State97 var state *ansiState98 if prevState == nil {97- state = &ansiState{-1, -1, false}99+ state = &ansiState{-1, -1, 0}100 } else {99- state = &ansiState{prevState.fg, prevState.bg, prevState.bold}101+ state = &ansiState{prevState.fg, prevState.bg, prevState.attr}102 }103 if ansiCode[1] != '[' || ansiCode[len(ansiCode)-1] != 'm' {104 return state105 }3 unmodified lines109 110 init := func() {111 state.fg = -1112 state.bg = -1111- state.bold = false113+ state.attr = 0114 state256 = 0115 }116 117 ansiCode = ansiCode[2 : len(ansiCode)-1]15 unmodified lines133 state.fg = -1134 case 49:135 state.bg = -1136 case 1:135- state.bold = true137+ state.attr = curses.Bold138+ case 2:139+ state.attr = curses.Dim140+ case 4:141+ state.attr = curses.Underline142+ case 5:143+ state.attr = curses.Blink144+ case 7:145+ state.attr = curses.Reverse146 case 0:147 init()148 default:149 if num >= 30 && num <= 37 {24 unmodified linesmodified:src/result.go+5 added-5 removed
12 unmodified lines13 14 type colorOffset struct {15 offset [2]int3216 color int17- bold bool17+ attr curses.Attr18 index int3219 }20 21 type rank struct {68 unmodified lines90 func minRank() rank {91 return rank{index: 0, points: [4]uint16{math.MaxUint16, 0, 0, 0}}92 }93 94-func (result *Result) colorOffsets(matchOffsets []Offset, color int, bold bool, current bool) []colorOffset {94+func (result *Result) colorOffsets(matchOffsets []Offset, color int, attr curses.Attr, current bool) []colorOffset {95 itemColors := result.item.Colors()96 97 if len(itemColors) == 0 {98 var offsets []colorOffset99 for _, off := range matchOffsets {100 101- offsets = append(offsets, colorOffset{offset: [2]int32{off[0], off[1]}, color: color, bold: bold})101+ offsets = append(offsets, colorOffset{offset: [2]int32{off[0], off[1]}, color: color, attr: attr})102 }103 return offsets104 }105 35 unmodified lines141 add := func(idx int) {142 if curr != 0 && idx > start {143 if curr == -1 {144 colors = append(colors, colorOffset{145- offset: [2]int32{int32(start), int32(idx)}, color: color, bold: bold})145+ offset: [2]int32{int32(start), int32(idx)}, color: color, attr: attr})146 } else {147 ansi := itemColors[curr-1]148 fg := ansi.color.fg149 if fg == -1 {13 unmodified lines163 }164 colors = append(colors, colorOffset{165 offset: [2]int32{int32(start), int32(idx)},166 color: curses.PairFor(fg, bg),167- bold: ansi.color.bold || bold})167+ attr: ansi.color.attr | attr})168 }169 }170 }171 for idx, col := range cols {69 unmodified linesmodified:src/terminal.go+19 added-19 removed
524 unmodified lines525 }526 527 func (t *Terminal) printPrompt() {528 t.move(0, 0, true)529- t.window.CPrint(C.ColPrompt, true, t.prompt)530- t.window.CPrint(C.ColNormal, true, string(t.input))529+ t.window.CPrint(C.ColPrompt, C.Bold, t.prompt)530+ t.window.CPrint(C.ColNormal, C.Bold, string(t.input))531 }532 533 func (t *Terminal) printInfo() {534 if t.inlineInfo {535 t.move(0, displayWidth([]rune(t.prompt))+displayWidth(t.input)+1, true)536 if t.reading {537- t.window.CPrint(C.ColSpinner, true, " < ")537+ t.window.CPrint(C.ColSpinner, C.Bold, " < ")538 } else {539- t.window.CPrint(C.ColPrompt, true, " < ")539+ t.window.CPrint(C.ColPrompt, C.Bold, " < ")540 }541 } else {542 t.move(1, 0, true)543 if t.reading {544 duration := int64(spinnerDuration)545 idx := (time.Now().UnixNano() % (duration * int64(len(_spinner)))) / duration546- t.window.CPrint(C.ColSpinner, true, _spinner[idx])546+ t.window.CPrint(C.ColSpinner, C.Bold, _spinner[idx])547 }548 t.move(1, 2, false)549 }550 10 unmodified lines561 }562 if t.progress > 0 && t.progress < 100 {563 output += fmt.Sprintf(" (%d%%)", t.progress)564 }565- t.window.CPrint(C.ColInfo, false, output)565+ t.window.CPrint(C.ColInfo, 0, output)566 }567 568 func (t *Terminal) printHeader() {569 if len(t.header) == 0 {15 unmodified lines585 text: util.RunesToChars([]rune(trimmed)),586 colors: colors}587 588 t.move(line, 2, true)589- t.printHighlighted(&Result{item: item}, false, C.ColHeader, 0, false)589+ t.printHighlighted(&Result{item: item}, 0, C.ColHeader, 0, false)590 }591 }592 593 func (t *Terminal) printList() {25 unmodified lines619 }620 } else if current {621 label = ">"622 }623- t.window.CPrint(C.ColCursor, true, label)623+ t.window.CPrint(C.ColCursor, C.Bold, label)624 if current {625 if selected {626- t.window.CPrint(C.ColSelected, true, ">")626+ t.window.CPrint(C.ColSelected, C.Bold, ">")627 } else {628- t.window.CPrint(C.ColCurrent, true, " ")628+ t.window.CPrint(C.ColCurrent, C.Bold, " ")629 }630- t.printHighlighted(result, true, C.ColCurrent, C.ColCurrentMatch, true)630+ t.printHighlighted(result, C.Bold, C.ColCurrent, C.ColCurrentMatch, true)631 } else {632 if selected {633- t.window.CPrint(C.ColSelected, true, ">")633+ t.window.CPrint(C.ColSelected, C.Bold, ">")634 } else {635 t.window.Print(" ")636 }637- t.printHighlighted(result, false, 0, C.ColMatch, false)637+ t.printHighlighted(result, 0, 0, C.ColMatch, false)638 }639 }640 641 func trimRight(runes []rune, width int) ([]rune, int) {47 unmodified lines689 }690 return false691 }692 693-func (t *Terminal) printHighlighted(result *Result, bold bool, col1 int, col2 int, current bool) {693+func (t *Terminal) printHighlighted(result *Result, attr C.Attr, col1 int, col2 int, current bool) {694 item := result.item695 696 // Overflow697 text := make([]rune, item.text.Length())16 unmodified lines714 for _, offset := range charOffsets {715 maxe = util.Max(maxe, int(offset[1]))716 }717 718- offsets := result.colorOffsets(charOffsets, col2, bold, current)718+ offsets := result.colorOffsets(charOffsets, col2, attr, current)719 maxWidth := t.window.Width - 3720 maxe = util.Constrain(maxe+util.Min(maxWidth/2-2, t.hscrollOff), 0, len(text))721 if overflow(text, maxWidth) {722 if t.hscroll {40 unmodified lines763 b := util.Constrain32(offset.offset[0], index, maxOffset)764 e := util.Constrain32(offset.offset[1], index, maxOffset)765 766 substr, prefixWidth = processTabs(text[index:b], prefixWidth)767- t.window.CPrint(col1, bold, substr)767+ t.window.CPrint(col1, attr, substr)768 769 if b < e {770 substr, prefixWidth = processTabs(text[b:e], prefixWidth)771- t.window.CPrint(offset.color, offset.bold, substr)771+ t.window.CPrint(offset.color, offset.attr, substr)772 }773 774 index = e775 if index >= maxOffset {1 unmodified line777 }778 }779 if index < maxOffset {780 substr, _ = processTabs(text[index:], prefixWidth)781- t.window.CPrint(col1, bold, substr)781+ t.window.CPrint(col1, attr, substr)782 }783 }784 785 func numLinesMax(str string, max int) int {25 unmodified lines811 return true812 }813 }814 if ansi != nil && ansi.colored() {815- return t.pwindow.CFill(str, ansi.fg, ansi.bg, ansi.bold)815+ return t.pwindow.CFill(str, ansi.fg, ansi.bg, ansi.attr)816 }817 return t.pwindow.Fill(str)818 })819 }669 unmodified lines