From 7cc82f809ef47b85168caab6d9834d61ccf4735c Mon Sep 17 00:00:00 2001 From: "Roi Martin (@nibble_ds)" Date: Thu, 23 Jan 2014 23:42:52 +0100 Subject: [PATCH] Fix View.Word() --- view.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/view.go b/view.go index d3ec6bf..db3c278 100644 --- a/view.go +++ b/view.go @@ -262,13 +262,13 @@ func (v *View) Word(x, y int) (string, error) { return "", errors.New("invalid point") } l := string(v.lines[y]) - nl := strings.LastIndex(l[:x], " ") + nl := strings.LastIndexFunc(l[:x], indexFunc) if nl == -1 { nl = 0 } else { nl = nl + 1 } - nr := strings.Index(l[x:], " ") + nr := strings.IndexFunc(l[x:], indexFunc) if nr == -1 { nr = len(l) } else { @@ -276,3 +276,9 @@ func (v *View) Word(x, y int) (string, error) { } 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 +}