From 31eb6be473df4930679259cfa284ef4cc80d1d8c Mon Sep 17 00:00:00 2001 From: chzyer <0@0xdf.com> Date: Fri, 2 Sep 2016 20:10:31 +0800 Subject: [PATCH] support insert tag char (#74) --- complete_tag.go | 7 +++++++ readline.go | 3 +++ runebuf.go | 8 +++++++- runes.go | 4 ++++ 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 complete_tag.go diff --git a/complete_tag.go b/complete_tag.go new file mode 100644 index 0000000..2039458 --- /dev/null +++ b/complete_tag.go @@ -0,0 +1,7 @@ +package readline + +type TabCompleter struct{} + +func (t *TabCompleter) Do([]rune, int) ([][]rune, int) { + return [][]rune{[]rune("\t")}, 0 +} diff --git a/readline.go b/readline.go index 4b866fb..20a2108 100644 --- a/readline.go +++ b/readline.go @@ -93,6 +93,9 @@ func (c *Config) Init() error { c.EOFPrompt = "" } + if c.AutoComplete == nil { + c.AutoComplete = &TabCompleter{} + } if c.FuncGetWidth == nil { c.FuncGetWidth = GetScreenWidth } diff --git a/runebuf.go b/runebuf.go index e402aed..538832e 100644 --- a/runebuf.go +++ b/runebuf.go @@ -430,7 +430,13 @@ func (r *RuneBuffer) output() []byte { } } else { - buf.Write([]byte(string(r.buf))) + for idx := range r.buf { + if r.buf[idx] == '\t' { + buf.WriteString(strings.Repeat(" ", TabWidth)) + } else { + buf.WriteRune(r.buf[idx]) + } + } if r.isInLineEdge() { buf.Write([]byte(" \b")) } diff --git a/runes.go b/runes.go index b85af7a..8ed54ce 100644 --- a/runes.go +++ b/runes.go @@ -6,6 +6,7 @@ import ( ) var runes = Runes{} +var TabWidth = 4 type Runes struct{} @@ -98,6 +99,9 @@ var doubleWidth = []*unicode.RangeTable{ } func (Runes) Width(r rune) int { + if r == '\t' { + return TabWidth + } if unicode.IsOneOf(zeroWidth, r) { return 0 }