aboutsummaryrefslogtreecommitdiff
path: root/selecting.go
diff options
context:
space:
mode:
Diffstat (limited to 'selecting.go')
-rw-r--r--selecting.go42
1 files changed, 34 insertions, 8 deletions
diff --git a/selecting.go b/selecting.go
index 0817d3d..a56ccee 100644
--- a/selecting.go
+++ b/selecting.go
@@ -8,30 +8,56 @@ import (
)
type selectingModel struct {
+ initialFilter string
list list.Model
}
-func StartSelecting() selectingModel {
+func StartSelecting(initialFilter string) *selectingModel {
items, err := getItems()
if err != nil {
panic(err)
}
- list := list.New(items, list.NewDefaultDelegate(), 0, 0)
- list.Title = "Commands"
+ lst := list.New(items, list.NewDefaultDelegate(), 0, 0)
+ lst.Title = "Commands"
m := selectingModel{
- list: list,
+ list: lst,
+ initialFilter: initialFilter,
}
- return m
+ return &m
}
-func (m selectingModel) Init() tea.Cmd {
+func (m *selectingModel) Init() tea.Cmd {
+ if m.initialFilter != "" {
+ // ok so this is scuffed!
+ // there's no way to just set the filter, so instead we mimic typing it.
+ // we can ignore every command except the last one, which will just update our visible items
+ lst, cmd := m.list.Update(tea.KeyMsg{
+ Type: tea.KeyRunes,
+ Runes: []rune{'/'},
+ });
+
+ for _, chr := range m.initialFilter {
+ lst, cmd = lst.Update(tea.KeyMsg{
+ Type: tea.KeyRunes,
+ Runes: []rune{chr},
+ });
+ }
+
+ lst, _ = lst.Update(tea.KeyMsg{
+ Type: tea.KeyEnter,
+ Runes: []rune{},
+ })
+ m.list = lst;
+ return tea.Batch(tea.EnterAltScreen, cmd);
+ }
+
return tea.EnterAltScreen
}
-func (m selectingModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
+func (m *selectingModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
@@ -97,7 +123,7 @@ func (m selectingModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Batch(cmds...)
}
-func (m selectingModel) View() string {
+func (m *selectingModel) View() string {
return appStyle.Render(m.list.View())
}