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

Input Modal/Field Works

This commit is contained in:
Brian Buller 2015-05-03 17:11:50 -05:00
parent d03ef8f013
commit 0fc0c58c6d
3 changed files with 97 additions and 62 deletions

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"github.com/nsf/termbox-go" "github.com/nsf/termbox-go"
"gogs.bullercodeworks.com/brian/termbox-util" "gogs.bullercodeworks.com/brian/termbox-util"
"os" "os"
@ -20,9 +21,12 @@ func main() {
mainLoop() mainLoop()
} }
var input *termbox_util.InputField //var input *termbox_util.InputField
var input *termbox_util.InputModal
//var input *termbox_util.InputModal var new_key, new_val string
var mode string
var drawY int
var added_stuff []string
func layoutAndDrawScreen() { func layoutAndDrawScreen() {
termbox.Clear(0, termbox.ColorBlack) termbox.Clear(0, termbox.ColorBlack)
@ -34,20 +38,57 @@ func drawScreen() {
w, h := termbox.Size() w, h := termbox.Size()
termbox_util.DrawStringAtPoint(termbox_util.AlignText("Termbox Utility Test", w, termbox_util.ALIGN_CENTER), 0, 0, termbox.ColorWhite, termbox.ColorRed) termbox_util.DrawStringAtPoint(termbox_util.AlignText("Termbox Utility Test", w, termbox_util.ALIGN_CENTER), 0, 0, termbox.ColorWhite, termbox.ColorRed)
if input == nil { if input == nil {
// mw, mh := w/4, h/4 mw, mh := w/4, h/4
// mx, my := w-(mw/2), h-(mh/2)
mw, mh := w/4, 2
mx, my := (w/2)-(mw/2), (h/2)-(mh/2) 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.CreateInputModal("", mx, my, mw, mh, termbox.ColorWhite, termbox.ColorBlack)
input = termbox_util.CreateInputField(mx, my, mw, mh, termbox.ColorWhite, termbox.ColorBlack) input.Clear()
input.SetBordered(true)
} }
if mode == "bucket" {
if input.IsDone() {
added_stuff = append(added_stuff, fmt.Sprintf("New Bucket %s", input.GetValue()))
input.Clear()
mode = ""
} else {
input.Draw() 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)
}
}
}
func handleKeyEvent(event termbox.Event) bool { func handleKeyEvent(event termbox.Event) bool {
if event.Key == termbox.KeyEsc { if event.Key == termbox.KeyEsc {
return false 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 { } else {
input.HandleKeyPress(event) input.HandleKeyPress(event)
} }
@ -55,6 +96,7 @@ func handleKeyEvent(event termbox.Event) bool {
} }
func mainLoop() { func mainLoop() {
added_stuff = append(added_stuff, "Ctrl+B = Add Bucket; Ctrl+P = Add Pair")
layoutAndDrawScreen() layoutAndDrawScreen()
for { for {
event := termbox.PollEvent() event := termbox.PollEvent()

View File

@ -75,8 +75,8 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool {
} 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, string(event.Ch))
} else { } else {
str_pt_1 := i.value str_pt_1 := i.value[:(len(i.value) + i.cursor)]
str_pt_2 := i.value 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) 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 { if i.cursor+len(i.value) == 0 {
str_pt_1 = "" str_pt_1 = ""
str_pt_2 = i.value[1:] str_pt_2 = i.value[1:]
cursor_rune = rune(i.value[1]) cursor_rune = rune(i.value[0])
} else if i.cursor == 0 { } else if i.cursor == 0 {
str_pt_1 = i.value str_pt_1 = i.value
str_pt_2 = "" str_pt_2 = ""

View File

@ -7,15 +7,19 @@ import (
type InputModal struct { type InputModal struct {
title string title string
text string text string
value string input *InputField
x, y, width, height int x, y, width, height int
show_help bool show_help bool
cursor int cursor int
bg, fg termbox.Attribute bg, fg termbox.Attribute
is_done bool
value string
} }
func CreateInputModal(text 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{text: text, 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.bordered = true
return &i return &i
} }
@ -31,12 +35,6 @@ func (i *InputModal) SetText(s string) *InputModal {
return i 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) GetX() int { return i.x }
func (i *InputModal) SetX(x int) *InputModal { func (i *InputModal) SetX(x int) *InputModal {
i.x = x i.x = x
@ -66,24 +64,6 @@ func (i *InputModal) ShowHelp(b bool) *InputModal {
return i 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) GetBackground() termbox.Attribute { return i.bg }
func (i *InputModal) SetBackground(bg termbox.Attribute) *InputModal { func (i *InputModal) SetBackground(bg termbox.Attribute) *InputModal {
i.bg = bg i.bg = bg
@ -95,18 +75,33 @@ func (i *InputModal) SetForeground(fg termbox.Attribute) *InputModal {
i.fg = fg i.fg = fg
return i 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 { func (i *InputModal) HandleKeyPress(event termbox.Event) bool {
if event.Key == termbox.KeyEnter { if event.Key == termbox.KeyEnter {
// Done editing // Done editing
} else if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 { i.value = i.input.GetValue()
i.value = i.value[:len(i.value)-1] i.is_done = true
i.cursor -= 1
} else {
i.value += string(event.Ch)
i.cursor += 1
}
return true return true
} else {
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
@ -114,21 +109,19 @@ func (i *InputModal) Draw() {
// 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)
DrawBorder(i.x+2, i.y+2, i.x+i.width-2, i.y+4, i.fg, i.bg) next_y := i.y + 1
// TODO: Output Cursor at appropriate spot // The title
var output_string_1, output_string_2 string if i.title != "" {
var cursor_rune rune DrawStringAtPoint(i.title, i.x+1, next_y, i.fg, i.bg)
if len(i.value) > 0 { next_y += 1
output_string_1 = i.value[:(len(i.value) - 1 + i.cursor)] FillWithChar('-', i.x+1, next_y, i.x+i.width-1, next_y, i.fg, i.bg)
output_string_2 = i.value[(len(i.value) - 1 + i.cursor):] next_y += 1
cursor_rune = ' '
} else {
output_string_1 = ""
output_string_2 = ""
cursor_rune = ' '
} }
if i.text != "" {
DrawStringAtPoint(output_string_1, i.x+3, i.y+3, i.fg, i.bg) DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg)
termbox.SetCell(i.x+len(output_string_1), i.y+3, cursor_rune, i.bg, i.fg) next_y += 1
DrawStringAtPoint(output_string_2, i.x+3+len(output_string_1)+1, i.y+3, i.fg, i.bg) }
i.input.SetY(next_y)
i.input.Draw()
next_y += 3
} }