Add non-ascii support

Fixes #30
This commit is contained in:
erroneousboat 2016-10-21 21:23:25 +02:00
parent 145241e31c
commit ada81bbfeb
2 changed files with 20 additions and 12 deletions

View File

@ -9,6 +9,7 @@ import (
// Input is the definition of an Input component
type Input struct {
Par *termui.Par
Text []rune
CursorPosition int
}
@ -16,6 +17,7 @@ type Input struct {
func CreateInput() *Input {
input := &Input{
Par: termui.NewPar(""),
Text: make([]rune, 0),
CursorPosition: 0,
}
@ -69,9 +71,13 @@ func (i *Input) SendMessage(svc *service.SlackService, channel string, message s
}
// Insert will insert a given key at the place of the current CursorPosition
func (i *Input) Insert(key string) {
if len(i.Par.Text) < i.Par.InnerBounds().Dx()-1 {
i.Par.Text = i.Par.Text[0:i.CursorPosition] + key + i.Par.Text[i.CursorPosition:]
func (i *Input) Insert(key rune) {
if len(i.Text) < i.Par.InnerBounds().Dx()-1 {
first := append(i.Text[0:i.CursorPosition], key)
i.Text = append(first, i.Text[i.CursorPosition:]...)
i.Par.Text = string(i.Text)
i.MoveCursorRight()
}
}
@ -79,15 +85,17 @@ func (i *Input) Insert(key string) {
// Backspace will remove a character in front of the CursorPosition
func (i *Input) Backspace() {
if i.CursorPosition > 0 {
i.Par.Text = i.Par.Text[0:i.CursorPosition-1] + i.Par.Text[i.CursorPosition:]
i.Text = append(i.Text[0:i.CursorPosition-1], i.Text[i.CursorPosition:]...)
i.Par.Text = string(i.Text)
i.MoveCursorLeft()
}
}
// Delete will remove a character at the CursorPosition
func (i *Input) Delete() {
if i.CursorPosition < len(i.Par.Text) {
i.Par.Text = i.Par.Text[0:i.CursorPosition] + i.Par.Text[i.CursorPosition+1:]
if i.CursorPosition < len(i.Text) {
i.Text = append(i.Text[0:i.CursorPosition], i.Text[i.CursorPosition+1:]...)
i.Par.Text = string(i.Text)
}
}
@ -119,7 +127,7 @@ func (i *Input) Clear() {
i.CursorPosition = 0
}
// Text returns the text currently in the input
func (i *Input) Text() string {
// GetText returns the text currently in the input
func (i *Input) GetText() string {
return i.Par.Text
}

View File

@ -58,7 +58,7 @@ func anyKeyHandler(ctx *context.AppContext) {
case termbox.KeyEnter:
actionSend(ctx)
case termbox.KeySpace:
actionInput(ctx.View, " ")
actionInput(ctx.View, ' ')
case termbox.KeyBackspace, termbox.KeyBackspace2:
actionBackSpace(ctx.View)
case termbox.KeyDelete:
@ -68,7 +68,7 @@ func anyKeyHandler(ctx *context.AppContext) {
case termbox.KeyArrowLeft:
actionMoveCursorLeft(ctx.View)
default:
actionInput(ctx.View, string(ev.Ch))
actionInput(ctx.View, ev.Ch)
}
}
}
@ -130,7 +130,7 @@ func actionResize(ctx *context.AppContext) {
termui.Render(termui.Body)
}
func actionInput(view *views.View, key string) {
func actionInput(view *views.View, key rune) {
view.Input.Insert(key)
termui.Render(view.Input)
}
@ -160,7 +160,7 @@ func actionSend(ctx *context.AppContext) {
// Clear message before sending, to combat
// quick succession of actionSend
message := ctx.View.Input.Text()
message := ctx.View.Input.GetText()
ctx.View.Input.Clear()
ctx.View.Refresh()