Skip to content

Commit 0801396

Browse files
committed
choice: Menu add showQuit option
1 parent 3d527ab commit 0801396

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

choice/choice.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func choiceStr(choice any) string {
2525
}
2626

2727
// Menu function is used to generate a string representation of the menu, associating choices with numbers.
28-
func Menu[E any](choices []E) string {
28+
func Menu[E any](choices []E, showQuit bool) string {
2929
if len(choices) == 0 {
3030
return ""
3131
}
@@ -38,7 +38,9 @@ func Menu[E any](choices []E) string {
3838
for i, choice := range choices {
3939
fmt.Fprintf(&b, "%s. %s\n", fmt.Sprintf(option, i+1), choiceStr(choice))
4040
}
41-
fmt.Fprintf(&b, "%s. Quit\n", fmt.Sprintf(fmt.Sprintf("%%%ds", digit), "q"))
41+
if showQuit {
42+
fmt.Fprintf(&b, "%s. Quit\n", fmt.Sprintf(fmt.Sprintf("%%%ds", digit), "q"))
43+
}
4244
return b.String()
4345
}
4446

choice/choice_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestMenu(t *testing.T) {
3232
10. jj
3333
q. Quit
3434
`
35-
if s := Menu(choices); s != expect {
35+
if s := Menu(choices, true); s != expect {
3636
t.Errorf("expected %q; got %q", expect, s)
3737
}
3838
}

0 commit comments

Comments
 (0)