Skip to content

Commit a897371

Browse files
VindaarAraq
authored andcommitted
move readPasswordFromStdin from rdstdin to terminal (#7266)
1 parent 7740b12 commit a897371

File tree

3 files changed

+54
-45
lines changed

3 files changed

+54
-45
lines changed

changelog.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,7 @@ bar()
262262
263263
import std / [strutils, os, osproc]
264264
import someNimblePackage / [strutils, os]
265-
```
265+
```
266+
267+
- The ``readPasswordFromStdin`` proc has been moved from the ``rdstdin``
268+
to the ``terminal`` module, thus it does not depend on linenoise anymore.

lib/impure/rdstdin.nim

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,6 @@ when defined(Windows):
7373
discard readConsoleInputW(hStdin, irInputRecord, 1, dwEventsRead)
7474
return result
7575

76-
from unicode import toUTF8, Rune, runeLenAt
77-
78-
proc readPasswordFromStdin*(prompt: string, password: var TaintedString):
79-
bool {.tags: [ReadIOEffect, WriteIOEffect].} =
80-
## Reads a `password` from stdin without printing it. `password` must not
81-
## be ``nil``! Returns ``false`` if the end of the file has been reached,
82-
## ``true`` otherwise.
83-
password.setLen(0)
84-
stdout.write(prompt)
85-
while true:
86-
let c = getch()
87-
case c.char
88-
of '\r', chr(0xA):
89-
break
90-
of '\b':
91-
# ensure we delete the whole UTF-8 character:
92-
var i = 0
93-
var x = 1
94-
while i < password.len:
95-
x = runeLenAt(password, i)
96-
inc i, x
97-
password.setLen(max(password.len - x, 0))
98-
else:
99-
password.add(toUTF8(c.Rune))
100-
stdout.write "\n"
101-
10276
else:
10377
import linenoise, termios
10478

@@ -124,21 +98,3 @@ else:
12498
linenoise.free(buffer)
12599
result = true
126100

127-
proc readPasswordFromStdin*(prompt: string, password: var TaintedString):
128-
bool {.tags: [ReadIOEffect, WriteIOEffect].} =
129-
password.setLen(0)
130-
let fd = stdin.getFileHandle()
131-
var cur, old: Termios
132-
discard fd.tcgetattr(cur.addr)
133-
old = cur
134-
cur.c_lflag = cur.c_lflag and not Cflag(ECHO)
135-
discard fd.tcsetattr(TCSADRAIN, cur.addr)
136-
stdout.write prompt
137-
result = stdin.readLine(password)
138-
stdout.write "\n"
139-
discard fd.tcsetattr(TCSADRAIN, old.addr)
140-
141-
proc readPasswordFromStdin*(prompt: string): TaintedString =
142-
## Reads a password from stdin without printing it.
143-
result = TaintedString("")
144-
discard readPasswordFromStdin(prompt, result)

lib/pure/terminal.nim

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,56 @@ proc getch*(): char =
736736
result = stdin.readChar()
737737
discard fd.tcsetattr(TCSADRAIN, addr oldMode)
738738

739+
when defined(windows):
740+
from unicode import toUTF8, Rune, runeLenAt
741+
742+
proc readPasswordFromStdin*(prompt: string, password: var TaintedString):
743+
bool {.tags: [ReadIOEffect, WriteIOEffect].} =
744+
## Reads a `password` from stdin without printing it. `password` must not
745+
## be ``nil``! Returns ``false`` if the end of the file has been reached,
746+
## ``true`` otherwise.
747+
password.string.setLen(0)
748+
stdout.write(prompt)
749+
while true:
750+
let c = getch()
751+
case c.char
752+
of '\r', chr(0xA):
753+
break
754+
of '\b':
755+
# ensure we delete the whole UTF-8 character:
756+
var i = 0
757+
var x = 1
758+
while i < password.len:
759+
x = runeLenAt(password.string, i)
760+
inc i, x
761+
password.string.setLen(max(password.len - x, 0))
762+
else:
763+
password.string.add(toUTF8(c.Rune))
764+
stdout.write "\n"
765+
766+
else:
767+
import termios
768+
769+
proc readPasswordFromStdin*(prompt: string, password: var TaintedString):
770+
bool {.tags: [ReadIOEffect, WriteIOEffect].} =
771+
password.string.setLen(0)
772+
let fd = stdin.getFileHandle()
773+
var cur, old: Termios
774+
discard fd.tcgetattr(cur.addr)
775+
old = cur
776+
cur.c_lflag = cur.c_lflag and not Cflag(ECHO)
777+
discard fd.tcsetattr(TCSADRAIN, cur.addr)
778+
stdout.write prompt
779+
result = stdin.readLine(password)
780+
stdout.write "\n"
781+
discard fd.tcsetattr(TCSADRAIN, old.addr)
782+
783+
proc readPasswordFromStdin*(prompt = "password: "): TaintedString =
784+
## Reads a password from stdin without printing it.
785+
result = TaintedString("")
786+
discard readPasswordFromStdin(prompt, result)
787+
788+
739789
# Wrappers assuming output to stdout:
740790
template hideCursor*() = hideCursor(stdout)
741791
template showCursor*() = showCursor(stdout)

0 commit comments

Comments
 (0)