Skip to content

Commit 313f583

Browse files
authored
Merge pull request #62 from reeflective/dev
dev
2 parents dbc7d95 + fd2ba57 commit 313f583

File tree

11 files changed

+52
-45
lines changed

11 files changed

+52
-45
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,5 @@ Other contributions, as well as bug fixes and reviews are also welcome.
106106
The following is a currently moving list of possible enhancements to be made in order to reach `v1.0`:
107107
- [ ] Ensure to the best extent possible a thread-safe access to the command API.
108108
- [ ] Clearer integration/alignment of the various I/O references between raw readline and commands.
109-
- [ ] Clearer and sane model for asynchronous control/cancel of commands.
109+
- [ ] Clearer and sane model for asynchronous control/cancel of commands (with OnKillRun in cobra)
110110
- [ ] Test suite for most important or risky code paths.

completer.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (c *Console) complete(line []rune, pos int) readline.Completions {
5656

5757
// Assign both completions and command/flags/args usage strings.
5858
comps := readline.CompleteRaw(raw)
59-
comps = comps.Usage(completions.Usage)
59+
comps = comps.Usage("%s", completions.Usage)
6060
comps = c.justifyCommandComps(comps)
6161

6262
// If any errors arose from the completion call itself.
@@ -251,8 +251,10 @@ func splitCompWords(input string) (words []string, remainder string, err error)
251251
if len(next) == 0 {
252252
remainder = string(escapeChar)
253253
err = errUnterminatedEscape
254-
return
254+
255+
return words, remainder, err
255256
}
257+
256258
c2, l2 := utf8.DecodeRuneInString(next)
257259
if c2 == '\n' {
258260
input = next[l2:]
@@ -261,6 +263,7 @@ func splitCompWords(input string) (words []string, remainder string, err error)
261263
}
262264

263265
var word string
266+
264267
word, input, err = splitCompWord(input, &buf)
265268
if err != nil {
266269
return words, word + input, err

console.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func New(app string) *Console {
106106

107107
// Defaults
108108
console.EmptyChars = []rune{' ', '\t'}
109+
109110
return console
110111
}
111112

errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func (e Err) Error() string {
5656
if len(e.message) > 0 {
5757
return fmt.Sprintf("%s: %s", e.message, e.err.Error())
5858
}
59+
5960
return e.err.Error()
6061
}
6162

example/interrupt.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313
// readline receives an io.EOF error, which is returned with CtrlD.
1414
func exitCtrlD(c *console.Console) {
1515
reader := bufio.NewReader(os.Stdin)
16+
1617
fmt.Print("Confirm exit (Y/y): ")
18+
1719
text, _ := reader.ReadString('\n')
1820
answer := strings.TrimSpace(text)
1921

example/main-commands.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ func mainMenuCommands(app *console.Console) console.Commands {
571571
}
572572

573573
flagMap := make(carapace.ActionMap)
574+
574575
cmd.Flags().VisitAll(func(f *pflag.Flag) {
575576
if f.Name == "file" || strings.Contains(f.Usage, "file") {
576577
flagMap[f.Name] = carapace.ActionFiles()
@@ -583,6 +584,7 @@ func mainMenuCommands(app *console.Console) console.Commands {
583584
for i := 0; i < 10; i++ {
584585
hosts = append(hosts, fmt.Sprintf("host%d", i))
585586
}
587+
586588
c.PositionalCompletion(carapace.ActionValues(hosts...))
587589
}
588590

example/menu.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func makeClientCommands(app *console.Console) console.Commands {
6363
app.Printf("This message is more important, printing it below the prompt and in every menu")
6464
return
6565
}
66-
menu.TransientPrintf(messages[count])
66+
menu.TransientPrintf("%s", messages[count])
6767
count++
6868
}
6969
}()
@@ -136,7 +136,7 @@ func makeClientCommands(app *console.Console) console.Commands {
136136
if count == 5 {
137137
return nil
138138
}
139-
menu.TransientPrintf(messages[count] + "\n")
139+
menu.TransientPrintf("%s", messages[count]+"\n")
140140
count++
141141
}
142142
}
@@ -164,7 +164,6 @@ func setupPrompt(m *console.Menu) {
164164
return fmt.Sprintf(prompt, dir)
165165
}
166166

167-
p.Secondary = func() string { return ">" }
168167
p.Right = func() string {
169168
return "\x1b[1;30m" + time.Now().Format("03:04:05.000") + "\x1b[0m"
170169
}

go.mod

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
module github.com/reeflective/console
22

3-
go 1.21
3+
go 1.23.6
44

55
require (
66
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
7-
github.com/reeflective/readline v1.0.15
7+
github.com/reeflective/readline v1.1.1
88
github.com/rsteube/carapace v0.46.3-0.20231214181515-27e49f3c3b69
99
github.com/spf13/cobra v1.8.1
10-
github.com/spf13/pflag v1.0.5
11-
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa
10+
github.com/spf13/pflag v1.0.6
11+
golang.org/x/exp v0.0.0-20250210185358-939b2ce775ac
1212
mvdan.cc/sh/v3 v3.7.0
1313
)
1414

1515
require (
16-
github.com/carapace-sh/carapace v1.1.3 // indirect
17-
github.com/carapace-sh/carapace-shlex v1.0.1 // indirect
1816
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1917
github.com/rivo/uniseg v0.4.7 // indirect
2018
github.com/rsteube/carapace-shlex v0.1.1 // indirect
21-
golang.org/x/sys v0.24.0 // indirect
22-
golang.org/x/term v0.23.0 // indirect
19+
golang.org/x/sys v0.30.0 // indirect
20+
golang.org/x/term v0.29.0 // indirect
2321
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
2422
gopkg.in/yaml.v3 v3.0.1 // indirect
2523
)

go.sum

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
github.com/carapace-sh/carapace v1.1.3 h1:EpkXrD7+I8TpvdnwGCrPnaDTS24dO514dyUiG+8QCj4=
2-
github.com/carapace-sh/carapace v1.1.3/go.mod h1:djegtVDi/3duSAqZNU+/nCq7XtDRMRZUb5bW0O/HnEs=
3-
github.com/carapace-sh/carapace-shlex v1.0.1 h1:ww0JCgWpOVuqWG7k3724pJ18Lq8gh5pHQs9j3ojUs1c=
4-
github.com/carapace-sh/carapace-shlex v1.0.1/go.mod h1:lJ4ZsdxytE0wHJ8Ta9S7Qq0XpjgjU0mdfCqiI2FHx7M=
51
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
62
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
73
github.com/frankban/quicktest v1.14.5 h1:dfYrrRyLtiqT9GyKXgdh+k4inNeTvmGbuSgZ3lx3GhA=
84
github.com/frankban/quicktest v1.14.5/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
9-
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
10-
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
5+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
6+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
117
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
128
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
139
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
@@ -19,12 +15,10 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
1915
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
2016
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
2117
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
22-
github.com/reeflective/readline v1.0.13 h1:TeJmYw9B7VRPZWfNExr9QHxL1m0iSicyqBSQIRn39Ss=
23-
github.com/reeflective/readline v1.0.13/go.mod h1:3iOe/qyb2jEy0KqLrNlb/CojBVqxga9ACqz/VU22H6A=
2418
github.com/reeflective/readline v1.0.15 h1:uB/M1sAc2yZGO14Ujgr/imLwQXqGdOhDDWAEHF+MBaE=
2519
github.com/reeflective/readline v1.0.15/go.mod h1:3iOe/qyb2jEy0KqLrNlb/CojBVqxga9ACqz/VU22H6A=
26-
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
27-
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
20+
github.com/reeflective/readline v1.1.1 h1:gplCdkwknFmly5BFBwVFJnMmQ/nWMrQvtkk7HizvGV0=
21+
github.com/reeflective/readline v1.1.1/go.mod h1:CwNkh9BmFBBCSO6mdDaNWb34rOqQsI9eYbxyqvOEazY=
2822
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
2923
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
3024
github.com/rogpeppe/go-internal v1.10.1-0.20230524175051-ec119421bb97 h1:3RPlVWzZ/PDqmVuf/FKHARG5EMid/tl7cv54Sw/QRVY=
@@ -34,24 +28,18 @@ github.com/rsteube/carapace v0.46.3-0.20231214181515-27e49f3c3b69/go.mod h1:4ZC5
3428
github.com/rsteube/carapace-shlex v0.1.1 h1:fRQEBBKyYKm4TXUabm4tzH904iFWSmXJl3UZhMfQNYU=
3529
github.com/rsteube/carapace-shlex v0.1.1/go.mod h1:zPw1dOFwvLPKStUy9g2BYKanI6bsQMATzDMYQQybo3o=
3630
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
37-
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
3831
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
3932
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
4033
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
41-
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
4234
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
43-
golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611 h1:qCEDpW1G+vcj3Y7Fy52pEM1AWm3abj8WimGYejI3SC4=
44-
golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
45-
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa h1:ELnwvuAXPNtPk1TJRuGkI9fDTwym6AYBu0qzT8AcHdI=
46-
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
47-
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
48-
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
49-
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
50-
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
51-
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
52-
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
53-
golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
54-
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
35+
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
36+
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
37+
golang.org/x/exp v0.0.0-20250210185358-939b2ce775ac h1:l5+whBCLH3iH2ZNHYLbAe58bo7yrN4mVcnkHDYz5vvs=
38+
golang.org/x/exp v0.0.0-20250210185358-939b2ce775ac/go.mod h1:hH+7mtFmImwwcMvScyxUhjuVHR3HGaDPMn9rMSUUbxo=
39+
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
40+
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
41+
golang.org/x/term v0.29.0 h1:L6pJp37ocefwRRtYPKSWOWzOtWSxVajvz2ldH/xi3iU=
42+
golang.org/x/term v0.29.0/go.mod h1:6bl4lRlvVuDgSf3179VpIxBF0o10JUpXWOnI7nErv7s=
5543
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
5644
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
5745
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

line.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func split(input string, hl bool) (words []string, remainder string, err error)
9191
}
9292

9393
input = input[l:]
94+
9495
continue
9596
} else if c == escapeChar {
9697
// Look ahead for escaped newline so we can skip over it
@@ -99,9 +100,12 @@ func split(input string, hl bool) (words []string, remainder string, err error)
99100
if hl {
100101
remainder = string(escapeChar)
101102
}
103+
102104
err = errUnterminatedEscape
103-
return
105+
106+
return words, remainder, err
104107
}
108+
105109
c2, l2 := utf8.DecodeRuneInString(next)
106110
if c2 == '\n' {
107111
if hl {
@@ -111,20 +115,25 @@ func split(input string, hl bool) (words []string, remainder string, err error)
111115
words[len(words)-1] += string(c) + string(c2)
112116
}
113117
}
118+
114119
input = next[l2:]
120+
115121
continue
116122
}
117123
}
118124

119125
var word string
126+
120127
word, input, err = splitWord(input, &buf, hl)
121128
if err != nil {
122129
remainder = input
123-
return
130+
return words, remainder, err
124131
}
132+
125133
words = append(words, word)
126134
}
127-
return
135+
136+
return words, remainder, err
128137
}
129138

130139
// splitWord has been modified to return the remainder of the input (the part that has not been
@@ -185,6 +194,7 @@ escape:
185194
}
186195
input = input[l:]
187196
}
197+
188198
goto raw
189199

190200
single:
@@ -270,11 +280,13 @@ func trimSpacesMatch(remain []string) (trimmed []string) {
270280

271281
func (c *Console) lineEmpty(line string) bool {
272282
empty := true
283+
273284
for _, r := range line {
274285
if !strings.ContainsRune(string(c.EmptyChars), r) {
275286
empty = false
276287
break
277288
}
278289
}
290+
279291
return empty
280292
}

0 commit comments

Comments
 (0)