Skip to content

Commit

Permalink
add set prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
chzyer committed Sep 27, 2015
1 parent 8dc3117 commit 7537bea
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
13 changes: 11 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:])
}

Expand All @@ -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",
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions readline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
8 changes: 6 additions & 2 deletions runebuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand Down

0 comments on commit 7537bea

Please sign in to comment.