diff --git a/example/demo.gif b/example/demo.gif index 64fbb6c..c26c2e0 100644 Binary files a/example/demo.gif and b/example/demo.gif differ diff --git a/example/main.go b/example/main.go index 1b8865a..92d334a 100644 --- a/example/main.go +++ b/example/main.go @@ -18,7 +18,7 @@ bye: quit func main() { l, err := readline.NewEx(&readline.Config{ - Prompt: "home -> ", + Prompt: "home ยป ", HistoryFile: "/tmp/readline.tmp", }) if err != nil { diff --git a/runebuf.go b/runebuf.go index 5ed9af3..decb923 100644 --- a/runebuf.go +++ b/runebuf.go @@ -8,18 +8,22 @@ import ( type RuneBuffer struct { buf []rune idx int - prompt []byte + prompt []rune w io.Writer } func NewRuneBuffer(w io.Writer, prompt string) *RuneBuffer { rb := &RuneBuffer{ - prompt: []byte(prompt), + prompt: []rune(prompt), w: w, } return rb } +func (r *RuneBuffer) PromptLen() int { + return RunesWidth(r.prompt) +} + func (r *RuneBuffer) Runes() []rune { return r.buf } @@ -189,11 +193,11 @@ func (r *RuneBuffer) MoveToLineEnd() { } func (r *RuneBuffer) LineCount() int { - return LineCount(RunesWidth(r.buf) + len(r.prompt)) + return LineCount(RunesWidth(r.buf) + r.PromptLen()) } func (r *RuneBuffer) IdxLine() int { - totalWidth := RunesWidth(r.buf[:r.idx]) + len(r.prompt) + totalWidth := RunesWidth(r.buf[:r.idx]) + r.PromptLen() w := getWidth() line := 0 for totalWidth >= w { @@ -214,7 +218,7 @@ func (r *RuneBuffer) Refresh() { func (r *RuneBuffer) Output() []byte { buf := bytes.NewBuffer(nil) buf.Write(r.CleanOutput()) - buf.Write(r.prompt) + buf.WriteString(string(r.prompt)) buf.Write([]byte(string(r.buf))) if len(r.buf) > r.idx { buf.Write(bytes.Repeat([]byte{'\b'}, len(r.buf)-r.idx)) diff --git a/search.go b/search.go index 0e6dfbd..5edd8c6 100644 --- a/search.go +++ b/search.go @@ -121,7 +121,7 @@ func (o *opSearch) SearchRefresh(x int) { if x < 0 { x = o.buf.idx } - x += len(o.buf.prompt) + x += o.buf.PromptLen() x = x % getWidth() if o.markStart > 0 { diff --git a/utils.go b/utils.go index 3b52c42..4080904 100644 --- a/utils.go +++ b/utils.go @@ -124,7 +124,7 @@ func LineCount(w int) int { func RunesWidth(r []rune) (length int) { for i := 0; i < len(r); i++ { - if utf8.RuneLen(r[i]) > 1 { + if utf8.RuneLen(r[i]) > 3 { length += 2 } else { length += 1