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
Provenance
- Commit
b431e22- Tree
78ea8c6- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
d94dfe0
9 files changed+51 added−54 removed
modified:src/util/eventbox.go+1 added
77 unmodified lines78 b.ignore[event] = true79 }80 }81 82+// WaitFor blocks the execution until the event is received83 func (b *EventBox) WaitFor(event EventType) {84 looping := true85 for looping {86 b.Wait(func(events *Events) {10 unmodified linesmodified:src/ansi_test.go+11 added-11 removed
4 unmodified lines5 "testing"6 )7 8 func 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 }15 16 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 }26 27- check(func(offsets []AnsiOffset) {27+ check(func(offsets []ansiOffset) {28 if len(offsets) > 0 {29 t.Fail()30 }31 })32 33 src = "\x1b[0mhello world"34- check(func(offsets []AnsiOffset) {34+ check(func(offsets []ansiOffset) {35 if len(offsets) > 0 {36 t.Fail()37 }38 })39 40 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 })47 48 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 })55 56 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 })63 64 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 })71 72 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 lines80 })81 82 // {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 linesmodified:src/ansi.go+4 added-4 removed
5 unmodified lines6 "strconv"7 "strings"8 )9 10-type AnsiOffset struct {10+type ansiOffset struct {11 offset [2]int3212 color ansiState13 }14 19 unmodified lines34 func init() {35 ansiRegex = regexp.MustCompile("\x1b\\[[0-9;]*m")36 }37 38-func ExtractColor(str *string) (*string, []AnsiOffset) {39- offsets := make([]AnsiOffset, 0)38+func extractColor(str *string) (*string, []ansiOffset) {39+ var offsets []ansiOffset40 41 var output bytes.Buffer42 var state *ansiState43 11 unmodified lines55 56 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 linesmodified:src/core.go+7 added-7 removed
62 unmodified lines63 // Event channel64 eventBox := util.NewEventBox()65 66 // 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 line86 // 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 lines103 index: uint32(index),104 colors: nil,105 rank: Rank{0, 0, uint32(index)}}106 107- trimmed, colors := extractColors(item.text)107+ trimmed, colors := ansiProcessor(item.text)108 item.text = trimmed109 item.colors = colors110 return &item111 })129 unmodified linesmodified:src/item_test.go+6 added-6 removed
79 unmodified lines80 // ++++++++ ++++++++++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}]90 91- 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 linesmodified:src/item.go+9 added-9 removed
5 unmodified lines6 7 // Offset holds two 32-bit integers denoting the offsets of a matched substring8 type Offset [2]int329 10-type ColorOffset struct {10+type colorOffset struct {11 offset [2]int3212 color int13 bold bool14 }4 unmodified lines19 origText *string20 transformed *Transformed21 index uint3222 offsets []Offset23- colors []AnsiOffset23+ colors []ansiOffset24 rank Rank25 }26 27 // Rank is used to sort the search result37 unmodified lines65 }66 return *i.text67 }68 69-func (item *Item) ColorOffsets(color int, bold bool, current bool) []ColorOffset {69+func (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 }77 78 // 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 lines108 // ++++++++ ++++++++++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 linesmodified:src/matcher.go+5 added-11 removed
131 unmodified lines132 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 }139 140 cancelled := util.NewAtomicBool(false)9 unmodified lines150 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 lines184 if count == numChunks {185 break186 }187 194- if !empty && m.reqBox.Peek(reqReset) {188+ if m.reqBox.Peek(reqReset) {189 return nil, wait()190 }191 192 if time.Now().Sub(startedAt) > progressMinDuration {5 unmodified lines198 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 }204 205 // Reset is called to interrupt/signal the ongoing search206 func (m *Matcher) Reset(chunks []*Chunk, patternRunes []rune, cancel bool, final bool) {10 unmodified linesmodified:src/merger.go+2 added
16 unmodified lines17 final bool18 count int19 }20 21+// PassMerger returns a new Merger that simply returns the items in the22+// original order23 func PassMerger(chunks *[]*Chunk, tac bool) *Merger {24 mg := Merger{25 chunks: chunks,26 tac: tac,88 unmodified linesmodified:src/terminal.go+6 added-6 removed
45 unmodified lines46 at time.Time47 text *string48 }49 50-type ByTimeOrder []selectedItem50+type byTimeOrder []selectedItem51 52-func (a ByTimeOrder) Len() int {52+func (a byTimeOrder) Len() int {53 return len(a)54 }55 56-func (a ByTimeOrder) Swap(i, j int) {56+func (a byTimeOrder) Swap(i, j int) {57 a[i], a[j] = a[j], a[i]58 }59 60-func (a ByTimeOrder) Less(i, j int) bool {60+func (a byTimeOrder) Less(i, j int) bool {61 return a[i].at.Before(a[j].at)62 }63 64 var _spinner = []string{`-`, `\`, `|`, `/`, `-`, `\`, `|`, `/`}94 unmodified lines159 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 lines308 }309 310 // 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