Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve rdstdin: fix genode bug, remove duplication, improve docs #18448

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 32 additions & 41 deletions lib/impure/rdstdin.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,32 @@
## is used. This suffices because Windows' console already provides the
## wanted functionality.

runnableExamples("-r:off"):
echo readLineFromStdin("Is Nim awesome? (Y/n): ")
var line: string
while true:
let ok = readLineFromStdin("How are you? ", line)
if not ok: break # ctrl-C or ctrl-D will cause a break
if line.len > 0: echo line
echo "exiting"
const useLinenoise = not (defined(windows) or defined(genode))

when defined(windows):
proc readLineFromStdin*(prompt: string): string {.
tags: [ReadIOEffect, WriteIOEffect].} =
## Reads a line from stdin.
stdout.write(prompt)
result = readLine(stdin)

proc readLineFromStdin*(prompt: string, line: var string): bool {.
tags: [ReadIOEffect, WriteIOEffect].} =
## Reads a `line` from stdin. `line` must not be
## `nil`! May throw an IO exception.
## A line of text may be delimited by `CR`, `LF` or
## `CRLF`. The newline character(s) are not part of the returned string.
## Returns `false` if the end of the file has been reached, `true`
## otherwise. If `false` is returned `line` contains no new data.
stdout.write(prompt)
result = readLine(stdin, line)

elif defined(genode):
proc readLineFromStdin*(prompt: string): string {.
tags: [ReadIOEffect, WriteIOEffect].} =
stdin.readLine()

proc readLineFromStdin*(prompt: string, line: var string): bool {.
tags: [ReadIOEffect, WriteIOEffect].} =
stdin.readLine(line)

else:
when useLinenoise:
import linenoise

proc readLineFromStdin*(prompt: string, line: var string): bool {.
tags: [ReadIOEffect, WriteIOEffect].} =
proc readLineFromStdin*(prompt: string, line: var string): bool {.
tags: [ReadIOEffect, WriteIOEffect].} =
## Reads a `line` from stdin. May raise `IOError`.
##
## A line of text may be delimited by ``\r``, ``\n`` or ``\r\n``, which are not
## part of the returned string.
##
## On platforms that support `linenoise`, it will be used.
##
## Returns `false` if the end of the file has been reached (or `^C` was entered),
## in which case `line.len == 0`. Entering an empty string would still return
## true.
runnableExamples("-r:off"):
echo readLineFromStdin("Is Nim awesome? (Y/n): ")
var line: string
while true:
let ok = readLineFromStdin("How are you? ", line)
if not ok: break # ctrl-C or ctrl-D will cause a break
if line.len > 0: echo line
echo "exiting"
when useLinenoise:
var buffer = linenoise.readLine(prompt)
if isNil(buffer):
line.setLen(0)
Expand All @@ -63,7 +48,13 @@ else:
historyAdd(buffer)
linenoise.free(buffer)
result = true
else:
stdout.write(prompt)
result = readLine(stdin, line)

proc readLineFromStdin*(prompt: string): string {.inline.} =
if not readLineFromStdin(prompt, result):
raise newException(IOError, "Linenoise returned nil")
proc readLineFromStdin*(prompt: string): string {.inline.} =
## Outplace overload.
runnableExamples("-r:off"):
echo readLineFromStdin("Is Nim awesome? (Y/n): ")
if not readLineFromStdin(prompt, result):
raise newException(IOError, "no data returned by `readLineFromStdin`")