Fix View.Word()

This commit is contained in:
Roi Martin (@nibble_ds) 2014-01-23 23:42:52 +01:00
parent f51a568142
commit 7cc82f809e
1 changed files with 8 additions and 2 deletions

10
view.go
View File

@ -262,13 +262,13 @@ func (v *View) Word(x, y int) (string, error) {
return "", errors.New("invalid point") return "", errors.New("invalid point")
} }
l := string(v.lines[y]) l := string(v.lines[y])
nl := strings.LastIndex(l[:x], " ") nl := strings.LastIndexFunc(l[:x], indexFunc)
if nl == -1 { if nl == -1 {
nl = 0 nl = 0
} else { } else {
nl = nl + 1 nl = nl + 1
} }
nr := strings.Index(l[x:], " ") nr := strings.IndexFunc(l[x:], indexFunc)
if nr == -1 { if nr == -1 {
nr = len(l) nr = len(l)
} else { } else {
@ -276,3 +276,9 @@ func (v *View) Word(x, y int) (string, error) {
} }
return string(l[nl:nr]), nil return string(l[nl:nr]), nil
} }
// indexFunc allows to split lines by words taking into account spaces
// and 0
func indexFunc(r rune) bool {
return r == ' ' || r == 0
}