Skip to content

Commit f48f177

Browse files
authored
Add search for cli menu (#78)
* feat: Implement search for cli menu * chore(internal/utils): Add tests for helper functions * refactor: Fix sonar code smells * fix(scripts): Fix style fmt script * style: Gofmt * refactor: Fix codecl warnings * refactor: Fix sonar bugs * tests: Improve tests coverage
1 parent ca984a1 commit f48f177

File tree

6 files changed

+145
-9
lines changed

6 files changed

+145
-9
lines changed

cmd/aoc-cli/handlers.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/briandowns/spinner"
1313
"github.com/manifoldco/promptui"
14+
promptlist "github.com/manifoldco/promptui/list"
1415
log "github.com/obalunenko/logger"
1516
"github.com/urfave/cli/v2"
1617

@@ -73,19 +74,21 @@ func menu(ctx context.Context) cli.ActionFunc {
7374

7475
years := puzzles.GetYears()
7576

77+
items := makeMenuItemsList(years, exit)
78+
7679
prompt := promptui.Select{
7780
Label: "Years menu (exit' for exit)",
78-
Items: append(years, exit),
81+
Items: items,
7982
Size: pageSize,
8083
CursorPos: 0,
8184
IsVimMode: false,
8285
HideHelp: false,
8386
HideSelected: false,
8487
Templates: nil,
8588
Keys: nil,
86-
Searcher: nil,
89+
Searcher: searcher(items),
8790
StartInSearchMode: false,
88-
Pointer: nil,
91+
Pointer: promptui.DefaultCursor,
8992
Stdin: nil,
9093
Stdout: nil,
9194
}
@@ -97,17 +100,19 @@ func menu(ctx context.Context) cli.ActionFunc {
97100
func menuPuzzle(ctx context.Context, year string) error {
98101
solvers := puzzles.DaysByYear(year)
99102

103+
items := makeMenuItemsList(solvers, back, exit)
104+
100105
prompt := promptui.Select{
101106
Label: "Puzzles menu (exit' for exit; back - to return to year selection)",
102-
Items: append(solvers, back, exit),
107+
Items: items,
103108
Size: pageSize,
104109
CursorPos: 0,
105110
IsVimMode: false,
106111
HideHelp: false,
107112
HideSelected: false,
108113
Templates: nil,
109114
Keys: nil,
110-
Searcher: nil,
115+
Searcher: searcher(items),
111116
StartInSearchMode: false,
112117
Pointer: promptui.DefaultCursor,
113118
Stdin: nil,
@@ -117,6 +122,28 @@ func menuPuzzle(ctx context.Context, year string) error {
117122
return handlePuzzleChoices(ctx, year, prompt)
118123
}
119124

125+
func makeMenuItemsList(list []string, commands ...string) []string {
126+
items := make([]string, 0, len(list)+len(commands))
127+
128+
items = append(items, list...)
129+
130+
items = append(items, commands...)
131+
132+
return items
133+
}
134+
135+
func searcher(items []string) promptlist.Searcher {
136+
return func(input string, index int) bool {
137+
itm := items[index]
138+
139+
itm = strings.ReplaceAll(strings.ToLower(itm), " ", "")
140+
141+
input = strings.ReplaceAll(strings.ToLower(input), " ", "")
142+
143+
return strings.Contains(itm, input)
144+
}
145+
}
146+
120147
func handleYearChoices(ctx context.Context, opt promptui.Select) error {
121148
for {
122149
_, choice, err := opt.Run()

cmd/aoc-cli/handlers_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_makeMenuItemsList(t *testing.T) {
10+
type args struct {
11+
list []string
12+
commands []string
13+
}
14+
15+
tests := []struct {
16+
name string
17+
args args
18+
want []string
19+
}{
20+
{
21+
name: "without commands",
22+
args: args{
23+
list: []string{"1", "2", "3"},
24+
commands: nil,
25+
},
26+
want: []string{"1", "2", "3"},
27+
},
28+
{
29+
name: "with commands",
30+
args: args{
31+
list: []string{"1", "2", "3"},
32+
commands: []string{"cmd1", "cmd2"},
33+
},
34+
want: []string{"1", "2", "3", "cmd1", "cmd2"},
35+
},
36+
}
37+
38+
for _, tt := range tests {
39+
t.Run(tt.name, func(t *testing.T) {
40+
got := makeMenuItemsList(tt.args.list, tt.args.commands...)
41+
42+
assert.Equal(t, tt.want, got)
43+
})
44+
}
45+
}
46+
47+
func Test_searcher(t *testing.T) {
48+
items := makeMenuItemsList([]string{"one", "two", "three"}, exit)
49+
50+
s := searcher(items)
51+
52+
assert.True(t, s("o", 0))
53+
54+
assert.Panics(t, func() {
55+
s("o", 10)
56+
})
57+
58+
assert.True(t, s("t", 2))
59+
60+
assert.False(t, s("1", 2))
61+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package utils
2+
3+
import (
4+
"io"
5+
"path/filepath"
6+
"strings"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestReaderFromFile(t *testing.T) {
14+
type args struct {
15+
tb testing.TB
16+
fpath string
17+
}
18+
19+
tests := []struct {
20+
name string
21+
args args
22+
want io.Reader
23+
}{
24+
{
25+
name: "",
26+
args: args{
27+
tb: t,
28+
fpath: filepath.Join("testdata", "reader.txt"),
29+
},
30+
want: strings.NewReader("Hello there!\n"),
31+
},
32+
}
33+
34+
for _, tt := range tests {
35+
t.Run(tt.name, func(t *testing.T) {
36+
got := ReaderFromFile(tt.args.tb, tt.args.fpath)
37+
38+
gotR, err := io.ReadAll(got)
39+
require.NoError(t, err)
40+
41+
wantR, err := io.ReadAll(tt.want)
42+
require.NoError(t, err)
43+
44+
assert.Equal(t, string(wantR), string(gotR))
45+
})
46+
}
47+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello there!

internal/puzzles/solutions/2016/day02/solution.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ func (s solution) Part2(input io.Reader) (string, error) {
3636
func part2(input io.Reader) (string, error) {
3737
kpd := loadKeypadPart2()
3838

39-
return getPassword(kpd, input)
39+
return getDoorCode(kpd, input)
4040
}
4141

4242
func part1(input io.Reader) (string, error) {
4343
kpd := loadKeypadPart1()
4444

45-
return getPassword(kpd, input)
45+
return getDoorCode(kpd, input)
4646
}
4747

48-
func getPassword(kpd keypad, input io.Reader) (string, error) {
48+
func getDoorCode(kpd keypad, input io.Reader) (string, error) {
4949
reader := bufio.NewReader(input)
5050

5151
var pwd strings.Builder

scripts/style/fmt.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ checkInstalled 'gofmt'
1515

1616
GO_FILES=$(find . -type f -name '*.go' | grep -v 'vendor' | grep -v '.git')
1717

18-
gofmt -s -w -l ${GO_FILES}
18+
gofmt -s -w ${GO_FILES}
1919

2020
echo "${SCRIPT_NAME} done."

0 commit comments

Comments
 (0)