1
0
mirror of https://github.com/br0xen/termbox-util.git synced 2024-11-25 23:03:13 +00:00

Linted the library

This commit is contained in:
Brian Buller 2015-10-21 11:51:22 -05:00
parent a018159e66
commit 1f6f263ed9
5 changed files with 229 additions and 103 deletions

0
termbox-test/main.go Normal file → Executable file
View File

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)
} }
} }

View File

@ -1,10 +1,12 @@
package termbox_util package termboxUtil
import ( import (
"fmt" "fmt"
"github.com/nsf/termbox-go" "github.com/nsf/termbox-go"
) )
// InputField is a field for inputting text
type InputField struct { type InputField struct {
value string value string
x, y, width, height int x, y, width, height int
@ -13,47 +15,67 @@ type InputField struct {
bordered bool bordered bool
} }
// CreateInputField creates an input field at x, y that is w by h
func CreateInputField(x, y, w, h int, fg, bg termbox.Attribute) *InputField { func CreateInputField(x, y, w, h int, fg, bg termbox.Attribute) *InputField {
i := InputField{x: x, y: y, width: w, height: h, fg: fg, bg: bg} i := InputField{x: x, y: y, width: w, height: h, fg: fg, bg: bg}
return &i return &i
} }
// GetValue gets the current text that is in the 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
func (i *InputField) SetValue(s string) *InputField { func (i *InputField) SetValue(s string) *InputField {
i.value = s i.value = s
return i return i
} }
// 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
func (i *InputField) SetX(x int) *InputField { func (i *InputField) SetX(x int) *InputField {
i.x = x i.x = x
return i return i
} }
// 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
func (i *InputField) SetY(y int) *InputField { func (i *InputField) SetY(y int) *InputField {
i.y = y i.y = y
return i return i
} }
// 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
func (i *InputField) SetWidth(w int) *InputField { func (i *InputField) SetWidth(w int) *InputField {
i.width = w i.width = w
return i return i
} }
// 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
func (i *InputField) SetHeight(h int) *InputField { func (i *InputField) SetHeight(h int) *InputField {
i.height = h i.height = h
return i return i
} }
// 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
func (i *InputField) SetBordered(b bool) *InputField { func (i *InputField) SetBordered(b bool) *InputField {
i.bordered = b i.bordered = b
return i return i
} }
// 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.KeyEnter {
// Done editing // Done editing
@ -63,54 +85,67 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool {
} }
} else if event.Key == termbox.KeyArrowLeft { } else if event.Key == termbox.KeyArrowLeft {
if i.cursor+len(i.value) > 0 { if i.cursor+len(i.value) > 0 {
i.cursor -= 1 i.cursor--
} }
} else if event.Key == termbox.KeyArrowRight { } else if event.Key == termbox.KeyArrowRight {
if i.cursor < 0 { if i.cursor < 0 {
i.cursor += 1 i.cursor++
} }
} else if event.Key == termbox.KeyCtrlU { } else if event.Key == termbox.KeyCtrlU {
// Ctrl+U Clears the Input // Ctrl+U Clears the Input
i.value = "" i.value = ""
} else { } else {
// Get the rune to add to our value. Space and Tab are special cases where
// we can't use the event's rune directly
var ch string
switch event.Key {
case termbox.KeySpace:
ch = " "
case termbox.KeyTab:
ch = "\t"
default:
ch = string(event.Ch)
}
if i.cursor+len(i.value) == 0 { if i.cursor+len(i.value) == 0 {
i.value = fmt.Sprintf("%s%s", string(event.Ch), i.value) i.value = fmt.Sprintf("%s%s", ch, i.value)
} else if i.cursor == 0 { } else if i.cursor == 0 {
i.value = fmt.Sprintf("%s%s", i.value, string(event.Ch)) i.value = fmt.Sprintf("%s%s", i.value, ch)
} else { } else {
str_pt_1 := i.value[:(len(i.value) + i.cursor)] strPt1 := i.value[:(len(i.value) + i.cursor)]
str_pt_2 := i.value[(len(i.value) + i.cursor):] strPt2 := i.value[(len(i.value) + i.cursor):]
i.value = fmt.Sprintf("%s%s%s", str_pt_1, string(event.Ch), str_pt_2) i.value = fmt.Sprintf("%s%s%s", strPt1, ch, strPt2)
} }
} }
return true return true
} }
// Draw outputs the input field on the screen
func (i *InputField) Draw() { func (i *InputField) Draw() {
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)
} }
var str_pt_1, str_pt_2 string var strPt1, strPt2 string
var cursor_rune rune var cursorRune rune
if len(i.value) > 0 { if len(i.value) > 0 {
if i.cursor+len(i.value) == 0 { if i.cursor+len(i.value) == 0 {
str_pt_1 = "" strPt1 = ""
str_pt_2 = i.value[1:] strPt2 = i.value[1:]
cursor_rune = rune(i.value[0]) cursorRune = rune(i.value[0])
} else if i.cursor == 0 { } else if i.cursor == 0 {
str_pt_1 = i.value strPt1 = i.value
str_pt_2 = "" strPt2 = ""
cursor_rune = ' ' cursorRune = ' '
} else { } else {
str_pt_1 = i.value[:(len(i.value) + i.cursor)] strPt1 = i.value[:(len(i.value) + i.cursor)]
str_pt_2 = i.value[(len(i.value)+i.cursor)+1:] strPt2 = i.value[(len(i.value)+i.cursor)+1:]
cursor_rune = rune(i.value[len(i.value)+i.cursor]) cursorRune = rune(i.value[len(i.value)+i.cursor])
} }
} else { } else {
str_pt_1, str_pt_2, cursor_rune = "", "", ' ' strPt1, strPt2, cursorRune = "", "", ' '
} }
x, y := DrawStringAtPoint(str_pt_1, i.x+1, i.y+1, i.fg, i.bg) x, y := DrawStringAtPoint(strPt1, i.x+1, i.y+1, i.fg, i.bg)
termbox.SetCell(x, y, cursor_rune, i.bg, i.fg) termbox.SetCell(x, y, cursorRune, i.bg, i.fg)
DrawStringAtPoint(str_pt_2, x+1, y, i.fg, i.bg) DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg)
} }

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)

