Buffers ?!
This commit is contained in:
@@ -72,16 +72,36 @@ func (w *SimpleList) Init(id string, style tcell.Style) {
|
||||
}
|
||||
return false
|
||||
},
|
||||
tcell.KeyPgDn: func(_ *tcell.EventKey) bool { return w.PageDn() },
|
||||
tcell.KeyPgUp: func(_ *tcell.EventKey) bool { return w.PageUp() },
|
||||
})
|
||||
w.keyMap.AddRune('j', func(ev *tcell.EventKey) bool {
|
||||
if w.vimMode {
|
||||
return w.MoveDown()
|
||||
if !w.vimMode {
|
||||
return false
|
||||
}
|
||||
return w.MoveDown()
|
||||
})
|
||||
w.keyMap.AddRune('k', func(ev *tcell.EventKey) bool {
|
||||
if !w.vimMode {
|
||||
return false
|
||||
}
|
||||
return w.MoveUp()
|
||||
})
|
||||
w.keyMap.AddRune('b', func(ev *tcell.EventKey) bool {
|
||||
if !w.vimMode {
|
||||
return false
|
||||
}
|
||||
if ev.Modifiers()&tcell.ModCtrl != 0 {
|
||||
return w.PageUp()
|
||||
}
|
||||
return false
|
||||
})
|
||||
w.keyMap.AddRune('k', func(ev *tcell.EventKey) bool {
|
||||
if w.vimMode {
|
||||
return w.MoveUp()
|
||||
w.keyMap.AddRune('f', func(ev *tcell.EventKey) bool {
|
||||
if !w.vimMode {
|
||||
return false
|
||||
}
|
||||
if ev.Modifiers()&tcell.ModCtrl != 0 {
|
||||
return w.PageDn()
|
||||
}
|
||||
return false
|
||||
})
|
||||
@@ -89,6 +109,7 @@ func (w *SimpleList) Init(id string, style tcell.Style) {
|
||||
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() }
|
||||
|
||||
@@ -134,7 +155,33 @@ func (w *SimpleList) Draw(screen tcell.Screen) {
|
||||
}
|
||||
}
|
||||
x, y = x+1, y+1
|
||||
for i := range w.list {
|
||||
h := w.h - brdSz
|
||||
ln := len(w.list)
|
||||
st, ed := 0, ln-1
|
||||
if ln == 0 {
|
||||
return
|
||||
}
|
||||
if ln > w.h-2 {
|
||||
mid := h / 2
|
||||
if w.cursor < mid {
|
||||
// List needs to begin at 0
|
||||
ed = h + 1
|
||||
} else if w.cursor > ln-mid {
|
||||
// List needs to begin at ln-h
|
||||
st = ln - h + 1
|
||||
} else {
|
||||
st = w.cursor - mid
|
||||
ed = st + h + 1
|
||||
}
|
||||
}
|
||||
// ed cannot be higher than ln-1
|
||||
if st < 0 {
|
||||
st = 0
|
||||
}
|
||||
if ed > ln-1 {
|
||||
ed = ln - 1
|
||||
}
|
||||
for i := st; i <= ed; i++ {
|
||||
rev := false
|
||||
if i == w.cursor {
|
||||
rev = true
|
||||
@@ -215,7 +262,7 @@ func (w *SimpleList) MoveUp() bool {
|
||||
}
|
||||
|
||||
func (w *SimpleList) MoveDown() bool {
|
||||
if w.cursor < len(w.list)-1 {
|
||||
if w.cursor <= len(w.list)-2 {
|
||||
w.cursor++
|
||||
if w.onChange != nil {
|
||||
w.onChange(w.cursor, w.list[w.cursor])
|
||||
@@ -230,6 +277,28 @@ func (w *SimpleList) MoveDown() bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (w *SimpleList) PageUp() bool {
|
||||
w.cursor -= w.h
|
||||
if len(w.border) > 0 {
|
||||
w.cursor += 2
|
||||
}
|
||||
if w.cursor < 0 {
|
||||
w.cursor = 0
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (w *SimpleList) PageDn() bool {
|
||||
w.cursor += w.h
|
||||
if len(w.border) > 0 {
|
||||
w.cursor -= 2
|
||||
}
|
||||
if w.cursor > len(w.list)-2 {
|
||||
w.cursor = len(w.list) - 2
|
||||
}
|
||||
return true
|
||||
}
|
||||
func (w *SimpleList) SetTitle(ttl string) { w.title = ttl }
|
||||
func (w *SimpleList) SetList(l []string) { w.list = l }
|
||||
func (w *SimpleList) Clear() {
|
||||
@@ -238,7 +307,11 @@ func (w *SimpleList) Clear() {
|
||||
delete(w.itemsStyle, k)
|
||||
}
|
||||
}
|
||||
func (w *SimpleList) Add(l string) { w.list = append(w.list, l) }
|
||||
|
||||
func (w *SimpleList) Add(l string) {
|
||||
w.list = append(w.list, l)
|
||||
}
|
||||
|
||||
func (w *SimpleList) Remove(l string) {
|
||||
var idx int
|
||||
var found bool
|
||||
@@ -289,4 +362,6 @@ func (w *SimpleList) Log(txt string, args ...any) {
|
||||
|
||||
func (w *SimpleList) SetOnChange(c func(int, string) bool) { w.onChange = c }
|
||||
|
||||
func (w *SimpleList) GetSelectedItem() string { return w.list[w.cursor] }
|
||||
func (w *SimpleList) GetAllItems() []string { return w.list }
|
||||
func (w *SimpleList) GetAllItemStyles() map[int]tcell.Style { return w.itemsStyle }
|
||||
|
||||
Reference in New Issue
Block a user