-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
325 lines (303 loc) · 7.68 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package main
import (
"fmt"
"log"
"os"
"runtime/pprof"
bubblecomplete "github.com/mikecbone/bubblecomplete"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var (
validCommandStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#00c300")).Bold(true)
unknownCommandStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#ff6e67")).Bold(true)
unknownCommandErrorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#ff6e67")).Faint(true)
)
type model struct {
bubblecomplete bubblecomplete.Model
command string
err error
}
var commands = []*bubblecomplete.Command{
{
Command: "cat",
Description: "Concatenate and display the content of files",
PositionalArguments: []*bubblecomplete.PositionalArgument{
{
Name: "File",
Description: "File to display",
Type: bubblecomplete.FileArgument,
Required: true,
},
},
Flags: []*bubblecomplete.Flag{
{
LongFlag: "--show-ends",
Description: "Display $ at end of each line",
Type: bubblecomplete.BoolArgument,
},
{
ShortFlag: "-n",
LongFlag: "--number",
Description: "Number all output lines",
Type: bubblecomplete.BoolArgument,
},
},
},
{
Command: "cp",
Description: "Copy files and directories",
PositionalArguments: []*bubblecomplete.PositionalArgument{
{
Name: "file",
Description: "File to copy",
Type: bubblecomplete.FileDirArgument,
Required: true,
},
{
Name: "destination",
Description: "Destination to copy the file to",
Type: bubblecomplete.DirArgument,
Required: true,
},
},
Flags: []*bubblecomplete.Flag{
{
ShortFlag: "-r",
Description: "Copy directories recursively",
Type: bubblecomplete.BoolArgument,
},
{
ShortFlag: "-f",
Description: "Copy directories recursively",
Type: bubblecomplete.BoolArgument,
},
{
ShortFlag: "-t",
Description: "Copy directories recursively",
Type: bubblecomplete.BoolArgument,
},
},
},
{
Command: "git",
Description: "Git is a distributed version control system",
SubCommands: []*bubblecomplete.Command{
{
Command: "status",
Description: "Show the working tree status",
},
{
Command: "stash",
Description: "Stash the changes in a dirty working directory away",
SubCommands: []*bubblecomplete.Command{
{
Command: "pop",
Description: "Remove a single stashed state from the stash list and apply it on top of the current working tree state",
},
{
Command: "apply",
Description: "Like pop, but do not remove the state from the stash list",
},
},
},
{
Command: "commit",
Description: "Record changes to the repository",
Flags: []*bubblecomplete.Flag{
{
ShortFlag: "-m",
LongFlag: "--message",
Description: "Use the given message as the commit message",
Type: bubblecomplete.StringArgument,
},
{
ShortFlag: "-a",
LongFlag: "--all",
Description: "Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected",
Type: bubblecomplete.BoolArgument,
},
{
LongFlag: "--amend",
Description: "Replace the tip of the current branch by creating a new commit",
Type: bubblecomplete.BoolArgument,
},
},
},
{
Command: "push",
Description: "Update remote refs along with associated objects",
PositionalArguments: []*bubblecomplete.PositionalArgument{
{
Name: "remote",
Description: "Remote repository to push to",
Type: bubblecomplete.StringArgument,
Required: false,
},
{
Name: "branch",
Description: "Branch to push",
Type: bubblecomplete.StringArgument,
Required: false,
},
},
},
{
Command: "pull",
Description: "Fetch from and integrate with another repository or a local branch",
},
{
Command: "clone",
Description: "Clone a repository into a new directory",
},
{
Command: "checkout",
Description: "Switch branches or restore working tree files",
},
{
Command: "branch",
Description: "List, create, or delete branches",
},
{
Command: "merge",
Description: "Join two or more development histories together",
},
{
Command: "rebase",
Description: "Reapply commits on top of another base tip",
},
{
Command: "tag",
Description: "Create, list, delete or verify a tag object signed with GPG",
},
},
Flags: []*bubblecomplete.Flag{
{
LongFlag: "--version",
Description: "Print the Git version",
Type: bubblecomplete.BoolArgument,
},
{
LongFlag: "--help",
Description: "Show the help message",
Type: bubblecomplete.BoolArgument,
Persistent: true,
},
},
},
{
Command: "go",
Description: "Go is a statically typed, compiled programming language designed at Google",
SubCommands: []*bubblecomplete.Command{
{
Command: "run",
Description: "Compile and run Go program",
},
{
Command: "build",
Description: "Compile packages and dependencies",
SubCommands: []*bubblecomplete.Command{
{
Command: "test",
},
{
Command: "all",
},
},
},
{
Command: "test",
Description: "Test packages",
},
},
},
}
func initialModel() tea.Model {
bc, err := bubblecomplete.New(commands, 100)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
bc.CompletionsPosition = bubblecomplete.PositionBelow
bc.ShowBorderScroll = true
home, _ := os.UserHomeDir()
historyFilePath := home + "/.bubblecomplete_history.json"
bc.SetHistoryFilePath(historyFilePath)
bc.SetPlaceholder("Enter Command...")
bc.HistoryLimit = 50
m := model{
bubblecomplete: bc,
}
return m
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
m.bubblecomplete, cmd = m.bubblecomplete.Update(msg)
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlC:
return m, tea.Quit
case tea.KeyEsc:
if m.bubblecomplete.ShowingCompletions() {
m.bubblecomplete.CloseCompletions()
}
}
case tea.WindowSizeMsg:
m.bubblecomplete.SetWidth(msg.Width)
case bubblecomplete.SelectedCommandMsg:
m.command = msg.Command
m.err = msg.Err
}
return m, cmd
}
func (m model) View() string {
if m.bubblecomplete.Err != nil {
return m.bubblecomplete.Err.Error()
}
text := "Enter Command:"
if m.command != "" {
text = "Entered Command: "
if m.err != nil {
text += unknownCommandStyle.Render(
m.command,
) + unknownCommandErrorStyle.Render(
" ["+m.err.Error()+"]",
)
} else {
text += validCommandStyle.Render(m.command)
}
}
return text + "\n" + m.bubblecomplete.View()
}
func main() {
// Profiling CPU before the main workload is started
f, e := os.Create("cpu.prof")
if e != nil {
log.Fatal(e)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
p := tea.NewProgram(initialModel())
if _, err := p.Run(); err != nil {
fmt.Printf("Error: %v", err)
os.Exit(1)
}
// Profiling memory after the main workload is completed
fMem, er := os.Create("mem.prof")
if er != nil {
log.Fatal(er)
}
pprof.WriteHeapProfile(fMem)
fMem.Close()
// Profiling goroutines after the main workload is completed
fGoroutine, err := os.Create("goroutine.prof")
if err != nil {
log.Fatal(err)
}
pprof.Lookup("goroutine").WriteTo(fGoroutine, 0)
fGoroutine.Close()
}