mirror of
https://github.com/br0xen/termbox-util.git
synced 2024-11-25 14:53:14 +00:00
Sync with gogs repo
Starting work on scrollframe
This commit is contained in:
parent
9cd402ccf0
commit
e173cdcce0
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -1,10 +1,6 @@
|
|||||||
package termboxUtil
|
package termboxUtil
|
||||||
|
|
||||||
import (
|
import "github.com/nsf/termbox-go"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"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 {
|
||||||
@ -13,6 +9,8 @@ type InputField struct {
|
|||||||
cursor int
|
cursor int
|
||||||
fg, bg termbox.Attribute
|
fg, bg termbox.Attribute
|
||||||
bordered bool
|
bordered bool
|
||||||
|
wrap bool
|
||||||
|
multiline bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
@ -25,61 +23,69 @@ 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
|
||||||
|
func (i *InputField) DoesWrap() bool { return i.wrap }
|
||||||
|
|
||||||
|
// SetWrap sets whether we wrap the text at width.
|
||||||
|
func (i *InputField) SetWrap(b bool) {
|
||||||
|
i.wrap = b
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsMultiline returns true or false if this field can have multiple lines
|
||||||
|
func (i *InputField) IsMultiline() bool { return i.multiline }
|
||||||
|
|
||||||
|
// SetMultiline sets whether the field can have multiple lines
|
||||||
|
func (i *InputField) SetMultiline(b bool) {
|
||||||
|
i.multiline = b
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleKeyPress accepts the termbox event and returns whether it was consumed
|
// HandleKeyPress accepts the termbox event and returns whether it was consumed
|
||||||
func (i *InputField) HandleKeyPress(event termbox.Event) bool {
|
func (i *InputField) HandleKeyPress(event termbox.Event) bool {
|
||||||
if event.Key == termbox.KeyEnter {
|
if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 {
|
||||||
// Done editing
|
|
||||||
} else if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 {
|
|
||||||
if len(i.value) > 0 {
|
if len(i.value) > 0 {
|
||||||
i.value = i.value[:len(i.value)-1]
|
i.value = i.value[:len(i.value)-1]
|
||||||
}
|
}
|
||||||
@ -92,8 +98,8 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool {
|
|||||||
i.cursor++
|
i.cursor++
|
||||||
}
|
}
|
||||||
} else if event.Key == termbox.KeyCtrlU {
|
} else if event.Key == termbox.KeyCtrlU {
|
||||||
// Ctrl+U Clears the Input
|
// Ctrl+U Clears the Input (before the cursor)
|
||||||
i.value = ""
|
i.value = i.value[i.cursor:]
|
||||||
} else {
|
} else {
|
||||||
// Get the rune to add to our value. Space and Tab are special cases where
|
// Get the rune to add to our value. Space and Tab are special cases where
|
||||||
// we can't use the event's rune directly
|
// we can't use the event's rune directly
|
||||||
@ -103,18 +109,26 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool {
|
|||||||
ch = " "
|
ch = " "
|
||||||
case termbox.KeyTab:
|
case termbox.KeyTab:
|
||||||
ch = "\t"
|
ch = "\t"
|
||||||
|
/* Multiline is disabled right now
|
||||||
|
case termbox.KeyEnter:
|
||||||
|
if i.multiline {
|
||||||
|
ch = "\n"
|
||||||
|
}
|
||||||
|
*/
|
||||||
default:
|
default:
|
||||||
ch = string(event.Ch)
|
if KeyIsAlphaNumeric(event) || KeyIsSymbol(event) {
|
||||||
|
ch = string(event.Ch)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if i.cursor+len(i.value) == 0 {
|
if i.cursor+len(i.value) == 0 {
|
||||||
i.value = fmt.Sprintf("%s%s", ch, i.value)
|
i.value = string(ch) + i.value
|
||||||
} else if i.cursor == 0 {
|
} else if i.cursor == 0 {
|
||||||
i.value = fmt.Sprintf("%s%s", i.value, ch)
|
i.value = i.value + string(ch)
|
||||||
} else {
|
} else {
|
||||||
strPt1 := i.value[:(len(i.value) + i.cursor)]
|
strPt1 := i.value[:(len(i.value) + i.cursor)]
|
||||||
strPt2 := i.value[(len(i.value) + i.cursor):]
|
strPt2 := i.value[(len(i.value) + i.cursor):]
|
||||||
i.value = fmt.Sprintf("%s%s%s", strPt1, ch, strPt2)
|
i.value = strPt1 + string(ch) + strPt2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
@ -122,8 +136,19 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool {
|
|||||||
|
|
||||||
// Draw outputs the input field on the screen
|
// Draw outputs the input field on the screen
|
||||||
func (i *InputField) Draw() {
|
func (i *InputField) Draw() {
|
||||||
|
maxWidth := i.width
|
||||||
|
maxHeight := i.height
|
||||||
|
x, y := i.x, i.y
|
||||||
|
startX := i.x
|
||||||
|
startY := i.y
|
||||||
if i.bordered {
|
if i.bordered {
|
||||||
DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg)
|
DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg)
|
||||||
|
maxWidth--
|
||||||
|
maxHeight--
|
||||||
|
x++
|
||||||
|
y++
|
||||||
|
startX++
|
||||||
|
startY++
|
||||||
}
|
}
|
||||||
|
|
||||||
var strPt1, strPt2 string
|
var strPt1, strPt2 string
|
||||||
@ -145,7 +170,56 @@ func (i *InputField) Draw() {
|
|||||||
} else {
|
} else {
|
||||||
strPt1, strPt2, cursorRune = "", "", ' '
|
strPt1, strPt2, cursorRune = "", "", ' '
|
||||||
}
|
}
|
||||||
x, y := DrawStringAtPoint(strPt1, i.x+1, i.y+1, i.fg, i.bg)
|
// Original:
|
||||||
termbox.SetCell(x, y, cursorRune, i.bg, i.fg)
|
/*
|
||||||
DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg)
|
x, y = DrawStringAtPoint(strPt1, i.x+1, i.y+1, i.fg, i.bg)
|
||||||
|
termbox.SetCell(x, y, cursorRune, i.bg, i.fg)
|
||||||
|
DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg)
|
||||||
|
*/
|
||||||
|
if i.wrap {
|
||||||
|
// Split the text into maxWidth chunks
|
||||||
|
for len(strPt1) > maxWidth {
|
||||||
|
breakAt := maxWidth
|
||||||
|
DrawStringAtPoint(strPt1[:breakAt], x, y, i.fg, i.bg)
|
||||||
|
x = startX
|
||||||
|
y++
|
||||||
|
strPt1 = strPt1[breakAt:]
|
||||||
|
}
|
||||||
|
x, y = DrawStringAtPoint(strPt1, x, y, i.fg, i.bg)
|
||||||
|
if x >= maxWidth {
|
||||||
|
y++
|
||||||
|
x = startX
|
||||||
|
}
|
||||||
|
termbox.SetCell(x, y, cursorRune, i.bg, i.fg)
|
||||||
|
x++
|
||||||
|
if len(strPt2) > 0 {
|
||||||
|
lenLeft := maxWidth - len(strPt1) - 1
|
||||||
|
if lenLeft > 0 && len(strPt2) > lenLeft {
|
||||||
|
DrawStringAtPoint(strPt2[:lenLeft], x+1, y, i.fg, i.bg)
|
||||||
|
strPt2 = strPt2[lenLeft:]
|
||||||
|
}
|
||||||
|
for len(strPt2) > maxWidth {
|
||||||
|
breakAt := maxWidth
|
||||||
|
DrawStringAtPoint(strPt2[:breakAt], x, y, i.fg, i.bg)
|
||||||
|
x = startX
|
||||||
|
y++
|
||||||
|
strPt2 = strPt2[breakAt:]
|
||||||
|
}
|
||||||
|
x, y = DrawStringAtPoint(strPt2, x, y, i.fg, i.bg)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for len(strPt1)+len(strPt2)+1 > maxWidth {
|
||||||
|
if len(strPt1) >= len(strPt2) {
|
||||||
|
if len(strPt1) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
strPt1 = strPt1[1:]
|
||||||
|
} else {
|
||||||
|
strPt2 = strPt2[:len(strPt2)-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
x, y = DrawStringAtPoint(strPt1, i.x+1, i.y+1, i.fg, i.bg)
|
||||||
|
termbox.SetCell(x, y, cursorRune, i.bg, i.fg)
|
||||||
|
DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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,19 +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
|
||||||
|
func (i *InputModal) SetInputWrap(b bool) {
|
||||||
|
i.input.SetWrap(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
|
@ -15,7 +15,7 @@ type Menu struct {
|
|||||||
disabledBg, disabledFg termbox.Attribute
|
disabledBg, disabledFg termbox.Attribute
|
||||||
isDone bool
|
isDone bool
|
||||||
bordered bool
|
bordered bool
|
||||||
vimMode bool
|
vimMode bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateMenu Creates a menu with the specified attributes
|
// CreateMenu Creates a menu with the specified attributes
|
||||||
@ -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,62 +186,55 @@ 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
|
||||||
func (i *Menu) HandleKeyPress(event termbox.Event) bool {
|
func (i *Menu) HandleKeyPress(event termbox.Event) bool {
|
||||||
if event.Key == termbox.KeyEnter {
|
if event.Key == termbox.KeyEnter || event.Key == termbox.KeySpace {
|
||||||
i.isDone = true
|
i.isDone = true
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -310,6 +292,11 @@ func (i *Menu) Draw() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the currently selected option is disabled, move to the next
|
||||||
|
if i.GetSelectedOption().IsDisabled() {
|
||||||
|
i.SelectNextOption()
|
||||||
|
}
|
||||||
|
|
||||||
// Print the options
|
// Print the options
|
||||||
if len(i.options) > 0 {
|
if len(i.options) > 0 {
|
||||||
for idx := range i.options {
|
for idx := range i.options {
|
||||||
@ -343,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
|
||||||
@ -374,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
|
||||||
|
@ -12,6 +12,7 @@ type ProgressBar struct {
|
|||||||
emptyChar rune
|
emptyChar rune
|
||||||
bordered bool
|
bordered bool
|
||||||
alignment TextAlignment
|
alignment TextAlignment
|
||||||
|
colorized bool
|
||||||
|
|
||||||
x, y int
|
x, y int
|
||||||
width, height int
|
width, height int
|
||||||
@ -35,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
|
||||||
@ -64,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'
|
||||||
@ -93,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'
|
||||||
@ -104,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
|
||||||
@ -134,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
|
||||||
@ -145,33 +134,38 @@ 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
|
||||||
|
// depending on how full it is:
|
||||||
|
// 10% - Red
|
||||||
|
// 50% - Yellow
|
||||||
|
// 80% - Green
|
||||||
|
func (i *ProgressBar) SetColorized(c bool) {
|
||||||
|
i.colorized = c
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleKeyPress accepts the termbox event and returns whether it was consumed
|
// HandleKeyPress accepts the termbox event and returns whether it was consumed
|
||||||
@ -183,11 +177,21 @@ func (i *ProgressBar) HandleKeyPress(event termbox.Event) bool {
|
|||||||
func (i *ProgressBar) Draw() {
|
func (i *ProgressBar) Draw() {
|
||||||
// For now, just draw a [#### ] bar
|
// For now, just draw a [#### ] bar
|
||||||
// TODO: make this more advanced
|
// TODO: make this more advanced
|
||||||
|
useFg := i.fg
|
||||||
|
if i.colorized {
|
||||||
|
if i.GetPercent() < 10 {
|
||||||
|
useFg = termbox.ColorRed
|
||||||
|
} else if i.GetPercent() < 50 {
|
||||||
|
useFg = termbox.ColorYellow
|
||||||
|
} else {
|
||||||
|
useFg = termbox.ColorGreen
|
||||||
|
}
|
||||||
|
}
|
||||||
drawX, drawY := i.x, i.y
|
drawX, drawY := i.x, i.y
|
||||||
fillWidth, fillHeight := i.width-2, i.height
|
fillWidth, fillHeight := i.width-2, i.height
|
||||||
DrawStringAtPoint("[", drawX, drawY, i.fg, i.bg)
|
DrawStringAtPoint("[", drawX, drawY, i.fg, i.bg)
|
||||||
numFull := int(float64(fillWidth) * float64(i.progress) / float64(i.total))
|
numFull := int(float64(fillWidth) * float64(i.progress) / float64(i.total))
|
||||||
FillWithChar(i.fullChar, drawX+1, drawY, drawX+1+numFull, drawY+(fillHeight-1), i.fg, i.bg)
|
FillWithChar(i.fullChar, drawX+1, drawY, drawX+1+numFull, drawY+(fillHeight-1), useFg, i.bg)
|
||||||
DrawStringAtPoint("]", drawX+i.width-1, drawY, i.fg, i.bg)
|
DrawStringAtPoint("]", drawX+i.width-1, drawY, i.fg, i.bg)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
155
termbox_scrollframe.go
Normal file
155
termbox_scrollframe.go
Normal 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])
|
||||||
|
}
|
||||||
|
}
|
@ -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
|
||||||
|
|
||||||
@ -19,6 +32,49 @@ const (
|
|||||||
AlignRight
|
AlignRight
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/* Basic Input Helpers */
|
||||||
|
|
||||||
|
// KeyIsAlphaNumeric Returns whether the termbox event is an
|
||||||
|
// Alpha-Numeric Key Press
|
||||||
|
func KeyIsAlphaNumeric(event termbox.Event) bool {
|
||||||
|
return KeyIsAlpha(event) || KeyIsNumeric(event)
|
||||||
|
}
|
||||||
|
|
||||||
|
// KeyIsAlpha Returns whether the termbox event is a
|
||||||
|
// alphabetic Key press
|
||||||
|
func KeyIsAlpha(event termbox.Event) bool {
|
||||||
|
k := event.Ch
|
||||||
|
if (k >= 'a' && k <= 'z') || (k >= 'A' && k <= 'Z') {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// KeyIsNumeric Returns whether the termbox event is a
|
||||||
|
// numeric Key press
|
||||||
|
func KeyIsNumeric(event termbox.Event) bool {
|
||||||
|
k := event.Ch
|
||||||
|
if k >= '0' && k <= '9' {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// KeyIsSymbol Returns whether the termbox event is a
|
||||||
|
// symbol Key press
|
||||||
|
func KeyIsSymbol(event termbox.Event) bool {
|
||||||
|
symbols := []rune{'!', '@', '#', '$', '%', '^', '&', '*',
|
||||||
|
'(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '|',
|
||||||
|
';', ':', '"', '\'', ',', '<', '.', '>', '/', '?', '`', '~'}
|
||||||
|
k := event.Ch
|
||||||
|
for i := range symbols {
|
||||||
|
if k == symbols[i] {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
/* Basic Output Helpers */
|
/* Basic Output Helpers */
|
||||||
|
|
||||||
// DrawStringAtPoint Draw a string of text at x, y with foreground color fg, background color bg
|
// DrawStringAtPoint Draw a string of text at x, y with foreground color fg, background color bg
|
||||||
|
Loading…
Reference in New Issue
Block a user