1
0
mirror of https://github.com/br0xen/termbox-util.git synced 2024-11-22 13:33:15 +00:00

Fixing a few bugs

This commit is contained in:
Brian Buller 2020-02-20 10:08:19 -06:00
parent f463131c44
commit dc6d6950ba
2 changed files with 34 additions and 39 deletions

View File

@ -1,6 +1,10 @@
package termboxUtil package termboxUtil
import "github.com/nsf/termbox-go" import (
"fmt"
"github.com/nsf/termbox-go"
)
// Frame is a frame for holding other elements // Frame is a frame for holding other elements
// It manages it's own x/y, tab index // It manages it's own x/y, tab index
@ -15,6 +19,8 @@ type Frame struct {
tabSkip bool tabSkip bool
active bool active bool
title string title string
status string
rightStatus string
} }
// CreateFrame creates a Frame at x, y that is w by h // CreateFrame creates a Frame at x, y that is w by h
@ -28,6 +34,8 @@ func CreateFrame(x, y, w, h int, fg, bg termbox.Attribute) *Frame {
func (c *Frame) SetTitle(title string) { c.title = title } func (c *Frame) SetTitle(title string) { c.title = title }
func (c *Frame) SetStatus(status string) { c.status = status }
// Setting color attributes on a frame trickles down to its controls // Setting color attributes on a frame trickles down to its controls
func (c *Frame) SetActiveFgColor(fg termbox.Attribute) { func (c *Frame) SetActiveFgColor(fg termbox.Attribute) {
c.activeFg = fg c.activeFg = fg
@ -57,80 +65,58 @@ func (c *Frame) IsActive() bool { return c.active }
func (c *Frame) GetID() string { return c.id } func (c *Frame) GetID() string { return c.id }
// SetID sets this control's ID // SetID sets this control's ID
func (c *Frame) SetID(newID string) { func (c *Frame) SetID(newID string) { c.id = newID }
c.id = newID
}
// GetX returns the x position of the frame // GetX returns the x position of the frame
func (c *Frame) GetX() int { return c.x } func (c *Frame) GetX() int { return c.x }
// SetX sets the x position of the frame // SetX sets the x position of the frame
func (c *Frame) SetX(x int) { func (c *Frame) SetX(x int) { c.x = x }
c.x = x
}
// GetY returns the y position of the frame // GetY returns the y position of the frame
func (c *Frame) GetY() int { return c.y } func (c *Frame) GetY() int { return c.y }
// SetY sets the y position of the frame // SetY sets the y position of the frame
func (c *Frame) SetY(y int) { func (c *Frame) SetY(y int) { c.y = y }
c.y = y
}
// GetWidth returns the current width of the frame // GetWidth returns the current width of the frame
func (c *Frame) GetWidth() int { return c.width } func (c *Frame) GetWidth() int { return c.width }
// SetWidth sets the current width of the frame // SetWidth sets the current width of the frame
func (c *Frame) SetWidth(w int) { func (c *Frame) SetWidth(w int) { c.width = w }
c.width = w
}
// GetHeight returns the current height of the frame // GetHeight returns the current height of the frame
func (c *Frame) GetHeight() int { return c.height } func (c *Frame) GetHeight() int { return c.height }
// SetHeight sets the current height of the frame // SetHeight sets the current height of the frame
func (c *Frame) SetHeight(h int) { func (c *Frame) SetHeight(h int) { c.height = h }
c.height = h
}
// GetFgColor returns the foreground color // GetFgColor returns the foreground color
func (c *Frame) GetFgColor() termbox.Attribute { return c.fg } func (c *Frame) GetFgColor() termbox.Attribute { return c.fg }
// SetFgColor sets the foreground color // SetFgColor sets the foreground color
func (c *Frame) SetFgColor(fg termbox.Attribute) { func (c *Frame) SetFgColor(fg termbox.Attribute) { c.fg = fg }
c.fg = fg
}
// GetBgColor returns the background color // GetBgColor returns the background color
func (c *Frame) GetBgColor() termbox.Attribute { return c.bg } func (c *Frame) GetBgColor() termbox.Attribute { return c.bg }
// SetBgColor sets the current background color // SetBgColor sets the current background color
func (c *Frame) SetBgColor(bg termbox.Attribute) { func (c *Frame) SetBgColor(bg termbox.Attribute) { c.bg = bg }
c.bg = bg
}
// IsBordered returns true or false if this frame has a border // IsBordered returns true or false if this frame has a border
func (c *Frame) IsBordered() bool { return c.bordered } func (c *Frame) IsBordered() bool { return c.bordered }
// SetBordered sets whether we render a border around the frame // SetBordered sets whether we render a border around the frame
func (c *Frame) SetBordered(b bool) { func (c *Frame) SetBordered(b bool) { c.bordered = b }
c.bordered = b
}
// IsTabSkipped returns whether this modal has it's tabskip flag set // IsTabSkipped returns whether this modal has it's tabskip flag set
func (c *Frame) IsTabSkipped() bool { func (c *Frame) IsTabSkipped() bool { return c.tabSkip }
return c.tabSkip
}
// SetTabSkip sets the tabskip flag for this control // SetTabSkip sets the tabskip flag for this control
func (c *Frame) SetTabSkip(b bool) { func (c *Frame) SetTabSkip(b bool) { c.tabSkip = b }
c.tabSkip = b
}
// AddControl adds a control to the frame // AddControl adds a control to the frame
func (c *Frame) AddControl(t termboxControl) { func (c *Frame) AddControl(t termboxControl) { c.controls = append(c.controls, t) }
c.controls = append(c.controls, t)
}
func (c *Frame) ResetTabIndex() { func (c *Frame) ResetTabIndex() {
for k, v := range c.controls { for k, v := range c.controls {
@ -204,14 +190,19 @@ 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 the currently active control consumes the event, we don't need to handle it
if c.controls[c.tabIdx].HandleEvent(event) { if c.controls[c.tabIdx].HandleEvent(event) {
c.rightStatus = fmt.Sprintf("C (%d/%d)", c.tabIdx, len(c.controls))
return true return true
} }
// All that a frame cares about is tabbing around
if event.Key == termbox.KeyTab { if event.Key == termbox.KeyTab {
ret := !c.IsOnLastControl() ret := !c.IsOnLastControl()
c.FindNextTabStop() c.FindNextTabStop()
c.rightStatus = fmt.Sprintf("N (%d/%d)", c.tabIdx, len(c.controls))
return ret return ret
} }
c.rightStatus = fmt.Sprintf("B (%d/%d)", c.tabIdx, len(c.controls))
return false return false
} }
@ -274,4 +265,10 @@ func (c *Frame) Draw() {
} }
c.DrawControl(c.controls[idx]) c.DrawControl(c.controls[idx])
} }
if c.status != "" {
DrawStringAtPoint(" "+c.status+" ", c.x+1, c.y+c.height, borderFg, borderBg)
}
if c.rightStatus != "" {
DrawStringAtPoint(" "+c.rightStatus+" ", (c.x+c.width)-len(c.rightStatus)-2, c.y+c.height, borderFg, borderBg)
}
} }

