Add cursor functionality
This commit is contained in:
parent
9cad8953d3
commit
fee679b4d0
@ -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
|
||||
}
|
||||
|
@ -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{
|
||||
|
Loading…
Reference in New Issue
Block a user