-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
179 lines (153 loc) · 3.96 KB
/
main.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"fmt"
"log"
"os"
structs "loot/structs"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
const (
nav structs.Mode = iota
edit
createTitle
createCommand
)
type model struct {
list list.Model
commandTitleInput textinput.Model
commandInput textinput.Model
mode structs.Mode
width int
height int
}
var docStyle = lipgloss.NewStyle().Margin(1, 2)
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
log.Println(msg)
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
if msg.String() == "ctrl+c" {
return m, tea.Quit
}
if m.commandTitleInput.Focused(){
if m.mode == createTitle {
if key.Matches(msg, structs.Keymaps.Enter) {
m.mode = createCommand
m.commandTitleInput.Blur()
m.commandInput.Focus()
var cmd tea.Cmd
cmd = textinput.Blink
cmds = append(cmds, cmd)
}
}
var cmd tea.Cmd
m.commandTitleInput, cmd = m.commandTitleInput.Update(msg)
cmds = append(cmds, cmd)
}else if m.commandInput.Focused() {
if key.Matches(msg, structs.Keymaps.Enter) {
cmds = append(cmds, CreateCommand(
m.commandTitleInput.Value(),
m.commandInput.Value(),
))
m.commandTitleInput.SetValue("")
m.commandInput.SetValue("")
m.commandInput.Blur()
m.mode = nav
}
var cmd tea.Cmd
m.commandInput, cmd = m.commandInput.Update(msg)
cmds = append(cmds, cmd)
}else{
switch {
case key.Matches(msg, structs.Keymaps.Create):
m.mode = createTitle
m.commandTitleInput.Focus()
var cmd tea.Cmd
cmd = textinput.Blink
cmds = append(cmds, cmd)
case key.Matches(msg, structs.Keymaps.Delete):
items := m.list.Items()
var cmd tea.Cmd
if len(items) > 0 {
cmd = DeleteCommand(m.getActiveProjectID())
}
cmds = append(cmds, cmd)
}
}
case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)
case structs.UpdateCommandMsg:
m.mode = nav
}
if m.list.FilterState() != list.Filtering {
items := []list.Item{}
items = append(items, GetAllItems()...)
m.list.SetItems(items)
}
if msg != "q" && (m.mode != createTitle && m.mode != createCommand) {
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
}
func (m model) View() string {
if m.commandTitleInput.Focused() {
return docStyle.Render(m.list.View() + "\n" + m.commandTitleInput.View())
}
if m.commandInput.Focused() {
return docStyle.Render(m.list.View() + "\n" + m.commandInput.View())
}
return docStyle.Render(m.list.View())
}
func (m model) getActiveProjectID() string {
items := m.list.Items()
activeItem := items[m.list.Index()]
return activeItem.(structs.Item).CommandTitle
}
func main() {
file, err := os.OpenFile("loot.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer file.Close()
log.SetOutput(file)
items := []list.Item{}
items = append(items, GetAllItems()...)
commandTitleInput := textinput.New()
commandTitleInput.Prompt = "$ "
commandTitleInput.Placeholder = "Command Name..."
commandTitleInput.CharLimit = 100
commandTitleInput.Width = 50
commandInput := textinput.New()
commandInput.Prompt = "$ "
commandInput.Placeholder = "Command..."
commandInput.CharLimit = 500
commandInput.Width = 50
m := model{
list: list.New(items, list.NewDefaultDelegate(), 0, 0),
commandInput: commandInput,
commandTitleInput: commandTitleInput,
}
m.list.Title = "Commands"
m.list.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{
structs.Keymaps.Create,
structs.Keymaps.Delete,
structs.Keymaps.Back,
}
}
p := tea.NewProgram(m, tea.WithAltScreen())
if _, err := p.Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}