Skip to content

Commit

Permalink
add runes package
Browse files Browse the repository at this point in the history
  • Loading branch information
chzyer committed Oct 4, 2015
1 parent bfa8c1d commit 55809b4
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 224 deletions.
6 changes: 4 additions & 2 deletions complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"fmt"
"io"

"github.com/chzyer/readline/runes"
)

type AutoCompleter interface {
Expand Down Expand Up @@ -65,7 +67,7 @@ func (o *opCompleter) OnComplete() {
buf := o.op.buf
rs := buf.Runes()

if o.IsInCompleteMode() && RunesEqual(rs, o.candidateSource) {
if o.IsInCompleteMode() && runes.Equal(rs, o.candidateSource) {
o.EnterCompleteSelectMode()
o.doSelect()
return
Expand Down Expand Up @@ -176,7 +178,7 @@ func (o *opCompleter) CompleteRefresh() {
lineCnt := o.op.buf.CursorLineCount()
colWidth := 0
for _, c := range o.candidate {
w := RunesWidth(c)
w := runes.WidthAll(c)
if w > colWidth {
colWidth = w
}
Expand Down
6 changes: 4 additions & 2 deletions complete_helper.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package readline

import "github.com/chzyer/readline/runes"

type PrefixCompleter struct {
Name []rune
Children []*PrefixCompleter
Expand All @@ -25,14 +27,14 @@ func (p *PrefixCompleter) Do(line []rune, pos int) (newLine [][]rune, offset int
var lineCompleter *PrefixCompleter
for _, child := range p.Children {
if len(line) >= len(child.Name) {
if RunesHasPrefix(line, child.Name) {
if runes.HasPrefix(line, child.Name) {
newLine = append(newLine, child.Name)
offset = len(child.Name)
lineCompleter = child
goNext = true
}
} else {
if RunesHasPrefix(child.Name, line) {
if runes.HasPrefix(child.Name, line) {
newLine = append(newLine, child.Name[len(line):])
offset = len(line)
lineCompleter = child
Expand Down
8 changes: 5 additions & 3 deletions history.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"container/list"
"os"
"strings"

"github.com/chzyer/readline/runes"
)

type hisItem struct {
Expand Down Expand Up @@ -110,7 +112,7 @@ func (o *opHistory) FindHistoryBck(isNewSearch bool, rs []rune, start int) (int,
item = item[:start]
}
}
idx := RunesIndexBck(item, rs)
idx := runes.IndexAllBck(item, rs)
if idx < 0 {
continue
}
Expand All @@ -135,7 +137,7 @@ func (o *opHistory) FindHistoryFwd(isNewSearch bool, rs []rune, start int) (int,
continue
}
}
idx := RunesIndex(item, rs)
idx := runes.IndexAll(item, rs)
if idx < 0 {
continue
}
Expand Down Expand Up @@ -187,7 +189,7 @@ func (o *opHistory) NewHistory(current []rune) {
prev := back.Prev()
if prev != nil {
use := o.showItem(o.current.Value.(*hisItem))
if RunesEqual(use, prev.Value.(*hisItem).Source) {
if runes.Equal(use, prev.Value.(*hisItem).Source) {
o.current = o.history.Back()
o.current.Value.(*hisItem).Clean()
o.historyVer++
Expand Down
16 changes: 9 additions & 7 deletions runebuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package readline
import (
"bytes"
"io"

"github.com/chzyer/readline/runes"
)

type runeBufferBck struct {
Expand Down Expand Up @@ -44,11 +46,11 @@ func NewRuneBuffer(w io.Writer, prompt string) *RuneBuffer {
}

func (r *RuneBuffer) CurrentWidth(x int) int {
return RunesWidth(r.buf[:x])
return runes.WidthAll(r.buf[:x])
}

func (r *RuneBuffer) PromptLen() int {
return RunesWidth(RunesColorFilter(r.prompt))
return runes.WidthAll(runes.ColorFilter(r.prompt))
}

func (r *RuneBuffer) RuneSlice(i int) []rune {
Expand Down Expand Up @@ -267,7 +269,7 @@ func (r *RuneBuffer) MoveToLineEnd() {
}

func (r *RuneBuffer) LineCount() int {
return LineCount(RunesWidth(r.buf) + r.PromptLen())
return LineCount(runes.WidthAll(r.buf) + r.PromptLen())
}

func (r *RuneBuffer) MoveTo(ch rune, prevChar, reverse bool) (success bool) {
Expand Down Expand Up @@ -300,7 +302,7 @@ func (r *RuneBuffer) MoveTo(ch rune, prevChar, reverse bool) (success bool) {
}

func (r *RuneBuffer) IdxLine() int {
totalWidth := RunesWidth(r.buf[:r.idx]) + r.PromptLen()
totalWidth := runes.WidthAll(r.buf[:r.idx]) + r.PromptLen()
w := getWidth()
if w == 0 {
return 0
Expand Down Expand Up @@ -336,7 +338,7 @@ func (r *RuneBuffer) output() []byte {
buf.WriteString(string(r.prompt))
buf.Write([]byte(string(r.buf)))
if len(r.buf) > r.idx {
buf.Write(bytes.Repeat([]byte{'\b'}, RunesWidth(r.buf[r.idx:])))
buf.Write(runes.Backspace(r.buf[r.idx:]))
}
return buf.Bytes()
}
Expand All @@ -350,9 +352,9 @@ func (r *RuneBuffer) Reset() []rune {

func (r *RuneBuffer) calWidth(m int) int {
if m > 0 {
return RunesWidth(r.buf[r.idx : r.idx+m])
return runes.WidthAll(r.buf[r.idx : r.idx+m])
}
return RunesWidth(r.buf[r.idx+m : r.idx])
return runes.WidthAll(r.buf[r.idx+m : r.idx])
}

func (r *RuneBuffer) SetStyle(start, end int, style string) {
Expand Down
152 changes: 152 additions & 0 deletions runes/runes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package runes

import (
"bytes"
"unicode"
)

func Equal(a, b []rune) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}

// Search in runes from end to front
func IndexAllBck(r, sub []rune) int {
for i := len(r) - len(sub); i >= 0; i-- {
found := true
for j := 0; j < len(sub); j++ {
if r[i+j] != sub[j] {
found = false
break
}
}
if found {
return i
}
}
return -1
}

// Search in runes from front to end
func IndexAll(r, sub []rune) int {
for i := 0; i < len(r); i++ {
found := true
if len(r[i:]) < len(sub) {
return -1
}
for j := 0; j < len(sub); j++ {
if r[i+j] != sub[j] {
found = false
break
}
}
if found {
return i
}
}
return -1
}

func Index(r rune, rs []rune) int {
for i := 0; i < len(rs); i++ {
if rs[i] == r {
return i
}
}
return -1
}

func ColorFilter(r []rune) []rune {
newr := make([]rune, 0, len(r))
for pos := 0; pos < len(r); pos++ {
if r[pos] == '\033' && r[pos+1] == '[' {
idx := Index('m', r[pos+2:])
if idx == -1 {
continue
}
pos += idx + 2
continue
}
newr = append(newr, r[pos])
}
return newr
}

var zeroWidth = []*unicode.RangeTable{
unicode.Mn,
unicode.Me,
unicode.Cc,
unicode.Cf,
}

var doubleWidth = []*unicode.RangeTable{
unicode.Han,
unicode.Hangul,
unicode.Hiragana,
unicode.Katakana,
}

func Width(r rune) int {
if unicode.IsOneOf(zeroWidth, r) {
return 0
}
if unicode.IsOneOf(doubleWidth, r) {
return 2
}
return 1
}

func WidthAll(r []rune) (length int) {
for i := 0; i < len(r); i++ {
length += Width(r[i])
}
return
}

func Backspace(r []rune) []byte {
return bytes.Repeat([]byte{'\b'}, WidthAll(r))
}

func Copy(r []rune) []rune {
n := make([]rune, len(r))
copy(n, r)
return n
}

func HasPrefix(r, prefix []rune) bool {
if len(r) < len(prefix) {
return false
}
return Equal(r[:len(prefix)], prefix)
}

func Aggregate(candicate [][]rune) (same []rune, size int) {
for i := 0; i < len(candicate[0]); i++ {
for j := 0; j < len(candicate)-1; j++ {
if i >= len(candicate[j]) || i >= len(candicate[j+1]) {
goto aggregate
}
if candicate[j][i] != candicate[j+1][i] {
goto aggregate
}
}
size = i + 1
}
aggregate:
if size > 0 {
same = Copy(candicate[0][:size])
for i := 0; i < len(candicate); i++ {
n := Copy(candicate[i])
copy(n, n[size:])
candicate[i] = n[:len(n)-size]
}
}
return
}
68 changes: 68 additions & 0 deletions runes/runes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package runes

import (
"reflect"
"testing"
)

type twidth struct {
r []rune
length int
}

func TestRuneWidth(t *testing.T) {
runes := []twidth{
{[]rune("☭"), 1},
{[]rune("a"), 1},
{[]rune("你"), 2},
{ColorFilter([]rune("☭\033[13;1m你")), 3},
}
for _, r := range runes {
if w := WidthAll(r.r); w != r.length {
t.Fatal("result not expect", r.r, r.length, w)
}
}
}

type tagg struct {
r [][]rune
e [][]rune
length int
}

func TestAggRunes(t *testing.T) {
runes := []tagg{
{
[][]rune{[]rune("ab"), []rune("a"), []rune("abc")},
[][]rune{[]rune("b"), []rune(""), []rune("bc")},
1,
},
{
[][]rune{[]rune("addb"), []rune("ajkajsdf"), []rune("aasdfkc")},
[][]rune{[]rune("ddb"), []rune("jkajsdf"), []rune("asdfkc")},
1,
},
{
[][]rune{[]rune("ddb"), []rune("ajksdf"), []rune("aasdfkc")},
[][]rune{[]rune("ddb"), []rune("ajksdf"), []rune("aasdfkc")},
0,
},
{
[][]rune{[]rune("ddb"), []rune("ddajksdf"), []rune("ddaasdfkc")},
[][]rune{[]rune("b"), []rune("ajksdf"), []rune("aasdfkc")},
2,
},
}
for _, r := range runes {
same, off := Aggregate(r.r)
if off != r.length {
t.Fatal("result not expect", off)
}
if len(same) != off {
t.Fatal("result not expect", same)
}
if !reflect.DeepEqual(r.r, r.e) {
t.Fatal("result not expect")
}
}
}
Loading

0 comments on commit 55809b4

Please sign in to comment.