mirror of
https://github.com/br0xen/termbox-util.git
synced 2024-11-22 05:33:13 +00:00
Input Modal/Field Works
This commit is contained in:
parent
d03ef8f013
commit
0fc0c58c6d
@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/nsf/termbox-go"
|
||||
"gogs.bullercodeworks.com/brian/termbox-util"
|
||||
"os"
|
||||
@ -20,9 +21,12 @@ func main() {
|
||||
mainLoop()
|
||||
}
|
||||
|
||||
var input *termbox_util.InputField
|
||||
|
||||
//var input *termbox_util.InputModal
|
||||
//var input *termbox_util.InputField
|
||||
var input *termbox_util.InputModal
|
||||
var new_key, new_val string
|
||||
var mode string
|
||||
var drawY int
|
||||
var added_stuff []string
|
||||
|
||||
func layoutAndDrawScreen() {
|
||||
termbox.Clear(0, termbox.ColorBlack)
|
||||
@ -34,20 +38,57 @@ func drawScreen() {
|
||||
w, h := termbox.Size()
|
||||
termbox_util.DrawStringAtPoint(termbox_util.AlignText("Termbox Utility Test", w, termbox_util.ALIGN_CENTER), 0, 0, termbox.ColorWhite, termbox.ColorRed)
|
||||
if input == nil {
|
||||
// mw, mh := w/4, h/4
|
||||
// mx, my := w-(mw/2), h-(mh/2)
|
||||
mw, mh := w/4, 2
|
||||
mw, mh := w/4, h/4
|
||||
mx, my := (w/2)-(mw/2), (h/2)-(mh/2)
|
||||
// input = termbox_util.CreateInputModal("Test Input", mx, my, mw, mh, termbox.ColorWhite, termbox.ColorBlack)
|
||||
input = termbox_util.CreateInputField(mx, my, mw, mh, termbox.ColorWhite, termbox.ColorBlack)
|
||||
input.SetBordered(true)
|
||||
input = termbox_util.CreateInputModal("", mx, my, mw, mh, termbox.ColorWhite, termbox.ColorBlack)
|
||||
input.Clear()
|
||||
}
|
||||
if mode == "bucket" {
|
||||
if input.IsDone() {
|
||||
added_stuff = append(added_stuff, fmt.Sprintf("New Bucket %s", input.GetValue()))
|
||||
input.Clear()
|
||||
mode = ""
|
||||
} else {
|
||||
input.Draw()
|
||||
}
|
||||
} else if mode == "pair" {
|
||||
if input.IsDone() {
|
||||
if new_key == "" {
|
||||
new_key = input.GetValue()
|
||||
input.Clear()
|
||||
input.SetTitle("Pair Value")
|
||||
} else {
|
||||
added_stuff = append(added_stuff, fmt.Sprintf("New Pair %s => %s", new_key, input.GetValue()))
|
||||
mode = ""
|
||||
input.Clear()
|
||||
}
|
||||
}
|
||||
if mode == "pair" && !input.IsDone() {
|
||||
input.Draw()
|
||||
}
|
||||
}
|
||||
if mode == "" {
|
||||
for i := range added_stuff {
|
||||
termbox_util.DrawStringAtPoint(added_stuff[i], 1, 3+i, termbox.ColorWhite, termbox.ColorRed)
|
||||
}
|
||||
}
|
||||
input.Draw()
|
||||
}
|
||||
|
||||
func handleKeyEvent(event termbox.Event) bool {
|
||||
if event.Key == termbox.KeyEsc {
|
||||
return false
|
||||
} else if event.Key == termbox.KeyCtrlB {
|
||||
mode = "bucket"
|
||||
new_key = ""
|
||||
new_val = ""
|
||||
input.Clear()
|
||||
input.SetTitle("Bucket Name")
|
||||
} else if event.Key == termbox.KeyCtrlP {
|
||||
mode = "pair"
|
||||
new_key = ""
|
||||
new_val = ""
|
||||
input.Clear()
|
||||
input.SetTitle("Pair Key")
|
||||
} else {
|
||||
input.HandleKeyPress(event)
|
||||
}
|
||||
@ -55,6 +96,7 @@ func handleKeyEvent(event termbox.Event) bool {
|
||||
}
|
||||
|
||||
func mainLoop() {
|
||||
added_stuff = append(added_stuff, "Ctrl+B = Add Bucket; Ctrl+P = Add Pair")
|
||||
layoutAndDrawScreen()
|
||||
for {
|
||||
event := termbox.PollEvent()
|
||||
|
@ -75,8 +75,8 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool {
|
||||
} else if i.cursor == 0 {
|
||||
i.value = fmt.Sprintf("%s%s", i.value, string(event.Ch))
|
||||
} else {
|
||||
str_pt_1 := i.value
|
||||
str_pt_2 := i.value
|
||||
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)
|
||||
}
|
||||
}
|
||||
@ -94,7 +94,7 @@ func (i *InputField) Draw() {
|
||||
if i.cursor+len(i.value) == 0 {
|
||||
str_pt_1 = ""
|
||||
str_pt_2 = i.value[1:]
|
||||
cursor_rune = rune(i.value[1])
|
||||
cursor_rune = rune(i.value[0])
|
||||
} else if i.cursor == 0 {
|
||||
str_pt_1 = i.value
|
||||
str_pt_2 = ""
|
||||
|
@ -7,15 +7,19 @@ import (
|
||||
type InputModal struct {
|
||||
title string
|
||||
text string
|
||||
value string
|
||||
input *InputField
|
||||
x, y, width, height int
|
||||
show_help bool
|
||||
cursor int
|
||||
bg, fg termbox.Attribute
|
||||
is_done bool
|
||||
value string
|
||||
}
|
||||
|
||||
func CreateInputModal(text string, x, y, width, height int, fg, bg termbox.Attribute) *InputModal {
|
||||
i := InputModal{text: text, x: x, y: y, width: width, height: height, fg: fg, bg: bg}
|
||||
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.input.bordered = true
|
||||
return &i
|
||||
}
|
||||
|
||||
@ -31,12 +35,6 @@ func (i *InputModal) SetText(s string) *InputModal {
|
||||
return i
|
||||
}
|
||||
|
||||
func (i *InputModal) GetValue() string { return i.value }
|
||||
func (i *InputModal) SetValue(s string) *InputModal {
|
||||
i.value = s
|
||||
return i
|
||||
}
|
||||
|
||||
func (i *InputModal) GetX() int { return i.x }
|
||||
func (i *InputModal) SetX(x int) *InputModal {
|
||||
i.x = x
|
||||
@ -66,24 +64,6 @@ func (i *InputModal) ShowHelp(b bool) *InputModal {
|
||||
return i
|
||||
}
|
||||
|
||||
func (i *InputModal) GetCursorPos() int { return i.cursor }
|
||||
func (i *InputModal) SetCursorPos(c int) *InputModal {
|
||||
i.cursor = c
|
||||
return i
|
||||
}
|
||||
func (i *InputModal) MoveCursorLeft() *InputModal {
|
||||
if len(i.value)+(i.GetCursorPos()) > 0 {
|
||||
i.cursor = i.GetCursorPos() - 1
|
||||
}
|
||||
return i
|
||||
}
|
||||
func (i *InputModal) MoveCursorRight() *InputModal {
|
||||
if i.GetCursorPos() < 0 {
|
||||
i.cursor = i.GetCursorPos() + 1
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
func (i *InputModal) GetBackground() termbox.Attribute { return i.bg }
|
||||
func (i *InputModal) SetBackground(bg termbox.Attribute) *InputModal {
|
||||
i.bg = bg
|
||||
@ -95,18 +75,33 @@ func (i *InputModal) SetForeground(fg termbox.Attribute) *InputModal {
|
||||
i.fg = fg
|
||||
return i
|
||||
}
|
||||
func (i *InputModal) SetDone(b bool) *InputModal {
|
||||
i.is_done = b
|
||||
return i
|
||||
}
|
||||
func (i *InputModal) IsDone() bool {
|
||||
return i.is_done
|
||||
}
|
||||
func (i *InputModal) GetValue() string {
|
||||
return i.value
|
||||
}
|
||||
func (i *InputModal) Clear() *InputModal {
|
||||
i.title = ""
|
||||
i.text = ""
|
||||
i.input.SetValue("")
|
||||
i.is_done = false
|
||||
return i
|
||||
}
|
||||
|
||||
func (i *InputModal) HandleKeyPress(event termbox.Event) bool {
|
||||
if event.Key == termbox.KeyEnter {
|
||||
// Done editing
|
||||
} else if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 {
|
||||
i.value = i.value[:len(i.value)-1]
|
||||
i.cursor -= 1
|
||||
i.value = i.input.GetValue()
|
||||
i.is_done = true
|
||||
return true
|
||||
} else {
|
||||
i.value += string(event.Ch)
|
||||
i.cursor += 1
|
||||
return i.input.HandleKeyPress(event)
|
||||
}
|
||||
return true
|
||||
}
|
||||
func (i *InputModal) Draw() {
|
||||
// First blank out the area we'll be putting the modal
|
||||
@ -114,21 +109,19 @@ func (i *InputModal) Draw() {
|
||||
// Now draw the border
|
||||
DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg)
|
||||
|
||||
DrawBorder(i.x+2, i.y+2, i.x+i.width-2, i.y+4, i.fg, i.bg)
|
||||
// TODO: Output Cursor at appropriate spot
|
||||
var output_string_1, output_string_2 string
|
||||
var cursor_rune rune
|
||||
if len(i.value) > 0 {
|
||||
output_string_1 = i.value[:(len(i.value) - 1 + i.cursor)]
|
||||
output_string_2 = i.value[(len(i.value) - 1 + i.cursor):]
|
||||
cursor_rune = ' '
|
||||
} else {
|
||||
output_string_1 = ""
|
||||
output_string_2 = ""
|
||||
cursor_rune = ' '
|
||||
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
|
||||
}
|
||||
|
||||
DrawStringAtPoint(output_string_1, i.x+3, i.y+3, i.fg, i.bg)
|
||||
termbox.SetCell(i.x+len(output_string_1), i.y+3, cursor_rune, i.bg, i.fg)
|
||||
DrawStringAtPoint(output_string_2, i.x+3+len(output_string_1)+1, i.y+3, i.fg, i.bg)
|
||||
if i.text != "" {
|
||||
DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg)
|
||||
next_y += 1
|
||||
}
|
||||
i.input.SetY(next_y)
|
||||
i.input.Draw()
|
||||
next_y += 3
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user