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

View File

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

View File

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

View File

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