diff --git a/src/ansi.go b/src/ansi.go index 650a374..fbfa050 100644 --- a/src/ansi.go +++ b/src/ansi.go @@ -7,7 +7,7 @@ import ( "strings" ) -type AnsiOffset struct { +type ansiOffset struct { offset [2]int32 color ansiState } @@ -35,8 +35,8 @@ func init() { ansiRegex = regexp.MustCompile("\x1b\\[[0-9;]*m") } -func ExtractColor(str *string) (*string, []AnsiOffset) { - offsets := make([]AnsiOffset, 0) +func extractColor(str *string) (*string, []ansiOffset) { + var offsets []ansiOffset var output bytes.Buffer var state *ansiState @@ -56,7 +56,7 @@ func ExtractColor(str *string) (*string, []AnsiOffset) { if newState.colored() { // Append new offset state = newState - offsets = append(offsets, AnsiOffset{[2]int32{newLen, newLen}, *state}) + offsets = append(offsets, ansiOffset{[2]int32{newLen, newLen}, *state}) } else { // Discard state state = nil diff --git a/src/ansi_test.go b/src/ansi_test.go index 37196dd..d5e0e43 100644 --- a/src/ansi_test.go +++ b/src/ansi_test.go @@ -6,7 +6,7 @@ import ( ) 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 || offset.color.fg != fg || offset.color.bg != bg || offset.color.bold != bold { t.Error(offset, b, e, fg, bg, bold) @@ -15,8 +15,8 @@ func TestExtractColor(t *testing.T) { src := "hello world" clean := "\x1b[0m" - check := func(assertion func(ansiOffsets []AnsiOffset)) { - output, ansiOffsets := ExtractColor(&src) + check := func(assertion func(ansiOffsets []ansiOffset)) { + output, ansiOffsets := extractColor(&src) if *output != "hello world" { t.Errorf("Invalid output: {}", output) } @@ -24,21 +24,21 @@ func TestExtractColor(t *testing.T) { assertion(ansiOffsets) } - check(func(offsets []AnsiOffset) { + check(func(offsets []ansiOffset) { if len(offsets) > 0 { t.Fail() } }) src = "\x1b[0mhello world" - check(func(offsets []AnsiOffset) { + check(func(offsets []ansiOffset) { if len(offsets) > 0 { t.Fail() } }) src = "\x1b[1mhello world" - check(func(offsets []AnsiOffset) { + check(func(offsets []ansiOffset) { if len(offsets) != 1 { t.Fail() } @@ -46,7 +46,7 @@ func TestExtractColor(t *testing.T) { }) src = "hello \x1b[34;45;1mworld" - check(func(offsets []AnsiOffset) { + check(func(offsets []ansiOffset) { if len(offsets) != 1 { t.Fail() } @@ -54,7 +54,7 @@ func TestExtractColor(t *testing.T) { }) src = "hello \x1b[34;45;1mwor\x1b[34;45;1mld" - check(func(offsets []AnsiOffset) { + check(func(offsets []ansiOffset) { if len(offsets) != 1 { t.Fail() } @@ -62,7 +62,7 @@ func TestExtractColor(t *testing.T) { }) src = "hello \x1b[34;45;1mwor\x1b[0mld" - check(func(offsets []AnsiOffset) { + check(func(offsets []ansiOffset) { if len(offsets) != 1 { 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" - check(func(offsets []AnsiOffset) { + check(func(offsets []ansiOffset) { if len(offsets) != 3 { t.Fail() } @@ -81,7 +81,7 @@ func TestExtractColor(t *testing.T) { // {38,48};5;{38,48} 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 { t.Fail() } diff --git a/src/core.go b/src/core.go index d561ab4..ebb7d66 100644 --- a/src/core.go +++ b/src/core.go @@ -64,20 +64,20 @@ func Run(options *Options) { eventBox := util.NewEventBox() // ANSI code processor - extractColors := func(data *string) (*string, []AnsiOffset) { + ansiProcessor := func(data *string) (*string, []ansiOffset) { // By default, we do nothing return data, nil } if opts.Ansi { if opts.Color { - extractColors = func(data *string) (*string, []AnsiOffset) { - return ExtractColor(data) + ansiProcessor = func(data *string) (*string, []ansiOffset) { + return extractColor(data) } } else { // When color is disabled but ansi option is given, // we simply strip out ANSI codes from the input - extractColors = func(data *string) (*string, []AnsiOffset) { - trimmed, _ := ExtractColor(data) + ansiProcessor = func(data *string) (*string, []ansiOffset) { + trimmed, _ := extractColor(data) return trimmed, nil } } @@ -87,7 +87,7 @@ func Run(options *Options) { var chunkList *ChunkList if len(opts.WithNth) == 0 { chunkList = NewChunkList(func(data *string, index int) *Item { - data, colors := extractColors(data) + data, colors := ansiProcessor(data) return &Item{ text: data, index: uint32(index), @@ -104,7 +104,7 @@ func Run(options *Options) { colors: nil, rank: Rank{0, 0, uint32(index)}} - trimmed, colors := extractColors(item.text) + trimmed, colors := ansiProcessor(item.text) item.text = trimmed item.colors = colors return &item diff --git a/src/item.go b/src/item.go index f9a464f..c94166f 100644 --- a/src/item.go +++ b/src/item.go @@ -7,7 +7,7 @@ import ( // Offset holds two 32-bit integers denoting the offsets of a matched substring type Offset [2]int32 -type ColorOffset struct { +type colorOffset struct { offset [2]int32 color int bold bool @@ -20,7 +20,7 @@ type Item struct { transformed *Transformed index uint32 offsets []Offset - colors []AnsiOffset + colors []ansiOffset rank Rank } @@ -66,17 +66,17 @@ func (i *Item) AsString() string { 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 { - offsets := make([]ColorOffset, 0) + var offsets []colorOffset 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 } // Find max column - var maxCol int32 = 0 + var maxCol int32 for _, off := range item.offsets { if off[1] > maxCol { maxCol = off[1] @@ -109,11 +109,11 @@ func (item *Item) ColorOffsets(color int, bold bool, current bool) []ColorOffset // --++++++++-- --++++++++++--- curr := 0 start := 0 - offsets := make([]ColorOffset, 0) + var offsets []colorOffset add := func(idx int) { if curr != 0 && idx > start { if curr == -1 { - offsets = append(offsets, ColorOffset{ + offsets = append(offsets, colorOffset{ offset: Offset{int32(start), int32(idx)}, color: color, bold: bold}) } else { ansi := item.colors[curr-1] @@ -121,7 +121,7 @@ func (item *Item) ColorOffsets(color int, bold bool, current bool) []ColorOffset if current { bg = int(curses.DarkBG) } - offsets = append(offsets, ColorOffset{ + offsets = append(offsets, colorOffset{ offset: Offset{int32(start), int32(idx)}, color: curses.PairFor(ansi.color.fg, bg), bold: ansi.color.bold || bold}) diff --git a/src/item_test.go b/src/item_test.go index 0249edf..4eea8c1 100644 --- a/src/item_test.go +++ b/src/item_test.go @@ -81,14 +81,14 @@ func TestColorOffset(t *testing.T) { // --++++++++-- --++++++++++--- item := Item{ offsets: []Offset{Offset{5, 15}, Offset{25, 35}}, - colors: []AnsiOffset{ - AnsiOffset{[2]int32{0, 20}, ansiState{1, 5, false}}, - AnsiOffset{[2]int32{22, 27}, ansiState{2, 6, true}}, - AnsiOffset{[2]int32{30, 32}, ansiState{3, 7, false}}, - AnsiOffset{[2]int32{33, 40}, ansiState{4, 8, true}}}} + colors: []ansiOffset{ + ansiOffset{[2]int32{0, 20}, ansiState{1, 5, false}}, + ansiOffset{[2]int32{22, 27}, ansiState{2, 6, true}}, + ansiOffset{[2]int32{30, 32}, ansiState{3, 7, false}}, + 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}] - offsets := item.ColorOffsets(99, false, true) + offsets := item.colorOffsets(99, false, true) assert := func(idx int, b int32, e int32, c int, bold bool) { o := offsets[idx] if o.offset[0] != b || o.offset[1] != e || o.color != c || o.bold != bold { diff --git a/src/matcher.go b/src/matcher.go index 06352f5..a3a9bd0 100644 --- a/src/matcher.go +++ b/src/matcher.go @@ -133,8 +133,7 @@ func (m *Matcher) scan(request MatchRequest) (*Merger, bool) { return EmptyMerger, false } pattern := request.pattern - empty := pattern.IsEmpty() - if empty { + if pattern.IsEmpty() { return PassMerger(&request.chunks, m.tac), false } @@ -152,19 +151,14 @@ func (m *Matcher) scan(request MatchRequest) (*Merger, bool) { defer func() { waitGroup.Done() }() sliceMatches := []*Item{} for _, chunk := range chunks { - var matches []*Item - if empty { - matches = *chunk - } else { - matches = request.pattern.Match(chunk) - } + matches := request.pattern.Match(chunk) sliceMatches = append(sliceMatches, matches...) if cancelled.Get() { return } countChan <- len(matches) } - if !empty && m.sort { + if m.sort { if m.tac { sort.Sort(ByRelevanceTac(sliceMatches)) } else { @@ -191,7 +185,7 @@ func (m *Matcher) scan(request MatchRequest) (*Merger, bool) { break } - if !empty && m.reqBox.Peek(reqReset) { + if m.reqBox.Peek(reqReset) { return nil, wait() } @@ -205,7 +199,7 @@ func (m *Matcher) scan(request MatchRequest) (*Merger, bool) { partialResult := <-resultChan 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 diff --git a/src/merger.go b/src/merger.go index 21a7459..bc5d5de 100644 --- a/src/merger.go +++ b/src/merger.go @@ -18,6 +18,8 @@ type Merger struct { count int } +// PassMerger returns a new Merger that simply returns the items in the +// original order func PassMerger(chunks *[]*Chunk, tac bool) *Merger { mg := Merger{ chunks: chunks, diff --git a/src/terminal.go b/src/terminal.go index ea0e826..bf64ec7 100644 --- a/src/terminal.go +++ b/src/terminal.go @@ -47,17 +47,17 @@ type selectedItem struct { text *string } -type ByTimeOrder []selectedItem +type byTimeOrder []selectedItem -func (a ByTimeOrder) Len() int { +func (a byTimeOrder) Len() int { 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] } -func (a ByTimeOrder) Less(i, j int) bool { +func (a byTimeOrder) Less(i, j int) bool { return a[i].at.Before(a[j].at) } @@ -160,7 +160,7 @@ func (t *Terminal) output() { for _, sel := range t.selected { sels = append(sels, sel) } - sort.Sort(ByTimeOrder(sels)) + sort.Sort(byTimeOrder(sels)) for _, sel := range sels { fmt.Println(*sel.text) } @@ -309,7 +309,7 @@ func (*Terminal) printHighlighted(item *Item, bold bool, col1 int, col2 int, cur // Overflow text := []rune(*item.text) - offsets := item.ColorOffsets(col2, bold, current) + offsets := item.colorOffsets(col2, bold, current) maxWidth := C.MaxX() - 3 fullWidth := displayWidth(text) if fullWidth > maxWidth { diff --git a/src/util/eventbox.go b/src/util/eventbox.go index e16b736..ccdbb99 100644 --- a/src/util/eventbox.go +++ b/src/util/eventbox.go @@ -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) { looping := true for looping {