-
Notifications
You must be signed in to change notification settings - Fork 3
/
commands.v
173 lines (150 loc) · 3.84 KB
/
commands.v
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
// Verminal - Terminal Emulator in V
module main
import iui as ui
import os
import gx
fn cmd_theme(mut win ui.Window, mut tbox ui.TextArea, args []string) {
if args.len == 1 {
tbox.lines << 'Usage: theme <dark,light,red,green>'
return
}
if args[1] == 'light' {
win.set_theme(ui.theme_default())
}
if args[1] == 'gray' {
win.set_theme(ui.theme_dark())
}
if args[1] == 'dark' {
win.set_theme(theme_dark())
}
if args[1] == 'red' {
win.set_theme(ui.theme_black_red())
}
if args[1] == 'green' {
win.set_theme(theme_green())
}
}
pub fn theme_green() ui.Theme {
return ui.Theme{
name: 'Terminal Green'
text_color: gx.rgb(24, 245, 24)
background: gx.black
button_bg_normal: gx.black
button_bg_hover: gx.black
button_bg_click: gx.black
button_border_normal: gx.rgb(130, 130, 130)
button_border_hover: gx.black
button_border_click: gx.black
menubar_background: gx.rgb(30, 30, 30)
menubar_border: gx.rgb(30, 30, 30)
dropdown_background: gx.black
dropdown_border: gx.black
textbox_background: gx.black
textbox_border: gx.black
checkbox_selected: gx.gray
checkbox_bg: gx.black
progressbar_fill: gx.gray
scroll_track_color: gx.black
scroll_bar_color: gx.gray
}
}
fn cmd_cd(mut win ui.Window, mut tbox ui.TextArea, args []string) {
mut path := win.extra_map['path']
if args.len == 1 {
tbox.text = tbox.text + path
return
}
if args[1] == '..' {
path = path.substr(0, path.replace('\\', '/').last_index('/') or { 0 }) // '
} else {
if os.is_abs_path(args[1]) {
path = os.real_path(args[1])
} else {
path = os.real_path(path + '/' + args[1])
}
}
if os.exists(path) {
win.extra_map['path'] = path
} else {
tbox.text = tbox.text + 'Cannot find the path specified: ' + path
}
}
fn tree_cmd(dir string, mut tbox ui.TextArea, tabs int) {
path := os.real_path(dir)
files := os.ls(path) or { [] }
for file in files {
joined := os.join_path(path, file)
if os.is_dir(joined) {
tbox.lines << '│\t'.repeat(tabs) + '├───' + file
tree_cmd(joined, mut tbox, tabs + 1)
}
}
if tabs == 0 {
add_new_input_line(mut tbox)
}
}
fn cmd_dir(mut tbox ui.TextArea, path string, args []string) {
mut ls := os.ls(os.real_path(path)) or { [''] }
mut txt := ' Directory of ' + path + '\n\n'
for file in ls {
txt = txt + '\t' + file + '\n'
}
// os.file_last_mod_unix(os.real_path(path + '/' + file)).str()
tbox.text = tbox.text + txt
}
fn cmd_v(mut tbox ui.TextArea, args []string) {
mut pro := os.execute('cmd /min /c ' + args.join(' '))
tbox.text = tbox.text + pro.output.trim_space()
}
fn cmd_exec(mut win ui.Window, mut tbox ui.TextArea, args []string) {
// Make sure we are in the correct directory
os.chdir(win.extra_map['path']) or { tbox.lines << err.str() }
if os.user_os() == 'windows' {
cmd_exec_win(mut win, mut tbox, args)
} else {
cmd_exec_unix(mut win, mut tbox, args)
}
}
// Linux
fn cmd_exec_unix(mut win ui.Window, mut tbox ui.TextArea, args []string) {
mut cmd := os.Command{
path: args.join(' ')
}
cmd.start() or { tbox.lines << err.str() }
for !cmd.eof {
out := cmd.read_line()
if out.len > 0 {
for line in out.split_into_lines() {
tbox.lines << line.trim_space()
}
}
}
add_new_input_line(mut tbox)
cmd.close() or { tbox.lines << err.str() }
}
// Windows;
// os.Command not fully implemented on Windows, so cmd.exe is used
//
fn cmd_exec_win(mut win ui.Window, mut tbox ui.TextArea, args []string) {
mut pro := os.new_process('cmd')
mut argsa := ['/min', '/c', args.join(' ')]
pro.set_args(argsa)
pro.set_redirect_stdio()
pro.run()
for pro.is_alive() {
out := pro.stdout_read()
if out.len > 0 {
for line in out.split_into_lines() {
tbox.lines << line.trim_space()
}
}
err := pro.stderr_read()
if err.len > 0 {
for line in err.split('\n') {
tbox.lines << line.trim_space()
}
}
}
add_new_input_line(mut tbox)
pro.close()
}