Repository navigation
Expand file tree
/
Copy pathinteractive.mbt
More file actions
165 lines (156 loc) · 3.97 KB
/
Copy pathinteractive.mbt
File metadata and controls
165 lines (156 loc) · 3.97 KB
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
///|
/// Interactive terminal helpers for `mb tool install -i`.
///
/// The prompt reads single keypresses from a raw terminal and renders a
/// checkbox list with ANSI escapes. ESC cancels.
///|
/// Whether a prompt can be shown (stdin and stdout are terminals).
fn can_prompt() -> Bool {
mb_isatty_stdin() != 0 && is_tty()
}
///|
/// Write raw text to stdout without a trailing newline.
fn write_raw(text : String) -> Unit {
ignore(mb_write(cstring(text)))
}
///|
fn in_array(items : Array[String], s : String) -> Bool {
for item in items {
if item == s {
return true
}
}
false
}
///|
fn count_selected(selected : Array[Bool]) -> Int32 {
let mut n = 0
for s in selected {
if s {
n = n + 1
}
}
n
}
///|
fn render_select(
names : Array[String],
selected : Array[Bool],
cursor : Int32,
redraw : Bool,
) -> Unit {
let mut out = ""
if redraw {
out = out + "\u{1B}[" + (names.length() + 1).to_string() + "A"
}
out = out + "\r\u{1B}[J"
out = out +
"Select executables to install (\u{2191}/\u{2193} move, space toggle, a all, enter confirm, esc cancel)\n"
for i, name in names {
let arrow = if i == cursor { "\u{276F}" } else { " " }
let mark = if selected[i] { "x" } else { " " }
out = out + " " + arrow + " [" + mark + "] " + name + "\n"
}
write_raw(out)
}
///|
fn clear_select(lines : Int32) -> Unit {
write_raw("\u{1B}[" + (lines + 1).to_string() + "A\r\u{1B}[J")
}
///|
/// Ask the user which executables to install. Returns the selected names, or
/// `None` when the user cancels with ESC (or EOF).
fn select_executables(names : Array[String]) -> Array[String]? {
let n = names.length()
if n == 0 {
return Some([])
}
if mb_tty_raw() != 0 {
return None
}
let result = run_select(names, Array::make(n, true), 0)
clear_select(n)
write_raw("\u{1B}[?25h")
ignore(mb_tty_restore())
result
}
///|
/// The key loop behind `select_executables`, run while the terminal is raw.
fn run_select(
names : Array[String],
selected : Array[Bool],
start_cursor : Int32,
) -> Array[String]? {
let n = names.length()
let mut cursor = start_cursor
write_raw("\u{1B}[?25l")
render_select(names, selected, cursor, false)
while true {
let key = mb_read_byte(0)
if key == -1 || key == 27 || key == 3 {
// EOF, ESC or Ctrl-C. ESC may also start an arrow-key sequence.
if key != 27 {
return None
}
let next = mb_read_byte(50)
if next == 91 {
let code = mb_read_byte(50)
if code == 65 {
cursor = (cursor + n - 1) % n
} else if code == 66 {
cursor = (cursor + 1) % n
}
} else {
return None
}
} else if key == 32 {
selected[cursor] = !selected[cursor]
} else if key == 13 || key == 10 {
if count_selected(selected) > 0 {
let picked : Array[String] = []
for i, name in names {
if selected[i] {
picked.push(name)
}
}
return Some(picked)
}
} else if key == 97 || key == 65 {
// 'a' / 'A': toggle everything.
let all = count_selected(selected) != n
for i = 0; i < n; i = i + 1 {
selected[i] = all
}
} else if key == 106 {
cursor = (cursor + 1) % n
} else if key == 107 {
cursor = (cursor + n - 1) % n
}
render_select(names, selected, cursor, true)
}
None
}
///|
/// Return the names to install: with `interactive` and more than one candidate,
/// ask the user; ESC cancels the install.
fn choose_names(
names : Array[String],
interactive : Bool,
) -> Array[String] raise MbError {
if !interactive || names.length() <= 1 {
return names
}
if !can_prompt() {
raise MbError(
"cannot choose executables on a non-interactive terminal; candidates: " +
names.join(", "),
)
}
match select_executables(names) {
None => {
println("Installation cancelled.")
raise Coded("installation cancelled", 0)
}
Some(chosen) => chosen
}
}