Add a new line after readPasswordFromStdin, add result
This commit is contained in:
parent
18fb3a391c
commit
d6d152e451
1 changed files with 11 additions and 5 deletions
|
|
@ -33,9 +33,11 @@ when defined(Windows):
|
||||||
stdout.write(prompt)
|
stdout.write(prompt)
|
||||||
result = readLine(stdin, line)
|
result = readLine(stdin, line)
|
||||||
|
|
||||||
proc readPasswordFromStdin*(prompt: string, password: var TaintedString) =
|
proc readPasswordFromStdin*(prompt: string, password: var TaintedString):
|
||||||
|
bool {.tags: [ReadIOEffect, WriteIOEffect].} =
|
||||||
## Reads a `password` from stdin without printing it. `password` must not
|
## Reads a `password` from stdin without printing it. `password` must not
|
||||||
## be ``nil``!
|
## be ``nil``! Returns ``false`` if the end of the file has been reached,
|
||||||
|
## ``true`` otherwise.
|
||||||
proc getch(): cint {.header: "<conio.h>", importc: "_getch".}
|
proc getch(): cint {.header: "<conio.h>", importc: "_getch".}
|
||||||
|
|
||||||
password.setLen(0)
|
password.setLen(0)
|
||||||
|
|
@ -50,6 +52,8 @@ when defined(Windows):
|
||||||
password.setLen(password.len - 1)
|
password.setLen(password.len - 1)
|
||||||
else:
|
else:
|
||||||
password.add(c)
|
password.add(c)
|
||||||
|
stdout.write "\n"
|
||||||
|
# TODO: How to detect EOF on Windows?
|
||||||
|
|
||||||
else:
|
else:
|
||||||
import readline, history, termios, unsigned
|
import readline, history, termios, unsigned
|
||||||
|
|
@ -80,7 +84,8 @@ else:
|
||||||
|
|
||||||
discard readline.bind_key('\t'.ord, doNothing)
|
discard readline.bind_key('\t'.ord, doNothing)
|
||||||
|
|
||||||
proc readPasswordFromStdin*(prompt: string, password: var TaintedString) =
|
proc readPasswordFromStdin*(prompt: string, password: var TaintedString):
|
||||||
|
bool {.tags: [ReadIOEffect, WriteIOEffect].} =
|
||||||
password.setLen(0)
|
password.setLen(0)
|
||||||
let fd = stdin.getFileHandle()
|
let fd = stdin.getFileHandle()
|
||||||
var cur, old: Termios
|
var cur, old: Termios
|
||||||
|
|
@ -89,10 +94,11 @@ else:
|
||||||
cur.lflag = cur.lflag and not Tcflag(ECHO)
|
cur.lflag = cur.lflag and not Tcflag(ECHO)
|
||||||
discard fd.tcsetattr(TCSADRAIN, cur.addr)
|
discard fd.tcsetattr(TCSADRAIN, cur.addr)
|
||||||
stdout.write prompt
|
stdout.write prompt
|
||||||
discard stdin.readLine(password)
|
result = stdin.readLine(password)
|
||||||
|
stdout.write "\n"
|
||||||
discard fd.tcsetattr(TCSADRAIN, old.addr)
|
discard fd.tcsetattr(TCSADRAIN, old.addr)
|
||||||
|
|
||||||
proc readPasswordFromStdin*(prompt: string): TaintedString =
|
proc readPasswordFromStdin*(prompt: string): TaintedString =
|
||||||
## Reads a password from stdin without printing it.
|
## Reads a password from stdin without printing it.
|
||||||
result = TaintedString("")
|
result = TaintedString("")
|
||||||
readPasswordFromStdin(prompt, result)
|
discard readPasswordFromStdin(prompt, result)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue