From 6d8d25cf1fdf61f3f95abb0de34dee77f5d666ec Mon Sep 17 00:00:00 2001 From: erroneousboat Date: Sun, 3 Dec 2017 13:18:32 +0100 Subject: [PATCH] Add support for double width runes Fixes #61 --- components/input.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/components/input.go b/components/input.go index 26ac1e8..ce1ab20 100644 --- a/components/input.go +++ b/components/input.go @@ -2,6 +2,7 @@ package components import ( "github.com/erroneousboat/termui" + runewidth "github.com/mattn/go-runewidth" "github.com/erroneousboat/slack-term/service" ) @@ -34,8 +35,9 @@ func CreateInputComponent() *Input { func (i *Input) Buffer() termui.Buffer { buf := i.Par.Buffer() - // Set visible cursor + // Set visible cursor, get char at screen cursor position char := buf.At(i.Par.InnerX()+i.CursorPositionScreen, i.Par.Block.InnerY()) + buf.Set( i.Par.InnerX()+i.CursorPositionScreen, i.Par.Block.InnerY(), @@ -84,16 +86,15 @@ func (i *Input) Insert(key rune) { // Combine left and right side i.Text = append(left, i.Text[i.CursorPositionText:]...) - // Set visible range of component i.MoveCursorRight() } // Backspace will remove a character in front of the CursorPositionText func (i *Input) Backspace() { if i.CursorPositionText > 0 { - i.Text = append(i.Text[0:i.CursorPositionText-1], i.Text[i.CursorPositionText:]...) - i.Par.Text = string(i.Text[i.Offset:]) i.MoveCursorLeft() + i.Text = append(i.Text[0:i.CursorPositionText], i.Text[i.CursorPositionText+1:]...) + i.Par.Text = string(i.Text[i.Offset:]) } } @@ -134,7 +135,7 @@ func (i *Input) ScrollLeft() { i.Offset-- } } else { - i.CursorPositionScreen-- + i.CursorPositionScreen -= i.GetRuneWidthRight() } } @@ -148,10 +149,22 @@ func (i *Input) ScrollRight() { i.Offset++ } } else { - i.CursorPositionScreen++ + i.CursorPositionScreen += i.GetRuneWidthLeft() } } +// GetRuneWidthLeft will get the width of a rune on the left side +// of the CursorPositionText +func (i *Input) GetRuneWidthLeft() int { + return runewidth.RuneWidth(i.Text[i.CursorPositionText-1]) +} + +// GetRuneWidthLeft will get the width of a rune on the right side +// of the CursorPositionText +func (i *Input) GetRuneWidthRight() int { + return runewidth.RuneWidth(i.Text[i.CursorPositionText]) +} + // IsEmpty will return true when the input is empty func (i *Input) IsEmpty() bool { if i.Par.Text == "" {