-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconsole.go
More file actions
53 lines (46 loc) · 1.01 KB
/
console.go
File metadata and controls
53 lines (46 loc) · 1.01 KB
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
package tui
import (
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
)
type ConsoleModel struct {
Model textinput.Model
EnterHandler func(value string)
}
func NewConsoleModel() ConsoleModel {
ti := textinput.New()
ti.Placeholder = "Enter command"
ti.Focus()
ti.CharLimit = 156
ti.Width = 20
return ConsoleModel{
Model: ti,
}
}
func (m ConsoleModel) Init() tea.Cmd {
return textinput.Blink
}
func (m ConsoleModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEsc, tea.KeyCtrlC, tea.KeyCtrlQ:
return m, tea.Quit
case tea.KeyEnter:
m.EnterHandler(m.Model.Value())
m.Model.Reset()
m.Model.Update(tea.Quit())
return m, nil
}
}
m.Model, cmd = m.Model.Update(msg)
return m, cmd
}
func (m ConsoleModel) View() string {
return m.Model.View()
}
func (m ConsoleModel) OnEnter(handler func(value string)) ConsoleModel {
m.EnterHandler = handler
return m
}