1
0
mirror of https://github.com/br0xen/termbox-util.git synced 2024-11-22 21:43:14 +00:00

Started interfacing everything

Building a scroll frame too.
This commit is contained in:
Brian Buller 2016-01-16 23:04:01 -06:00
parent e843ea92b9
commit 3cf01a010f
8 changed files with 290 additions and 168 deletions

View File

@ -1,6 +1,8 @@
package termboxUtil package termboxUtil
import ( import (
"strings"
"github.com/nsf/termbox-go" "github.com/nsf/termbox-go"
) )
@ -21,18 +23,16 @@ func CreateASCIIArt(c []string, x, y int, fg, bg termbox.Attribute) *ASCIIArt {
func (i *ASCIIArt) GetX() int { return i.x } func (i *ASCIIArt) GetX() int { return i.x }
// SetX set the x position of the modal to x // SetX set the x position of the modal to x
func (i *ASCIIArt) SetX(x int) *ASCIIArt { func (i *ASCIIArt) SetX(x int) {
i.x = x i.x = x
return i
} }
// GetY Return the y position of the modal // GetY Return the y position of the modal
func (i *ASCIIArt) GetY() int { return i.y } func (i *ASCIIArt) GetY() int { return i.y }
// SetY Set the y position of the modal to y // SetY Set the y position of the modal to y
func (i *ASCIIArt) SetY(y int) *ASCIIArt { func (i *ASCIIArt) SetY(y int) {
i.y = y i.y = y
return i
} }
// GetHeight Returns the number of strings in the contents slice // GetHeight Returns the number of strings in the contents slice
@ -40,10 +40,45 @@ func (i *ASCIIArt) GetHeight() int {
return len(i.contents) return len(i.contents)
} }
// SetHeight truncates lines from the bottom of the ascii art
func (i *ASCIIArt) SetHeight(h int) {
if len(i.contents) > h {
i.contents = i.contents[:h]
} else {
for j := len(i.contents); j < h; j++ {
i.contents = append(i.contents, "")
}
}
}
// GetWidth Returns the number of strings in the contents slice
func (i *ASCIIArt) GetWidth() int {
// Find the longest line
var ret int
for j := range i.contents {
if len(i.contents[j]) > ret {
ret = len(i.contents[j])
}
}
return ret
}
// SetWidth Sets all lines in the contents to width w
func (i *ASCIIArt) SetWidth(w int) {
// Find the longest line
for j := range i.contents {
mkUp := w - len(i.contents[j])
if mkUp > 0 {
i.contents[j] = i.contents[j] + strings.Repeat(" ", mkUp)
} else {
i.contents[j] = i.contents[j][:w]
}
}
}
// SetContents Sets the contents of i to c // SetContents Sets the contents of i to c
func (i *ASCIIArt) SetContents(c []string) *ASCIIArt { func (i *ASCIIArt) SetContents(c []string) {
i.contents = c i.contents = c
return i
} }
// GetContents returns the ascii art // GetContents returns the ascii art
@ -52,33 +87,30 @@ func (i *ASCIIArt) GetContents() []string {
} }
// SetContentLine Sets a specific line of the contents to s // SetContentLine Sets a specific line of the contents to s
func (i *ASCIIArt) SetContentLine(s string, idx int) *ASCIIArt { func (i *ASCIIArt) SetContentLine(s string, idx int) {
if idx >= 0 && idx < len(i.contents) { if idx >= 0 && idx < len(i.contents) {
i.contents[idx] = s i.contents[idx] = s
} }
return i
} }
// GetBackground Return the current background color of the modal // GetBackground Return the current background color of the modal
func (i *ASCIIArt) GetBackground() termbox.Attribute { return i.bg } func (i *ASCIIArt) GetBackground() termbox.Attribute { return i.bg }
// SetBackground Set the current background color to bg // SetBackground Set the current background color to bg
func (i *ASCIIArt) SetBackground(bg termbox.Attribute) *ASCIIArt { func (i *ASCIIArt) SetBackground(bg termbox.Attribute) {
i.bg = bg i.bg = bg
return i
} }
// GetForeground Return the current foreground color // GetForeground Return the current foreground color
func (i *ASCIIArt) GetForeground() termbox.Attribute { return i.fg } func (i *ASCIIArt) GetForeground() termbox.Attribute { return i.fg }
// SetForeground Set the foreground color to fg // SetForeground Set the foreground color to fg
func (i *ASCIIArt) SetForeground(fg termbox.Attribute) *ASCIIArt { func (i *ASCIIArt) SetForeground(fg termbox.Attribute) {
i.fg = fg i.fg = fg
return i
} }
// Align Align the Ascii art over width width with alignment a // Align Align the Ascii art over width width with alignment a
func (i *ASCIIArt) Align(a TextAlignment, width int) *ASCIIArt { func (i *ASCIIArt) Align(a TextAlignment, width int) {
// First get the width of the longest string in the slice // First get the width of the longest string in the slice
var newContents []string var newContents []string
incomingLength := 0 incomingLength := 0
@ -91,7 +123,6 @@ func (i *ASCIIArt) Align(a TextAlignment, width int) *ASCIIArt {
newContents = append(newContents, AlignText(AlignText(line, incomingLength, AlignLeft), width, a)) newContents = append(newContents, AlignText(AlignText(line, incomingLength, AlignLeft), width, a))
} }
i.contents = newContents i.contents = newContents
return i
} }
// HandleKeyPress accepts the termbox event and returns whether it was consumed // HandleKeyPress accepts the termbox event and returns whether it was consumed

View File

