subsonic-tui/internal/tui/views/status_line.go
Sagi Dayan 92e3c4ea7d
All checks were successful
ci / check for spelling errors (pull_request) Successful in 21s
ci / code quality (lint/tests) (pull_request) Successful in 2m29s
ci / Make sure build does not fail (pull_request) Successful in 2m25s
ci / notify-fail (pull_request) Has been skipped
ci / check for spelling errors (push) Successful in 22s
ci / code quality (lint/tests) (push) Successful in 1m53s
ci / Make sure build does not fail (push) Successful in 2m55s
ci / notify-fail (push) Has been skipped
Added ci workflow + fixed linting issues
Signed-off-by: Sagi Dayan <sagidayan@gmail.com>
2024-12-18 18:02:01 +02:00

104 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()
}