2024-01-16 15:21:35 +00:00
|
|
|
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()
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
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
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
str := fmt.Sprintf(format, a...)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
s.view.Clear()
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
txt := tview.NewTextView().SetDynamicColors(true)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
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
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
func (s *statusLine) Update() {
|
|
|
|
s.onUpdateFunc()
|
|
|
|
}
|