diff options
Diffstat (limited to 'lib/impure/rdstdin.nim')
-rw-r--r--[-rwxr-xr-x] | lib/impure/rdstdin.nim | 96 |
1 files changed, 54 insertions, 42 deletions
diff --git a/lib/impure/rdstdin.nim b/lib/impure/rdstdin.nim index cf076e929..f4fc26380 100755..100644 --- a/lib/impure/rdstdin.nim +++ b/lib/impure/rdstdin.nim @@ -1,62 +1,74 @@ # # -# Nimrod's Runtime Library -# (c) Copyright 2012 Andreas Rumpf +# Nim's Runtime Library +# (c) Copyright 2015 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. # -## This module contains code for reading from `stdin`:idx:. On UNIX the GNU -## readline library is wrapped and set up to provide default key bindings -## (e.g. you can navigate with the arrow keys). On Windows ``system.readLine`` -## is used. This suffices because Windows' console already provides the +## This module contains code for reading from `stdin`:idx:. On UNIX the +## linenoise library is wrapped and set up to provide default key bindings +## (e.g. you can navigate with the arrow keys). On Windows `system.readLine` +## is used. This suffices because Windows' console already provides the ## wanted functionality. -when defined(Windows): - proc ReadLineFromStdin*(prompt: string): TaintedString {. - tags: [FReadIO, FWriteIO].} = +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 defined(windows): + when defined(nimPreviewSlimSystem): + import std/syncio + + proc readLineFromStdin*(prompt: string): string {. + tags: [ReadIOEffect, WriteIOEffect].} = ## Reads a line from stdin. stdout.write(prompt) + stdout.flushFile() result = readLine(stdin) - proc ReadLineFromStdin*(prompt: string, line: var TaintedString): bool {. - tags: [FReadIO, FWriteIO].} = + 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. + ## `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: - import readline, history - - proc ReadLineFromStdin*(prompt: string): TaintedString {. - tags: [FReadIO, FWriteIO].} = - var buffer = readline.readLine(prompt) - if isNil(buffer): quit(0) - result = TaintedString($buffer) - if result.string.len > 0: - add_history(buffer) - readline.free(buffer) - - proc ReadLineFromStdin*(prompt: string, line: var TaintedString): bool {. - tags: [FReadIO, FWriteIO].} = - var buffer = readline.readLine(prompt) - if isNil(buffer): quit(0) - line = TaintedString($buffer) - if line.string.len > 0: - add_history(buffer) - readline.free(buffer) - # XXX how to determine CTRL+D? - result = true + import std/linenoise - # initialization: - # disable auto-complete: - proc doNothing(a, b: cint): cint {.cdecl, procvar.} = nil - - discard readline.bind_key('\t'.ord, doNothing) + proc readLineFromStdin*(prompt: string, line: var string): bool {. + tags: [ReadIOEffect, WriteIOEffect].} = + var buffer = linenoise.readLine(prompt) + if isNil(buffer): + line.setLen(0) + return false + line = $buffer + if line.len > 0: + historyAdd(buffer) + linenoise.free(buffer) + result = true + proc readLineFromStdin*(prompt: string): string {.inline.} = + if not readLineFromStdin(prompt, result): + raise newException(IOError, "Linenoise returned nil") |