From d3696f547f677453b6a1495556ba236de54cb427 Mon Sep 17 00:00:00 2001 From: kitsunyan Date: Mon, 2 Apr 2018 14:54:16 +0300 Subject: Discard input before reading from stdin --- src/format.nim | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src') 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: "".} + +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 -- cgit v1.2.3-70-g09d2