-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gu.gleam
394 lines (340 loc) · 10.3 KB
/
gu.gleam
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import shellout
import gleam/int
import gleam/string
import gleam/list
import gleam/option.{type Option, None, Some}
// v4.0.1
pub const zenity: List(String) = ["zenity"]
pub fn help_all(command: List(String)) -> List(String) {
["--help-all", ..command]
}
pub fn about(command: List(String)) -> List(String) {
["--about", ..command]
}
pub fn version(command: List(String)) -> List(String) {
["--version", ..command]
}
pub fn set_text(command: List(String), text: String) -> List(String) {
add_opt(command, Some(text), "text")
}
pub fn set_title(command: List(String), title: String) -> List(String) {
add_opt(command, Some(title), "title")
}
pub fn set_width(command: List(String), width: Int) -> List(String) {
add_opt(command, Some(int.to_string(width)), "width")
}
pub fn set_height(command: List(String), height: Int) -> List(String) {
add_opt(command, Some(int.to_string(height)), "height")
}
pub fn set_timeout(command: List(String), timeout: Int) -> List(String) {
add_opt(command, Some(int.to_string(timeout)), "timeout")
}
pub fn set_ok_label(command: List(String), ok_label: String) -> List(String) {
add_opt(command, Some(ok_label), "ok-label")
}
pub fn set_cancel_label(command: List(String), cancel_label: String) -> List(String) {
add_opt(command, Some(cancel_label), "cancel-label")
}
pub fn add_extra_button(command: List(String), extra_button: String) -> List(String) {
add_opt(command, Some(extra_button), "extra-button")
}
pub fn set_modal(command: List(String)) -> List(String) {
["--modal", ..command]
}
pub const type_error: String = "--error"
pub fn new_error(command: List(String)) -> List(String) {
[type_error, ..command]
}
pub const type_info: String = "--info"
pub fn new_info(command: List(String)) -> List(String) {
[type_info, ..command]
}
pub const type_question: String = "--question"
pub fn new_question(command: List(String)) -> List(String) {
[type_question, ..command]
}
pub const type_warning: String = "--warning"
pub fn new_warning(command: List(String)) -> List(String) {
[type_warning, ..command]
}
pub fn new_message_opts(
command: List(String),
text text: Option(String),
icon icon: Option(String),
no_wrap no_wrap: Bool,
no_markup no_markup: Bool,
ellipsize ellipsize: Bool,
) -> List(String) {
command
|> add_opt(text, "text")
|> add_opt(icon, "icon")
|> add_bool(no_wrap, "no-wrap")
|> add_bool(no_markup, "no-markup")
|> add_bool(ellipsize, "ellipsize")
}
pub fn new_calendar(
command: List(String),
text text: Option(String),
day day: Option(Int),
month month: Option(Int),
year year: Option(Int),
date_format date_format: Option(String),
) -> List(String) {
["--calendar", ..command]
|> add_opt(text, "text")
|> add_opt_int(day, "day")
|> add_opt_int(month, "month")
|> add_opt_int(year, "year")
|> add_opt(date_format, "date-format")
}
pub fn new_entry(
command: List(String),
text text: Option(String),
entry_text entry_text: Option(String),
hide_text hide_text: Bool,
) -> List(String) {
["--entry", ..command]
|> add_opt(text, "text")
|> add_opt(entry_text, "entry-text")
|> add_bool(hide_text, "hide-text")
}
pub fn new_file_selection(
command: List(String),
filename filename: Option(String),
multiple multiple: Bool,
directory directory: Bool,
save save: Bool,
separator separator: Option(String),
file_filter file_filter: Option(List(String)),
) -> List(String) {
["--file-selection", ..command]
|> add_opt(filename, "filename")
|> add_bool(multiple, "multiple")
|> add_bool(directory, "directory")
|> add_bool(save, "save")
|> add_opt(separator, "separator")
|> add_opt_list(file_filter, "file-filter")
}
pub fn new_list(command: List(String)) -> List(String) {
["--list", ..command]
}
pub fn new_list_opts(
command: List(String),
checklist checklist: Bool,
radiolist radiolist: Bool,
imagelist imagelist: Bool,
separator separator: Option(String),
multiple multiple: Bool,
editable editable: Bool,
print_column print_column: Option(String),
hide_column hide_column: Option(Int),
hide_header hide_header: Bool,
) -> List(String) {
command
|> add_bool(checklist, "checklist")
|> add_bool(radiolist, "radiolist")
|> add_bool(imagelist, "imagelist")
|> add_opt(separator, "separator")
|> add_bool(multiple, "multiple")
|> add_bool(editable, "editable")
|> add_opt(print_column, "print-column")
|> add_opt_int(hide_column, "hide-column")
|> add_bool(hide_header, "hide-header")
}
pub fn add_column(command: List(String), column: String) -> List(String) {
add_opt(command, Some(column), "column")
}
pub fn add_row(command: List(String), row: List(String)) -> List(String) {
row
|> list.reverse()
|> list.append(command)
}
pub fn new_notification(
command: List(String),
text text: Option(String),
icon icon: Option(String),
listen listen: Bool,
) -> List(String) {
["--notification", ..command]
|> add_opt(text, "text")
|> add_opt(icon, "icon")
|> add_bool(listen, "listen")
}
// TODO: stdin
pub fn new_progress(
command: List(String),
text text: Option(String),
percentage percentage: Option(String),
pulsate pulsate: Bool,
auto_close auto_close: Bool,
auto_kill auto_kill: Bool,
no_cancel no_cancel: Bool,
time_remaining time_remaining: Bool,
) -> List(String) {
["--progress", ..command]
|> add_opt(text, "text")
|> add_opt(percentage, "percentage")
|> add_bool(pulsate, "pulsate")
|> add_bool(auto_close, "auto-close")
|> add_bool(auto_kill, "auto-kill")
|> add_bool(no_cancel, "no-cancel")
|> add_bool(time_remaining, "time-remaining")
}
pub fn new_question_opts(
command: List(String),
default_cancel default_cancel: Bool,
switch switch: Bool,
) -> List(String) {
command
|> add_bool(default_cancel, "default-cancel")
|> add_bool(switch, "switch")
}
pub fn new_scale(
command: List(String),
text text: Option(String),
value value: Option(Int),
min_value min_value: Option(Int),
max_value max_value: Option(Int),
step step: Option(Int),
print_partial print_partial: Bool,
hide_value hide_value: Bool,
) -> List(String) {
["--scale", ..command]
|> add_opt(text, "text")
|> add_opt_int(value, "value")
|> add_opt_int(min_value, "min-value")
|> add_opt_int(max_value, "max-value")
|> add_opt_int(step, "step")
|> add_bool(print_partial, "print-partial")
|> add_bool(hide_value, "hide-value")
}
pub fn new_text_info(
command: List(String),
filename filename: Option(String),
editable editable: Bool,
font font: Option(String),
checkbox checkbox: Option(String),
auto_scroll auto_scroll: Bool,
) -> List(String) {
["--text-info", ..command]
|> add_opt(filename, "filename")
|> add_bool(editable, "editable")
|> add_opt(font, "font")
|> add_opt(checkbox, "checkbox")
|> add_bool(auto_scroll, "auto-scroll")
}
pub fn new_color_selection(
command: List(String),
color color: Option(String),
show_palette show_palette: Bool,
) -> List(String) {
["--color-selection", ..command]
|> add_opt(color, "color")
|> add_bool(show_palette, "show-palette")
}
pub fn new_password(command: List(String), username username: Bool) -> List(String) {
["--password", ..command]
|> add_bool(username, "username")
}
pub fn new_forms(command: List(String)) -> List(String) {
["--forms", ..command]
}
pub fn add_entry(command: List(String), entry: String) -> List(String) {
add_opt(command, Some(entry), "add-entry")
}
pub fn add_password(command: List(String), password: String) -> List(String) {
add_opt(command, Some(password), "add-password")
}
pub fn add_calendar(command: List(String), calendar: String) -> List(String) {
add_opt(command, Some(calendar), "add-calendar")
}
pub fn add_combo_and_values(
command: List(String),
combo: String,
values values: List(String),
) -> List(String) {
command
|> add_opt(Some(combo), "add-combo")
|> add_opt(Some(string.join(values, "|")), "combo-values")
}
pub fn add_list_and_values(
command: List(String),
list_name: String,
values values: List(String),
column_values column_values: Option(List(String)),
) -> List(String) {
command
|> add_opt(Some(list_name), "add-list")
|> add_opt(Some(string.join(values, "|")), "list-values")
|> add_opt_list(column_values, "column-values")
}
pub fn set_separator(command: List(String), separator: String) -> List(String) {
add_opt(command, Some(separator), "separator")
}
pub fn set_show_header(command: List(String)) -> List(String) {
["--show-header", ..command]
}
pub fn set_forms_date_format(command: List(String), format: String) -> List(String) {
add_opt(command, Some(format), "forms-date-format")
}
pub fn add_value(command: List(String), value: String) -> List(String) {
[value, ..command]
}
pub fn add_opt(command: List(String), opt: Option(String), str: String) -> List(String) {
case opt {
Some(val) -> ["--" <> str <> "=" <> val, ..command]
None -> command
}
}
pub fn add_opt_int(command: List(String), opt: Option(Int), str: String) -> List(String) {
case opt {
Some(val) -> ["--" <> str <> "=" <> int.to_string(val), ..command]
None -> command
}
}
pub fn add_opt_list(
command: List(String),
opt_list: Option(List(String)),
str: String,
) -> List(String) {
case opt_list {
Some(val) -> ["--" <> str <> "=" <> string.join(val, "|"), ..command]
None -> command
}
}
pub fn add_bool(command: List(String), opt: Bool, str: String) -> List(String) {
case opt {
True -> ["--" <> str, ..command]
False -> command
}
}
pub fn parse(str: String) -> String {
str
|> string.trim_right
}
pub fn parse_list(str: String, separator: String) -> List(String) {
str
|> string.trim_right
|> string.split(separator)
}
pub fn run(command: List(String), errors errors: Bool) -> Option(#(Int, String)) {
let opts = case errors {
True -> []
False -> [shellout.LetBeStderr]
}
case list.reverse(command) {
[run, ..with] ->
case shellout.command(run: run, with: with, in: ".", opt: opts) {
Ok(val) -> Some(#(0, val))
Error(err) ->
case errors {
True -> Some(err)
False -> None
}
}
_ -> None
}
}
pub fn exit(status: Int) -> Nil {
shellout.exit(status)
}