View File

@ -1,29 +1,37 @@
package termbox_util package termboxUtil
import ( import (
"fmt" "fmt"
"github.com/nsf/termbox-go"
"strings" "strings"
"github.com/nsf/termbox-go"
) )
// TextAlignment is an int value for how we're aligning text
type TextAlignment int type TextAlignment int
const ( const (
ALIGN_LEFT = iota // AlignLeft Aligns text to the left
ALIGN_CENTER AlignLeft = iota
ALIGN_RIGHT // AlignCenter Aligns text to the center
AlignCenter
// AlignRight Aligns text to the right
AlignRight
) )
/* Basic Output Helpers */ /* Basic Output Helpers */
// DrawStringAtPoint Draw a string of text at x, y with foreground color fg, background color bg
func DrawStringAtPoint(str string, x int, y int, fg termbox.Attribute, bg termbox.Attribute) (int, int) { func DrawStringAtPoint(str string, x int, y int, fg termbox.Attribute, bg termbox.Attribute) (int, int) {
x_pos := x xPos := x
for _, runeValue := range str { for _, runeValue := range str {
termbox.SetCell(x_pos, y, runeValue, fg, bg) termbox.SetCell(xPos, y, runeValue, fg, bg)
x_pos++ xPos++
} }
return x_pos, y return xPos, y
} }
// FillWithChar Fills from x1,y1 through x2,y2 with the rune r, foreground color fg, background bg
func FillWithChar(r rune, x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.Attribute) { func FillWithChar(r rune, x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.Attribute) {
for xx := x1; xx <= x2; xx++ { for xx := x1; xx <= x2; xx++ {
for yx := y1; yx <= y2; yx++ { for yx := y1; yx <= y2; yx++ {
@ -32,6 +40,7 @@ func FillWithChar(r rune, x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.A
} }
} }
// DrawBorder Draw a border around the area inside x1,y1 -> x2, y2
func DrawBorder(x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.Attribute) { func DrawBorder(x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.Attribute) {
termbox.SetCell(x1, y1, '┌', fg, bg) termbox.SetCell(x1, y1, '┌', fg, bg)
FillWithChar('─', x1+1, y1, x2-1, y1, fg, bg) FillWithChar('─', x1+1, y1, x2-1, y1, fg, bg)
@ -45,23 +54,23 @@ func DrawBorder(x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.Attribute)
termbox.SetCell(x2, y2, '┘', fg, bg) termbox.SetCell(x2, y2, '┘', fg, bg)
} }
// AlignText Aligns the text txt within width characters using the specified alignment
func AlignText(txt string, width int, align TextAlignment) string { func AlignText(txt string, width int, align TextAlignment) string {
num_spaces := width - len(txt) numSpaces := width - len(txt)
switch align { switch align {
case ALIGN_CENTER: case AlignCenter:
if num_spaces/2 > 0 { if numSpaces/2 > 0 {
return fmt.Sprintf("%s%s%s", return fmt.Sprintf("%s%s%s",
strings.Repeat(" ", num_spaces/2), strings.Repeat(" ", numSpaces/2),
txt, strings.Repeat(" ", num_spaces/2), txt, strings.Repeat(" ", numSpaces/2),
) )
} else {
return txt
} }
case ALIGN_RIGHT: return txt
return fmt.Sprintf("%s%s", strings.Repeat(" ", num_spaces), txt) case AlignRight:
return fmt.Sprintf("%s%s", strings.Repeat(" ", numSpaces), txt)
default: default:
return fmt.Sprintf("%s%s", txt, strings.Repeat(" ", num_spaces)) return fmt.Sprintf("%s%s", txt, strings.Repeat(" ", numSpaces))
} }
} }
/* More advanced things are in their respective files*/ /* More advanced things are in their respective files */