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

Linted termbox_confirmmodal.go

This commit is contained in:
Brian Buller 2015-10-21 11:24:32 -05:00
parent 8aa5f1a4c0
commit 607a76b51e

View File

@ -1,143 +1,184 @@
package termbox_util package termboxUtil
import ( import (
"github.com/nsf/termbox-go" "github.com/nsf/termbox-go"
) )
// ConfirmModal is a modal with yes/no (or similar) buttons
type ConfirmModal struct { type ConfirmModal struct {
title string title string
text string text string
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
accepted bool accepted bool
value string value string
is_visible bool isVisible bool
} }
// CreateConfirmModal Creates a confirmation modal with the specified attributes
func CreateConfirmModal(title string, x, y, width, height int, fg, bg termbox.Attribute) *ConfirmModal { func CreateConfirmModal(title string, x, y, width, height int, fg, bg termbox.Attribute) *ConfirmModal {
i := ConfirmModal{title: title, x: x, y: y, width: width, height: height, fg: fg, bg: bg} i := ConfirmModal{title: title, x: x, y: y, width: width, height: height, fg: fg, bg: bg}
if i.title == "" && i.text == "" { if i.title == "" && i.text == "" {
i.title = "Confirm?" i.title = "Confirm?"
} }
i.show_help = true i.showHelp = true
return &i return &i
} }
// GetTitle returns the current title of the modal
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
func (i *ConfirmModal) SetTitle(s string) *ConfirmModal { func (i *ConfirmModal) SetTitle(s string) *ConfirmModal {
i.title = s i.title = s
return i return i
} }
// 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
func (i *ConfirmModal) SetText(s string) *ConfirmModal { func (i *ConfirmModal) SetText(s string) *ConfirmModal {
i.text = s i.text = s
return i return i
} }
// 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
func (i *ConfirmModal) SetX(x int) *ConfirmModal { func (i *ConfirmModal) SetX(x int) *ConfirmModal {
i.x = x i.x = x
return i return i
} }
// 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
func (i *ConfirmModal) SetY(y int) *ConfirmModal { func (i *ConfirmModal) SetY(y int) *ConfirmModal {
i.y = y i.y = y
return i return i
} }
// 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
func (i *ConfirmModal) SetWidth(width int) *ConfirmModal { func (i *ConfirmModal) SetWidth(width int) *ConfirmModal {
i.width = width i.width = width
return i return i
} }
// 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
func (i *ConfirmModal) SetHeight(height int) *ConfirmModal { func (i *ConfirmModal) SetHeight(height int) *ConfirmModal {
i.height = height i.height = height
return i return i
} }
func (i *ConfirmModal) HelpIsShown() bool { return i.show_help } // HelpIsShown returns true or false if the help is displayed
func (i *ConfirmModal) HelpIsShown() bool { return i.showHelp }
// ShowHelp sets whether or not to display the help text
func (i *ConfirmModal) ShowHelp(b bool) *ConfirmModal { func (i *ConfirmModal) ShowHelp(b bool) *ConfirmModal {
i.show_help = b i.showHelp = b
return i return i
} }
// 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
func (i *ConfirmModal) SetBackground(bg termbox.Attribute) *ConfirmModal { func (i *ConfirmModal) SetBackground(bg termbox.Attribute) *ConfirmModal {
i.bg = bg i.bg = bg
return i return i
} }
// 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
func (i *ConfirmModal) SetForeground(fg termbox.Attribute) *ConfirmModal { func (i *ConfirmModal) SetForeground(fg termbox.Attribute) *ConfirmModal {
i.fg = fg i.fg = fg
return i return i
} }
func (i *ConfirmModal) IsDone() bool { return i.is_done } // IsDone returns whether the user has answered the modal
func (i *ConfirmModal) IsDone() bool { return i.isDone }
// SetDone sets whether the modal has completed it's purpose
func (i *ConfirmModal) SetDone(b bool) *ConfirmModal { func (i *ConfirmModal) SetDone(b bool) *ConfirmModal {
i.is_done = b i.isDone = b
return i return i
} }
// Show sets the visibility flag of the modal to true
func (i *ConfirmModal) Show() *ConfirmModal { func (i *ConfirmModal) Show() *ConfirmModal {
i.is_visible = true i.isVisible = true
return i
}
func (i *ConfirmModal) Hide() *ConfirmModal {
i.is_visible = false
return i return i
} }
// Hide sets the visibility flag of the modal to false
func (i *ConfirmModal) Hide() *ConfirmModal {
i.isVisible = false
return i
}
// 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
func (i *ConfirmModal) Clear() *ConfirmModal { func (i *ConfirmModal) Clear() *ConfirmModal {
i.title = "" i.title = ""
i.text = "" i.text = ""
i.accepted = false i.accepted = false
i.is_done = false i.isDone = false
return i return i
} }
// HandleKeyPress handles the termbox event and returns whether it was consumed
func (i *ConfirmModal) HandleKeyPress(event termbox.Event) bool { func (i *ConfirmModal) HandleKeyPress(event termbox.Event) bool {
if event.Ch == 'Y' || event.Ch == 'y' { if event.Ch == 'Y' || event.Ch == 'y' {
i.accepted = true i.accepted = true
i.is_done = true i.isDone = true
return true return true
} else if event.Ch == 'N' || event.Ch == 'n' { } else if event.Ch == 'N' || event.Ch == 'n' {
i.accepted = false i.accepted = false
i.is_done = true i.isDone = true
return true return true
} }
return false return false
} }
// Draw draws the modal
func (i *ConfirmModal) Draw() { func (i *ConfirmModal) Draw() {
// 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)
// 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)
next_y := i.y + 1 nextY := i.y + 1
// The title // The title
if i.title != "" { if i.title != "" {
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++
} }
next_y += 2 nextY += 2
if i.show_help { if i.showHelp {
help_string := " (Y/y) Confirm. (N/n) Reject. " helpString := " (Y/y) Confirm. (N/n) Reject. "
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)
} }
} }