mirror of
https://github.com/br0xen/termbox-util.git
synced 2024-11-26 07:03:14 +00:00
Merge remote-tracking branch 'refs/remotes/origin/master'
Conflicts: termbox_inputmodal.go
This commit is contained in:
commit
c5cb0ca9fe
133
termbox_confirmmodal.go
Normal file
133
termbox_confirmmodal.go
Normal 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)
|
||||
}
|
||||
}
|
@ -69,6 +69,9 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool {
|
||||
if i.cursor < 0 {
|
||||
i.cursor += 1
|
||||
}
|
||||
} else if event.Key == termbox.KeyCtrlU {
|
||||
// Ctrl+U Clears the Input
|
||||
i.value = ""
|
||||
} else {
|
||||
if i.cursor+len(i.value) == 0 {
|
||||
i.value = fmt.Sprintf("%s%s", string(event.Ch), i.value)
|
||||
|
@ -13,12 +13,12 @@ type InputModal struct {
|
||||
cursor int
|
||||
bg, fg termbox.Attribute
|
||||
is_done bool
|
||||
value string
|
||||
}
|
||||
|
||||
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.input.bordered = true
|
||||
return &i
|
||||
}
|
||||
@ -82,13 +82,13 @@ func (i *InputModal) SetDone(b bool) *InputModal {
|
||||
func (i *InputModal) IsDone() bool {
|
||||
return i.is_done
|
||||
}
|
||||
|
||||
func (i *InputModal) GetValue() string { return i.input.GetValue() }
|
||||
func (i *InputModal) SetValue(s string) *InputModal {
|
||||
i.input.SetValue(s)
|
||||
return i
|
||||
}
|
||||
func (i *InputModal) GetValue() string {
|
||||
return i.input.GetValue()
|
||||
}
|
||||
|
||||
func (i *InputModal) Clear() *InputModal {
|
||||
i.title = ""
|
||||
i.text = ""
|
||||
@ -100,13 +100,13 @@ func (i *InputModal) Clear() *InputModal {
|
||||
func (i *InputModal) HandleKeyPress(event termbox.Event) bool {
|
||||
if event.Key == termbox.KeyEnter {
|
||||
// Done editing
|
||||
i.value = i.input.GetValue()
|
||||
i.is_done = true
|
||||
return true
|
||||
} else {
|
||||
return i.input.HandleKeyPress(event)
|
||||
}
|
||||
}
|
||||
|
||||
func (i *InputModal) 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)
|
||||
@ -123,9 +123,14 @@ func (i *InputModal) Draw() {
|
||||
}
|
||||
if i.text != "" {
|
||||
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.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)
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
FillWithChar('|', x1, y1, x1, y2, fg, bg)
|
||||
FillWithChar('|', x2, y1, x2, y2, fg, bg)
|
||||
FillWithChar('-', x1, y1, x2, y1, fg, bg)
|
||||
FillWithChar('-', x1, y2, x2, y2, fg, bg)
|
||||
termbox.SetCell(x1, y1, '┌', fg, bg)
|
||||
FillWithChar('─', x1+1, y1, x2-1, y1, fg, bg)
|
||||
termbox.SetCell(x2, y1, '┐', 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 {
|
||||
|
Loading…
Reference in New Issue
Block a user