-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
96 lines (75 loc) · 1.76 KB
/
model.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
package main
import (
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
const terminalWidth = 90
type (
model struct {
viewport viewport.Model
textinput textinput.Model
surface *surface
}
forceUpdateMsg struct{}
)
var _ tea.Model = new(model)
func initialModel(surface *surface) model {
m := model{
textinput: textinput.New(),
surface: surface,
}
m.textinput.Width = terminalWidth
m.textinput.Placeholder = "Send a command..."
m.textinput.Prompt = "| "
m.textinput.Focus()
m.viewport = viewport.New(terminalWidth, m.height(40))
m.viewport.SetContent(m.surface.buf.String())
m.viewport.GotoBottom()
return m
}
func (m model) Init() tea.Cmd {
return textinput.Blink
}
func (m model) Update(message tea.Msg) (tea.Model, tea.Cmd) {
var (
cmd tea.Cmd
cmds []tea.Cmd
)
switch msg := message.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlC:
m.textinput.Reset()
case tea.KeyEsc:
return m, tea.Quit
case tea.KeyEnter:
m.surface.glas.SendInput(m.textinput.Value())
m.textinput.Reset()
}
case forceUpdateMsg:
m.viewport.SetContent(m.surface.buf.String())
m.viewport.GotoBottom()
case tea.WindowSizeMsg:
m.viewport.Height = m.height(msg.Height)
case error:
m.surface.errCh <- msg
return m, nil
}
m.viewport, cmd = m.viewport.Update(message)
cmds = append(cmds, cmd)
m.textinput, cmd = m.textinput.Update(message)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m model) View() string {
return lipgloss.JoinVertical(
lipgloss.Top,
m.viewport.View(),
m.textinput.View(),
)
}
func (m model) height(h int) int {
return h - lipgloss.Height(m.textinput.View())
}