View File

@ -34,6 +34,7 @@ func CreateMenu(title string, options []string, x, y, width, height int, fg, bg
disabledFg: bg, disabledBg: bg, disabledFg: bg, disabledBg: bg,
activeFg: fg, activeBg: bg, activeFg: fg, activeBg: bg,
bordered: true, bordered: true,
tabSkip: false,
} }
for _, line := range options { for _, line := range options {
c.options = append(c.options, MenuOption{text: line}) c.options = append(c.options, MenuOption{text: line})
@ -58,11 +59,8 @@ func (c *Menu) GetID() string { return c.id }
// SetID sets this control's ID // SetID sets this control's ID
func (c *Menu) SetID(newID string) { c.id = newID } func (c *Menu) SetID(newID string) { c.id = newID }
// IsTabSkipped is always true for a label func (c *Menu) IsTabSkipped() bool { return c.tabSkip }
func (c *Menu) IsTabSkipped() bool { return true } func (c *Menu) SetTabSkip(b bool) { c.tabSkip = b }
// This doesn't do anything for a label
func (c *Menu) SetTabSkip(b bool) {}
// GetTitle returns the current title of the menu // GetTitle returns the current title of the menu
func (c *Menu) GetTitle() string { return c.title } func (c *Menu) GetTitle() string { return c.title }