@ -32,114 +32,101 @@ func CreateConfirmModal(title string, x, y, width, height int, fg, bg termbox.At
func (i *ConfirmModal) GetTitle() string { return i.title } func (i *ConfirmModal) GetTitle() string { return i.title }
// SetTitle sets the current title of the modal to s // SetTitle sets the current title of the modal to s
func (i *ConfirmModal) SetTitle(s string) *ConfirmModal { func (i *ConfirmModal) SetTitle(s string) {
i.title = s i.title = s
return i
} }
// GetText returns the current text of the modal // GetText returns the current text of the modal
func (i *ConfirmModal) GetText() string { return i.text } func (i *ConfirmModal) GetText() string { return i.text }
// SetText sets the text of the modal to s // SetText sets the text of the modal to s
func (i *ConfirmModal) SetText(s string) *ConfirmModal { func (i *ConfirmModal) SetText(s string) {
i.text = s i.text = s
return i
} }
// GetX returns the current x coordinate of the modal // GetX returns the current x coordinate of the modal
func (i *ConfirmModal) GetX() int { return i.x } func (i *ConfirmModal) GetX() int { return i.x }
// SetX sets the current x coordinate of the modal to x // SetX sets the current x coordinate of the modal to x
func (i *ConfirmModal) SetX(x int) *ConfirmModal { func (i *ConfirmModal) SetX(x int) {
i.x = x i.x = x
return i
} }
// GetY returns the current y coordinate of the modal // GetY returns the current y coordinate of the modal
func (i *ConfirmModal) GetY() int { return i.y } func (i *ConfirmModal) GetY() int { return i.y }
// SetY sets the current y coordinate of the modal to y // SetY sets the current y coordinate of the modal to y
func (i *ConfirmModal) SetY(y int) *ConfirmModal { func (i *ConfirmModal) SetY(y int) {
i.y = y i.y = y
return i
} }
// GetWidth returns the current width of the modal // GetWidth returns the current width of the modal
func (i *ConfirmModal) GetWidth() int { return i.width } func (i *ConfirmModal) GetWidth() int { return i.width }
// SetWidth sets the current modal width to width // SetWidth sets the current modal width to width
func (i *ConfirmModal) SetWidth(width int) *ConfirmModal { func (i *ConfirmModal) SetWidth(width int) {
i.width = width i.width = width
return i
} }
// GetHeight returns the current height of the modal // GetHeight returns the current height of the modal
func (i *ConfirmModal) GetHeight() int { return i.height } func (i *ConfirmModal) GetHeight() int { return i.height }
// SetHeight set the height of the modal to height // SetHeight set the height of the modal to height
func (i *ConfirmModal) SetHeight(height int) *ConfirmModal { func (i *ConfirmModal) SetHeight(height int) {
i.height = height i.height = height
return i
} }
// HelpIsShown returns true or false if the help is displayed // HelpIsShown returns true or false if the help is displayed
func (i *ConfirmModal) HelpIsShown() bool { return i.showHelp } func (i *ConfirmModal) HelpIsShown() bool { return i.showHelp }
// ShowHelp sets whether or not to display the help text // ShowHelp sets whether or not to display the help text
func (i *ConfirmModal) ShowHelp(b bool) *ConfirmModal { func (i *ConfirmModal) ShowHelp(b bool) {
i.showHelp = b i.showHelp = b
return i
} }
// GetBackground returns the current background color // GetBackground returns the current background color
func (i *ConfirmModal) GetBackground() termbox.Attribute { return i.bg } func (i *ConfirmModal) GetBackground() termbox.Attribute { return i.bg }
// SetBackground sets the background color to bg // SetBackground sets the background color to bg
func (i *ConfirmModal) SetBackground(bg termbox.Attribute) *ConfirmModal { func (i *ConfirmModal) SetBackground(bg termbox.Attribute) {
i.bg = bg i.bg = bg
return i
} }
// GetForeground returns the current foreground color // GetForeground returns the current foreground color
func (i *ConfirmModal) GetForeground() termbox.Attribute { return i.fg } func (i *ConfirmModal) GetForeground() termbox.Attribute { return i.fg }
// SetForeground sets the current foreground color to fg // SetForeground sets the current foreground color to fg
func (i *ConfirmModal) SetForeground(fg termbox.Attribute) *ConfirmModal { func (i *ConfirmModal) SetForeground(fg termbox.Attribute) {
i.fg = fg i.fg = fg
return i
} }
// IsDone returns whether the user has answered the modal // IsDone returns whether the user has answered the modal
func (i *ConfirmModal) IsDone() bool { return i.isDone } func (i *ConfirmModal) IsDone() bool { return i.isDone }
// SetDone sets whether the modal has completed it's purpose // SetDone sets whether the modal has completed it's purpose
func (i *ConfirmModal) SetDone(b bool) *ConfirmModal { func (i *ConfirmModal) SetDone(b bool) {
i.isDone = b i.isDone = b
return i
} }
// Show sets the visibility flag of the modal to true // Show sets the visibility flag of the modal to true
func (i *ConfirmModal) Show() *ConfirmModal { func (i *ConfirmModal) Show() {
i.isVisible = true i.isVisible = true
return i
} }
// Hide sets the visibility flag of the modal to false // Hide sets the visibility flag of the modal to false
func (i *ConfirmModal) Hide() *ConfirmModal { func (i *ConfirmModal) Hide() {
i.isVisible = false i.isVisible = false
return i
} }
// IsAccepted returns whether the user accepted the modal // IsAccepted returns whether the user accepted the modal
func (i *ConfirmModal) IsAccepted() bool { return i.accepted } func (i *ConfirmModal) IsAccepted() bool { return i.accepted }
// Clear clears all of the non-positional parameters of the modal // Clear clears all of the non-positional parameters of the modal
func (i *ConfirmModal) Clear() *ConfirmModal { func (i *ConfirmModal) Clear() {
i.title = "" i.title = ""
i.text = "" i.text = ""
i.accepted = false i.accepted = false
i.isDone = false i.isDone = false
return i
} }
// HandleKeyPress handles the termbox event and returns whether it was consumed // HandleKeyPress handles the termbox event and returns whether it was consumed

View File

@ -27,72 +27,64 @@ func CreateInputField(x, y, w, h int, fg, bg termbox.Attribute) *InputField {
func (i *InputField) GetValue() string { return i.value } func (i *InputField) GetValue() string { return i.value }
// SetValue sets the current text in the InputField to s // SetValue sets the current text in the InputField to s
func (i *InputField) SetValue(s string) *InputField { func (i *InputField) SetValue(s string) {
i.value = s i.value = s
return i
} }
// GetX returns the x position of the input field // GetX returns the x position of the input field
func (i *InputField) GetX() int { return i.x } func (i *InputField) GetX() int { return i.x }
// SetX sets the x position of the input field // SetX sets the x position of the input field
func (i *InputField) SetX(x int) *InputField { func (i *InputField) SetX(x int) {
i.x = x i.x = x
return i
} }
// GetY returns the y position of the input field // GetY returns the y position of the input field
func (i *InputField) GetY() int { return i.y } func (i *InputField) GetY() int { return i.y }
// SetY sets the y position of the input field // SetY sets the y position of the input field
func (i *InputField) SetY(y int) *InputField { func (i *InputField) SetY(y int) {
i.y = y i.y = y
return i
} }
// GetWidth returns the current width of the input field // GetWidth returns the current width of the input field
func (i *InputField) GetWidth() int { return i.width } func (i *InputField) GetWidth() int { return i.width }
// SetWidth sets the current width of the input field // SetWidth sets the current width of the input field
func (i *InputField) SetWidth(w int) *InputField { func (i *InputField) SetWidth(w int) {
i.width = w i.width = w
return i
} }
// GetHeight returns the current height of the input field // GetHeight returns the current height of the input field
func (i *InputField) GetHeight() int { return i.height } func (i *InputField) GetHeight() int { return i.height }
// SetHeight sets the current height of the input field // SetHeight sets the current height of the input field
func (i *InputField) SetHeight(h int) *InputField { func (i *InputField) SetHeight(h int) {
i.height = h i.height = h
return i
} }
// IsBordered returns true or false if this input field has a border // IsBordered returns true or false if this input field has a border
func (i *InputField) IsBordered() bool { return i.bordered } func (i *InputField) IsBordered() bool { return i.bordered }
// SetBordered sets whether we render a border around the input field // SetBordered sets whether we render a border around the input field
func (i *InputField) SetBordered(b bool) *InputField { func (i *InputField) SetBordered(b bool) {
i.bordered = b i.bordered = b
return i
} }
// DoesWrap returns true or false if this input field wraps text // DoesWrap returns true or false if this input field wraps text
func (i *InputField) DoesWrap() bool { return i.wrap } func (i *InputField) DoesWrap() bool { return i.wrap }
// SetWrap sets whether we wrap the text at width. // SetWrap sets whether we wrap the text at width.
func (i *InputField) SetWrap(b bool) *InputField { func (i *InputField) SetWrap(b bool) {
i.wrap = b i.wrap = b
return i
} }
// IsMultiline returns true or false if this field can have multiple lines // IsMultiline returns true or false if this field can have multiple lines
func (i *InputField) IsMultiline() bool { return i.multiline } func (i *InputField) IsMultiline() bool { return i.multiline }
// SetMultiline sets whether the field can have multiple lines // SetMultiline sets whether the field can have multiple lines
func (i *InputField) SetMultiline(b bool) *InputField { func (i *InputField) SetMultiline(b bool) {
i.multiline = b i.multiline = b
return i
} }
// HandleKeyPress accepts the termbox event and returns whether it was consumed // HandleKeyPress accepts the termbox event and returns whether it was consumed

View File

@ -30,99 +30,87 @@ func CreateInputModal(title string, x, y, width, height int, fg, bg termbox.Attr
func (i *InputModal) GetTitle() string { return i.title } func (i *InputModal) GetTitle() string { return i.title }
// SetTitle Sets the title of the modal to s // SetTitle Sets the title of the modal to s
func (i *InputModal) SetTitle(s string) *InputModal { func (i *InputModal) SetTitle(s string) {
i.title = s i.title = s
return i
} }
// GetText Return the text of the modal // GetText Return the text of the modal
func (i *InputModal) GetText() string { return i.text } func (i *InputModal) GetText() string { return i.text }
// SetText Set the text of the modal to s // SetText Set the text of the modal to s
func (i *InputModal) SetText(s string) *InputModal { func (i *InputModal) SetText(s string) {
i.text = s i.text = s
return i
} }
// GetX Return the x position of the modal // GetX Return the x position of the modal
func (i *InputModal) GetX() int { return i.x } func (i *InputModal) GetX() int { return i.x }
// SetX set the x position of the modal to x // SetX set the x position of the modal to x
func (i *InputModal) SetX(x int) *InputModal { func (i *InputModal) SetX(x int) {
i.x = x i.x = x
return i
} }
// GetY Return the y position of the modal // GetY Return the y position of the modal
func (i *InputModal) GetY() int { return i.y } func (i *InputModal) GetY() int { return i.y }
// SetY Set the y position of the modal to y // SetY Set the y position of the modal to y
func (i *InputModal) SetY(y int) *InputModal { func (i *InputModal) SetY(y int) {
i.y = y i.y = y
return i
} }
// GetWidth Return the width of the modal // GetWidth Return the width of the modal
func (i *InputModal) GetWidth() int { return i.width } func (i *InputModal) GetWidth() int { return i.width }
// SetWidth Set the width of the modal to width // SetWidth Set the width of the modal to width
func (i *InputModal) SetWidth(width int) *InputModal { func (i *InputModal) SetWidth(width int) {
i.width = width i.width = width
return i
} }
// GetHeight Return the height of the modal // GetHeight Return the height of the modal
func (i *InputModal) GetHeight() int { return i.height } func (i *InputModal) GetHeight() int { return i.height }
// SetHeight Set the height of the modal to height // SetHeight Set the height of the modal to height
func (i *InputModal) SetHeight(height int) *InputModal { func (i *InputModal) SetHeight(height int) {
i.height = height i.height = height
return i
} }
// HelpIsShown Returns whether the modal is showing it's help text or not // HelpIsShown Returns whether the modal is showing it's help text or not
func (i *InputModal) HelpIsShown() bool { return i.showHelp } func (i *InputModal) HelpIsShown() bool { return i.showHelp }
// ShowHelp Set the "Show Help" flag // ShowHelp Set the "Show Help" flag
func (i *InputModal) ShowHelp(b bool) *InputModal { func (i *InputModal) ShowHelp(b bool) {
i.showHelp = b i.showHelp = b
return i
} }
// GetBackground Return the current background color of the modal // GetBackground Return the current background color of the modal
func (i *InputModal) GetBackground() termbox.Attribute { return i.bg } func (i *InputModal) GetBackground() termbox.Attribute { return i.bg }
// SetBackground Set the current background color to bg // SetBackground Set the current background color to bg
func (i *InputModal) SetBackground(bg termbox.Attribute) *InputModal { func (i *InputModal) SetBackground(bg termbox.Attribute) {
i.bg = bg i.bg = bg
return i
} }
// GetForeground Return the current foreground color // GetForeground Return the current foreground color
func (i *InputModal) GetForeground() termbox.Attribute { return i.fg } func (i *InputModal) GetForeground() termbox.Attribute { return i.fg }
// SetForeground Set the foreground color to fg // SetForeground Set the foreground color to fg
func (i *InputModal) SetForeground(fg termbox.Attribute) *InputModal { func (i *InputModal) SetForeground(fg termbox.Attribute) {
i.fg = fg i.fg = fg
return i
} }
// Show Sets the visibility flag to true // Show Sets the visibility flag to true
func (i *InputModal) Show() *InputModal { func (i *InputModal) Show() {
i.isVisible = true i.isVisible = true
return i
} }
// Hide Sets the visibility flag to false // Hide Sets the visibility flag to false
func (i *InputModal) Hide() *InputModal { func (i *InputModal) Hide() {
i.isVisible = false i.isVisible = false
return i
} }
// SetDone Sets the flag that tells whether this modal has completed it's purpose // SetDone Sets the flag that tells whether this modal has completed it's purpose
func (i *InputModal) SetDone(b bool) *InputModal { func (i *InputModal) SetDone(b bool) {
i.isDone = b i.isDone = b
return i
} }
// IsDone Returns the "isDone" flag // IsDone Returns the "isDone" flag
@ -134,25 +122,22 @@ func (i *InputModal) IsDone() bool {
func (i *InputModal) GetValue() string { return i.input.GetValue() } func (i *InputModal) GetValue() string { return i.input.GetValue() }
// SetValue Sets the value of the input to s // SetValue Sets the value of the input to s
func (i *InputModal) SetValue(s string) *InputModal { func (i *InputModal) SetValue(s string) {
i.input.SetValue(s) i.input.SetValue(s)
return i
} }
// SetInputWrap sets whether the input field will wrap long text or not // SetInputWrap sets whether the input field will wrap long text or not
func (i *InputModal) SetInputWrap(b bool) *InputModal { func (i *InputModal) SetInputWrap(b bool) {
i.input.SetWrap(b) i.input.SetWrap(b)
return i
} }
// Clear Resets all non-positional parameters of the modal // Clear Resets all non-positional parameters of the modal
func (i *InputModal) Clear() *InputModal { func (i *InputModal) Clear() {
i.title = "" i.title = ""
i.text = "" i.text = ""
i.input.SetValue("") i.input.SetValue("")
i.isDone = false i.isDone = false
i.isVisible = false i.isVisible = false
return i
} }
// HandleKeyPress Handle the termbox event, return true if it was consumed // HandleKeyPress Handle the termbox event, return true if it was consumed

View File

@ -39,9 +39,8 @@ func CreateMenu(title string, options []string, x, y, width, height int, fg, bg
func (i *Menu) GetTitle() string { return i.title } func (i *Menu) GetTitle() string { return i.title }
// SetTitle sets the current title of the menu to s // SetTitle sets the current title of the menu to s
func (i *Menu) SetTitle(s string) *Menu { func (i *Menu) SetTitle(s string) {
i.title = s i.title = s
return i
} }
// GetOptions returns the current options of the menu // GetOptions returns the current options of the menu
@ -50,54 +49,49 @@ func (i *Menu) GetOptions() []MenuOption {
} }
// SetOptions set the menu's options to opts // SetOptions set the menu's options to opts
func (i *Menu) SetOptions(opts []MenuOption) *Menu { func (i *Menu) SetOptions(opts []MenuOption) {
i.options = opts i.options = opts
return i
} }
// SetOptionsFromStrings sets the options of this menu from a slice of strings // SetOptionsFromStrings sets the options of this menu from a slice of strings
func (i *Menu) SetOptionsFromStrings(opts []string) *Menu { func (i *Menu) SetOptionsFromStrings(opts []string) {
var newOpts []MenuOption var newOpts []MenuOption
for _, v := range opts { for _, v := range opts {
newOpts = append(newOpts, *CreateOptionFromText(v)) newOpts = append(newOpts, *CreateOptionFromText(v))
} }
return i.SetOptions(newOpts) i.SetOptions(newOpts)
} }
// GetX returns the current x coordinate of the menu // GetX returns the current x coordinate of the menu
func (i *Menu) GetX() int { return i.x } func (i *Menu) GetX() int { return i.x }
// SetX sets the current x coordinate of the menu to x // SetX sets the current x coordinate of the menu to x
func (i *Menu) SetX(x int) *Menu { func (i *Menu) SetX(x int) {
i.x = x i.x = x
return i
} }
// GetY returns the current y coordinate of the menu // GetY returns the current y coordinate of the menu
func (i *Menu) GetY() int { return i.y } func (i *Menu) GetY() int { return i.y }
// SetY sets the current y coordinate of the menu to y // SetY sets the current y coordinate of the menu to y
func (i *Menu) SetY(y int) *Menu { func (i *Menu) SetY(y int) {
i.y = y i.y = y
return i
} }
// GetWidth returns the current width of the menu // GetWidth returns the current width of the menu
func (i *Menu) GetWidth() int { return i.width } func (i *Menu) GetWidth() int { return i.width }
// SetWidth sets the current menu width to width // SetWidth sets the current menu width to width
func (i *Menu) SetWidth(width int) *Menu { func (i *Menu) SetWidth(width int) {
i.width = width i.width = width
return i
} }
// GetHeight returns the current height of the menu // GetHeight returns the current height of the menu
func (i *Menu) GetHeight() int { return i.height } func (i *Menu) GetHeight() int { return i.height }
// SetHeight set the height of the menu to height // SetHeight set the height of the menu to height
func (i *Menu) SetHeight(height int) *Menu { func (i *Menu) SetHeight(height int) {
i.height = height i.height = height
return i
} }
// GetSelectedOption returns the current selected option // GetSelectedOption returns the current selected option
@ -140,7 +134,7 @@ func (i *Menu) GetSelectedIndex() int {
} }
// SetSelectedOption sets the current selected option to v (if it's valid) // SetSelectedOption sets the current selected option to v (if it's valid)
func (i *Menu) SetSelectedOption(v *MenuOption) *Menu { func (i *Menu) SetSelectedOption(v *MenuOption) {
for idx := range i.options { for idx := range i.options {
if &i.options[idx] == v { if &i.options[idx] == v {
i.options[idx].Select() i.options[idx].Select()
@ -148,35 +142,30 @@ func (i *Menu) SetSelectedOption(v *MenuOption) *Menu {
i.options[idx].Unselect() i.options[idx].Unselect()
} }
} }
return i
} }
// SelectPrevOption Decrements the selected option (if it can) // SelectPrevOption Decrements the selected option (if it can)
func (i *Menu) SelectPrevOption() *Menu { func (i *Menu) SelectPrevOption() {
idx := i.GetSelectedIndex() idx := i.GetSelectedIndex()
for idx >= 0 { for idx >= 0 {
idx-- idx--
testOption := i.GetOptionFromIndex(idx) testOption := i.GetOptionFromIndex(idx)
if testOption != nil && !testOption.IsDisabled() { if testOption != nil && !testOption.IsDisabled() {
i.SetSelectedOption(testOption) i.SetSelectedOption(testOption)
return i
} }
} }
return i
} }
// SelectNextOption Increments the selected option (if it can) // SelectNextOption Increments the selected option (if it can)
func (i *Menu) SelectNextOption() *Menu { func (i *Menu) SelectNextOption() {
idx := i.GetSelectedIndex() idx := i.GetSelectedIndex()
for idx < len(i.options) { for idx < len(i.options) {
idx++ idx++
testOption := i.GetOptionFromIndex(idx) testOption := i.GetOptionFromIndex(idx)
if testOption != nil && !testOption.IsDisabled() { if testOption != nil && !testOption.IsDisabled() {
i.SetSelectedOption(testOption) i.SetSelectedOption(testOption)
return i
} }
} }
return i
} }
// SetOptionDisabled Disables the specified option // SetOptionDisabled Disables the specified option
@ -197,57 +186,50 @@ func (i *Menu) SetOptionEnabled(idx int) {
func (i *Menu) HelpIsShown() bool { return i.showHelp } func (i *Menu) HelpIsShown() bool { return i.showHelp }
// ShowHelp sets whether or not to display the help text // ShowHelp sets whether or not to display the help text
func (i *Menu) ShowHelp(b bool) *Menu { func (i *Menu) ShowHelp(b bool) {
i.showHelp = b i.showHelp = b
return i
} }
// GetBackground returns the current background color // GetBackground returns the current background color
func (i *Menu) GetBackground() termbox.Attribute { return i.bg } func (i *Menu) GetBackground() termbox.Attribute { return i.bg }
// SetBackground sets the background color to bg // SetBackground sets the background color to bg
func (i *Menu) SetBackground(bg termbox.Attribute) *Menu { func (i *Menu) SetBackground(bg termbox.Attribute) {
i.bg = bg i.bg = bg
return i
} }
// GetForeground returns the current foreground color // GetForeground returns the current foreground color
func (i *Menu) GetForeground() termbox.Attribute { return i.fg } func (i *Menu) GetForeground() termbox.Attribute { return i.fg }
// SetForeground sets the current foreground color to fg // SetForeground sets the current foreground color to fg
func (i *Menu) SetForeground(fg termbox.Attribute) *Menu { func (i *Menu) SetForeground(fg termbox.Attribute) {
i.fg = fg i.fg = fg
return i
} }
// IsDone returns whether the user has answered the modal // IsDone returns whether the user has answered the modal
func (i *Menu) IsDone() bool { return i.isDone } func (i *Menu) IsDone() bool { return i.isDone }
// SetDone sets whether the modal has completed it's purpose // SetDone sets whether the modal has completed it's purpose
func (i *Menu) SetDone(b bool) *Menu { func (i *Menu) SetDone(b bool) {
i.isDone = b i.isDone = b
return i
} }
// IsBordered returns true or false if this menu has a border // IsBordered returns true or false if this menu has a border
func (i *Menu) IsBordered() bool { return i.bordered } func (i *Menu) IsBordered() bool { return i.bordered }
// SetBordered sets whether we render a border around the menu // SetBordered sets whether we render a border around the menu
func (i *Menu) SetBordered(b bool) *Menu { func (i *Menu) SetBordered(b bool) {
i.bordered = b i.bordered = b
return i
} }
// EnableVimMode Enables h,j,k,l navigation // EnableVimMode Enables h,j,k,l navigation
func (i *Menu) EnableVimMode() *Menu { func (i *Menu) EnableVimMode() {
i.vimMode = true i.vimMode = true
return i
} }
// DisableVimMode Disables h,j,k,l navigation // DisableVimMode Disables h,j,k,l navigation
func (i *Menu) DisableVimMode() *Menu { func (i *Menu) DisableVimMode() {
i.vimMode = false i.vimMode = false
return i
} }
// HandleKeyPress handles the termbox event and returns whether it was consumed // HandleKeyPress handles the termbox event and returns whether it was consumed
@ -348,24 +330,21 @@ func CreateOptionFromText(s string) *MenuOption {
} }
// SetText Sets the text for this option // SetText Sets the text for this option
func (i *MenuOption) SetText(s string) *MenuOption { func (i *MenuOption) SetText(s string) {
i.text = s i.text = s
return i
} }
// GetText Returns the text for this option // GetText Returns the text for this option
func (i *MenuOption) GetText() string { return i.text } func (i *MenuOption) GetText() string { return i.text }
// Disable Sets this option to disabled // Disable Sets this option to disabled
func (i *MenuOption) Disable() *MenuOption { func (i *MenuOption) Disable() {
i.disabled = true i.disabled = true
return i
} }
// Enable Sets this option to enabled // Enable Sets this option to enabled
func (i *MenuOption) Enable() *MenuOption { func (i *MenuOption) Enable() {
i.disabled = false i.disabled = false
return i
} }
// IsDisabled returns whether this option is enabled // IsDisabled returns whether this option is enabled
@ -379,21 +358,18 @@ func (i *MenuOption) IsSelected() bool {
} }
// Select Sets this option to selected // Select Sets this option to selected
func (i *MenuOption) Select() *MenuOption { func (i *MenuOption) Select() {
i.selected = true i.selected = true
return i
} }
// Unselect Sets this option to not selected // Unselect Sets this option to not selected
func (i *MenuOption) Unselect() *MenuOption { func (i *MenuOption) Unselect() {
i.selected = false i.selected = false
return i
} }
// SetHelpText Sets this option's help text to s // SetHelpText Sets this option's help text to s
func (i *MenuOption) SetHelpText(s string) *MenuOption { func (i *MenuOption) SetHelpText(s string) {
i.helpText = s i.helpText = s
return i
} }
// GetHelpText Returns the help text for this option // GetHelpText Returns the help text for this option

View File

@ -36,27 +36,24 @@ func (i *ProgressBar) GetProgress() int {
} }
// SetProgress sets the current progress of the bar // SetProgress sets the current progress of the bar
func (i *ProgressBar) SetProgress(p int) *ProgressBar { func (i *ProgressBar) SetProgress(p int) {
if (p <= i.total || i.allowOverflow) || (p >= 0 || i.allowUnderflow) { if (p <= i.total || i.allowOverflow) || (p >= 0 || i.allowUnderflow) {
i.progress = p i.progress = p
} }
return i
} }
// IncrProgress increments the current progress of the bar // IncrProgress increments the current progress of the bar
func (i *ProgressBar) IncrProgress() *ProgressBar { func (i *ProgressBar) IncrProgress() {
if i.progress < i.total || i.allowOverflow { if i.progress < i.total || i.allowOverflow {
i.progress++ i.progress++
} }
return i
} }
// DecrProgress decrements the current progress of the bar // DecrProgress decrements the current progress of the bar
func (i *ProgressBar) DecrProgress() *ProgressBar { func (i *ProgressBar) DecrProgress() {
if i.progress > 0 || i.allowUnderflow { if i.progress > 0 || i.allowUnderflow {
i.progress-- i.progress--
} }
return i
} }
// GetPercent returns the percent full of the bar // GetPercent returns the percent full of the bar
@ -65,27 +62,23 @@ func (i *ProgressBar) GetPercent() int {
} }
// EnableOverflow Tells the progress bar that it can go over the total // EnableOverflow Tells the progress bar that it can go over the total
func (i *ProgressBar) EnableOverflow() *ProgressBar { func (i *ProgressBar) EnableOverflow() {
i.allowOverflow = true i.allowOverflow = true
return i
} }
// DisableOverflow Tells the progress bar that it can NOT go over the total // DisableOverflow Tells the progress bar that it can NOT go over the total
func (i *ProgressBar) DisableOverflow() *ProgressBar { func (i *ProgressBar) DisableOverflow() {
i.allowOverflow = false i.allowOverflow = false
return i
} }
// EnableUnderflow Tells the progress bar that it can go below zero // EnableUnderflow Tells the progress bar that it can go below zero
func (i *ProgressBar) EnableUnderflow() *ProgressBar { func (i *ProgressBar) EnableUnderflow() {
i.allowUnderflow = true i.allowUnderflow = true
return i
} }
// DisableUnderflow Tells the progress bar that it can NOT go below zero // DisableUnderflow Tells the progress bar that it can NOT go below zero
func (i *ProgressBar) DisableUnderflow() *ProgressBar { func (i *ProgressBar) DisableUnderflow() {
i.allowUnderflow = false i.allowUnderflow = false
return i
} }
// GetFullChar returns the rune used for 'full' // GetFullChar returns the rune used for 'full'
@ -94,9 +87,8 @@ func (i *ProgressBar) GetFullChar() rune {
} }
// SetFullChar sets the rune used for 'full' // SetFullChar sets the rune used for 'full'
func (i *ProgressBar) SetFullChar(f rune) *ProgressBar { func (i *ProgressBar) SetFullChar(f rune) {
i.fullChar = f i.fullChar = f
return i
} }
// GetEmptyChar gets the rune used for 'empty' // GetEmptyChar gets the rune used for 'empty'
@ -105,27 +97,24 @@ func (i *ProgressBar) GetEmptyChar() rune {
} }
// SetEmptyChar sets the rune used for 'empty' // SetEmptyChar sets the rune used for 'empty'
func (i *ProgressBar) SetEmptyChar(f rune) *ProgressBar { func (i *ProgressBar) SetEmptyChar(f rune) {
i.emptyChar = f i.emptyChar = f
return i
} }
// GetX Return the x position of the Progress Bar // GetX Return the x position of the Progress Bar
func (i *ProgressBar) GetX() int { return i.x } func (i *ProgressBar) GetX() int { return i.x }
// SetX set the x position of the ProgressBar to x // SetX set the x position of the ProgressBar to x
func (i *ProgressBar) SetX(x int) *ProgressBar { func (i *ProgressBar) SetX(x int) {
i.x = x i.x = x
return i
} }
// GetY Return the y position of the ProgressBar // GetY Return the y position of the ProgressBar
func (i *ProgressBar) GetY() int { return i.y } func (i *ProgressBar) GetY() int { return i.y }
// SetY Set the y position of the ProgressBar to y // SetY Set the y position of the ProgressBar to y
func (i *ProgressBar) SetY(y int) *ProgressBar { func (i *ProgressBar) SetY(y int) {
i.y = y i.y = y
return i
} }
// GetHeight returns the height of the progress bar // GetHeight returns the height of the progress bar
@ -135,9 +124,8 @@ func (i *ProgressBar) GetHeight() int {
} }
// SetHeight Sets the height of the progress bar // SetHeight Sets the height of the progress bar
func (i *ProgressBar) SetHeight(h int) *ProgressBar { func (i *ProgressBar) SetHeight(h int) {
i.height = h i.height = h
return i
} }
// GetWidth returns the width of the progress bar // GetWidth returns the width of the progress bar
@ -146,33 +134,29 @@ func (i *ProgressBar) GetWidth() int {
} }
// SetWidth Sets the width of the progress bar // SetWidth Sets the width of the progress bar
func (i *ProgressBar) SetWidth(w int) *ProgressBar { func (i *ProgressBar) SetWidth(w int) {
i.width = w i.width = w
return i
} }
// GetBackground Return the current background color of the modal // GetBackground Return the current background color of the modal
func (i *ProgressBar) GetBackground() termbox.Attribute { return i.bg } func (i *ProgressBar) GetBackground() termbox.Attribute { return i.bg }
// SetBackground Set the current background color to bg // SetBackground Set the current background color to bg
func (i *ProgressBar) SetBackground(bg termbox.Attribute) *ProgressBar { func (i *ProgressBar) SetBackground(bg termbox.Attribute) {
i.bg = bg i.bg = bg
return i
} }
// GetForeground Return the current foreground color // GetForeground Return the current foreground color
func (i *ProgressBar) GetForeground() termbox.Attribute { return i.fg } func (i *ProgressBar) GetForeground() termbox.Attribute { return i.fg }
// SetForeground Set the foreground color to fg // SetForeground Set the foreground color to fg
func (i *ProgressBar) SetForeground(fg termbox.Attribute) *ProgressBar { func (i *ProgressBar) SetForeground(fg termbox.Attribute) {
i.fg = fg i.fg = fg
return i
} }
// Align Tells which direction the progress bar empties // Align Tells which direction the progress bar empties
func (i *ProgressBar) Align(a TextAlignment) *ProgressBar { func (i *ProgressBar) Align(a TextAlignment) {
i.alignment = a i.alignment = a
return i
} }
// SetColorized sets whether the progress bar should be colored // SetColorized sets whether the progress bar should be colored
@ -180,9 +164,8 @@ func (i *ProgressBar) Align(a TextAlignment) *ProgressBar {
// 10% - Red // 10% - Red
// 50% - Yellow // 50% - Yellow
// 80% - Green // 80% - Green
func (i *ProgressBar) SetColorized(c bool) *ProgressBar { func (i *ProgressBar) SetColorized(c bool) {
i.colorized = c i.colorized = c
return i
} }
// HandleKeyPress accepts the termbox event and returns whether it was consumed // HandleKeyPress accepts the termbox event and returns whether it was consumed

155
termbox_scrollframe.go Normal file
View File

@ -0,0 +1,155 @@
package termboxUtil
import "github.com/nsf/termbox-go"
// ScrollFrame is a frame for holding other elements
// It manages it's own x/y, tab index
type ScrollFrame struct {
x, y, width, height int
scrollX, scrollY int
tabIdx int
fg, bg termbox.Attribute
bordered bool
controls []termboxControl
}
// CreateScrollFrame creates Scrolling Frame at x, y that is w by h
func CreateScrollFrame(x, y, w, h int, fg, bg termbox.Attribute) *ScrollFrame {
s := ScrollFrame{x: x, y: y, width: w, height: h, fg: fg, bg: bg}
return &s
}
// GetX returns the x position of the scroll frame
func (s *ScrollFrame) GetX() int { return s.x }
// SetX sets the x position of the scroll frame
func (s *ScrollFrame) SetX(x int) {
s.x = x
}
// GetY returns the y position of the scroll frame
func (s *ScrollFrame) GetY() int { return s.y }
// SetY sets the y position of the scroll frame
func (s *ScrollFrame) SetY(y int) {
s.y = y
}
// GetWidth returns the current width of the scroll frame
func (s *ScrollFrame) GetWidth() int { return s.width }
// SetWidth sets the current width of the scroll frame
func (s *ScrollFrame) SetWidth(w int) {
s.width = w
}
// GetHeight returns the current height of the scroll frame
func (s *ScrollFrame) GetHeight() int { return s.height }
// SetHeight sets the current height of the scroll frame
func (s *ScrollFrame) SetHeight(h int) {
s.height = h
}
// IsBordered returns true or false if this scroll frame has a border
func (s *ScrollFrame) IsBordered() bool { return s.bordered }
// SetBordered sets whether we render a border around the scroll frame
func (s *ScrollFrame) SetBordered(b bool) {
s.bordered = b
}
// GetScrollX returns the x distance scrolled
func (s *ScrollFrame) GetScrollX() int {
return s.scrollX
}
// GetScrollY returns the y distance scrolled
func (s *ScrollFrame) GetScrollY() int {
return s.scrollY
}
// ScrollDown scrolls the frame down
func (s *ScrollFrame) ScrollDown() {
s.scrollY++
}
// ScrollUp scrolls the frame up
func (s *ScrollFrame) ScrollUp() {
if s.scrollY > 0 {
s.scrollY--
}
}
// ScrollLeft scrolls the frame left
func (s *ScrollFrame) ScrollLeft() {
if s.scrollX > 0 {
s.scrollX--
}
}
// ScrollRight scrolls the frame right
func (s *ScrollFrame) ScrollRight() {
s.scrollX++
}
// AddControl adds a control to the frame
func (s *ScrollFrame) AddControl(t termboxControl) {
s.controls = append(s.controls, t)
}
// DrawControl figures out the relative position of the control,
// sets it, draws it, then resets it.
func (s *ScrollFrame) DrawControl(t termboxControl) {
if s.IsVisible(t) {
ctlX, ctlY := t.GetX(), t.GetY()
t.SetX((s.GetX() + ctlX))
t.SetY((s.GetY() + ctlY))
t.Draw()
t.SetX(ctlX)
t.SetY(ctlY)
}
}
// IsVisible takes a Termbox Control and returns whether
// that control would be visible in the frame
func (s *ScrollFrame) IsVisible(t termboxControl) bool {
// Check if any part of t should be visible
cX, cY := t.GetX(), t.GetY()
if cX+t.GetWidth() >= s.scrollX && cX <= s.scrollX+s.width {
return cY+t.GetHeight() >= s.scrollY && cY <= s.scrollY+s.height
}
return false
}
// HandleKeyPress accepts the termbox event and returns whether it was consumed
func (s *ScrollFrame) HandleKeyPress(event termbox.Event) bool {
return false
}
// DrawToStrings generates a slice of strings with what should
// be drawn to the screen
func (s *ScrollFrame) DrawToStrings() []string {
return []string{}
}
// Draw outputs the Scoll Frame on the screen
func (s *ScrollFrame) Draw() {
maxWidth := s.width
maxHeight := s.height
x, y := s.x, s.y
startX := s.x
startY := s.y
if s.bordered {
DrawBorder(s.x, s.y, s.x+s.width, s.y+s.height, s.fg, s.bg)
maxWidth--
maxHeight--
x++
y++
startX++
startY++
}
for i := range s.controls {
s.DrawControl(s.controls[i])
}
}

View File

@ -7,6 +7,19 @@ import (
"github.com/nsf/termbox-go" "github.com/nsf/termbox-go"
) )
type termboxControl interface {
GetX() int
SetX(int)
GetY() int
SetY(int)
GetWidth() int
SetWidth(int)
GetHeight() int
SetHeight(int)
HandleKeyPress(termbox.Event) bool
Draw()
}
// TextAlignment is an int value for how we're aligning text // TextAlignment is an int value for how we're aligning text
type TextAlignment int type TextAlignment int