Linted termbox_inputmodal.go

This commit is contained in:
Brian Buller 2015-10-21 11:36:00 -05:00
parent 607a76b51e
commit 2dd65ed8b8
1 changed files with 72 additions and 31 deletions

View File

@ -1,152 +1,193 @@
package termbox_util package termboxUtil
import ( import (
"github.com/nsf/termbox-go" "github.com/nsf/termbox-go"
) )
// InputModal A modal for text input
type InputModal struct { type InputModal struct {
title string title string
text string text string
input *InputField input *InputField
x, y, width, height int x, y, width, height int
show_help bool showHelp bool
cursor int cursor int
bg, fg termbox.Attribute bg, fg termbox.Attribute
is_done bool isDone bool
is_visible bool isVisible bool
} }
// CreateInputModal Create an input modal with the given attributes
func CreateInputModal(title string, x, y, width, height int, fg, bg termbox.Attribute) *InputModal { func CreateInputModal(title string, x, y, width, height int, fg, bg termbox.Attribute) *InputModal {
i := InputModal{title: title, x: x, y: y, width: width, height: height, fg: fg, bg: bg} i := InputModal{title: title, x: x, y: y, width: width, height: height, fg: fg, bg: bg}
i.input = CreateInputField(i.x+1, i.y+3, i.width-2, 2, i.fg, i.bg) i.input = CreateInputField(i.x+1, i.y+3, i.width-2, 2, i.fg, i.bg)
i.show_help = true i.showHelp = true
i.input.bordered = true i.input.bordered = true
return &i return &i
} }
// GetTitle Return the title of the modal
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
func (i *InputModal) SetTitle(s string) *InputModal { func (i *InputModal) SetTitle(s string) *InputModal {
i.title = s i.title = s
return i return i
} }
// 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
func (i *InputModal) SetText(s string) *InputModal { func (i *InputModal) SetText(s string) *InputModal {
i.text = s i.text = s
return i return i
} }
// 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
func (i *InputModal) SetX(x int) *InputModal { func (i *InputModal) SetX(x int) *InputModal {
i.x = x i.x = x
return i return i
} }
// 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
func (i *InputModal) SetY(y int) *InputModal { func (i *InputModal) SetY(y int) *InputModal {
i.y = y i.y = y
return i return i
} }
// 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
func (i *InputModal) SetWidth(width int) *InputModal { func (i *InputModal) SetWidth(width int) *InputModal {
i.width = width i.width = width
return i return i
} }
// 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
func (i *InputModal) SetHeight(height int) *InputModal { func (i *InputModal) SetHeight(height int) *InputModal {
i.height = height i.height = height
return i return i
} }
func (i *InputModal) HelpIsShown() bool { return i.show_help } // HelpIsShown Returns whether the modal is showing it's help text or not
func (i *InputModal) HelpIsShown() bool { return i.showHelp }
// ShowHelp Set the "Show Help" flag
func (i *InputModal) ShowHelp(b bool) *InputModal { func (i *InputModal) ShowHelp(b bool) *InputModal {
i.show_help = b i.showHelp = b
return i return i
} }
// 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
func (i *InputModal) SetBackground(bg termbox.Attribute) *InputModal { func (i *InputModal) SetBackground(bg termbox.Attribute) *InputModal {
i.bg = bg i.bg = bg
return i return i
} }
// 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
func (i *InputModal) SetForeground(fg termbox.Attribute) *InputModal { func (i *InputModal) SetForeground(fg termbox.Attribute) *InputModal {
i.fg = fg i.fg = fg
return i return i
} }
// Show Sets the visibility flag to true
func (i *InputModal) Show() *InputModal { func (i *InputModal) Show() *InputModal {
i.is_visible = true i.isVisible = true
return i return i
} }
// Hide Sets the visibility flag to false
func (i *InputModal) Hide() *InputModal { func (i *InputModal) Hide() *InputModal {
i.is_visible = false i.isVisible = false
return i return i
} }
// 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) *InputModal {
i.is_done = b i.isDone = b
return i return i
} }
// IsDone Returns the "isDone" flag
func (i *InputModal) IsDone() bool { func (i *InputModal) IsDone() bool {
return i.is_done return i.isDone
} }
// GetValue Return the current value of the input
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
func (i *InputModal) SetValue(s string) *InputModal { func (i *InputModal) SetValue(s string) *InputModal {
i.input.SetValue(s) i.input.SetValue(s)
return i return i
} }
// Clear Resets all non-positional parameters of the modal
func (i *InputModal) Clear() *InputModal { func (i *InputModal) Clear() *InputModal {
i.title = "" i.title = ""
i.text = "" i.text = ""
i.input.SetValue("") i.input.SetValue("")
i.is_done = false i.isDone = false
i.is_visible = false i.isVisible = false
return i return i
} }
// HandleKeyPress Handle the termbox event, return true if it was consumed
func (i *InputModal) HandleKeyPress(event termbox.Event) bool { func (i *InputModal) HandleKeyPress(event termbox.Event) bool {
if event.Key == termbox.KeyEnter { if event.Key == termbox.KeyEnter {
// Done editing // Done editing
i.is_done = true i.isDone = true
return true return true
} else {
return i.input.HandleKeyPress(event)
} }
return i.input.HandleKeyPress(event)
} }
// Draw Draw the modal
func (i *InputModal) Draw() { func (i *InputModal) Draw() {
if i.is_visible { if i.isVisible {
// First blank out the area we'll be putting the modal // First blank out the area we'll be putting the modal
FillWithChar(' ', i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) FillWithChar(' ', i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg)
next_y := i.y + 1 nextY := i.y + 1
// The title // The title
if i.title != "" { if i.title != "" {
if len(i.title) > i.width { if len(i.title) > i.width {
diff := i.width - len(i.title) diff := i.width - len(i.title)
DrawStringAtPoint(i.title[:len(i.title)+diff-1], i.x+1, next_y, i.fg, i.bg) DrawStringAtPoint(i.title[:len(i.title)+diff-1], i.x+1, nextY, i.fg, i.bg)
} else { } else {
DrawStringAtPoint(i.title, i.x+1, next_y, i.fg, i.bg) DrawStringAtPoint(i.title, i.x+1, nextY, i.fg, i.bg)
} }
next_y += 1 nextY++
FillWithChar('-', i.x+1, next_y, i.x+i.width-1, next_y, i.fg, i.bg) FillWithChar('-', i.x+1, nextY, i.x+i.width-1, nextY, i.fg, i.bg)
next_y += 1 nextY++
} }
if i.text != "" { if i.text != "" {
DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg) DrawStringAtPoint(i.text, i.x+1, nextY, i.fg, i.bg)
next_y += 1 nextY++
} }
i.input.SetY(next_y) i.input.SetY(nextY)
i.input.Draw() i.input.Draw()
next_y += 3 nextY += 3
if i.show_help { if i.showHelp {
help_string := " (ENTER) to Accept. (ESC) to Cancel. " helpString := " (ENTER) to Accept. (ESC) to Cancel. "
help_x := (i.x + i.width - len(help_string)) - 1 helpX := (i.x + i.width - len(helpString)) - 1
DrawStringAtPoint(help_string, help_x, next_y, i.fg, i.bg) DrawStringAtPoint(helpString, helpX, nextY, i.fg, i.bg)
} }
// Now draw the border // Now draw the border
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)