-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserinput.R
370 lines (318 loc) · 13.4 KB
/
userinput.R
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
pkg.env <- new.env()
pkg.env$preset.input = character()
#' Prompts the user for confirmation in the console
#'
#' @param msg string the thing that you are want a yes/no answer to
#' @param default mixed optional. If supplied and y or yes or similar, will default to true.
#' If supplied and not similar to yes will default to no.
#' If not supplied, will not have a default and the user must choose an option
#' @return boolean
#' @details
#' Presents the user with a yes/no question and returns true or false depending on their answer
#' @export
Confirm <- function (msg, default = NULL) {
options <- c('Yes', 'No')
if (!is.null(default)) {
if (default %in% c('y', 'Y', 'yes', 1, TRUE)) {
default <- 1
} else {
default <- 2
}
options[default] <- paste(options[default], "(default)")
}
# output yes / no
cat(paste0(1:length(options), ") ", options, collapse = '\n'))
choice <- .GetValidatedInt(msg, max = length(options), default = default, parse.range = FALSE, equivalents = list('y' = 1, 'n' = 2))
if (choice == 1) {
return(TRUE)
} else {
return(FALSE)
}
}
#' Prompts the user to choose one of the given set of choices
#'
#' @param choices vector of strings
#' @param choosing.what string; used for presenting the instructions to the user
#' @param default int if the user just hits enter, this will be chosen
#' @param allow.range boolean if TRUE, the user can enter something like 2-4 which will return c(2,3,4)
#' @param optional boolean if TRUE, user can select 0 to return false (i.e. no choice)
#' @param numbering a vector of ints to use as the choice labels, instead of just numbering them starting at 1. optional
#' @return int the index of the choice selected by the user
#' @export
GetUserChoice <- function (choices, choosing.what = "one of the following", default = 1, allow.range = FALSE, optional = FALSE, numbers = NULL) {
#todo recursive validation like http://www.rexamples.com/4/Reading%20user%20input
cat(paste0("choose ", choosing.what, ":\n"))
if (is.null(numbers)) {
numbers <- 1:length(choices)
}
if (is.logical(optional) && optional) {
optional = 0;
}
if (is.numeric(optional)) {
choices <- c("none", choices)
numbers <- c(optional, numbers)
}
cat(paste0(numbers, ") ", choices, collapse = '\n'))
if (default %in% 1:length(choices)) {
cat(paste('\ndefault: ', default))
} else {
default = NA
}
if (.isConsecutiveNumbers(numbers)) {
msg <- paste0("enter int ",min(numbers)," to ",max(numbers),": ")
} else {
msg <- paste0("enter a number from the list: ")
}
choice <- .GetValidatedInt(msg, in.list = numbers, default = default, parse.range = allow.range)
return(choice)
}
#' Whether a list of numbers are consecutive
#' @param numbers numeric
#' @return logical
.isConsecutiveNumbers <- function (numbers) {
return(max(numbers) - min(numbers) + 1 == length(unique(numbers)))
}
#' allows the user to select 1 or more of the choices, one by one
#'
#' @param options string vector; list of choices
#' @param choosing.what string; instrucitons for user
#' @param default int or string "all"; which options should be selected if the just hits clicks 'enter'
#' @param all boolean; should there be an extra option at the end to choose all the options in the list?
#' @return int vector of the choice numbers
#' @export
GetMultiUserchoice <- function (options, choosing.what = 'one of the following', default = 1, all = FALSE, numbering = NULL) {
if (length(options) == 1 && (default == 1 || default == 'all')) {
# if there was only 1 option and the default is 1 or 'all',
# then just return that option without getting user input
return(c(1))
}
if (default == 'all') {
all <- TRUE
}
if (all) {
options <- c(options, 'all')
all.choice <- length(options)
if (default == 'all') {
default <- all.choice
}
} else {
all.choice <- -99 # can't choose all
}
options <- c(options, 'exit')
exit.choice <- length(options)
last.choice <- -1;
chosen <- c()
while(TRUE) {
if (max(last.choice) > 0) {
# if something has been chosen, change the default to exit
default = exit.choice
}
last.choice <- GetUserChoice(options, choosing.what, default = default, allow.range = TRUE, numbering = numbering)
should.exit <- exit.choice %in% last.choice
should.use.all <- all.choice %in% last.choice
if (should.use.all) {
chosen <- 1:length(options)
break()
}
if (should.exit) {
break()
} else {
chosen <- union(chosen, last.choice)
}
}
# setdiff also returns unique
chosen <- setdiff(chosen, c(exit.choice, all.choice))
return(chosen)
}
#' Prompts the user to enter an integer
#'
#' @param msg string; the message to display. eg, choose a number between 1 and 10, or choose from the following options
#' @param max int; optional. the highest allowed integer
#' @param min int; optional. the lowest allowed integer
#' @param default int; optional. The integer which will be returned if nothing is inputted (i.e. user hits return)
#' @param num.attempts int; The number of attempts attempted so far. The method recurses on itself to give the user another chance if
#' the input doesn't validate. This is only used by the recusive function call.
#' @param parse.range boolean; if TRUE, validates a range of int in the form "from-to", eg 2-4, and returns a vector containing that range
#' @param equivalents list; Allows the user to enter any of the values in the list which will be interpreted as the corresponding name in the list
#' @param quit string; If the input equals this, the program will quit. Allows the user to quit during a request for input
.GetValidatedInt <- function (msg,
max = NULL,
min = 1,
in.list = NULL,
default = NULL,
num.attempts = 0,
parse.range = FALSE,
equivalents = list(),
quit =
"Q") {
# if in.list is set, ignore max and min
if (!is.null(in.list)) {
max <- NULL
min <- NULL
}
max.attempts <- 8
choice <- .ReadLine(paste(msg, " : "))
if (choice == quit) {
stop('quitting')
}
if (!is.null(equivalents[[choice]])) {
choice <- equivalents[[choice]]
}
if (choice == '' && !is.null(default)) {
choice <- as.integer(default)
} else if (.IsInt(choice)) {
choice <- as.integer(choice)
} else if (parse.range && .IsRange(choice)) {
# split by colon and parse range
values <- regmatches(choice, gregexpr("-?[0-9]+", choice))
choice <- as.integer(values[[1]][1]):as.integer(values[[1]][2])
}
if (num.attempts > max.attempts) {
stop("you kept entering an invalid choice, idiot")
} else if (class(choice) != 'integer' ||
(!is.null(max) && max(choice) > max) ||
(!is.null(min) && min(choice) < min) ||
(!is.null(in.list) && !all(choice %in% in.list))) {
if (num.attempts == 0) {
msg <- paste("Invalid choice.", msg)
}
.GetValidatedInt(msg, max = max, min = min, in.list = in.list, default = default, num.attempts = num.attempts + 1, parse.range = parse.range, equivalents = equivalents)
} else {
return(choice)
}
}
.IsInt <- function (str) {
return(grepl("^-?[0-9]+$",str))
}
.IsRange <- function (str) {
return(grepl("^-?[0-9]+[ ]*:[ ]*-?[0-9]+$",str))
}
#' Prompts the user to enter an float
#'
#' @param msg string the message to display. eg, choose a number between 1 and 10, or choose from the following options
#' @param max float optional. the highest allowed number
#' @param min float optional. the lowest allowed number
#' @param default float optional. The integer which will be returned if nothing is inputted (i.e. user hits return)
#' @param num.attempts int; The number of attempts attempted so far. The method recurses on itself to give the user another chance if
#' the input doesn't validate. This is only used by the recusive function call.
#' @param parse.range boolean; if TRUE, validates a range of int in the form "from-to", eg 2-4, and returns a vector containing that range
#' @param equivalents list; Allows the user to enter any of the values in the list which will be interpreted as the corresponding name in the list
#' @param quit string; If the input equals this, the program will quit. Allows the user to quit during a request for input
#' TODO: refactor this to be more general. e.g. a list of validation rules as functions
.GetValidatedFloat <- function (msg = 'Enter a number', max = NA, min = 0, default = NA, num.attempts = 0, quit = "Q") {
max.attempts <- 8
val <- .ReadLine(paste(msg, " : "))
if (val == quit) {
stop('quitting')
}
if (val == '' && !is.na(default)) {
val <- as.numeric(default)
} else if (grepl("^-?[0-9]+.?[0-9]*$",val)) {
val <- as.numeric(val)
}
if (num.attempts > max.attempts) {
stop("you kept entering an invalid choice, idiot")
} else if (class(val) != 'numeric' || (!is.na(max) && max(val) > max) || (!is.na(min) && min(val) < min)) {
if (num.attempts == 0) {
msg <- paste("Invalid choice.", msg)
}
.GetValidatedFloat(msg, max = max, min = min, default = default, num.attempts = num.attempts + 1)
} else {
return(val)
}
}
#' Reads an int input from the user and re-prompts if they didn't enter an int
#' @param msg character
#' @param min int
#' @param max int
#' @param default int optional
#' @export
ReadInt <- function (msg = "Enter an integer", min = 1, max = NA, default = NULL) {
extra <- c();
if (!is.na(min)) {
extra <- c(extra, paste('min', min))
}
if (!is.na(max)) {
extra <- c(extra, paste('max', max))
}
if (!is.null(default)) {
extra <- c(extra, paste('default', default))
}
if (length(extra) > 0) {
msg <- paste0(msg, " (", paste(extra, collapse = ", "), ")")
}
val <- .GetValidatedInt(min = min, max = max, default = default, msg = msg)
return(val)
}
#' prompts the user for a directory
#'
#' @param msg the prompt to show to the user
#' @param create.if.missing boolean whether to create the directory if it is missing or prompt
#' @details
#' after the user enters in a directory, it will check if the directory exists.
#' If it doesn't, it will prompt the user if they want to create it, or if create.if.missing is TRUE
#' will create it without asking. It will only create the directory itself, not parent directories.
#' e.g. if the user enters /a/b/c and /a/b doesn't exist, it will not create it. But if /a/b exists and
#' /a/b/c doesn't exist, it will prompt to create c
#' @export
GetDirectory = function (msg = 'please enter a path to the directory', create.if.missing = FALSE) {
msg <- paste(msg, 'Enter . (dot) for the working directory. Enter blank string to cancel')
while (is.character(msg)) {
dir.path <- .ReadLine(paste(msg, " : "))
if (dir.path == "") {
return(FALSE)
} else if (!file.exists(dirname(dir.path))) {
msg <- paste("Sorry, ", dirname(dir.path), "doesn't exist. Please try again")
} else if (file.exists(dir.path) && !file.info(dir.path)$isdir) {
msg <- "Sorry, that path already exists as a file. Please try again"
} else {
msg <- FALSE
}
}
if(!file.exists(dir.path)) {
dir.missing.msg <- "The directory you entered doesn't exist. Would you like to create it?"
if (create.if.missing || Confirm(dir.missing.msg)) {
dir.create(dir.path)
} else {
return(FALSE)
}
}
return(dir.path)
}
#' Wrapper for .ReadLine which first check if any preset input exists
#' and will return it if it does exist or .ReadLine if it doesn't
#' @param prompt character
#' @return character
#' @details
#' By allowing the presetting of user input, unit tests and examples can be run without
#' pausing to wait for user input.
.ReadLine <- function (prompt) {
if (length(pkg.env$preset.input) > 0) {
auto.input <- pkg.env$preset.input[1]
cat(paste(prompt, auto.input, '(preset)'))
pkg.env$preset.input <- pkg.env$preset.input[-1]
return(auto.input)
} else {
return(readline(prompt))
}
}
#' sets the preset input global variable, which if not empty will be used instead
#' of .ReadLine.
#'
#' Allows tests to preset the userinput without interrupting the test with .ReadLine.
#' @param user.input.strings character
#' @details
#' This should probably not be used except for its designed purpose of unit
#' tests on scripts that use userinput. It could cause unexpected behaviour if
#' by mistake something is left in the preset.input variable. Use on.exit(Preset())
#' @export
Preset <- function (user.input.strings = character()) {
pkg.env$preset.input <- user.input.strings
}
#' Returns the preset input
#'
#' @return character
#' @export
GetPresets <- function () {
return(pkg.env$preset.input)
}