-
Notifications
You must be signed in to change notification settings - Fork 0
/
mode_history_lookback.go
127 lines (103 loc) · 2.78 KB
/
mode_history_lookback.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
package shell
import (
"github.com/DomBlack/bubble-shell/pkg/tui/history"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
)
type HistoryLookbackMode struct {
TriggerMsg tea.Msg
}
var _ Mode = (*HistoryLookbackMode)(nil)
func (h *HistoryLookbackMode) Enter(m Model) (Model, tea.Cmd) {
m.lookBack = 0
return m, func() tea.Msg {
return h.TriggerMsg
}
}
func (h *HistoryLookbackMode) Leave(m Model) (Model, tea.Cmd) {
m.input.Blur()
return m, nil
}
func (h *HistoryLookbackMode) Update(m Model, msg tea.Msg) (Model, tea.Cmd) {
var cmds []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.Up):
if len(m.history.Items) == 0 {
return m, nil
}
previousLine := m.history.Lookback(m.lookBack).Line
possibleLine := previousLine
for previousLine == possibleLine {
m.lookBack++
if m.lookBack > len(m.history.Items) {
m.lookBack = len(m.history.Items)
break
}
item := m.history.Lookback(m.lookBack)
if item.ItemType == history.Command {
possibleLine = item.Line
}
}
m.input.SetValue(possibleLine)
m.input.CursorEnd()
return m, nil
case key.Matches(msg, m.cfg.KeyMap.Down):
if len(m.history.Items) == 0 {
return m, nil
}
previousLine := m.history.Lookback(m.lookBack).Line
possibleLine := previousLine
for previousLine == possibleLine {
m.lookBack--
if m.lookBack <= 0 {
break
}
item := m.history.Lookback(m.lookBack)
if item.ItemType == history.Command {
possibleLine = item.Line
}
}
if m.lookBack <= 0 {
return m, m.Enter(&CommandEntryMode{})
} else {
m.input.SetValue(possibleLine)
}
return m, nil
case key.Matches(msg, m.cfg.KeyMap.Cancel):
return m, m.Enter(&CommandEntryMode{})
default:
if msg.Type == tea.KeyRight || msg.Type == tea.KeyTab {
return m, m.Enter(&CommandEntryMode{KeepInputContent: true})
} else {
return m, tea.Sequence(
m.Enter(&CommandEntryMode{KeepInputContent: true}),
func() tea.Msg { return msg },
)
}
}
}
return m, tea.Batch(cmds...)
}
func (h *HistoryLookbackMode) AdditionalView(m Model) string {
return ""
}
func (h *HistoryLookbackMode) ShortHelp(m Model, keyMap KeyMap) []key.Binding {
return []key.Binding{
keyMap.Up, keyMap.Down, keyMap.ExecuteCommand, keyMap.Cancel,
}
}
func (h *HistoryLookbackMode) FullHelp(m Model, keyMap KeyMap) [][]key.Binding {
return [][]key.Binding{
h.ShortHelp(m, keyMap),
}
}