diff --git a/termbox-test/main.go b/termbox-test/main.go old mode 100644 new mode 100755 diff --git a/termbox_confirmmodal.go b/termbox_confirmmodal.go index fc247c1..2b40187 100644 --- a/termbox_confirmmodal.go +++ b/termbox_confirmmodal.go @@ -1,143 +1,184 @@ -package termbox_util +package termboxUtil import ( "github.com/nsf/termbox-go" ) +// ConfirmModal is a modal with yes/no (or similar) buttons type ConfirmModal struct { title string text string x, y, width, height int - show_help bool + showHelp bool cursor int bg, fg termbox.Attribute - is_done bool + isDone bool accepted bool 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 { i := ConfirmModal{title: title, x: x, y: y, width: width, height: height, fg: fg, bg: bg} if i.title == "" && i.text == "" { i.title = "Confirm?" } - i.show_help = true + i.showHelp = true return &i } +// GetTitle returns the current title of the modal 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 { i.title = s return i } +// GetText returns the current text of the modal func (i *ConfirmModal) GetText() string { return i.text } + +// SetText sets the text of the modal to s func (i *ConfirmModal) SetText(s string) *ConfirmModal { i.text = s return i } +// GetX returns the current x coordinate of the modal 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 { i.x = x return i } + +// GetY returns the current y coordinate of the modal 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 { i.y = y return i } +// GetWidth returns the current width of the modal func (i *ConfirmModal) GetWidth() int { return i.width } + +// SetWidth sets the current modal width to width func (i *ConfirmModal) SetWidth(width int) *ConfirmModal { i.width = width return i } +// GetHeight returns the current height of the modal func (i *ConfirmModal) GetHeight() int { return i.height } + +// SetHeight set the height of the modal to height func (i *ConfirmModal) SetHeight(height int) *ConfirmModal { i.height = height 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 { - i.show_help = b + i.showHelp = b return i } +// GetBackground returns the current background color func (i *ConfirmModal) GetBackground() termbox.Attribute { return i.bg } + +// SetBackground sets the background color to bg func (i *ConfirmModal) SetBackground(bg termbox.Attribute) *ConfirmModal { i.bg = bg return i } +// GetForeground returns the current foreground color 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 { i.fg = fg 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 { - i.is_done = b + i.isDone = b return i } +// Show sets the visibility flag of the modal to true func (i *ConfirmModal) Show() *ConfirmModal { - i.is_visible = true - return i -} -func (i *ConfirmModal) Hide() *ConfirmModal { - i.is_visible = false + i.isVisible = true 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 } +// Clear clears all of the non-positional parameters of the modal func (i *ConfirmModal) Clear() *ConfirmModal { i.title = "" i.text = "" i.accepted = false - i.is_done = false + i.isDone = false return i } +// HandleKeyPress handles the termbox event and returns whether it was consumed func (i *ConfirmModal) HandleKeyPress(event termbox.Event) bool { if event.Ch == 'Y' || event.Ch == 'y' { i.accepted = true - i.is_done = true + i.isDone = true return true } else if event.Ch == 'N' || event.Ch == 'n' { i.accepted = false - i.is_done = true + i.isDone = true return true } return false } + +// Draw draws the modal func (i *ConfirmModal) Draw() { // 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) // Now draw the border 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 if i.title != "" { - DrawStringAtPoint(i.title, i.x+1, next_y, i.fg, i.bg) - next_y += 1 - FillWithChar('-', i.x+1, next_y, i.x+i.width-1, next_y, i.fg, i.bg) - next_y += 1 + DrawStringAtPoint(i.title, i.x+1, nextY, i.fg, i.bg) + nextY++ + FillWithChar('-', i.x+1, nextY, i.x+i.width-1, nextY, i.fg, i.bg) + nextY++ } if i.text != "" { - DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg) - next_y += 1 + DrawStringAtPoint(i.text, i.x+1, nextY, i.fg, i.bg) + nextY++ } - next_y += 2 - if i.show_help { - help_string := " (Y/y) Confirm. (N/n) Reject. " - help_x := (i.x + i.width) - len(help_string) - 1 - DrawStringAtPoint(help_string, help_x, next_y, i.fg, i.bg) + nextY += 2 + if i.showHelp { + helpString := " (Y/y) Confirm. (N/n) Reject. " + helpX := (i.x + i.width) - len(helpString) - 1 + DrawStringAtPoint(helpString, helpX, nextY, i.fg, i.bg) } } diff --git a/termbox_inputfield.go b/termbox_inputfield.go index 52c258d..f597ef8 100644 --- a/termbox_inputfield.go +++ b/termbox_inputfield.go @@ -1,10 +1,12 @@ -package termbox_util +package termboxUtil import ( "fmt" + "github.com/nsf/termbox-go" ) +// InputField is a field for inputting text type InputField struct { value string x, y, width, height int @@ -13,47 +15,67 @@ type InputField struct { 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 { i := InputField{x: x, y: y, width: w, height: h, fg: fg, bg: bg} return &i } +// GetValue gets the current text that is in the InputField 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 { i.value = s return i } +// GetX returns the x position of the input field func (i *InputField) GetX() int { return i.x } + +// SetX sets the x position of the input field func (i *InputField) SetX(x int) *InputField { i.x = x return i } +// GetY returns the y position of the input field func (i *InputField) GetY() int { return i.y } + +// SetY sets the y position of the input field func (i *InputField) SetY(y int) *InputField { i.y = y return i } +// GetWidth returns the current width of the input field func (i *InputField) GetWidth() int { return i.width } + +// SetWidth sets the current width of the input field func (i *InputField) SetWidth(w int) *InputField { i.width = w return i } +// GetHeight returns the current height of the input field func (i *InputField) GetHeight() int { return i.height } + +// SetHeight sets the current height of the input field func (i *InputField) SetHeight(h int) *InputField { i.height = h return i } +// IsBordered returns true or false if this input field has a border 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 { i.bordered = b return i } +// HandleKeyPress accepts the termbox event and returns whether it was consumed func (i *InputField) HandleKeyPress(event termbox.Event) bool { if event.Key == termbox.KeyEnter { // Done editing @@ -63,54 +85,67 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool { } } else if event.Key == termbox.KeyArrowLeft { if i.cursor+len(i.value) > 0 { - i.cursor -= 1 + i.cursor-- } } else if event.Key == termbox.KeyArrowRight { if i.cursor < 0 { - i.cursor += 1 + i.cursor++ } } else if event.Key == termbox.KeyCtrlU { // Ctrl+U Clears the Input i.value = "" } 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 { - 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 { - i.value = fmt.Sprintf("%s%s", i.value, string(event.Ch)) + i.value = fmt.Sprintf("%s%s", i.value, ch) } else { - str_pt_1 := i.value[:(len(i.value) + i.cursor)] - str_pt_2 := i.value[(len(i.value) + i.cursor):] - i.value = fmt.Sprintf("%s%s%s", str_pt_1, string(event.Ch), str_pt_2) + strPt1 := 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) } } return true } +// Draw outputs the input field on the screen func (i *InputField) Draw() { if i.bordered { 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 cursor_rune rune + var strPt1, strPt2 string + var cursorRune rune if len(i.value) > 0 { if i.cursor+len(i.value) == 0 { - str_pt_1 = "" - str_pt_2 = i.value[1:] - cursor_rune = rune(i.value[0]) + strPt1 = "" + strPt2 = i.value[1:] + cursorRune = rune(i.value[0]) } else if i.cursor == 0 { - str_pt_1 = i.value - str_pt_2 = "" - cursor_rune = ' ' + strPt1 = i.value + strPt2 = "" + cursorRune = ' ' } else { - str_pt_1 = i.value[:(len(i.value) + i.cursor)] - str_pt_2 = i.value[(len(i.value)+i.cursor)+1:] - cursor_rune = rune(i.value[len(i.value)+i.cursor]) + strPt1 = i.value[:(len(i.value) + i.cursor)] + strPt2 = i.value[(len(i.value)+i.cursor)+1:] + cursorRune = rune(i.value[len(i.value)+i.cursor]) } } 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) - termbox.SetCell(x, y, cursor_rune, i.bg, i.fg) - DrawStringAtPoint(str_pt_2, 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) } diff --git a/termbox_inputmodal.go b/termbox_inputmodal.go index e2b5f49..2a0ac23 100644 --- a/termbox_inputmodal.go +++ b/termbox_inputmodal.go @@ -1,152 +1,193 @@ -package termbox_util +package termboxUtil import ( "github.com/nsf/termbox-go" ) +// InputModal A modal for text input type InputModal struct { title string text string input *InputField x, y, width, height int - show_help bool + showHelp bool cursor int bg, fg termbox.Attribute - is_done bool - is_visible bool + isDone 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 { 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.show_help = true + i.showHelp = true i.input.bordered = true return &i } +// GetTitle Return the title of the modal func (i *InputModal) GetTitle() string { return i.title } + +// SetTitle Sets the title of the modal to s func (i *InputModal) SetTitle(s string) *InputModal { i.title = s return i } +// GetText Return the text of the modal func (i *InputModal) GetText() string { return i.text } + +// SetText Set the text of the modal to s func (i *InputModal) SetText(s string) *InputModal { i.text = s return i } +// GetX Return the x position of the modal 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 { i.x = x return i } + +// GetY Return the y position of the modal 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 { i.y = y return i } +// GetWidth Return the width of the modal func (i *InputModal) GetWidth() int { return i.width } + +// SetWidth Set the width of the modal to width func (i *InputModal) SetWidth(width int) *InputModal { i.width = width return i } +// GetHeight Return the height of the modal func (i *InputModal) GetHeight() int { return i.height } + +// SetHeight Set the height of the modal to height func (i *InputModal) SetHeight(height int) *InputModal { i.height = height 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 { - i.show_help = b + i.showHelp = b return i } +// GetBackground Return the current background color of the modal 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 { i.bg = bg return i } +// GetForeground Return the current foreground color func (i *InputModal) GetForeground() termbox.Attribute { return i.fg } + +// SetForeground Set the foreground color to fg func (i *InputModal) SetForeground(fg termbox.Attribute) *InputModal { i.fg = fg return i } +// Show Sets the visibility flag to true func (i *InputModal) Show() *InputModal { - i.is_visible = true + i.isVisible = true return i } + +// Hide Sets the visibility flag to false func (i *InputModal) Hide() *InputModal { - i.is_visible = false + i.isVisible = false return i } +// SetDone Sets the flag that tells whether this modal has completed it's purpose func (i *InputModal) SetDone(b bool) *InputModal { - i.is_done = b + i.isDone = b return i } + +// IsDone Returns the "isDone" flag 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() } + +// SetValue Sets the value of the input to s func (i *InputModal) SetValue(s string) *InputModal { i.input.SetValue(s) return i } +// Clear Resets all non-positional parameters of the modal func (i *InputModal) Clear() *InputModal { i.title = "" i.text = "" i.input.SetValue("") - i.is_done = false - i.is_visible = false + i.isDone = false + i.isVisible = false return i } +// HandleKeyPress Handle the termbox event, return true if it was consumed func (i *InputModal) HandleKeyPress(event termbox.Event) bool { if event.Key == termbox.KeyEnter { // Done editing - i.is_done = true + i.isDone = true return true - } else { - return i.input.HandleKeyPress(event) } + return i.input.HandleKeyPress(event) } +// Draw Draw the modal func (i *InputModal) Draw() { - if i.is_visible { + if i.isVisible { // 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) - next_y := i.y + 1 + nextY := i.y + 1 // The title if i.title != "" { if len(i.title) > i.width { 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 { - 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 - FillWithChar('-', i.x+1, next_y, i.x+i.width-1, next_y, i.fg, i.bg) - next_y += 1 + nextY++ + FillWithChar('-', i.x+1, nextY, i.x+i.width-1, nextY, i.fg, i.bg) + nextY++ } if i.text != "" { - DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg) - next_y += 1 + DrawStringAtPoint(i.text, i.x+1, nextY, i.fg, i.bg) + nextY++ } - i.input.SetY(next_y) + i.input.SetY(nextY) i.input.Draw() - next_y += 3 - if i.show_help { - help_string := " (ENTER) to Accept. (ESC) to Cancel. " - help_x := (i.x + i.width - len(help_string)) - 1 - DrawStringAtPoint(help_string, help_x, next_y, i.fg, i.bg) + nextY += 3 + if i.showHelp { + helpString := " (ENTER) to Accept. (ESC) to Cancel. " + helpX := (i.x + i.width - len(helpString)) - 1 + DrawStringAtPoint(helpString, helpX, nextY, i.fg, i.bg) } // Now draw the border DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) diff --git a/termbox_util.go b/termbox_util.go index 0041990..fd69c31 100644 --- a/termbox_util.go +++ b/termbox_util.go @@ -1,29 +1,37 @@ -package termbox_util +package termboxUtil import ( "fmt" - "github.com/nsf/termbox-go" "strings" + + "github.com/nsf/termbox-go" ) +// TextAlignment is an int value for how we're aligning text type TextAlignment int const ( - ALIGN_LEFT = iota - ALIGN_CENTER - ALIGN_RIGHT + // AlignLeft Aligns text to the left + AlignLeft = iota + // AlignCenter Aligns text to the center + AlignCenter + // AlignRight Aligns text to the right + AlignRight ) /* 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) { - x_pos := x + xPos := x for _, runeValue := range str { - termbox.SetCell(x_pos, y, runeValue, fg, bg) - x_pos++ + termbox.SetCell(xPos, y, runeValue, fg, bg) + 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) { for xx := x1; xx <= x2; xx++ { 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) { termbox.SetCell(x1, 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) } +// AlignText Aligns the text txt within width characters using the specified alignment func AlignText(txt string, width int, align TextAlignment) string { - num_spaces := width - len(txt) + numSpaces := width - len(txt) switch align { - case ALIGN_CENTER: - if num_spaces/2 > 0 { + case AlignCenter: + if numSpaces/2 > 0 { return fmt.Sprintf("%s%s%s", - strings.Repeat(" ", num_spaces/2), - txt, strings.Repeat(" ", num_spaces/2), + strings.Repeat(" ", numSpaces/2), + txt, strings.Repeat(" ", numSpaces/2), ) - } else { - return txt } - case ALIGN_RIGHT: - return fmt.Sprintf("%s%s", strings.Repeat(" ", num_spaces), txt) + return txt + case AlignRight: + return fmt.Sprintf("%s%s", strings.Repeat(" ", numSpaces), txt) 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 */