forked from mirror/readline
update example
This commit is contained in:
parent
80af385185
commit
a659448259
BIN
example/demo.gif
BIN
example/demo.gif
Binary file not shown.
Before Width: | Height: | Size: 666 KiB After Width: | Height: | Size: 1.0 MiB |
|
@ -18,7 +18,7 @@ bye: quit
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
l, err := readline.NewEx(&readline.Config{
|
l, err := readline.NewEx(&readline.Config{
|
||||||
Prompt: "home -> ",
|
Prompt: "home » ",
|
||||||
HistoryFile: "/tmp/readline.tmp",
|
HistoryFile: "/tmp/readline.tmp",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
14
runebuf.go
14
runebuf.go
|
@ -8,18 +8,22 @@ import (
|
||||||
type RuneBuffer struct {
|
type RuneBuffer struct {
|
||||||
buf []rune
|
buf []rune
|
||||||
idx int
|
idx int
|
||||||
prompt []byte
|
prompt []rune
|
||||||
w io.Writer
|
w io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRuneBuffer(w io.Writer, prompt string) *RuneBuffer {
|
func NewRuneBuffer(w io.Writer, prompt string) *RuneBuffer {
|
||||||
rb := &RuneBuffer{
|
rb := &RuneBuffer{
|
||||||
prompt: []byte(prompt),
|
prompt: []rune(prompt),
|
||||||
w: w,
|
w: w,
|
||||||
}
|
}
|
||||||
return rb
|
return rb
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *RuneBuffer) PromptLen() int {
|
||||||
|
return RunesWidth(r.prompt)
|
||||||
|
}
|
||||||
|
|
||||||
func (r *RuneBuffer) Runes() []rune {
|
func (r *RuneBuffer) Runes() []rune {
|
||||||
return r.buf
|
return r.buf
|
||||||
}
|
}
|
||||||
|
@ -189,11 +193,11 @@ func (r *RuneBuffer) MoveToLineEnd() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RuneBuffer) LineCount() int {
|
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 {
|
func (r *RuneBuffer) IdxLine() int {
|
||||||
totalWidth := RunesWidth(r.buf[:r.idx]) + len(r.prompt)
|
totalWidth := RunesWidth(r.buf[:r.idx]) + r.PromptLen()
|
||||||
w := getWidth()
|
w := getWidth()
|
||||||
line := 0
|
line := 0
|
||||||
for totalWidth >= w {
|
for totalWidth >= w {
|
||||||
|
@ -214,7 +218,7 @@ func (r *RuneBuffer) Refresh() {
|
||||||
func (r *RuneBuffer) Output() []byte {
|
func (r *RuneBuffer) Output() []byte {
|
||||||
buf := bytes.NewBuffer(nil)
|
buf := bytes.NewBuffer(nil)
|
||||||
buf.Write(r.CleanOutput())
|
buf.Write(r.CleanOutput())
|
||||||
buf.Write(r.prompt)
|
buf.WriteString(string(r.prompt))
|
||||||
buf.Write([]byte(string(r.buf)))
|
buf.Write([]byte(string(r.buf)))
|
||||||
if len(r.buf) > r.idx {
|
if len(r.buf) > r.idx {
|
||||||
buf.Write(bytes.Repeat([]byte{'\b'}, len(r.buf)-r.idx))
|
buf.Write(bytes.Repeat([]byte{'\b'}, len(r.buf)-r.idx))
|
||||||
|
|
|
@ -121,7 +121,7 @@ func (o *opSearch) SearchRefresh(x int) {
|
||||||
if x < 0 {
|
if x < 0 {
|
||||||
x = o.buf.idx
|
x = o.buf.idx
|
||||||
}
|
}
|
||||||
x += len(o.buf.prompt)
|
x += o.buf.PromptLen()
|
||||||
x = x % getWidth()
|
x = x % getWidth()
|
||||||
|
|
||||||
if o.markStart > 0 {
|
if o.markStart > 0 {
|
||||||
|
|
2
utils.go
2
utils.go
|
@ -124,7 +124,7 @@ func LineCount(w int) int {
|
||||||
|
|
||||||
func RunesWidth(r []rune) (length int) {
|
func RunesWidth(r []rune) (length int) {
|
||||||
for i := 0; i < len(r); i++ {
|
for i := 0; i < len(r); i++ {
|
||||||
if utf8.RuneLen(r[i]) > 1 {
|
if utf8.RuneLen(r[i]) > 3 {
|
||||||
length += 2
|
length += 2
|
||||||
} else {
|
} else {
|
||||||
length += 1
|
length += 1
|
||||||
|
|
Loading…
Reference in New Issue