Implement delete for Input

This commit is contained in:
erroneousboat 2016-10-09 14:59:48 +02:00
parent 243883466b
commit 4988399280
2 changed files with 19 additions and 5 deletions

View File

@ -71,19 +71,26 @@ 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:len(i.Par.Text)]
i.Par.Text = i.Par.Text[0:i.CursorPosition] + key + i.Par.Text[i.CursorPosition:]
i.MoveCursorRight()
}
}
// Remove will remove a character at the place of the current CursorPosition
func (i *Input) Remove() {
// 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:len(i.Par.Text)]
i.Par.Text = i.Par.Text[0:i.CursorPosition-1] + i.Par.Text[i.CursorPosition:]
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:]
}
}
// MoveCursorRight will increase the current CursorPosition with 1
func (i *Input) MoveCursorRight() {
if i.CursorPosition < len(i.Par.Text) {

View File

@ -45,6 +45,8 @@ func anyKeyHandler(ctx *context.AppContext) func(termui.Event) {
actionBackSpace(ctx.View)
case "C-8":
actionBackSpace(ctx.View)
case "<delete>":
actionDelete(ctx.View)
case "<right>":
actionMoveCursorRight(ctx.View)
case "<left>":
@ -112,7 +114,12 @@ func actionInput(view *views.View, key string) {
}
func actionBackSpace(view *views.View) {
view.Input.Remove()
view.Input.Backspace()
termui.Render(view.Input)
}
func actionDelete(view *views.View) {
view.Input.Delete()
termui.Render(view.Input)
}