aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkitsunyan2018-04-02 11:54:16 +0000
committerkitsunyan2018-04-02 11:54:16 +0000
commitd3696f547f677453b6a1495556ba236de54cb427 (patch)
tree7392ef6cab562fd84f27621155774e1862a41f51
parent343579d4f32e91440b4b1d0c78bd3e918f74c51a (diff)
Discard input before reading from stdin
-rw-r--r--src/format.nim10
1 files changed, 9 insertions, 1 deletions
diff --git a/src/format.nim b/src/format.nim
index b1659fc..c3a7e5e 100644
--- a/src/format.nim
+++ b/src/format.nim
@@ -1,5 +1,5 @@
import
- future, options, sequtils, strutils, times, unicode,
+ future, options, posix, sequtils, strutils, times, unicode,
utils
type
@@ -52,6 +52,13 @@ proc getWindowSize(): tuple[width: int, height: int] =
else:
(0, 0)
+proc tcflush(fd: cint, queueSelector: cint): cint
+ {.importc, header: "<termios.h>".}
+
+proc discardStream() =
+ if isatty(0) != 0:
+ discard tcflush(0, 0)
+
proc printError*(color: bool, s: string) =
stderr.writeLine(^Color.red, trp"error: ", ^Color.normal, s)
@@ -232,6 +239,7 @@ proc printColonUserInput*(color: bool, s: string,
default
else:
try:
+ discardStream()
stdin.readLine()
except EOFError:
cancel