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

Merge remote-tracking branch 'refs/remotes/origin/master'

Conflicts:
	termbox_inputmodal.go
This commit is contained in:
Brian Buller 2015-05-04 19:35:51 -05:00
commit c5cb0ca9fe
4 changed files with 157 additions and 10 deletions

133
termbox_confirmmodal.go Normal file
View File

@ -0,0 +1,133 @@
package termbox_util
import (
"github.com/nsf/termbox-go"
)
type ConfirmModal struct {
title string
text string
x, y, width, height int
show_help bool
cursor int
bg, fg termbox.Attribute
is_done bool
accepted bool
value string
}
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
return &i
}
func (i *ConfirmModal) GetTitle() string { return i.title }
func (i *ConfirmModal) SetTitle(s string) *ConfirmModal {
i.title = s
return i
}
func (i *ConfirmModal) GetText() string { return i.text }
func (i *ConfirmModal) SetText(s string) *ConfirmModal {
i.text = s
return i
}
func (i *ConfirmModal) GetX() int { return i.x }
func (i *ConfirmModal) SetX(x int) *ConfirmModal {
i.x = x
return i
}
func (i *ConfirmModal) GetY() int { return i.y }
func (i *ConfirmModal) SetY(y int) *ConfirmModal {
i.y = y
return i
}
func (i *ConfirmModal) GetWidth() int { return i.width }
func (i *ConfirmModal) SetWidth(width int) *ConfirmModal {
i.width = width
return i
}
func (i *ConfirmModal) GetHeight() int { return i.height }
func (i *ConfirmModal) SetHeight(height int) *ConfirmModal {
i.height = height
return i
}
func (i *ConfirmModal) HelpIsShown() bool { return i.show_help }
func (i *ConfirmModal) ShowHelp(b bool) *ConfirmModal {
i.show_help = b
return i
}
func (i *ConfirmModal) GetBackground() termbox.Attribute { return i.bg }
func (i *ConfirmModal) SetBackground(bg termbox.Attribute) *ConfirmModal {
i.bg = bg
return i
}
func (i *ConfirmModal) GetForeground() termbox.Attribute { return i.fg }
func (i *ConfirmModal) SetForeground(fg termbox.Attribute) *ConfirmModal {
i.fg = fg
return i
}
func (i *ConfirmModal) IsDone() bool { return i.is_done }
func (i *ConfirmModal) SetDone(b bool) *ConfirmModal {
i.is_done = b
return i
}
func (i *ConfirmModal) IsAccepted() bool { return i.accepted }
func (i *ConfirmModal) Clear() *ConfirmModal {
i.title = ""
i.text = ""
i.accepted = false
i.is_done = false
return i
}
func (i *ConfirmModal) HandleKeyPress(event termbox.Event) bool {
if event.Ch == 'Y' || event.Ch == 'y' {
i.accepted = true
i.is_done = true
return true
} else if event.Ch == 'N' || event.Ch == 'n' {
i.accepted = false
i.is_done = true
return true
}
return false
}
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
// 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
}
if i.text != "" {
DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg)
next_y += 1
}
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)
}
}

View File

@ -69,6 +69,9 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool {
if i.cursor < 0 { if i.cursor < 0 {
i.cursor += 1 i.cursor += 1
} }
} else if event.Key == termbox.KeyCtrlU {
// Ctrl+U Clears the Input
i.value = ""
} else { } else {
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", string(event.Ch), i.value)

View File

@ -13,12 +13,12 @@ type InputModal struct {
cursor int cursor int
bg, fg termbox.Attribute bg, fg termbox.Attribute
is_done bool is_done bool
value string
} }
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.input.bordered = true i.input.bordered = true
return &i return &i
} }
@ -82,13 +82,13 @@ func (i *InputModal) SetDone(b bool) *InputModal {
func (i *InputModal) IsDone() bool { func (i *InputModal) IsDone() bool {
return i.is_done return i.is_done
} }
func (i *InputModal) GetValue() string { return i.input.GetValue() }
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
} }
func (i *InputModal) GetValue() string {
return i.input.GetValue()
}
func (i *InputModal) Clear() *InputModal { func (i *InputModal) Clear() *InputModal {
i.title = "" i.title = ""
i.text = "" i.text = ""
@ -100,13 +100,13 @@ func (i *InputModal) Clear() *InputModal {
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.value = i.input.GetValue()
i.is_done = true i.is_done = true
return true return true
} else { } else {
return i.input.HandleKeyPress(event) return i.input.HandleKeyPress(event)
} }
} }
func (i *InputModal) Draw() { func (i *InputModal) 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)
@ -123,9 +123,14 @@ func (i *InputModal) Draw() {
} }
if i.text != "" { if i.text != "" {
DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg) DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg)
next_y += 1 next_y += 2
} }
i.input.SetY(next_y) i.input.SetY(next_y)
i.input.Draw() i.input.Draw()
next_y += 3 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)
}
} }

View File

@ -33,10 +33,16 @@ func FillWithChar(r rune, x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.A
} }
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) {
FillWithChar('|', x1, y1, x1, y2, fg, bg) termbox.SetCell(x1, y1, '┌', fg, bg)
FillWithChar('|', x2, y1, x2, y2, fg, bg) FillWithChar('─', x1+1, y1, x2-1, y1, fg, bg)
FillWithChar('-', x1, y1, x2, y1, fg, bg) termbox.SetCell(x2, y1, '┐', fg, bg)
FillWithChar('-', x1, y2, x2, y2, fg, bg)
FillWithChar('|', x1, y1+1, x1, y2-1, fg, bg)
FillWithChar('|', x2, y1+1, x2, y2-1, fg, bg)
termbox.SetCell(x1, y2, '└', fg, bg)
FillWithChar('─', x1+1, y2, x2-1, y2, fg, bg)
termbox.SetCell(x2, y2, '┘', fg, bg)
} }
func AlignText(txt string, width int, align TextAlignment) string { func AlignText(txt string, width int, align TextAlignment) string {