Some Updates
This commit is contained in:
255
wdgt_simple_list.go
Normal file
255
wdgt_simple_list.go
Normal file
@@ -0,0 +1,255 @@
|
||||
/*
|
||||
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 (
|
||||
wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
|
||||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
||||
type SimpleList struct {
|
||||
id string
|
||||
title string
|
||||
style tcell.Style
|
||||
active bool
|
||||
visible bool
|
||||
focusable bool
|
||||
|
||||
x, y int
|
||||
w, h int
|
||||
|
||||
border []rune
|
||||
cursor int
|
||||
cursorWrap bool
|
||||
list []string
|
||||
itemsStyle map[int]tcell.Style
|
||||
|
||||
onSelect func(int, string) bool
|
||||
keyMap KeyMap
|
||||
vimMode bool
|
||||
|
||||
logger func(string, ...any)
|
||||
}
|
||||
|
||||
var _ Widget = (*SimpleList)(nil)
|
||||
|
||||
func NewSimpleList(id string, style tcell.Style) *SimpleList {
|
||||
ret := &SimpleList{style: style}
|
||||
ret.Init(id, style)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (w *SimpleList) Init(id string, style tcell.Style) {
|
||||
w.id = id
|
||||
w.style = style
|
||||
w.focusable = true
|
||||
w.keyMap = NewKeyMap(map[tcell.Key]func(ev *tcell.EventKey) bool{
|
||||
tcell.KeyUp: func(_ *tcell.EventKey) bool { return w.MoveUp() },
|
||||
tcell.KeyDown: func(_ *tcell.EventKey) bool { return w.MoveDown() },
|
||||
tcell.KeyEnter: func(ev *tcell.EventKey) bool {
|
||||
if w.onSelect != nil && w.cursor < len(w.list) {
|
||||
return w.onSelect(w.cursor, w.list[w.cursor])
|
||||
}
|
||||
return false
|
||||
},
|
||||
})
|
||||
w.keyMap.AddRune('j', func(ev *tcell.EventKey) bool {
|
||||
if w.vimMode {
|
||||
return w.MoveDown()
|
||||
}
|
||||
return false
|
||||
})
|
||||
w.keyMap.AddRune('k', func(ev *tcell.EventKey) bool {
|
||||
if w.vimMode {
|
||||
return w.MoveUp()
|
||||
}
|
||||
return false
|
||||
})
|
||||
w.itemsStyle = make(map[int]tcell.Style)
|
||||
w.focusable = true
|
||||
}
|
||||
func (w *SimpleList) Id() string { return w.id }
|
||||
func (w *SimpleList) HandleResize(ev *tcell.EventResize) {
|
||||
w.w, w.h = ev.Size()
|
||||
}
|
||||
|
||||
func (w *SimpleList) SetKeyMap(km KeyMap) { w.keyMap = km }
|
||||
func (w *SimpleList) AddToKeyMap(km KeyMap) { w.keyMap.Merge(km) }
|
||||
func (w *SimpleList) RemoveFromKeyMap(km KeyMap) {
|
||||
for k := range km.Keys {
|
||||
w.keyMap.Remove(k)
|
||||
}
|
||||
for r := range km.Runes {
|
||||
w.keyMap.RemoveRune(r)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *SimpleList) HandleKey(ev *tcell.EventKey) bool {
|
||||
if !w.active || !w.focusable {
|
||||
return false
|
||||
}
|
||||
return w.keyMap.Handle(ev)
|
||||
}
|
||||
func (w *SimpleList) HandleTime(ev *tcell.EventTime) {}
|
||||
func (w *SimpleList) Draw(screen tcell.Screen) {
|
||||
dS := w.style
|
||||
if !w.active {
|
||||
dS = dS.Dim(true)
|
||||
}
|
||||
x, y := w.x, w.y
|
||||
brdSz := 0
|
||||
if len(w.border) > 0 {
|
||||
brdSz = 2
|
||||
if len(w.title) > 0 {
|
||||
wh.TitledBorderFilled(x, y, x+w.w, y+w.h, w.title, w.border, dS, screen)
|
||||
} else {
|
||||
wh.BorderFilled(x, y, x+w.w, y+w.h, w.border, dS, screen)
|
||||
}
|
||||
}
|
||||
x, y = x+1, y+1
|
||||
for i := range w.list {
|
||||
rev := false
|
||||
if i == w.cursor {
|
||||
rev = true
|
||||
}
|
||||
txt := w.list[i]
|
||||
if len(txt) > w.w-brdSz && w.w-brdSz >= 0 {
|
||||
txt = txt[:(w.w - brdSz)]
|
||||
}
|
||||
var ok bool
|
||||
var s tcell.Style
|
||||
if s, ok = w.itemsStyle[i]; !ok {
|
||||
s = dS
|
||||
}
|
||||
wh.DrawText(x, y, txt, s.Reverse(rev), screen)
|
||||
y += 1
|
||||
}
|
||||
}
|
||||
|
||||
func (w *SimpleList) Active() bool { return w.active }
|
||||
func (w *SimpleList) SetActive(a bool) { w.active = a }
|
||||
func (w *SimpleList) Visible() bool { return w.visible }
|
||||
func (w *SimpleList) SetVisible(a bool) { w.visible = a }
|
||||
func (w *SimpleList) SetX(x int) { w.x = x }
|
||||
func (w *SimpleList) SetY(y int) { w.y = y }
|
||||
func (w *SimpleList) GetX() int { return w.x }
|
||||
func (w *SimpleList) GetY() int { return w.y }
|
||||
func (w *SimpleList) GetPos() Coord { return Coord{X: w.x, Y: w.y} }
|
||||
func (w *SimpleList) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
|
||||
func (w *SimpleList) SetW(x int) { w.w = x }
|
||||
func (w *SimpleList) SetH(y int) { w.h = y }
|
||||
func (w *SimpleList) GetW() int { return w.w }
|
||||
func (w *SimpleList) GetH() int { return w.y }
|
||||
func (w *SimpleList) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
|
||||
func (w *SimpleList) Focusable() bool { return w.focusable }
|
||||
func (w *SimpleList) SetFocusable(b bool) { w.focusable = b }
|
||||
func (w *SimpleList) WantW() int {
|
||||
lng := wh.Longest(w.list)
|
||||
if len(w.border) > 0 {
|
||||
return lng + 2
|
||||
}
|
||||
return lng
|
||||
}
|
||||
|
||||
func (w *SimpleList) WantH() int {
|
||||
lng := len(w.list)
|
||||
if len(w.border) > 0 {
|
||||
return lng + 2
|
||||
}
|
||||
return lng
|
||||
}
|
||||
|
||||
func (w *SimpleList) MinW() int {
|
||||
lng := wh.Longest(w.list)
|
||||
if lng > 80 {
|
||||
lng = 80
|
||||
}
|
||||
return 2 + lng
|
||||
}
|
||||
|
||||
func (w *SimpleList) MinH() int { return 4 }
|
||||
|
||||
func (w *SimpleList) SetCursorWrap(b bool) { w.cursorWrap = b }
|
||||
func (w *SimpleList) MoveUp() bool {
|
||||
if w.cursor > 0 {
|
||||
w.cursor--
|
||||
return true
|
||||
} else if w.cursorWrap {
|
||||
w.cursor = len(w.list) - 1
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (w *SimpleList) MoveDown() bool {
|
||||
if w.cursor < len(w.list)-1 {
|
||||
w.cursor++
|
||||
return true
|
||||
} else if w.cursorWrap {
|
||||
w.cursor = 0
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
func (w *SimpleList) SetTitle(ttl string) { w.title = ttl }
|
||||
func (w *SimpleList) SetList(l []string) { w.list = l }
|
||||
func (w *SimpleList) Clear() { w.list = []string{} }
|
||||
func (w *SimpleList) Add(l string) { w.list = append(w.list, l) }
|
||||
func (w *SimpleList) Remove(l string) {
|
||||
var idx int
|
||||
var found bool
|
||||
for idx = range w.list {
|
||||
if w.list[idx] == l {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if found {
|
||||
w.list = append(w.list[:idx], w.list[idx+1:]...)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *SimpleList) SetBorder(brd []rune) {
|
||||
if len(brd) == 0 {
|
||||
w.border = wh.BRD_SIMPLE
|
||||
} else {
|
||||
w.border = wh.ValidateBorder(brd)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *SimpleList) SetItemStyle(idx int, s tcell.Style) { w.itemsStyle[idx] = s }
|
||||
func (w *SimpleList) SetItem(idx int, txt string) {
|
||||
if len(w.list) < idx {
|
||||
w.list[idx] = txt
|
||||
}
|
||||
}
|
||||
func (w *SimpleList) SelectedIndex() int { return w.cursor }
|
||||
func (w *SimpleList) ClearBorder() { w.border = []rune{} }
|
||||
func (w *SimpleList) SetOnSelect(s func(int, string) bool) { w.onSelect = s }
|
||||
func (w *SimpleList) SetVimMode(b bool) { w.vimMode = b }
|
||||
|
||||
func (w *SimpleList) SetLogger(l func(string, ...any)) { w.logger = l }
|
||||
func (w *SimpleList) Log(txt string, args ...any) {
|
||||
if w.logger != nil {
|
||||
w.logger(txt, args...)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user