Work
This commit is contained in:
191
list.go
Normal file
191
list.go
Normal file
@@ -0,0 +1,191 @@
|
||||
/*
|
||||
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 (
|
||||
h "git.bullercodeworks.com/brian/tcell-widgets/helpers"
|
||||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
||||
type List struct {
|
||||
id string
|
||||
title string
|
||||
style tcell.Style
|
||||
active bool
|
||||
visible bool
|
||||
focusable bool
|
||||
|
||||
x, y int
|
||||
w, h int
|
||||
wantW, wantH int
|
||||
|
||||
border []rune
|
||||
cursor int
|
||||
cursorWrap bool
|
||||
list []string
|
||||
|
||||
onSelect func(string) bool
|
||||
keyMap KeyMap
|
||||
}
|
||||
|
||||
var _ Widget = (*List)(nil)
|
||||
|
||||
func NewList(id string, style tcell.Style) *List {
|
||||
ret := &List{style: style}
|
||||
ret.Init(id, style)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (w *List) Init(id string, style tcell.Style) {
|
||||
w.id = id
|
||||
w.style = style
|
||||
w.keyMap = NewKeyMap(map[tcell.Key]func() bool{
|
||||
tcell.KeyUp: w.MoveUp,
|
||||
tcell.KeyDown: w.MoveDown,
|
||||
tcell.KeyEnter: func() bool {
|
||||
if w.onSelect != nil && w.cursor < len(w.list) {
|
||||
return w.onSelect(w.list[w.cursor])
|
||||
}
|
||||
return false
|
||||
},
|
||||
})
|
||||
}
|
||||
func (w *List) Id() string { return w.id }
|
||||
func (w *List) HandleResize(ev *tcell.EventResize) {}
|
||||
|
||||
func (w *List) HandleKey(ev *tcell.EventKey) bool {
|
||||
if !w.active || !w.focusable {
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
func (w *List) HandleTime(ev *tcell.EventTime) {}
|
||||
func (w *List) 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 {
|
||||
h.TitledBorderFilled(x, y, x+w.w, y+w.h, w.title, w.border, dS, screen)
|
||||
} else {
|
||||
h.BorderFilled(x, y, x+w.w, y+w.h, w.border, dS, screen)
|
||||
}
|
||||
}
|
||||
x, y = x+1, y+1
|
||||
for i := range w.list {
|
||||
if i == w.cursor {
|
||||
dS = dS.Reverse(true)
|
||||
}
|
||||
txt := w.list[i]
|
||||
if len(txt) > w.w-brdSz {
|
||||
txt = txt[:(w.w - brdSz)]
|
||||
h.DrawText(x, y, txt, dS, screen)
|
||||
}
|
||||
}
|
||||
}
|
||||
func (w *List) Active() bool { return w.active }
|
||||
func (w *List) SetActive(a bool) { w.active = a }
|
||||
func (w *List) Visible() bool { return w.visible }
|
||||
func (w *List) SetVisible(a bool) { w.visible = a }
|
||||
func (w *List) SetX(x int) { w.x = x }
|
||||
func (w *List) SetY(y int) { w.y = y }
|
||||
func (w *List) GetX() int { return w.x }
|
||||
func (w *List) GetY() int { return w.y }
|
||||
func (w *List) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
|
||||
func (w *List) SetW(x int) { w.w = x }
|
||||
func (w *List) SetH(y int) { w.h = y }
|
||||
func (w *List) GetW() int { return w.w }
|
||||
func (w *List) GetH() int { return w.y }
|
||||
func (w *List) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
|
||||
func (w *List) Focusable() bool { return w.focusable }
|
||||
func (w *List) WantW() int {
|
||||
lng := h.Longest(w.list)
|
||||
if len(w.border) > 0 {
|
||||
return lng + 2
|
||||
}
|
||||
return lng
|
||||
}
|
||||
|
||||
func (w *List) WantH() int {
|
||||
lng := len(w.list)
|
||||
if len(w.border) > 0 {
|
||||
return lng + 2
|
||||
}
|
||||
return lng
|
||||
}
|
||||
|
||||
func (w *List) SetFocusable(f bool) { w.focusable = f }
|
||||
|
||||
func (w *List) SetCursorWrap(b bool) { w.cursorWrap = b }
|
||||
|
||||
func (w *List) 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 *List) MoveDown() bool {
|
||||
if w.cursor < len(w.list) {
|
||||
w.cursor++
|
||||
return true
|
||||
} else if w.cursorWrap {
|
||||
w.cursor = 0
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (w *List) SetTitle(ttl string) { w.title = ttl }
|
||||
func (w *List) SetList(l []string) { w.list = l }
|
||||
func (w *List) Add(l string) { w.list = append(w.list, l) }
|
||||
func (w *List) 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 *List) SetBorder(brd []rune) {
|
||||
if len(brd) == 0 {
|
||||
w.border = h.BRD_SIMPLE
|
||||
} else {
|
||||
w.border = h.ValidateBorder(brd)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *List) ClearBorder() { w.border = []rune{} }
|
||||
Reference in New Issue
Block a user