subsonic-tui/internal/tui/views/status_line.go
Sagi Dayan 48661005be
initial commit
Signed-off-by: Sagi Dayan <sagidayan@gmail.com>
2024-09-01 16:30:56 +03:00

98 lines
2 KiB
Go

package views
import (
"fmt"
"git.dayanhub.com/sagi/subsonic-tui/internal/config"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
var _ View = &statusLine{}
type statusLine struct {
view *tview.Flex
mode Statusmode
onUpdateFunc func()
onSearchFunc func(quary string)
}
type Statusmode int
const (
StatusModeLog Statusmode = iota
StatusModeSearch Statusmode = iota
)
func NewStatusLine() *statusLine {
status := tview.NewFlex()
status.SetBackgroundColor(config.ColorBackground)
// Default empty box
status.AddItem(EmptyBox, 0, 1, false)
return &statusLine{
view: status,
mode: StatusModeLog,
}
}
func (s *statusLine) SetSearchFunc(f func(quary string)) {
s.onSearchFunc = f
}
func (s *statusLine) Mode() Statusmode {
return s.mode
}
func (s *statusLine) Search() {
s.mode = StatusModeSearch
s.view.Clear()
label := "Search: "
_, _, w, _ := s.view.GetRect()
query := ""
inputField := tview.NewInputField()
inputField.
SetLabel(label).
SetFieldWidth(w - len(label)).
SetDoneFunc(func(key tcell.Key) {
if key == tcell.KeyEnter {
s.mode = StatusModeLog
s.onSearchFunc(query)
} else if key == tcell.KeyEsc {
s.mode = StatusModeLog
s.onSearchFunc("")
}
}).
SetChangedFunc(func(text string) {
query = text
})
inputField.Focus(nil)
inputField.SetBackgroundColor(config.ColorBackground)
inputField.SetFieldBackgroundColor(config.ColorBackground)
s.view.AddItem(inputField, 0, 1, true)
s.Update()
}
func (s *statusLine) SetOnUpdateFunc(f func()) {
s.onUpdateFunc = f
}
func (s *statusLine) Log(format string, a ...any) {
if s.mode != StatusModeLog {
return
}
str := fmt.Sprintf(format, a...)
s.view.Clear()
txt := tview.NewTextView().SetDynamicColors(true)
txt.SetBackgroundColor(config.ColorBackground)
txt.SetText(str)
s.view.AddItem(txt, 0, 1, false)
s.Update()
}
func (s *statusLine) GetView() tview.Primitive {
return s.view
}
func (s *statusLine) Update() {
s.onUpdateFunc()
}