Private
Public Access
1
0
Files
Kordophone/prompt/prompt.go

64 lines
1.1 KiB
Go

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()
}