Source Edit

This module contains code for reading from stdin. 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.

Example: cmd: -r:off

  1. import std/rdstdin
  2. echo readLineFromStdin("Is Nim awesome? (Y/n): ")
  3. var line: string
  4. while true:
  5. let ok = readLineFromStdin("How are you? ", line)
  6. if not ok: break # ctrl-C or ctrl-D will cause a break
  7. if line.len > 0: echo line
  8. echo "exiting"

Procs

  1. proc readLineFromStdin(prompt: string): string {.
  2. ...tags: [ReadIOEffect, WriteIOEffect], raises: [IOError, EOFError],
  3. forbids: [].}

Reads a line from stdin. Source Edit

  1. proc readLineFromStdin(prompt: string; line: var string): bool {.
  2. ...tags: [ReadIOEffect, WriteIOEffect], raises: [IOError], forbids: [].}

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. Source Edit