add set prompt

This commit is contained in:
Cheney 2015-09-27 18:54:26 +08:00
parent 8dc3117d78
commit 7537bea372
4 changed files with 25 additions and 4 deletions

View File

@ -12,8 +12,9 @@ import (
func usage(w io.Writer) {
io.WriteString(w, `
sayhello: start to display oneline log per second
bye: quit
setprompt <prompt>
say <hello>
bye
`[1:])
}
@ -22,6 +23,7 @@ var completer = readline.NewPrefixCompleter(
readline.PcItem("hello"),
readline.PcItem("bye"),
),
readline.PcItem("setprompt"),
readline.PcItem("bye"),
readline.PcItem("help"),
readline.PcItem("go",
@ -52,6 +54,13 @@ func main() {
switch {
case line == "help":
usage(l.Stderr())
case strings.HasPrefix(line, "setprompt"):
prompt := line[10:]
if prompt == "" {
log.Println("setprompt <prompt>")
break
}
l.SetPrompt(prompt)
case strings.HasPrefix(line, "say"):
line := strings.TrimSpace(line[3:])
if len(line) == 0 {

View File

@ -49,6 +49,10 @@ func NewOperation(t *Terminal, cfg *Config) *Operation {
return op
}
func (o *Operation) SetPrompt(s string) {
o.buf.SetPrompt(s)
}
func (o *Operation) ioloop() {
for {
keepInSearchMode := false

View File

@ -50,6 +50,10 @@ func New(prompt string) (*Instance, error) {
return NewEx(&Config{Prompt: prompt})
}
func (i *Instance) SetPrompt(s string) {
i.o.SetPrompt(s)
}
func (i *Instance) Stdout() io.Writer {
return i.o.Stdout()
}

View File

@ -16,12 +16,16 @@ type RuneBuffer struct {
func NewRuneBuffer(w io.Writer, prompt string) *RuneBuffer {
rb := &RuneBuffer{
prompt: []rune(prompt),
w: w,
}
rb.SetPrompt(prompt)
return rb
}
func (r *RuneBuffer) SetPrompt(prompt string) {
r.prompt = []rune(prompt)
}
func (r *RuneBuffer) CurrentWidth(x int) int {
return RunesWidth(r.buf[:x])
}