Skip to content

Commit

Permalink
Add support for double width runes
Browse files Browse the repository at this point in the history
Fixes #61
  • Loading branch information
jpbruinsslot committed Dec 3, 2017
1 parent 9a02940 commit 1d33596
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions components/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package components

import (
"github.com/erroneousboat/termui"
runewidth "github.com/mattn/go-runewidth"

"github.com/erroneousboat/slack-term/service"
)
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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:])
}
}

Expand Down Expand Up @@ -134,7 +135,7 @@ func (i *Input) ScrollLeft() {
i.Offset--
}
} else {
i.CursorPositionScreen--
i.CursorPositionScreen -= i.GetRuneWidthRight()
}
}

Expand All @@ -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 == "" {
Expand Down

0 comments on commit 1d33596

Please sign in to comment.