mirror of
https://github.com/Llewellynvdm/fzf.git
synced 2024-11-22 21:05:09 +00:00
Code cleanup
This commit is contained in:
parent
d94dfe0876
commit
b431e227da
@ -7,7 +7,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AnsiOffset struct {
|
type ansiOffset struct {
|
||||||
offset [2]int32
|
offset [2]int32
|
||||||
color ansiState
|
color ansiState
|
||||||
}
|
}
|
||||||
@ -35,8 +35,8 @@ func init() {
|
|||||||
ansiRegex = regexp.MustCompile("\x1b\\[[0-9;]*m")
|
ansiRegex = regexp.MustCompile("\x1b\\[[0-9;]*m")
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExtractColor(str *string) (*string, []AnsiOffset) {
|
func extractColor(str *string) (*string, []ansiOffset) {
|
||||||
offsets := make([]AnsiOffset, 0)
|
var offsets []ansiOffset
|
||||||
|
|
||||||
var output bytes.Buffer
|
var output bytes.Buffer
|
||||||
var state *ansiState
|
var state *ansiState
|
||||||
@ -56,7 +56,7 @@ func ExtractColor(str *string) (*string, []AnsiOffset) {
|
|||||||
if newState.colored() {
|
if newState.colored() {
|
||||||
// Append new offset
|
// Append new offset
|
||||||
state = newState
|
state = newState
|
||||||
offsets = append(offsets, AnsiOffset{[2]int32{newLen, newLen}, *state})
|
offsets = append(offsets, ansiOffset{[2]int32{newLen, newLen}, *state})
|
||||||
} else {
|
} else {
|
||||||
// Discard state
|
// Discard state
|
||||||
state = nil
|
state = nil
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestExtractColor(t *testing.T) {
|
func TestExtractColor(t *testing.T) {
|
||||||
assert := func(offset AnsiOffset, b int32, e int32, fg int, bg int, bold bool) {
|
assert := func(offset ansiOffset, b int32, e int32, fg int, bg int, bold bool) {
|
||||||
if offset.offset[0] != b || offset.offset[1] != e ||
|
if offset.offset[0] != b || offset.offset[1] != e ||
|
||||||
offset.color.fg != fg || offset.color.bg != bg || offset.color.bold != bold {
|
offset.color.fg != fg || offset.color.bg != bg || offset.color.bold != bold {
|
||||||
t.Error(offset, b, e, fg, bg, bold)
|
t.Error(offset, b, e, fg, bg, bold)
|
||||||
@ -15,8 +15,8 @@ func TestExtractColor(t *testing.T) {
|
|||||||
|
|
||||||
src := "hello world"
|
src := "hello world"
|
||||||
clean := "\x1b[0m"
|
clean := "\x1b[0m"
|
||||||
check := func(assertion func(ansiOffsets []AnsiOffset)) {
|
check := func(assertion func(ansiOffsets []ansiOffset)) {
|
||||||
output, ansiOffsets := ExtractColor(&src)
|
output, ansiOffsets := extractColor(&src)
|
||||||
if *output != "hello world" {
|
if *output != "hello world" {
|
||||||
t.Errorf("Invalid output: {}", output)
|
t.Errorf("Invalid output: {}", output)
|
||||||
}
|
}
|
||||||
@ -24,21 +24,21 @@ func TestExtractColor(t *testing.T) {
|
|||||||
assertion(ansiOffsets)
|
assertion(ansiOffsets)
|
||||||
}
|
}
|
||||||
|
|
||||||
check(func(offsets []AnsiOffset) {
|
check(func(offsets []ansiOffset) {
|
||||||
if len(offsets) > 0 {
|
if len(offsets) > 0 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
src = "\x1b[0mhello world"
|
src = "\x1b[0mhello world"
|
||||||
check(func(offsets []AnsiOffset) {
|
check(func(offsets []ansiOffset) {
|
||||||
if len(offsets) > 0 {
|
if len(offsets) > 0 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
src = "\x1b[1mhello world"
|
src = "\x1b[1mhello world"
|
||||||
check(func(offsets []AnsiOffset) {
|
check(func(offsets []ansiOffset) {
|
||||||
if len(offsets) != 1 {
|
if len(offsets) != 1 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
@ -46,7 +46,7 @@ func TestExtractColor(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
src = "hello \x1b[34;45;1mworld"
|
src = "hello \x1b[34;45;1mworld"
|
||||||
check(func(offsets []AnsiOffset) {
|
check(func(offsets []ansiOffset) {
|
||||||
if len(offsets) != 1 {
|
if len(offsets) != 1 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
@ -54,7 +54,7 @@ func TestExtractColor(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
src = "hello \x1b[34;45;1mwor\x1b[34;45;1mld"
|
src = "hello \x1b[34;45;1mwor\x1b[34;45;1mld"
|
||||||
check(func(offsets []AnsiOffset) {
|
check(func(offsets []ansiOffset) {
|
||||||
if len(offsets) != 1 {
|
if len(offsets) != 1 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
@ -62,7 +62,7 @@ func TestExtractColor(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
src = "hello \x1b[34;45;1mwor\x1b[0mld"
|
src = "hello \x1b[34;45;1mwor\x1b[0mld"
|
||||||
check(func(offsets []AnsiOffset) {
|
check(func(offsets []ansiOffset) {
|
||||||
if len(offsets) != 1 {
|
if len(offsets) != 1 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
@ -70,7 +70,7 @@ func TestExtractColor(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
src = "hello \x1b[34;48;5;233;1mwo\x1b[38;5;161mr\x1b[0ml\x1b[38;5;161md"
|
src = "hello \x1b[34;48;5;233;1mwo\x1b[38;5;161mr\x1b[0ml\x1b[38;5;161md"
|
||||||
check(func(offsets []AnsiOffset) {
|
check(func(offsets []ansiOffset) {
|
||||||
if len(offsets) != 3 {
|
if len(offsets) != 3 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
@ -81,7 +81,7 @@ func TestExtractColor(t *testing.T) {
|
|||||||
|
|
||||||
// {38,48};5;{38,48}
|
// {38,48};5;{38,48}
|
||||||
src = "hello \x1b[38;5;38;48;5;48;1mwor\x1b[38;5;48;48;5;38ml\x1b[0md"
|
src = "hello \x1b[38;5;38;48;5;48;1mwor\x1b[38;5;48;48;5;38ml\x1b[0md"
|
||||||
check(func(offsets []AnsiOffset) {
|
check(func(offsets []ansiOffset) {
|
||||||
if len(offsets) != 2 {
|
if len(offsets) != 2 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
14
src/core.go
14
src/core.go
@ -64,20 +64,20 @@ func Run(options *Options) {
|
|||||||
eventBox := util.NewEventBox()
|
eventBox := util.NewEventBox()
|
||||||
|
|
||||||
// ANSI code processor
|
// ANSI code processor
|
||||||
extractColors := func(data *string) (*string, []AnsiOffset) {
|
ansiProcessor := func(data *string) (*string, []ansiOffset) {
|
||||||
// By default, we do nothing
|
// By default, we do nothing
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
if opts.Ansi {
|
if opts.Ansi {
|
||||||
if opts.Color {
|
if opts.Color {
|
||||||
extractColors = func(data *string) (*string, []AnsiOffset) {
|
ansiProcessor = func(data *string) (*string, []ansiOffset) {
|
||||||
return ExtractColor(data)
|
return extractColor(data)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// When color is disabled but ansi option is given,
|
// When color is disabled but ansi option is given,
|
||||||
// we simply strip out ANSI codes from the input
|
// we simply strip out ANSI codes from the input
|
||||||
extractColors = func(data *string) (*string, []AnsiOffset) {
|
ansiProcessor = func(data *string) (*string, []ansiOffset) {
|
||||||
trimmed, _ := ExtractColor(data)
|
trimmed, _ := extractColor(data)
|
||||||
return trimmed, nil
|
return trimmed, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -87,7 +87,7 @@ func Run(options *Options) {
|
|||||||
var chunkList *ChunkList
|
var chunkList *ChunkList
|
||||||
if len(opts.WithNth) == 0 {
|
if len(opts.WithNth) == 0 {
|
||||||
chunkList = NewChunkList(func(data *string, index int) *Item {
|
chunkList = NewChunkList(func(data *string, index int) *Item {
|
||||||
data, colors := extractColors(data)
|
data, colors := ansiProcessor(data)
|
||||||
return &Item{
|
return &Item{
|
||||||
text: data,
|
text: data,
|
||||||
index: uint32(index),
|
index: uint32(index),
|
||||||
@ -104,7 +104,7 @@ func Run(options *Options) {
|
|||||||
colors: nil,
|
colors: nil,
|
||||||
rank: Rank{0, 0, uint32(index)}}
|
rank: Rank{0, 0, uint32(index)}}
|
||||||
|
|
||||||
trimmed, colors := extractColors(item.text)
|
trimmed, colors := ansiProcessor(item.text)
|
||||||
item.text = trimmed
|
item.text = trimmed
|
||||||
item.colors = colors
|
item.colors = colors
|
||||||
return &item
|
return &item
|
||||||
|
18
src/item.go
18
src/item.go
@ -7,7 +7,7 @@ import (
|
|||||||
// Offset holds two 32-bit integers denoting the offsets of a matched substring
|
// Offset holds two 32-bit integers denoting the offsets of a matched substring
|
||||||
type Offset [2]int32
|
type Offset [2]int32
|
||||||
|
|
||||||
type ColorOffset struct {
|
type colorOffset struct {
|
||||||
offset [2]int32
|
offset [2]int32
|
||||||
color int
|
color int
|
||||||
bold bool
|
bold bool
|
||||||
@ -20,7 +20,7 @@ type Item struct {
|
|||||||
transformed *Transformed
|
transformed *Transformed
|
||||||
index uint32
|
index uint32
|
||||||
offsets []Offset
|
offsets []Offset
|
||||||
colors []AnsiOffset
|
colors []ansiOffset
|
||||||
rank Rank
|
rank Rank
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,17 +66,17 @@ func (i *Item) AsString() string {
|
|||||||
return *i.text
|
return *i.text
|
||||||
}
|
}
|
||||||
|
|
||||||
func (item *Item) ColorOffsets(color int, bold bool, current bool) []ColorOffset {
|
func (item *Item) colorOffsets(color int, bold bool, current bool) []colorOffset {
|
||||||
if len(item.colors) == 0 {
|
if len(item.colors) == 0 {
|
||||||
offsets := make([]ColorOffset, 0)
|
var offsets []colorOffset
|
||||||
for _, off := range item.offsets {
|
for _, off := range item.offsets {
|
||||||
offsets = append(offsets, ColorOffset{offset: off, color: color, bold: bold})
|
offsets = append(offsets, colorOffset{offset: off, color: color, bold: bold})
|
||||||
}
|
}
|
||||||
return offsets
|
return offsets
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find max column
|
// Find max column
|
||||||
var maxCol int32 = 0
|
var maxCol int32
|
||||||
for _, off := range item.offsets {
|
for _, off := range item.offsets {
|
||||||
if off[1] > maxCol {
|
if off[1] > maxCol {
|
||||||
maxCol = off[1]
|
maxCol = off[1]
|
||||||
@ -109,11 +109,11 @@ func (item *Item) ColorOffsets(color int, bold bool, current bool) []ColorOffset
|
|||||||
// --++++++++-- --++++++++++---
|
// --++++++++-- --++++++++++---
|
||||||
curr := 0
|
curr := 0
|
||||||
start := 0
|
start := 0
|
||||||
offsets := make([]ColorOffset, 0)
|
var offsets []colorOffset
|
||||||
add := func(idx int) {
|
add := func(idx int) {
|
||||||
if curr != 0 && idx > start {
|
if curr != 0 && idx > start {
|
||||||
if curr == -1 {
|
if curr == -1 {
|
||||||
offsets = append(offsets, ColorOffset{
|
offsets = append(offsets, colorOffset{
|
||||||
offset: Offset{int32(start), int32(idx)}, color: color, bold: bold})
|
offset: Offset{int32(start), int32(idx)}, color: color, bold: bold})
|
||||||
} else {
|
} else {
|
||||||
ansi := item.colors[curr-1]
|
ansi := item.colors[curr-1]
|
||||||
@ -121,7 +121,7 @@ func (item *Item) ColorOffsets(color int, bold bool, current bool) []ColorOffset
|
|||||||
if current {
|
if current {
|
||||||
bg = int(curses.DarkBG)
|
bg = int(curses.DarkBG)
|
||||||
}
|
}
|
||||||
offsets = append(offsets, ColorOffset{
|
offsets = append(offsets, colorOffset{
|
||||||
offset: Offset{int32(start), int32(idx)},
|
offset: Offset{int32(start), int32(idx)},
|
||||||
color: curses.PairFor(ansi.color.fg, bg),
|
color: curses.PairFor(ansi.color.fg, bg),
|
||||||
bold: ansi.color.bold || bold})
|
bold: ansi.color.bold || bold})
|
||||||
|
@ -81,14 +81,14 @@ func TestColorOffset(t *testing.T) {
|
|||||||
// --++++++++-- --++++++++++---
|
// --++++++++-- --++++++++++---
|
||||||
item := Item{
|
item := Item{
|
||||||
offsets: []Offset{Offset{5, 15}, Offset{25, 35}},
|
offsets: []Offset{Offset{5, 15}, Offset{25, 35}},
|
||||||
colors: []AnsiOffset{
|
colors: []ansiOffset{
|
||||||
AnsiOffset{[2]int32{0, 20}, ansiState{1, 5, false}},
|
ansiOffset{[2]int32{0, 20}, ansiState{1, 5, false}},
|
||||||
AnsiOffset{[2]int32{22, 27}, ansiState{2, 6, true}},
|
ansiOffset{[2]int32{22, 27}, ansiState{2, 6, true}},
|
||||||
AnsiOffset{[2]int32{30, 32}, ansiState{3, 7, false}},
|
ansiOffset{[2]int32{30, 32}, ansiState{3, 7, false}},
|
||||||
AnsiOffset{[2]int32{33, 40}, ansiState{4, 8, true}}}}
|
ansiOffset{[2]int32{33, 40}, ansiState{4, 8, true}}}}
|
||||||
// [{[0 5] 9 false} {[5 15] 99 false} {[15 20] 9 false} {[22 25] 10 true} {[25 35] 99 false} {[35 40] 11 true}]
|
// [{[0 5] 9 false} {[5 15] 99 false} {[15 20] 9 false} {[22 25] 10 true} {[25 35] 99 false} {[35 40] 11 true}]
|
||||||
|
|
||||||
offsets := item.ColorOffsets(99, false, true)
|
offsets := item.colorOffsets(99, false, true)
|
||||||
assert := func(idx int, b int32, e int32, c int, bold bool) {
|
assert := func(idx int, b int32, e int32, c int, bold bool) {
|
||||||
o := offsets[idx]
|
o := offsets[idx]
|
||||||
if o.offset[0] != b || o.offset[1] != e || o.color != c || o.bold != bold {
|
if o.offset[0] != b || o.offset[1] != e || o.color != c || o.bold != bold {
|
||||||
|
@ -133,8 +133,7 @@ func (m *Matcher) scan(request MatchRequest) (*Merger, bool) {
|
|||||||
return EmptyMerger, false
|
return EmptyMerger, false
|
||||||
}
|
}
|
||||||
pattern := request.pattern
|
pattern := request.pattern
|
||||||
empty := pattern.IsEmpty()
|
if pattern.IsEmpty() {
|
||||||
if empty {
|
|
||||||
return PassMerger(&request.chunks, m.tac), false
|
return PassMerger(&request.chunks, m.tac), false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,19 +151,14 @@ func (m *Matcher) scan(request MatchRequest) (*Merger, bool) {
|
|||||||
defer func() { waitGroup.Done() }()
|
defer func() { waitGroup.Done() }()
|
||||||
sliceMatches := []*Item{}
|
sliceMatches := []*Item{}
|
||||||
for _, chunk := range chunks {
|
for _, chunk := range chunks {
|
||||||
var matches []*Item
|
matches := request.pattern.Match(chunk)
|
||||||
if empty {
|
|
||||||
matches = *chunk
|
|
||||||
} else {
|
|
||||||
matches = request.pattern.Match(chunk)
|
|
||||||
}
|
|
||||||
sliceMatches = append(sliceMatches, matches...)
|
sliceMatches = append(sliceMatches, matches...)
|
||||||
if cancelled.Get() {
|
if cancelled.Get() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
countChan <- len(matches)
|
countChan <- len(matches)
|
||||||
}
|
}
|
||||||
if !empty && m.sort {
|
if m.sort {
|
||||||
if m.tac {
|
if m.tac {
|
||||||
sort.Sort(ByRelevanceTac(sliceMatches))
|
sort.Sort(ByRelevanceTac(sliceMatches))
|
||||||
} else {
|
} else {
|
||||||
@ -191,7 +185,7 @@ func (m *Matcher) scan(request MatchRequest) (*Merger, bool) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if !empty && m.reqBox.Peek(reqReset) {
|
if m.reqBox.Peek(reqReset) {
|
||||||
return nil, wait()
|
return nil, wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,7 +199,7 @@ func (m *Matcher) scan(request MatchRequest) (*Merger, bool) {
|
|||||||
partialResult := <-resultChan
|
partialResult := <-resultChan
|
||||||
partialResults[partialResult.index] = partialResult.matches
|
partialResults[partialResult.index] = partialResult.matches
|
||||||
}
|
}
|
||||||
return NewMerger(partialResults, !empty && m.sort, m.tac), false
|
return NewMerger(partialResults, m.sort, m.tac), false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset is called to interrupt/signal the ongoing search
|
// Reset is called to interrupt/signal the ongoing search
|
||||||
|
@ -18,6 +18,8 @@ type Merger struct {
|
|||||||
count int
|
count int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PassMerger returns a new Merger that simply returns the items in the
|
||||||
|
// original order
|
||||||
func PassMerger(chunks *[]*Chunk, tac bool) *Merger {
|
func PassMerger(chunks *[]*Chunk, tac bool) *Merger {
|
||||||
mg := Merger{
|
mg := Merger{
|
||||||
chunks: chunks,
|
chunks: chunks,
|
||||||
|
@ -47,17 +47,17 @@ type selectedItem struct {
|
|||||||
text *string
|
text *string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ByTimeOrder []selectedItem
|
type byTimeOrder []selectedItem
|
||||||
|
|
||||||
func (a ByTimeOrder) Len() int {
|
func (a byTimeOrder) Len() int {
|
||||||
return len(a)
|
return len(a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a ByTimeOrder) Swap(i, j int) {
|
func (a byTimeOrder) Swap(i, j int) {
|
||||||
a[i], a[j] = a[j], a[i]
|
a[i], a[j] = a[j], a[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a ByTimeOrder) Less(i, j int) bool {
|
func (a byTimeOrder) Less(i, j int) bool {
|
||||||
return a[i].at.Before(a[j].at)
|
return a[i].at.Before(a[j].at)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,7 +160,7 @@ func (t *Terminal) output() {
|
|||||||
for _, sel := range t.selected {
|
for _, sel := range t.selected {
|
||||||
sels = append(sels, sel)
|
sels = append(sels, sel)
|
||||||
}
|
}
|
||||||
sort.Sort(ByTimeOrder(sels))
|
sort.Sort(byTimeOrder(sels))
|
||||||
for _, sel := range sels {
|
for _, sel := range sels {
|
||||||
fmt.Println(*sel.text)
|
fmt.Println(*sel.text)
|
||||||
}
|
}
|
||||||
@ -309,7 +309,7 @@ func (*Terminal) printHighlighted(item *Item, bold bool, col1 int, col2 int, cur
|
|||||||
|
|
||||||
// Overflow
|
// Overflow
|
||||||
text := []rune(*item.text)
|
text := []rune(*item.text)
|
||||||
offsets := item.ColorOffsets(col2, bold, current)
|
offsets := item.colorOffsets(col2, bold, current)
|
||||||
maxWidth := C.MaxX() - 3
|
maxWidth := C.MaxX() - 3
|
||||||
fullWidth := displayWidth(text)
|
fullWidth := displayWidth(text)
|
||||||
if fullWidth > maxWidth {
|
if fullWidth > maxWidth {
|
||||||
|
@ -79,6 +79,7 @@ func (b *EventBox) Unwatch(events ...EventType) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WaitFor blocks the execution until the event is received
|
||||||
func (b *EventBox) WaitFor(event EventType) {
|
func (b *EventBox) WaitFor(event EventType) {
|
||||||
looping := true
|
looping := true
|
||||||
for looping {
|
for looping {
|
||||||
|
Loading…
Reference in New Issue
Block a user