Skip to content
Myr

Code cleanup

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Junegunn Choi · committed by Junegunn Choi · parent d94dfe0 · Browse files · 9 files+51 added−54 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

9 files+51 added−54 removed

  • other:+51 added−54 removed

9 files+51 added−54 removed

modified:src/util/eventbox.go+1 added
77 unmodified lines
78 b.ignore[event] = true79 }80}8182// WaitFor blocks the execution until the event is received83func (b *EventBox) WaitFor(event EventType) {84 looping := true85 for looping {86 b.Wait(func(events *Events) {
10 unmodified lines
modified:src/ansi_test.go+11 added-11 removed
4 unmodified lines
5 "testing"6)78func TestExtractColor(t *testing.T) {9 assert := func(offset AnsiOffset, b int32, e int32, fg int, bg int, bold bool) {9 assert := func(offset ansiOffset, b int32, e int32, fg int, bg int, bold bool) {10 if offset.offset[0] != b || offset.offset[1] != e ||11 offset.color.fg != fg || offset.color.bg != bg || offset.color.bold != bold {12 t.Error(offset, b, e, fg, bg, bold)13 }14 }1516 src := "hello world"17 clean := "\x1b[0m"18 check := func(assertion func(ansiOffsets []AnsiOffset)) {19 output, ansiOffsets := ExtractColor(&src)18 check := func(assertion func(ansiOffsets []ansiOffset)) {19 output, ansiOffsets := extractColor(&src)20 if *output != "hello world" {21 t.Errorf("Invalid output: {}", output)22 }23 fmt.Println(src, ansiOffsets, clean)24 assertion(ansiOffsets)25 }2627 check(func(offsets []AnsiOffset) {27 check(func(offsets []ansiOffset) {28 if len(offsets) > 0 {29 t.Fail()30 }31 })3233 src = "\x1b[0mhello world"34 check(func(offsets []AnsiOffset) {34 check(func(offsets []ansiOffset) {35 if len(offsets) > 0 {36 t.Fail()37 }38 })3940 src = "\x1b[1mhello world"41 check(func(offsets []AnsiOffset) {41 check(func(offsets []ansiOffset) {42 if len(offsets) != 1 {43 t.Fail()44 }45 assert(offsets[0], 0, 11, -1, -1, true)46 })4748 src = "hello \x1b[34;45;1mworld"49 check(func(offsets []AnsiOffset) {49 check(func(offsets []ansiOffset) {50 if len(offsets) != 1 {51 t.Fail()52 }53 assert(offsets[0], 6, 11, 4, 5, true)54 })5556 src = "hello \x1b[34;45;1mwor\x1b[34;45;1mld"57 check(func(offsets []AnsiOffset) {57 check(func(offsets []ansiOffset) {58 if len(offsets) != 1 {59 t.Fail()60 }61 assert(offsets[0], 6, 11, 4, 5, true)62 })6364 src = "hello \x1b[34;45;1mwor\x1b[0mld"65 check(func(offsets []AnsiOffset) {65 check(func(offsets []ansiOffset) {66 if len(offsets) != 1 {67 t.Fail()68 }69 assert(offsets[0], 6, 9, 4, 5, true)70 })7172 src = "hello \x1b[34;48;5;233;1mwo\x1b[38;5;161mr\x1b[0ml\x1b[38;5;161md"73 check(func(offsets []AnsiOffset) {73 check(func(offsets []ansiOffset) {74 if len(offsets) != 3 {75 t.Fail()76 }77 assert(offsets[0], 6, 8, 4, 233, true)
2 unmodified lines
80 })8182 // {38,48};5;{38,48}83 src = "hello \x1b[38;5;38;48;5;48;1mwor\x1b[38;5;48;48;5;38ml\x1b[0md"84 check(func(offsets []AnsiOffset) {84 check(func(offsets []ansiOffset) {85 if len(offsets) != 2 {86 t.Fail()87 }88 assert(offsets[0], 6, 9, 38, 48, true)
3 unmodified lines
modified:src/ansi.go+4 added-4 removed
5 unmodified lines
6 "strconv"7 "strings"8)910type AnsiOffset struct {10type ansiOffset struct {11 offset [2]int3212 color ansiState13}14
19 unmodified lines
34func init() {35 ansiRegex = regexp.MustCompile("\x1b\\[[0-9;]*m")36}3738func ExtractColor(str *string) (*string, []AnsiOffset) {39 offsets := make([]AnsiOffset, 0)38func extractColor(str *string) (*string, []ansiOffset) {39 var offsets []ansiOffset4041 var output bytes.Buffer42 var state *ansiState43
11 unmodified lines
5556 if newState.colored() {57 // Append new offset58 state = newState59 offsets = append(offsets, AnsiOffset{[2]int32{newLen, newLen}, *state})59 offsets = append(offsets, ansiOffset{[2]int32{newLen, newLen}, *state})60 } else {61 // Discard state62 state = nil63 }
78 unmodified lines
modified:src/core.go+7 added-7 removed
62 unmodified lines
63 // Event channel64 eventBox := util.NewEventBox()6566 // ANSI code processor67 extractColors := func(data *string) (*string, []AnsiOffset) {67 ansiProcessor := func(data *string) (*string, []ansiOffset) {68 // By default, we do nothing69 return data, nil70 }71 if opts.Ansi {72 if opts.Color {73 extractColors = func(data *string) (*string, []AnsiOffset) {74 return ExtractColor(data)73 ansiProcessor = func(data *string) (*string, []ansiOffset) {74 return extractColor(data)75 }76 } else {77 // When color is disabled but ansi option is given,78 // we simply strip out ANSI codes from the input79 extractColors = func(data *string) (*string, []AnsiOffset) {80 trimmed, _ := ExtractColor(data)79 ansiProcessor = func(data *string) (*string, []ansiOffset) {80 trimmed, _ := extractColor(data)81 return trimmed, nil82 }83 }84 }
1 unmodified line
86 // Chunk list87 var chunkList *ChunkList88 if len(opts.WithNth) == 0 {89 chunkList = NewChunkList(func(data *string, index int) *Item {90 data, colors := extractColors(data)90 data, colors := ansiProcessor(data)91 return &Item{92 text: data,93 index: uint32(index),94 colors: colors,
8 unmodified lines
103 index: uint32(index),104 colors: nil,105 rank: Rank{0, 0, uint32(index)}}106107 trimmed, colors := extractColors(item.text)107 trimmed, colors := ansiProcessor(item.text)108 item.text = trimmed109 item.colors = colors110 return &item111 })
129 unmodified lines
modified:src/item_test.go+6 added-6 removed
79 unmodified lines
80 // ++++++++ ++++++++++81 // --++++++++-- --++++++++++---82 item := Item{83 offsets: []Offset{Offset{5, 15}, Offset{25, 35}},84 colors: []AnsiOffset{85 AnsiOffset{[2]int32{0, 20}, ansiState{1, 5, false}},86 AnsiOffset{[2]int32{22, 27}, ansiState{2, 6, true}},87 AnsiOffset{[2]int32{30, 32}, ansiState{3, 7, false}},88 AnsiOffset{[2]int32{33, 40}, ansiState{4, 8, true}}}}84 colors: []ansiOffset{85 ansiOffset{[2]int32{0, 20}, ansiState{1, 5, false}},86 ansiOffset{[2]int32{22, 27}, ansiState{2, 6, true}},87 ansiOffset{[2]int32{30, 32}, ansiState{3, 7, false}},88 ansiOffset{[2]int32{33, 40}, ansiState{4, 8, true}}}}89 // [{[0 5] 9 false} {[5 15] 99 false} {[15 20] 9 false} {[22 25] 10 true} {[25 35] 99 false} {[35 40] 11 true}]9091 offsets := item.ColorOffsets(99, false, true)91 offsets := item.colorOffsets(99, false, true)92 assert := func(idx int, b int32, e int32, c int, bold bool) {93 o := offsets[idx]94 if o.offset[0] != b || o.offset[1] != e || o.color != c || o.bold != bold {95 t.Error(o)
9 unmodified lines
modified:src/item.go+9 added-9 removed
5 unmodified lines
67// Offset holds two 32-bit integers denoting the offsets of a matched substring8type Offset [2]int32910type ColorOffset struct {10type colorOffset struct {11 offset [2]int3212 color int13 bold bool14}
4 unmodified lines
19 origText *string20 transformed *Transformed21 index uint3222 offsets []Offset23 colors []AnsiOffset23 colors []ansiOffset24 rank Rank25}2627// Rank is used to sort the search result
37 unmodified lines
65 }66 return *i.text67}6869func (item *Item) ColorOffsets(color int, bold bool, current bool) []ColorOffset {69func (item *Item) colorOffsets(color int, bold bool, current bool) []colorOffset {70 if len(item.colors) == 0 {71 offsets := make([]ColorOffset, 0)71 var offsets []colorOffset72 for _, off := range item.offsets {73 offsets = append(offsets, ColorOffset{offset: off, color: color, bold: bold})73 offsets = append(offsets, colorOffset{offset: off, color: color, bold: bold})74 }75 return offsets76 }7778 // Find max column79 var maxCol int32 = 079 var maxCol int3280 for _, off := range item.offsets {81 if off[1] > maxCol {82 maxCol = off[1]83 }
24 unmodified lines
108 // ++++++++ ++++++++++109 // --++++++++-- --++++++++++---110 curr := 0111 start := 0112 offsets := make([]ColorOffset, 0)112 var offsets []colorOffset113 add := func(idx int) {114 if curr != 0 && idx > start {115 if curr == -1 {116 offsets = append(offsets, ColorOffset{116 offsets = append(offsets, colorOffset{117 offset: Offset{int32(start), int32(idx)}, color: color, bold: bold})118 } else {119 ansi := item.colors[curr-1]120 bg := ansi.color.bg121 if current {122 bg = int(curses.DarkBG)123 }124 offsets = append(offsets, ColorOffset{124 offsets = append(offsets, colorOffset{125 offset: Offset{int32(start), int32(idx)},126 color: curses.PairFor(ansi.color.fg, bg),127 bold: ansi.color.bold || bold})128 }
81 unmodified lines
modified:src/matcher.go+5 added-11 removed
131 unmodified lines
132 if numChunks == 0 {133 return EmptyMerger, false134 }135 pattern := request.pattern136 empty := pattern.IsEmpty()137 if empty {136 if pattern.IsEmpty() {137 return PassMerger(&request.chunks, m.tac), false138 }139140 cancelled := util.NewAtomicBool(false)
9 unmodified lines
150 go func(idx int, chunks []*Chunk) {151 defer func() { waitGroup.Done() }()152 sliceMatches := []*Item{}153 for _, chunk := range chunks {155 var matches []*Item156 if empty {157 matches = *chunk158 } else {159 matches = request.pattern.Match(chunk)154 matches := request.pattern.Match(chunk)160 }155 sliceMatches = append(sliceMatches, matches...)156 if cancelled.Get() {157 return158 }159 countChan <- len(matches)160 }167 if !empty && m.sort {161 if m.sort {162 if m.tac {163 sort.Sort(ByRelevanceTac(sliceMatches))164 } else {165 sort.Sort(ByRelevance(sliceMatches))
18 unmodified lines
184 if count == numChunks {185 break186 }187194 if !empty && m.reqBox.Peek(reqReset) {188 if m.reqBox.Peek(reqReset) {189 return nil, wait()190 }191192 if time.Now().Sub(startedAt) > progressMinDuration {
5 unmodified lines
198 for range slices {199 partialResult := <-resultChan200 partialResults[partialResult.index] = partialResult.matches201 }208 return NewMerger(partialResults, !empty && m.sort, m.tac), false202 return NewMerger(partialResults, m.sort, m.tac), false203}204205// Reset is called to interrupt/signal the ongoing search206func (m *Matcher) Reset(chunks []*Chunk, patternRunes []rune, cancel bool, final bool) {
10 unmodified lines
modified:src/merger.go+2 added
16 unmodified lines
17 final bool18 count int19}2021// PassMerger returns a new Merger that simply returns the items in the22// original order23func PassMerger(chunks *[]*Chunk, tac bool) *Merger {24 mg := Merger{25 chunks: chunks,26 tac: tac,
88 unmodified lines
modified:src/terminal.go+6 added-6 removed
45 unmodified lines
46 at time.Time47 text *string48}4950type ByTimeOrder []selectedItem50type byTimeOrder []selectedItem5152func (a ByTimeOrder) Len() int {52func (a byTimeOrder) Len() int {53 return len(a)54}5556func (a ByTimeOrder) Swap(i, j int) {56func (a byTimeOrder) Swap(i, j int) {57 a[i], a[j] = a[j], a[i]58}5960func (a ByTimeOrder) Less(i, j int) bool {60func (a byTimeOrder) Less(i, j int) bool {61 return a[i].at.Before(a[j].at)62}6364var _spinner = []string{`-`, `\`, `|`, `/`, `-`, `\`, `|`, `/`}
94 unmodified lines
159 sels := make([]selectedItem, 0, len(t.selected))160 for _, sel := range t.selected {161 sels = append(sels, sel)162 }163 sort.Sort(ByTimeOrder(sels))163 sort.Sort(byTimeOrder(sels))164 for _, sel := range sels {165 fmt.Println(*sel.text)166 }167 }
140 unmodified lines
308 }309310 // Overflow311 text := []rune(*item.text)312 offsets := item.ColorOffsets(col2, bold, current)312 offsets := item.colorOffsets(col2, bold, current)313 maxWidth := C.MaxX() - 3314 fullWidth := displayWidth(text)315 if fullWidth > maxWidth {316 // Stri..
396 unmodified lines