-
Notifications
You must be signed in to change notification settings - Fork 4
/
example.lua
198 lines (180 loc) · 4.24 KB
/
example.lua
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
package.path = package.path .. ";./lib/tui/?/init.lua;./lib/tui/?.lua;./lib/utf8_simple/?.lua;./?/init.lua"
local sirocco = require "sirocco"
local Prompt = sirocco.prompt
local List = sirocco.list
local Password = sirocco.password
local Confirm = sirocco.confirm
local Composite = sirocco.composite
local colors = require "term".colors
-- Clear whole screen for demo
-- io.write("\27[2J\27[1;1H")
Prompt {
prompt = "A simple question\n❱ ",
placeholder = "A simple answer",
required = true
}:ask()
Composite {
prompt = "What's your birthday? ",
separator = " / ",
fields = {
{
placeholder = "YYYY",
filter = function(input)
return input:match("%d")
and input
or ""
end,
length = 4,
},
{
placeholder = "mm",
filter = function(input)
return input:match("%d")
and input
or ""
end,
length = 2,
},
{
placeholder = "dd",
filter = function(input)
return input:match("%d")
and input
or ""
end,
length = 2,
},
}
}:ask()
Prompt {
prompt = "Another question\n❱ ",
default = "With a default answer",
}:ask()
Prompt {
prompt = "What programming languages do you know ?\n❱ ",
placeholder = "Try tab to get some suggestions...",
possibleValues = {
"lua",
"c",
"javascript",
"php",
"python",
"rust",
"go"
}
}:ask()
Prompt {
prompt = "What's you education level?",
showPossibleValues = true,
possibleValues = {
"highschool",
"college",
"doctorate"
},
validator = function(buffer)
local ok = false
for _, v in ipairs {
"highschool",
"college",
"doctorate"
} do
if v == buffer then
ok = true
break
end
end
return ok, not ok and colors.red .. "Not a valid answer" .. colors.reset
end
}:ask()
List {
prompt = "How do you say 'Hello'?",
required = true,
items = {
{
value = "Hello",
label = "Hello"
},
{
value = "Bonjour",
label = "Bonjour"
},
{
value = "Ciao",
label = "Ciao"
},
},
}:ask()
List {
prompt = "Here's a list with some already selected options:",
default = { 2, 4 },
required = true,
items = {
{
value = "First",
label = "First"
},
{
value = "Second",
label = "Second"
},
{
value = "Third",
label = "Third"
},
{
value = "Fourth",
label = "Fourth"
},
},
}:ask()
List {
prompt = "Where are you from?",
multiple = false,
items = {
{
value = "New York",
label = "New York"
},
{
value = "Paris",
label = "Paris"
},
{
value = "Rome",
label = "Rome"
}
},
}:ask()
Password {
prompt = "Enter your secret\n❱ ",
}:ask()
Password {
prompt = "Enter your secret (hidden answer)\n❱ ",
hidden = true
}:ask()
Prompt {
prompt = "What's your birthday?\n❱ ",
placeholder = "YYYY-mm-dd",
validator = function(buffer)
if utf8.len(buffer) > 0 then
local matches = { buffer:match("([1-9][0-9][0-9][0-9])%-([0-1][0-9])%-([0-3][0-9])") }
if matches[1] == nil
or tonumber(matches[2]) > 12
or tonumber(matches[3]) > 31 then
return false, colors.yellow .. "Not a valid date!" .. colors.reset
end
end
return true
end
}:ask()
Prompt {
prompt = "Only numbers allowed\n❱ ",
filter = function(input)
return input:match("%d")
and input
or ""
end
}:ask()
Confirm {
prompt = "All finished?"
}:ask()