Private
Public Access
1
0

Prompt: adds interactive prompt (that does nothing right now)

This commit is contained in:
James Magahern
2023-06-22 12:03:37 -07:00
parent 27de41ddb2
commit a4c25df183
4 changed files with 96 additions and 4 deletions

63
prompt/prompt.go Normal file
View File

@@ -0,0 +1,63 @@
package prompt
import (
"io"
"github.com/rs/zerolog/log"
"github.com/chzyer/readline"
)
type Prompt struct {
rl *readline.Instance
}
func NewPrompt() *Prompt {
rl, err := readline.NewEx(&readline.Config{
Prompt: "\033[31m»\033[0m ",
HistoryFile: "/tmp/readline.tmp",
InterruptPrompt: "^C",
EOFPrompt: "exit",
HistorySearchFold: true,
})
if err != nil {
panic(err)
}
return &Prompt{
rl: rl,
}
}
func (p *Prompt) StartInteractive() error {
for {
line, err := p.rl.Readline()
if err == readline.ErrInterrupt {
if len(line) == 0 {
break
} else {
continue
}
} else if err == io.EOF {
break
}
switch {
case line == "exit":
return nil
default:
log.Info().Msgf("Line: %s", line)
}
}
return nil
}
func (p *Prompt) CleanAndRefreshForLogging() {
p.rl.Clean()
// xxx: Lazy hack to make sure this runs _after_ the log is written.
go p.rl.Refresh()
}