mirror of
https://github.com/br0xen/termbox-util.git
synced 2024-11-26 07:03:14 +00:00
A couple improvements
Working on the frame tabbing logic Added a text filter for inputfield
This commit is contained in:
parent
159d078f44
commit
11b03a8d70
@ -204,11 +204,15 @@ func (c *Frame) GetBottomY() int {
|
|||||||
|
|
||||||
// HandleEvent accepts the termbox event and returns whether it was consumed
|
// HandleEvent accepts the termbox event and returns whether it was consumed
|
||||||
func (c *Frame) HandleEvent(event termbox.Event) bool {
|
func (c *Frame) HandleEvent(event termbox.Event) bool {
|
||||||
if event.Key == termbox.KeyTab {
|
if c.controls[c.tabIdx].HandleEvent(event) {
|
||||||
c.FindNextTabStop()
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return c.controls[c.tabIdx].HandleEvent(event)
|
if event.Key == termbox.KeyTab {
|
||||||
|
ret := !c.IsOnLastControl()
|
||||||
|
c.FindNextTabStop()
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindNextTabStop finds the next control that can be tabbed to
|
// FindNextTabStop finds the next control that can be tabbed to
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
package termboxUtil
|
package termboxUtil
|
||||||
|
|
||||||
import "github.com/nsf/termbox-go"
|
import (
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/nsf/termbox-go"
|
||||||
|
)
|
||||||
|
|
||||||
// InputField is a field for inputting text
|
// InputField is a field for inputting text
|
||||||
type InputField struct {
|
type InputField struct {
|
||||||
@ -17,6 +21,8 @@ type InputField struct {
|
|||||||
multiline bool
|
multiline bool
|
||||||
tabSkip bool
|
tabSkip bool
|
||||||
active bool
|
active bool
|
||||||
|
|
||||||
|
filter func(*InputField, string, string) string
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateInputField creates an input field at x, y that is w by h
|
// CreateInputField creates an input field at x, y that is w by h
|
||||||
@ -24,6 +30,7 @@ func CreateInputField(x, y, w, h int, fg, bg termbox.Attribute) *InputField {
|
|||||||
c := InputField{x: x, y: y, width: w, height: h,
|
c := InputField{x: x, y: y, width: w, height: h,
|
||||||
fg: fg, bg: bg, cursorFg: bg, cursorBg: fg, activeFg: fg, activeBg: bg,
|
fg: fg, bg: bg, cursorFg: bg, cursorBg: fg, activeFg: fg, activeBg: bg,
|
||||||
}
|
}
|
||||||
|
c.filter = func(fld *InputField, o, n string) string { return n }
|
||||||
return &c
|
return &c
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,58 +60,44 @@ func (c *InputField) SetValue(s string) {
|
|||||||
func (c *InputField) GetX() int { return c.x }
|
func (c *InputField) GetX() int { return c.x }
|
||||||
|
|
||||||
// SetX sets the x position of the input field
|
// SetX sets the x position of the input field
|
||||||
func (c *InputField) SetX(x int) {
|
func (c *InputField) SetX(x int) { c.x = x }
|
||||||
c.x = x
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetY returns the y position of the input field
|
// GetY returns the y position of the input field
|
||||||
func (c *InputField) GetY() int { return c.y }
|
func (c *InputField) GetY() int { return c.y }
|
||||||
|
|
||||||
// SetY sets the y position of the input field
|
// SetY sets the y position of the input field
|
||||||
func (c *InputField) SetY(y int) {
|
func (c *InputField) SetY(y int) { c.y = y }
|
||||||
c.y = y
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetWidth returns the current width of the input field
|
// GetWidth returns the current width of the input field
|
||||||
func (c *InputField) GetWidth() int { return c.width }
|
func (c *InputField) GetWidth() int { return c.width }
|
||||||
|
|
||||||
// SetWidth sets the current width of the input field
|
// SetWidth sets the current width of the input field
|
||||||
func (c *InputField) SetWidth(w int) {
|
func (c *InputField) SetWidth(w int) { c.width = w }
|
||||||
c.width = w
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetHeight returns the current height of the input field
|
// GetHeight returns the current height of the input field
|
||||||
func (c *InputField) GetHeight() int { return c.height }
|
func (c *InputField) GetHeight() int { return c.height }
|
||||||
|
|
||||||
// SetHeight sets the current height of the input field
|
// SetHeight sets the current height of the input field
|
||||||
func (c *InputField) SetHeight(h int) {
|
func (c *InputField) SetHeight(h int) { c.height = h }
|
||||||
c.height = h
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetFgColor returns the foreground color
|
// GetFgColor returns the foreground color
|
||||||
func (c *InputField) GetFgColor() termbox.Attribute { return c.fg }
|
func (c *InputField) GetFgColor() termbox.Attribute { return c.fg }
|
||||||
|
|
||||||
// SetFgColor sets the foreground color
|
// SetFgColor sets the foreground color
|
||||||
func (c *InputField) SetFgColor(fg termbox.Attribute) {
|
func (c *InputField) SetFgColor(fg termbox.Attribute) { c.fg = fg }
|
||||||
c.fg = fg
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetBgColor returns the background color
|
// GetBgColor returns the background color
|
||||||
func (c *InputField) GetBgColor() termbox.Attribute { return c.bg }
|
func (c *InputField) GetBgColor() termbox.Attribute { return c.bg }
|
||||||
|
|
||||||
// SetBgColor sets the current background color
|
// SetBgColor sets the current background color
|
||||||
func (c *InputField) SetBgColor(bg termbox.Attribute) {
|
func (c *InputField) SetBgColor(bg termbox.Attribute) { c.bg = bg }
|
||||||
c.bg = bg
|
|
||||||
}
|
func (c *InputField) SetCursorFg(fg termbox.Attribute) { c.cursorFg = fg }
|
||||||
|
|
||||||
func (c *InputField) SetCursorFg(fg termbox.Attribute) {
|
|
||||||
c.cursorFg = fg
|
|
||||||
}
|
|
||||||
func (c *InputField) GetCursorFg() termbox.Attribute { return c.cursorFg }
|
func (c *InputField) GetCursorFg() termbox.Attribute { return c.cursorFg }
|
||||||
|
|
||||||
func (c *InputField) SetCursorBg(bg termbox.Attribute) {
|
func (c *InputField) SetCursorBg(bg termbox.Attribute) { c.cursorBg = bg }
|
||||||
c.cursorBg = bg
|
|
||||||
}
|
|
||||||
func (c *InputField) GetCursorBg() termbox.Attribute { return c.cursorBg }
|
func (c *InputField) GetCursorBg() termbox.Attribute { return c.cursorBg }
|
||||||
|
|
||||||
// IsBordered returns true or false if this input field has a border
|
// IsBordered returns true or false if this input field has a border
|
||||||
@ -143,6 +136,10 @@ func (c *InputField) SetMultiline(b bool) {
|
|||||||
|
|
||||||
// HandleEvent accepts the termbox event and returns whether it was consumed
|
// HandleEvent accepts the termbox event and returns whether it was consumed
|
||||||
func (c *InputField) HandleEvent(event termbox.Event) bool {
|
func (c *InputField) HandleEvent(event termbox.Event) bool {
|
||||||
|
prev := c.value
|
||||||
|
if event.Key == termbox.KeyTab { // There is no tabbing in here
|
||||||
|
return false
|
||||||
|
}
|
||||||
if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 {
|
if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 {
|
||||||
if c.cursor+len(c.value) > 0 {
|
if c.cursor+len(c.value) > 0 {
|
||||||
crs := len(c.value)
|
crs := len(c.value)
|
||||||
@ -193,6 +190,7 @@ func (c *InputField) HandleEvent(event termbox.Event) bool {
|
|||||||
c.value = strPt1 + string(ch) + strPt2
|
c.value = strPt1 + string(ch) + strPt2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
c.value = c.filter(c, prev, c.value)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -294,3 +292,16 @@ func (c *InputField) Draw() {
|
|||||||
DrawStringAtPoint(strPt2, x+1, y, useFg, useBg)
|
DrawStringAtPoint(strPt2, x+1, y, useFg, useBg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *InputField) SetTextFilter(filter func(*InputField, string, string) string) {
|
||||||
|
c.filter = filter
|
||||||
|
}
|
||||||
|
|
||||||
|
// Some handy text filters
|
||||||
|
func (c *InputField) InputFieldNumberFilter(fld *InputField, o, n string) string {
|
||||||
|
_, err := strconv.Atoi(n)
|
||||||
|
if err != nil {
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user