From e70a2a5817586e4e7df0ee1446f609bbd859164a Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Thu, 19 Mar 2015 01:59:14 +0900 Subject: Add support for ANSI color codes --- src/item.go | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) (limited to 'src/item.go') diff --git a/src/item.go b/src/item.go index 2b8a9d13..f9a464f0 100644 --- a/src/item.go +++ b/src/item.go @@ -1,8 +1,18 @@ package fzf +import ( + "github.com/junegunn/fzf/src/curses" +) + // Offset holds two 32-bit integers denoting the offsets of a matched substring type Offset [2]int32 +type ColorOffset struct { + offset [2]int32 + color int + bold bool +} + // Item represents each input line type Item struct { text *string @@ -10,6 +20,7 @@ type Item struct { transformed *Transformed index uint32 offsets []Offset + colors []AnsiOffset rank Rank } @@ -55,6 +66,79 @@ func (i *Item) AsString() string { return *i.text } +func (item *Item) ColorOffsets(color int, bold bool, current bool) []ColorOffset { + if len(item.colors) == 0 { + offsets := make([]ColorOffset, 0) + for _, off := range item.offsets { + offsets = append(offsets, ColorOffset{offset: off, color: color, bold: bold}) + } + return offsets + } + + // Find max column + var maxCol int32 = 0 + for _, off := range item.offsets { + if off[1] > maxCol { + maxCol = off[1] + } + } + for _, ansi := range item.colors { + if ansi.offset[1] > maxCol { + maxCol = ansi.offset[1] + } + } + cols := make([]int, maxCol) + + for colorIndex, ansi := range item.colors { + for i := ansi.offset[0]; i < ansi.offset[1]; i++ { + cols[i] = colorIndex + 1 // XXX + } + } + + for _, off := range item.offsets { + for i := off[0]; i < off[1]; i++ { + cols[i] = -1 + } + } + + // sort.Sort(ByOrder(offsets)) + + // Merge offsets + // ------------ ---- -- ---- + // ++++++++ ++++++++++ + // --++++++++-- --++++++++++--- + curr := 0 + start := 0 + offsets := make([]ColorOffset, 0) + add := func(idx int) { + if curr != 0 && idx > start { + if curr == -1 { + offsets = append(offsets, ColorOffset{ + offset: Offset{int32(start), int32(idx)}, color: color, bold: bold}) + } else { + ansi := item.colors[curr-1] + bg := ansi.color.bg + if current { + bg = int(curses.DarkBG) + } + offsets = append(offsets, ColorOffset{ + offset: Offset{int32(start), int32(idx)}, + color: curses.PairFor(ansi.color.fg, bg), + bold: ansi.color.bold || bold}) + } + } + } + for idx, col := range cols { + if col != curr { + add(idx) + start = idx + curr = col + } + } + add(int(maxCol)) + return offsets +} + // ByOrder is for sorting substring offsets type ByOrder []Offset -- cgit v1.2.3