summaryrefslogtreecommitdiff
path: root/menu/model.go
diff options
context:
space:
mode:
Diffstat (limited to 'menu/model.go')
-rw-r--r--menu/model.go33
1 files changed, 32 insertions, 1 deletions
diff --git a/menu/model.go b/menu/model.go
index aa7b00e..4480c96 100644
--- a/menu/model.go
+++ b/menu/model.go
@@ -1,6 +1,7 @@
package menu
import (
+ "github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/term"
"github.com/ewy1/pik/model"
@@ -9,6 +10,7 @@ import (
"github.com/ewy1/pik/viewport"
"github.com/spf13/pflag"
"os"
+ "strings"
)
type Model struct {
@@ -21,9 +23,31 @@ type Model struct {
Height int
Alt bool
AutoAlt bool
+ Search textinput.Model
Motd string
}
+func (m *Model) Highlights(src *model.HydratedSource, t model.HydratedTarget) bool {
+ val := m.Search.Value()
+ if val == "" {
+ return false
+ }
+
+ if strings.Contains(t.Target().Label(), val) {
+ return true
+ }
+
+ if strings.Contains(t.Description(src), val) {
+ return true
+ }
+
+ if strings.Contains(strings.Join(append(t.Target().Sub(), t.Target().Label()), " "), val) {
+ return true
+ }
+
+ return false
+}
+
func (m *Model) Init() tea.Cmd {
_, h, err := term.GetSize(0)
if err != nil {
@@ -79,6 +103,9 @@ func (m *Model) View() string {
}
result := m.State()
result = viewport.Process(result, m.Height)
+ if m.Search.Focused() {
+ result = m.AddSearch(result)
+ }
return result
}
@@ -89,13 +116,16 @@ func (m *Model) Result() (*model.HydratedSource, model.HydratedTarget) {
return m.SourceIndices[m.Index], m.Indices[m.Index]
}
-func (m *Model) Validate() {
+func (m *Model) Validate() (clamped bool) {
if m.Index < 0 {
m.Index = 0
+ return true
}
if m.Index > len(m.Indices)-1 {
m.Index = len(m.Indices) - 1
+ return true
}
+ return false
}
var ForcedInlineTerminals = map[string]string{
@@ -117,6 +147,7 @@ func NewModel(st *model.State, hydrators []model.Modder) *Model {
SourceIndices: make(map[int]*model.HydratedSource),
AutoAlt: !pflag.Lookup("inline").Changed && !isBanned,
Motd: motd.One(),
+ Search: textinput.New(),
}
idx := 0
for _, src := range st.Sources {