summaryrefslogtreecommitdiff
path: root/src/item.go
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2015-03-19 01:59:14 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2015-03-19 01:59:14 +0900
commite70a2a5817586e4e7df0ee1446f609bbd859164a (patch)
tree24ea4cb8865233ec89e2e2828a66727cf0b129f4 /src/item.go
parentd80a41bb6d1a507d65885d553a30d4e7dc7d0453 (diff)
downloadfzf-e70a2a5817586e4e7df0ee1446f609bbd859164a.tar.gz
Add support for ANSI color codes
Diffstat (limited to 'src/item.go')
-rw-r--r--src/item.go84
1 files changed, 84 insertions, 0 deletions
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