diff --git a/example/main.go b/example/main.go index 8875e14..87f9eba 100644 --- a/example/main.go +++ b/example/main.go @@ -12,8 +12,9 @@ import ( func usage(w io.Writer) { io.WriteString(w, ` -sayhello: start to display oneline log per second -bye: quit +setprompt +say +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 ") + break + } + l.SetPrompt(prompt) case strings.HasPrefix(line, "say"): line := strings.TrimSpace(line[3:]) if len(line) == 0 { diff --git a/operation.go b/operation.go index 85251a2..dc2cd02 100644 --- a/operation.go +++ b/operation.go @@ -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 diff --git a/readline.go b/readline.go index 76a25d4..e4032ad 100644 --- a/readline.go +++ b/readline.go @@ -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() } diff --git a/runebuf.go b/runebuf.go index 41607ed..724d6ef 100644 --- a/runebuf.go +++ b/runebuf.go @@ -16,12 +16,16 @@ type RuneBuffer struct { func NewRuneBuffer(w io.Writer, prompt string) *RuneBuffer { rb := &RuneBuffer{ - prompt: []rune(prompt), - w: w, + 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]) }