2015-03-18 16:59:14 +00:00
|
|
|
package fzf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-03-22 08:19:30 +00:00
|
|
|
"unicode/utf8"
|
2016-09-28 15:54:27 +00:00
|
|
|
|
|
|
|
"github.com/junegunn/fzf/src/curses"
|
2015-03-18 16:59:14 +00:00
|
|
|
)
|
|
|
|
|
2015-03-22 07:05:54 +00:00
|
|
|
type ansiOffset struct {
|
2015-03-18 16:59:14 +00:00
|
|
|
offset [2]int32
|
|
|
|
color ansiState
|
|
|
|
}
|
|
|
|
|
|
|
|
type ansiState struct {
|
|
|
|
fg int
|
|
|
|
bg int
|
2016-09-28 15:54:27 +00:00
|
|
|
attr curses.Attr
|
2015-03-18 16:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ansiState) colored() bool {
|
2016-09-28 15:54:27 +00:00
|
|
|
return s.fg != -1 || s.bg != -1 || s.attr > 0
|
2015-03-18 16:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ansiState) equals(t *ansiState) bool {
|
|
|
|
if t == nil {
|
|
|
|
return !s.colored()
|
|
|
|
}
|
2016-09-28 15:54:27 +00:00
|
|
|
return s.fg == t.fg && s.bg == t.bg && s.attr == t.attr
|
2015-03-18 16:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ansiRegex *regexp.Regexp
|
|
|
|
|
|
|
|
func init() {
|
2016-09-25 10:03:08 +00:00
|
|
|
ansiRegex = regexp.MustCompile("\x1b.[0-9;]*.")
|
2015-03-18 16:59:14 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 17:39:32 +00:00
|
|
|
func extractColor(str string, state *ansiState, proc func(string, *ansiState) bool) (string, *[]ansiOffset, *ansiState) {
|
2015-03-22 07:05:54 +00:00
|
|
|
var offsets []ansiOffset
|
2015-03-18 16:59:14 +00:00
|
|
|
var output bytes.Buffer
|
2015-07-22 05:19:45 +00:00
|
|
|
|
|
|
|
if state != nil {
|
|
|
|
offsets = append(offsets, ansiOffset{[2]int32{0, 0}, *state})
|
|
|
|
}
|
2015-03-18 16:59:14 +00:00
|
|
|
|
|
|
|
idx := 0
|
2015-08-02 05:00:18 +00:00
|
|
|
for _, offset := range ansiRegex.FindAllStringIndex(str, -1) {
|
2016-06-14 12:52:47 +00:00
|
|
|
prev := str[idx:offset[0]]
|
|
|
|
output.WriteString(prev)
|
|
|
|
if proc != nil && !proc(prev, state) {
|
2016-07-15 14:24:14 +00:00
|
|
|
return "", nil, nil
|
2016-06-14 12:52:47 +00:00
|
|
|
}
|
2015-08-02 05:00:18 +00:00
|
|
|
newState := interpretCode(str[offset[0]:offset[1]], state)
|
2015-03-18 16:59:14 +00:00
|
|
|
|
|
|
|
if !newState.equals(state) {
|
|
|
|
if state != nil {
|
|
|
|
// Update last offset
|
2015-05-21 17:20:10 +00:00
|
|
|
(&offsets[len(offsets)-1]).offset[1] = int32(utf8.RuneCount(output.Bytes()))
|
2015-03-18 16:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if newState.colored() {
|
|
|
|
// Append new offset
|
|
|
|
state = newState
|
2015-03-22 08:19:30 +00:00
|
|
|
newLen := int32(utf8.RuneCount(output.Bytes()))
|
2015-03-22 07:05:54 +00:00
|
|
|
offsets = append(offsets, ansiOffset{[2]int32{newLen, newLen}, *state})
|
2015-03-18 16:59:14 +00:00
|
|
|
} else {
|
|
|
|
// Discard state
|
|
|
|
state = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
idx = offset[1]
|
|
|
|
}
|
|
|
|
|
2015-08-02 05:00:18 +00:00
|
|
|
rest := str[idx:]
|
2015-03-18 16:59:14 +00:00
|
|
|
if len(rest) > 0 {
|
|
|
|
output.WriteString(rest)
|
|
|
|
if state != nil {
|
|
|
|
// Update last offset
|
2015-03-22 08:19:30 +00:00
|
|
|
(&offsets[len(offsets)-1]).offset[1] = int32(utf8.RuneCount(output.Bytes()))
|
2015-03-18 16:59:14 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-14 12:52:47 +00:00
|
|
|
if proc != nil {
|
|
|
|
proc(rest, state)
|
|
|
|
}
|
2016-08-18 17:39:32 +00:00
|
|
|
if len(offsets) == 0 {
|
|
|
|
return output.String(), nil, state
|
|
|
|
}
|
|
|
|
return output.String(), &offsets, state
|
2015-03-18 16:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func interpretCode(ansiCode string, prevState *ansiState) *ansiState {
|
|
|
|
// State
|
|
|
|
var state *ansiState
|
|
|
|
if prevState == nil {
|
2016-09-28 15:54:27 +00:00
|
|
|
state = &ansiState{-1, -1, 0}
|
2015-03-18 16:59:14 +00:00
|
|
|
} else {
|
2016-09-28 15:54:27 +00:00
|
|
|
state = &ansiState{prevState.fg, prevState.bg, prevState.attr}
|
2015-03-18 16:59:14 +00:00
|
|
|
}
|
2016-09-25 10:03:08 +00:00
|
|
|
if ansiCode[1] != '[' || ansiCode[len(ansiCode)-1] != 'm' {
|
2015-03-27 03:35:06 +00:00
|
|
|
return state
|
|
|
|
}
|
2015-03-18 16:59:14 +00:00
|
|
|
|
|
|
|
ptr := &state.fg
|
|
|
|
state256 := 0
|
|
|
|
|
|
|
|
init := func() {
|
|
|
|
state.fg = -1
|
|
|
|
state.bg = -1
|
2016-09-28 15:54:27 +00:00
|
|
|
state.attr = 0
|
2015-03-18 16:59:14 +00:00
|
|
|
state256 = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
ansiCode = ansiCode[2 : len(ansiCode)-1]
|
2015-03-22 16:24:31 +00:00
|
|
|
if len(ansiCode) == 0 {
|
|
|
|
init()
|
|
|
|
}
|
2015-03-18 16:59:14 +00:00
|
|
|
for _, code := range strings.Split(ansiCode, ";") {
|
|
|
|
if num, err := strconv.Atoi(code); err == nil {
|
|
|
|
switch state256 {
|
|
|
|
case 0:
|
|
|
|
switch num {
|
|
|
|
case 38:
|
|
|
|
ptr = &state.fg
|
|
|
|
state256++
|
|
|
|
case 48:
|
|
|
|
ptr = &state.bg
|
|
|
|
state256++
|
|
|
|
case 39:
|
|
|
|
state.fg = -1
|
|
|
|
case 49:
|
|
|
|
state.bg = -1
|
|
|
|
case 1:
|
2016-09-28 15:54:27 +00:00
|
|
|
state.attr = curses.Bold
|
|
|
|
case 2:
|
|
|
|
state.attr = curses.Dim
|
|
|
|
case 4:
|
|
|
|
state.attr = curses.Underline
|
|
|
|
case 5:
|
|
|
|
state.attr = curses.Blink
|
|
|
|
case 7:
|
|
|
|
state.attr = curses.Reverse
|
2015-03-18 16:59:14 +00:00
|
|
|
case 0:
|
|
|
|
init()
|
|
|
|
default:
|
|
|
|
if num >= 30 && num <= 37 {
|
|
|
|
state.fg = num - 30
|
|
|
|
} else if num >= 40 && num <= 47 {
|
|
|
|
state.bg = num - 40
|
2016-09-25 09:11:35 +00:00
|
|
|
} else if num >= 90 && num <= 97 {
|
|
|
|
state.fg = num - 90 + 8
|
|
|
|
} else if num >= 100 && num <= 107 {
|
|
|
|
state.bg = num - 100 + 8
|
2015-03-18 16:59:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
switch num {
|
|
|
|
case 5:
|
|
|
|
state256++
|
|
|
|
default:
|
|
|
|
state256 = 0
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
*ptr = num
|
|
|
|
state256 = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return state
|
|
|
|
}
|