Buffers ?!

This commit is contained in:
2025-10-15 16:25:18 -05:00
parent 7a1afd67ac
commit f823a24fe8
10 changed files with 375 additions and 51 deletions

View File

@@ -42,15 +42,16 @@ type FilePicker struct {
w, h int
wantW, wantH int
path string
wrkDir *os.File
layout *RelativeLayout
path string
fileList *SimpleList
btnSelect, btnCancel *Button
error error
keyMap, customKeyMap KeyMap
logger func(string, ...any)
}
var _ Widget = (*FilePicker)(nil)
@@ -65,24 +66,34 @@ func (w *FilePicker) Init(id string, style tcell.Style) {
w.id = id
w.style = style
w.layout = NewRelativeLayout(fmt.Sprintf("%s-layout", id), style)
w.fileList = NewSimpleList(fmt.Sprintf("%s-files", id), style)
w.btnCancel = NewButton(fmt.Sprintf("%s-cancel", id), style)
w.btnCancel.SetLabel("Cancel")
w.btnSelect = NewButton(fmt.Sprintf("%s-select", id), style)
w.btnSelect.SetLabel("Select")
w.layout.Add(w.btnSelect, nil, RelAncBR)
w.btnCancel = NewButton(fmt.Sprintf("%s-cancel", id), style)
w.btnCancel.SetLabel("Cancel")
w.layout.Add(w.btnCancel, nil, RelAncBL)
w.focusable = true
w.keyMap = BlankKeyMap()
w.customKeyMap = BlankKeyMap()
w.refreshFileList()
}
func (w *FilePicker) Id() string { return w.id }
func (w *FilePicker) HandleResize(ev *tcell.EventResize) {
w.w, w.h = ev.Size()
// ww, wh := w.w-2, w.h-2 // Trim border space
w.btnSelect.SetPos(Coord{X: w.x + w.w - w.btnSelect.WantW(), Y: w.y + w.h - 1})
w.btnCancel.SetPos(Coord{X: w.x + 1, Y: w.y + w.h - 1})
wd, ht := ev.Size()
w.SetW(wd)
w.SetH(ht)
w.fileList.SetPos(Coord{X: 0, Y: 0})
w.fileList.HandleResize(Coord{X: wd, Y: ht - 3}.ResizeEvent())
w.btnCancel.HandleResize(Coord{X: wd/2 - 1, Y: 3}.ResizeEvent())
w.btnCancel.SetPos(Coord{X: 0, Y: ht - 3})
w.btnSelect.HandleResize(Coord{X: wd/2 - 1, Y: 3}.ResizeEvent())
w.btnSelect.SetPos(Coord{X: wd/2 + 1, Y: ht - 3})
w.Log("%s: HandleResize(%d, %d)", w.id, wd, ht)
}
func (w *FilePicker) SetKeyMap(km KeyMap, def bool) {
@@ -110,17 +121,21 @@ func (w *FilePicker) HandleKey(ev *tcell.EventKey) bool {
b2 := w.customKeyMap.Handle(ev)
return b1 || b2
}
func (w *FilePicker) HandleTime(ev *tcell.EventTime) { w.layout.HandleTime(ev) }
func (w *FilePicker) HandleTime(ev *tcell.EventTime) {
w.fileList.HandleTime(ev)
w.btnCancel.HandleTime(ev)
w.btnSelect.HandleTime(ev)
}
func (w *FilePicker) Draw(screen tcell.Screen) {
if !w.visible {
return
}
ds := w.style.Dim(!w.active)
wh.TitledBorderFilled(w.x, w.y, w.x+w.w, w.y+w.h, w.title, wh.BRD_SIMPLE, ds, screen)
// TODO: Draw the file picker
wh.DrawText(w.x+1, w.y+1, "TODO: Draw Filepicker", ds, screen)
w.GetPos().DrawOffset(w.btnSelect, screen)
w.GetPos().DrawOffset(w.btnCancel, screen)
p := w.GetPos()
p.DrawOffset(w.fileList, screen)
p.DrawOffset(w.btnCancel, screen)
p.DrawOffset(w.btnSelect, screen)
}
func (w *FilePicker) Active() bool { return w.active }
@@ -170,8 +185,8 @@ func (w *FilePicker) SetPath(path string) error {
} else if !fs.IsDir() {
return fmt.Errorf("path must be a directory")
}
w.wrkDir = fl
w.path = path
w.refreshFileList()
return nil
}
@@ -182,3 +197,25 @@ func (w *FilePicker) SetOnSelect(sel func() bool) {
func (w *FilePicker) SetOnCancel(cnc func() bool) {
w.btnCancel.SetOnPressed(cnc)
}
func (w *FilePicker) refreshFileList() {
w.fileList.Clear()
if w.path == "" {
w.path = "./"
}
var entries []os.DirEntry
entries, w.error = os.ReadDir(w.path)
if w.error != nil {
return
}
for i := range entries {
w.fileList.Add(entries[i].Name())
}
}
func (w *FilePicker) SetLogger(l func(string, ...any)) { w.logger = l }
func (w *FilePicker) Log(txt string, args ...any) {
if w.logger != nil {
w.logger(txt, args...)
}
}