From fee679b4d02b8b463c5a1dcc87c7df23f33c23e9 Mon Sep 17 00:00:00 2001 From: erroneousboat Date: Tue, 13 Sep 2016 21:02:02 +0200 Subject: [PATCH] Add cursor functionality --- src/components/input.go | 49 +++++++++++++++++++++-------------------- src/views/chat.go | 4 ++-- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/components/input.go b/src/components/input.go index c1e18d0..4491703 100644 --- a/src/components/input.go +++ b/src/components/input.go @@ -3,7 +3,7 @@ package components import "github.com/gizak/termui" type Input struct { - Block *termui.Par + Par *termui.Par CursorPosition int CursorFgColor termui.Attribute CursorBgColor termui.Attribute @@ -11,88 +11,89 @@ type Input struct { func CreateInput() *Input { input := &Input{ - Block: termui.NewPar(""), + Par: termui.NewPar(""), CursorPosition: 0, CursorBgColor: termui.ColorBlack, CursorFgColor: termui.ColorWhite, } - input.Block.Height = 3 + input.Par.Height = 3 return input } // implements interface termui.Bufferer func (i *Input) Buffer() termui.Buffer { - return i.Block.Buffer() + buf := i.Par.Buffer() + + // Set cursor + char := buf.At(i.Par.InnerX()+i.CursorPosition, i.Par.Block.InnerY()) + buf.Set( + i.Par.InnerX()+i.CursorPosition, + i.Par.Block.InnerY(), + termui.Cell{Ch: char.Ch, Fg: termui.ColorBlack, Bg: termui.ColorWhite}, + ) + + return buf } // implements interface termui.GridBufferer func (i *Input) GetHeight() int { - return i.Block.GetHeight() + return i.Par.Block.GetHeight() } // implements interface termui.GridBufferer func (i *Input) SetWidth(w int) { - i.Block.SetWidth(w) + i.Par.SetWidth(w) } // implements interface termui.GridBufferer func (i *Input) SetX(x int) { - i.Block.SetX(x) + i.Par.SetX(x) } // implements interface termui.GridBufferer func (i *Input) SetY(y int) { - i.Block.SetY(y) + i.Par.SetY(y) } func (i *Input) Insert(key string) { - i.Block.Text = i.Block.Text + key + i.Par.Text = i.Par.Text[0:i.CursorPosition] + key + i.Par.Text[i.CursorPosition:len(i.Par.Text)] + i.MoveCursorRight() } func (i *Input) Remove() { if i.CursorPosition > 0 { - i.Block.Text = i.Block.Text[0:i.CursorPosition-1] + i.Block.Text[i.CursorPosition:len(i.Block.Text)] + i.Par.Text = i.Par.Text[0:i.CursorPosition-1] + i.Par.Text[i.CursorPosition:len(i.Par.Text)] i.MoveCursorLeft() } } func (i *Input) MoveCursorRight() { - if i.CursorPosition < len(i.Block.Text) { + if i.CursorPosition < len(i.Par.Text) { i.CursorPosition++ - i.Block.Block.Buffer().Set( - i.CursorPosition, // x - i.Block.Block.InnerY(), // y - termui.Cell{Ch: rune('$'), Fg: termui.ColorBlack, Bg: termui.ColorWhite}, - ) } } func (i *Input) MoveCursorLeft() { if i.CursorPosition > 0 { i.CursorPosition-- - i.Block.Block.Buffer().Set( - i.CursorPosition, // x - i.Block.Block.InnerY(), // y - termui.Cell{Ch: rune('$'), Fg: termui.ColorBlack, Bg: termui.ColorWhite}, - ) } } func (i *Input) IsEmpty() bool { - if i.Block.Text == "" { + if i.Par.Text == "" { return true } return false } func (i *Input) Clear() { - i.Block.Text = "" + i.Par.Text = "" i.CursorPosition = 0 } func (i *Input) Text() string { - return i.Block.Text + return i.Par.Text } diff --git a/src/views/chat.go b/src/views/chat.go index f56bede..ab5a2fa 100644 --- a/src/views/chat.go +++ b/src/views/chat.go @@ -15,8 +15,8 @@ type View struct { func CreateChatView() *View { input := components.CreateInput() - channels := components.CreateChannelsComponent(input.Block.Height) - chat := components.CreateChatComponent(input.Block.Height) + channels := components.CreateChannelsComponent(input.Par.Height) + chat := components.CreateChatComponent(input.Par.Height) mode := components.CreateModeComponent() view := &View{