summary refs log tree commit diff stats
path: root/lib/impure/rdstdin.nim
blob: a1f0291bf6e396f9f9e417f3ab4e2f41826bcfe7 (plain) (blame)
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
#
#
#            Nimrod's Runtime Library
#        (c) Copyright 2011 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 
## wanted functionality.

when defined(Windows):
  proc ReadLineFromStdin*(prompt: string): string = 
    ## Reads a line from stdin.
    stdout.write(prompt)
    result = readLine(stdin)

else:
  import readline, history
    
  proc ReadLineFromStdin*(prompt: string): string = 
    var buffer = readline.readLine(prompt)
    if isNil(buffer): quit(0)
    result = $buffer
    if result.len > 0:
      add_history(buffer)
    readline.free(buffer)

  # initialization:
  # disable auto-complete: 
  discard readline.bind_key('\t'.ord, readline.abort)