Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toggle-all shortcut #219

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion fuzzyfinder.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type state struct {
items []string // All item names.
allMatched []matching.Matched // All items.
matched []matching.Matched // Matched items against the input.
toggleAll bool // true -> select all, false -> unselect all

// x is the current index of the prompt line.
x int
Expand Down Expand Up @@ -112,6 +113,7 @@ func (f *finder) initFinder(items []string, matched []matching.Matched, opt opt)
f.state.items = items
f.state.matched = matched
f.state.allMatched = matched
f.state.toggleAll = true

if opt.beginAtTop {
f.state.cursorY = len(f.state.matched) - 1
Expand Down Expand Up @@ -202,7 +204,14 @@ func (f *finder) _draw() {
}

// Number line
for i, r := range fmt.Sprintf("%d/%d", len(f.state.matched), len(f.state.items)) {
var numLine string
if f.opt.multi {
numLine = fmt.Sprintf("%d/%d (%d)", len(f.state.matched), len(f.state.items), len(f.state.selection))
} else {
numLine = fmt.Sprintf("%d/%d", len(f.state.matched), len(f.state.items))
}

for i, r := range numLine {
style := tcell.StyleDefault.
Foreground(tcell.ColorYellow).
Background(tcell.ColorDefault)
Expand Down Expand Up @@ -588,6 +597,29 @@ func (f *finder) readKey(ctx context.Context) error {
if f.state.cursorY > 0 {
f.state.cursorY--
}
case tcell.KeyCtrlT:
if !f.opt.multi {
return nil
}

selected_count := len(f.state.selection)
if selected_count == 0 {
f.state.toggleAll = true // If nothing is selected, select all regardless of the previous state
} else if selected_count == len(f.state.matched) {
f.state.toggleAll = false // If everything is selected, unselect all regardless of the previous state
}

for currentY := 0; currentY < len(f.state.matched); currentY++ {
idx := f.state.matched[currentY].Idx
if _, ok := f.state.selection[idx]; ok && !f.state.toggleAll {
delete(f.state.selection, idx)
} else if !ok && f.state.toggleAll {
f.state.selection[idx] = f.state.selectionIdx
f.state.selectionIdx++
}
}

f.state.toggleAll = !f.state.toggleAll
default:
if e.Rune() != 0 {
width, _ := f.term.Size()
Expand Down
36 changes: 36 additions & 0 deletions fuzzyfinder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,26 @@ func TestFindMulti(t *testing.T) {
{tcell.KeyTab, rune(tcell.KeyTab), tcell.ModNone},
{tcell.KeyTab, rune(tcell.KeyTab), tcell.ModNone},
}...), expected: []int{0}},
"select all": {events: keys([]input{
{tcell.KeyCtrlT, 'T', tcell.ModCtrl},
}...), expected: []int{0, 1, 2, 3, 4, 5, 6, 7, 8}},
"select all toggle": {events: keys([]input{
{tcell.KeyCtrlT, 'T', tcell.ModCtrl},
{tcell.KeyCtrlT, 'T', tcell.ModCtrl},
}...), expected: []int{0}},
"ignore previous 'select all' state": {events: keys(
concat(
repeat(input{tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, 9),
repeat(input{tcell.KeyTab, rune(tcell.KeyTab), tcell.ModNone}, 9),
[]input{{tcell.KeyCtrlT, 'T', tcell.ModCtrl}},
)...), expected: []int{0}},
"ignore previous 'unselect all' state": {events: keys(
concat(
[]input{{tcell.KeyCtrlT, 'T', tcell.ModCtrl}},
repeat(input{tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, 9),
repeat(input{tcell.KeyTab, rune(tcell.KeyTab), tcell.ModNone}, 9),
[]input{{tcell.KeyCtrlT, 'T', tcell.ModCtrl}},
)...), expected: []int{0, 1, 2, 3, 4, 5, 6, 7, 8}},
"empty result": {events: runes("fffffff"), abort: true},
"resize window": {events: []tcell.Event{
tcell.NewEventResize(10, 10),
Expand Down Expand Up @@ -753,6 +773,22 @@ func keys(inputs ...input) []tcell.Event {
return k
}

func concat(inputs ...[]input) []input {
var res []input
for _, in := range inputs {
res = append(res, in...)
}
return res
}

func repeat(in input, count int) []input {
res := make([]input, count)
for i := 0; i < count; i++ {
res[i] = in
}
return res
}

type input struct {
key tcell.Key
ch rune
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/ktr0731/go-fuzzyfinder

require (
github.com/gdamore/tcell/v2 v2.6.0
github.com/gdamore/tcell/v2 v2.7.4
github.com/google/go-cmp v0.6.0
github.com/google/gofuzz v1.2.0
github.com/ktr0731/go-ansisgr v0.1.0
Expand Down
18 changes: 12 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.6.0 h1:OKbluoP9VYmJwZwq/iLb4BxwKcwGthaa1YNBJIyCySg=
github.com/gdamore/tcell/v2 v2.6.0/go.mod h1:be9omFATkdr0D9qewWW3d+MEvl5dha+Etb5y65J2H8Y=
github.com/gdamore/tcell/v2 v2.7.4 h1:sg6/UnTM9jGpZU+oFYAsDahfchWAFW8Xx2yFinNSAYU=
github.com/gdamore/tcell/v2 v2.7.4/go.mod h1:dSXtXTSK0VsW1biw65DZLZ2NKr7j0qP/0J7ONmsraWg=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
Expand All @@ -11,7 +11,6 @@ github.com/ktr0731/go-ansisgr v0.1.0/go.mod h1:G9lxwgBwH0iey0Dw5YQd7n6PmQTwTuTM/
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY=
Expand All @@ -25,28 +24,35 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
8 changes: 4 additions & 4 deletions mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,9 @@ func parseAttrV2(fg, bg tcell.Color, attr tcell.AttrMask) string {
params = append(params, "39")
case fg > tcell.Color255:
r, g, b := fg.RGB()
params = append(params, "38", "2", fmt.Sprint(r), fmt.Sprint(g), fmt.Sprint(b))
params = append(params, "38", "2", fmt.Sprintf("%d", r), fmt.Sprintf("%d", g), fmt.Sprintf("%d", b))
default:
params = append(params, "38", "5", fmt.Sprint(fg-tcell.ColorValid))
params = append(params, "38", "5", fmt.Sprintf("%d", fg-tcell.ColorValid))
}

switch {
Expand All @@ -383,9 +383,9 @@ func parseAttrV2(fg, bg tcell.Color, attr tcell.AttrMask) string {
params = append(params, "49")
case bg > tcell.Color255:
r, g, b := bg.RGB()
params = append(params, "48", "2", fmt.Sprint(r), fmt.Sprint(g), fmt.Sprint(b))
params = append(params, "48", "2", fmt.Sprintf("%d", r), fmt.Sprintf("%d", g), fmt.Sprintf("%d", b))
default:
params = append(params, "48", "5", fmt.Sprint(bg-tcell.ColorValid))
params = append(params, "48", "5", fmt.Sprintf("%d", bg-tcell.ColorValid))
}

return fmt.Sprintf("\x1b[%sm", strings.Join(params, ";"))
Expand Down