1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

os: fixes for os.input(), os.get_raw_stdin(), os.get_raw_line() in case of stdin EOF

This commit is contained in:
Delyan Angelov
2021-02-27 12:50:15 +02:00
parent e6b4f9ff09
commit 20f9bdfa8e
4 changed files with 42 additions and 21 deletions

View File

@@ -200,11 +200,23 @@ pub fn file_name(path string) string {
return path.all_after_last(path_separator)
}
// input returns a one-line string from stdin, after printing a prompt.
pub fn input(prompt string) string {
// input_opt returns a one-line string from stdin, after printing a prompt.
// In the event of error (end of input), it returns `none`.
pub fn input_opt(prompt string) ?string {
print(prompt)
flush()
return get_line()
res := get_raw_line()
if res.len > 0 {
return res.trim_right('\r\n')
}
return none
}
// input returns a one-line string from stdin, after printing a prompt.
// In the event of error (end of input), it returns '<EOF>'.
pub fn input(prompt string) string {
res := input_opt(prompt) or { return '<EOF>' }
return res
}
// get_line returns a one-line string from stdin
@@ -212,9 +224,8 @@ pub fn get_line() string {
str := get_raw_line()
$if windows {
return str.trim_right('\r\n')
} $else {
return str.trim_right('\n')
}
return str.trim_right('\n')
}
// get_lines returns an array of strings read from from stdin.