Remove multi-threaded, interactive input functionality.
It didn't really work when reading from /dev/tty anyways, as the program upstream from slfmt is probably also reading from /dev/tty and has priority.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import std/[atomics, json, options, os, posix, sequtils, setutils, strutils,
|
||||
terminal, termios, times, unicode]
|
||||
terminal, times, unicode]
|
||||
import cliutils, docopt, timeutils, zero_functional
|
||||
|
||||
from std/logging import Level
|
||||
@@ -128,63 +128,12 @@ const EXPECTATION_STYLES = [
|
||||
(ansiEscSeq(fg = cBrightWhite) & ansiEscSeq(bg = (r: 41, g: 84, b:98))) # cyan
|
||||
]
|
||||
|
||||
var
|
||||
controlChannel: Channel[CtrlCmd]
|
||||
stopFlag: Atomic[bool]
|
||||
var stopFlag: Atomic[bool]
|
||||
|
||||
|
||||
proc handleSignal(sig: cint) {.noconv.} = stopFlag.store(true)
|
||||
|
||||
|
||||
proc stripAnsi*(text: string): string =
|
||||
text.replace(FORMATTING_REGEX, "")
|
||||
|
||||
|
||||
proc initializeTTY(tty: File): Termios =
|
||||
let ttyFd = tty.getFileHandle.cint
|
||||
|
||||
var mode: Termios = default(Termios)
|
||||
if ttyFd.tcGetAttr(addr mode) != 0:
|
||||
raise newException(Exception, "couldn't read TTY attributes")
|
||||
|
||||
result = mode
|
||||
|
||||
mode.c_iflag = mode.c_iflag and not Cflag(BRKINT or ICRNL or INPCK or
|
||||
ISTRIP or IXON)
|
||||
mode.c_cflag = (mode.c_cflag and not Cflag(CSIZE or PARENB)) or CS8
|
||||
mode.c_lflag = (mode.c_lflag and not Cflag(ECHO or ICANON or IEXTEN)) or ISIG
|
||||
mode.c_cc[VMIN] = 1.char
|
||||
mode.c_cc[VTIME] = 0.char
|
||||
|
||||
if ttyFd.tcSetAttr(TCSANOW, addr mode) != 0:
|
||||
raise newException(Exception, "couldn't set TTY attributes")
|
||||
|
||||
|
||||
proc restoreTTY(tty: File, ttyMode: var Termios) =
|
||||
discard tcSetAttr(tty.getFileHandle.cint, TCSADRAIN, addr ttyMode)
|
||||
|
||||
|
||||
proc readTTYLoop() {.thread.} =
|
||||
var tty: File
|
||||
var ttyMode: Termios = default(Termios)
|
||||
if not open(tty, "/dev/tty", fmRead) or not isatty(tty):
|
||||
return # bail and don't worry about reading control characters
|
||||
|
||||
try:
|
||||
ttyMode = initializeTTY(tty)
|
||||
while not stopFlag.load():
|
||||
let ch = tty.readChar()
|
||||
|
||||
case ch
|
||||
of '\x12', 'r': controlChannel.send(ccReset) # Ctrl-R
|
||||
of '\x04' : controlChannel.send(ccQuit) # Ctrl-D
|
||||
else: discard
|
||||
|
||||
finally:
|
||||
restoreTTY(tty, ttyMode)
|
||||
close(tty)
|
||||
|
||||
|
||||
func getOrFail(n: JsonNode, key: string): JsonNode =
|
||||
if not n.contains(key):
|
||||
raise newException(ValueError, "missing " & key &
|
||||
@@ -447,8 +396,6 @@ proc eraseAndWriteLine(f: File, s: string) =
|
||||
|
||||
when isMainModule:
|
||||
|
||||
var readerThread: Thread[void]
|
||||
|
||||
try:
|
||||
let args = docopt(USAGE, version = VERSION)
|
||||
|
||||
@@ -467,13 +414,7 @@ when isMainModule:
|
||||
signal(SIGINT, handleSignal)
|
||||
signal(SIGTERM, handleSignal)
|
||||
|
||||
#[
|
||||
open(controlChannel)
|
||||
createThread(readerThread, readTTYLoop)
|
||||
]#
|
||||
|
||||
#stdout.hideCursor
|
||||
#
|
||||
stdout.hideCursor
|
||||
stdout.writeLine("") # burn a line
|
||||
var line: string = ""
|
||||
while stdin.readLine(line) and not stopFlag.load():
|
||||
@@ -515,9 +456,6 @@ when isMainModule:
|
||||
quit(QuitFailure)
|
||||
|
||||
finally:
|
||||
stopFlag.store(true)
|
||||
#close(controlChannel)
|
||||
#joinThreads(readerThread)
|
||||
stdout.writeLine(RESET_FORMATTING)
|
||||
stdout.write(eraseLine(emAll))
|
||||
stdout.showCursor
|
||||
|
||||
Reference in New Issue
Block a user