-
Notifications
You must be signed in to change notification settings - Fork 0
/
mode_history_search.go
132 lines (110 loc) · 3.45 KB
/
mode_history_search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package shell
import (
"github.com/DomBlack/bubble-shell/pkg/tui/history"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
)
type HistorySearchMode struct{}
var _ Mode = (*HistorySearchMode)(nil)
func (h *HistorySearchMode) Enter(m Model) (Model, tea.Cmd) {
m.searchDirBackwards = true
m.searchInput.Prompt = m.searchInputPrompt(true)
m.searchInput.SetValue("")
m.searchInput.CursorEnd()
m.searchInput.Focus()
return m, nil
}
func (h *HistorySearchMode) Leave(m Model) (Model, tea.Cmd) {
m.searchInput.SetValue("")
m.searchInput.Blur()
return m, nil
}
func (h *HistorySearchMode) Update(m Model, msg tea.Msg) (Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, m.cfg.KeyMap.ExecuteCommand):
line := m.input.Value()
historyItem := history.NewItem(m.input.Prompt, line, history.RunningStatus)
return m, tea.Sequence(
m.Enter(&CommandRunningMode{}),
m.history.AppendItem(historyItem),
m.ExecuteCommand(historyItem),
)
case key.Matches(msg, m.cfg.KeyMap.SearchHistoryBackwards):
foundIdx, found := m.history.Search(m.searchInput.Value(), m.lookBack, 1)
m.searchDirBackwards = true
m.searchInput.Prompt = m.searchInputPrompt(found)
return m.updateSearchResult(foundIdx), nil
case key.Matches(msg, m.cfg.KeyMap.SearchHistoryForwards):
foundIdx, found := m.history.Search(m.searchInput.Value(), m.lookBack, -1)
m.searchDirBackwards = false
m.searchInput.Prompt = m.searchInputPrompt(found)
return m.updateSearchResult(foundIdx), nil
case key.Matches(msg, m.cfg.KeyMap.Cancel):
return m, m.Enter(&CommandEntryMode{})
case msg.Type == tea.KeyLeft || msg.Type == tea.KeyRight || msg.Type == tea.KeyTab:
line := m.history.Lookback(m.lookBack).Line
m.input.SetValue(line)
if msg.Type == tea.KeyLeft {
return m, tea.Sequence(
m.Enter(&CommandEntryMode{KeepInputContent: true}),
func() tea.Msg { return msg },
)
} else {
return m, m.Enter(&CommandEntryMode{KeepInputContent: true})
}
default:
// If the search term has changed, reset the search
searchTerms := m.searchInput.Value()
if searchTerms != m.lastSearch {
foundIdx, found := m.history.Search(m.searchInput.Value(), 0, 1)
m.searchInput.Prompt = m.searchInputPrompt(found)
return m.updateSearchResult(foundIdx), nil
}
}
return m, nil
}
return m, nil
}
func (h *HistorySearchMode) AdditionalView(m Model) string {
return m.searchInput.View()
}
func (h *HistorySearchMode) ShortHelp(m Model, keyMap KeyMap) []key.Binding {
backwards := keyMap.SearchHistoryBackwards
backwards.SetHelp(backwards.Help().Key, "search backwards")
forwards := keyMap.SearchHistoryForwards
forwards.SetHelp(forwards.Help().Key, "search forwards")
return []key.Binding{
backwards, forwards, keyMap.ExecuteCommand, keyMap.Cancel,
}
}
func (h *HistorySearchMode) FullHelp(m Model, keyMap KeyMap) [][]key.Binding {
return [][]key.Binding{
h.ShortHelp(m, keyMap),
}
}
func (m Model) updateSearchResult(idx int) Model {
m.lookBack = idx
if m.lookBack == 0 {
m.input.SetValue("")
} else {
m.input.SetValue(m.history.Lookback(m.lookBack).Line)
}
return m
}
func (m Model) searchInputPrompt(foundResult bool) string {
if foundResult {
if m.searchDirBackwards {
return "bck-i-search: "
} else {
return "fwd-i-search: "
}
} else {
if m.searchDirBackwards {
return "failing bck-i-search: "
} else {
return "failing fwd-i-search: "
}
}
}