241 lines
6.0 KiB
Go
241 lines
6.0 KiB
Go
/*
|
|
Copyright © Brian Buller <brian@bullercodeworks.com>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
package widgets
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
|
|
"github.com/gdamore/tcell"
|
|
)
|
|
|
|
type Field struct {
|
|
id string
|
|
style tcell.Style
|
|
|
|
label string
|
|
value string
|
|
|
|
cursor int
|
|
visible bool
|
|
active bool
|
|
focusable bool
|
|
x, y int
|
|
w, h int
|
|
|
|
filter func(*tcell.EventKey) bool
|
|
onChange func(prev, curr string)
|
|
|
|
keyMap KeyMap
|
|
}
|
|
|
|
var _ Widget = (*Field)(nil)
|
|
|
|
func NewField(id string, style tcell.Style) *Field {
|
|
f := &Field{}
|
|
f.Init(id, style)
|
|
return f
|
|
}
|
|
|
|
func (w *Field) Init(id string, style tcell.Style) {
|
|
w.id = id
|
|
w.style = style
|
|
w.visible = true
|
|
w.filter = func(ev *tcell.EventKey) bool {
|
|
return wh.IsBS(*ev) ||
|
|
wh.KeyIsDisplayable(*ev)
|
|
}
|
|
w.keyMap = NewKeyMap(map[tcell.Key]func(ev *tcell.EventKey) bool{
|
|
tcell.KeyLeft: w.handleLeft,
|
|
tcell.KeyRight: w.handleRight,
|
|
tcell.KeyHome: w.handleHome,
|
|
tcell.KeyEnd: w.handleEnd,
|
|
tcell.KeyCtrlU: w.clearValueBeforeCursor,
|
|
})
|
|
w.focusable = true
|
|
}
|
|
|
|
func (w *Field) Id() string { return w.id }
|
|
func (w *Field) HandleResize(ev *tcell.EventResize) { w.w, w.h = ev.Size() }
|
|
|
|
func (w *Field) SetKeyMap(km KeyMap) { w.keyMap = km }
|
|
func (w *Field) AddToKeyMap(km KeyMap) { w.keyMap.Merge(km) }
|
|
func (w *Field) RemoveFromKeyMap(km KeyMap) {
|
|
for k := range km.Keys {
|
|
w.keyMap.Remove(k)
|
|
}
|
|
for r := range km.Runes {
|
|
w.keyMap.RemoveRune(r)
|
|
}
|
|
}
|
|
|
|
func (w *Field) HandleKey(ev *tcell.EventKey) bool {
|
|
if !w.active {
|
|
return false
|
|
}
|
|
if wh.IsBS(*ev) {
|
|
return w.handleBackspace(ev)
|
|
}
|
|
if ok := w.keyMap.Handle(ev); ok {
|
|
return true
|
|
}
|
|
if w.filter != nil && !w.filter(ev) {
|
|
return false
|
|
}
|
|
if ev.Key() == tcell.KeyRune {
|
|
w.SetValue(fmt.Sprintf("%s%s%s", w.value[:w.cursor], string(ev.Rune()), w.value[w.cursor:]))
|
|
w.cursor++
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
func (w *Field) HandleTime(ev *tcell.EventTime) {}
|
|
func (w *Field) Draw(screen tcell.Screen) {
|
|
if !w.visible {
|
|
return
|
|
}
|
|
useStyle := w.style.Dim(!w.active)
|
|
valueStyle := useStyle.Bold(w.active)
|
|
x := w.x
|
|
labelW := len(w.label)
|
|
if labelW > 0 {
|
|
wh.DrawText(w.x, w.y, w.label+": ", useStyle, screen)
|
|
x = x + labelW + 2
|
|
}
|
|
cursor := " "
|
|
var pre, post string
|
|
if len(w.value) > 0 {
|
|
pre = w.value[:w.cursor]
|
|
if w.cursor < len(w.value) {
|
|
cursor = string(w.value[w.cursor])
|
|
post = w.value[w.cursor+1:]
|
|
}
|
|
}
|
|
|
|
wh.DrawText(x, w.y, pre, valueStyle, screen)
|
|
x += len(pre)
|
|
if w.active {
|
|
wh.DrawText(x, w.y, cursor, valueStyle.Reverse(true).Blink(true), screen)
|
|
} else {
|
|
wh.DrawText(x, w.y, cursor, valueStyle, screen)
|
|
}
|
|
x += 1
|
|
wh.DrawText(x, w.y, post, valueStyle, screen)
|
|
}
|
|
|
|
func (w *Field) Active() bool { return w.active }
|
|
func (w *Field) SetActive(a bool) { w.active = a }
|
|
func (w *Field) Visible() bool { return w.visible }
|
|
func (w *Field) SetVisible(a bool) { w.visible = a }
|
|
func (w *Field) SetX(x int) { w.x = x }
|
|
func (w *Field) SetY(y int) { w.y = y }
|
|
func (w *Field) GetX() int { return w.x }
|
|
func (w *Field) GetY() int { return w.y }
|
|
func (w *Field) GetPos() Coord { return Coord{X: w.x, Y: w.y} }
|
|
func (w *Field) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
|
|
func (w *Field) SetW(wd int) { w.w = wd }
|
|
func (w *Field) SetH(h int) { w.h = h }
|
|
func (w *Field) GetW() int { return w.w }
|
|
func (w *Field) GetH() int { return w.h }
|
|
func (w *Field) WantW() int {
|
|
vM := wh.Max(40, len(w.value))
|
|
if len(w.label) > 0 {
|
|
return len(w.label) + vM + 3
|
|
}
|
|
return vM
|
|
}
|
|
|
|
func (w *Field) WantH() int { return 1 }
|
|
func (w *Field) SetSize(c Coord) {
|
|
w.SetW(c.X)
|
|
w.SetH(c.Y)
|
|
}
|
|
func (w *Field) Focusable() bool { return w.focusable }
|
|
func (w *Field) SetFocusable(b bool) { w.focusable = b }
|
|
func (w *Field) MinW() int {
|
|
return len(w.label) + len(w.value)
|
|
}
|
|
func (w *Field) MinH() int { return 1 }
|
|
|
|
/* Non-Widget-Interface Functions */
|
|
func (w *Field) handleBackspace(_ *tcell.EventKey) bool {
|
|
st := w.cursor
|
|
if w.cursor > 0 {
|
|
w.cursor--
|
|
}
|
|
if w.cursor < len(w.value) {
|
|
w.SetValue(w.value[:w.cursor] + w.value[w.cursor+1:])
|
|
}
|
|
if st != w.cursor {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (w *Field) handleLeft(ev *tcell.EventKey) bool {
|
|
if w.cursor > 0 {
|
|
w.cursor--
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (w *Field) handleRight(ev *tcell.EventKey) bool {
|
|
if w.cursor < len(w.value) {
|
|
w.cursor++
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (w *Field) clearValueBeforeCursor(ev *tcell.EventKey) bool {
|
|
w.SetValue(w.value[w.cursor:])
|
|
w.cursor = 0
|
|
return true
|
|
}
|
|
|
|
func (w *Field) handleHome(ev *tcell.EventKey) bool {
|
|
w.cursor = 0
|
|
return true
|
|
}
|
|
|
|
func (w *Field) handleEnd(ev *tcell.EventKey) bool {
|
|
w.cursor = len(w.value)
|
|
return true
|
|
}
|
|
|
|
func (w *Field) SetLabel(l string) { w.label = l }
|
|
func (w *Field) Label() string { return w.label }
|
|
func (w *Field) SetValue(v string) {
|
|
prev := w.value
|
|
w.value = v
|
|
w.doOnChange(prev, v)
|
|
}
|
|
func (w *Field) Value() string { return w.value }
|
|
|
|
func (w *Field) SetOnChange(v func(prev, curr string)) { w.onChange = v }
|
|
func (w *Field) doOnChange(prev, curr string) {
|
|
if w.onChange != nil {
|
|
w.onChange(prev, curr)
|
|
}
|
|
}
|