I have a program that outputs continuously to the terminal (think cmatrix) and I need to immediately catch terminal resizes to reset the output, but cant figure out how to do it properly. I need something to the effect of:
while true:
if terminalWasResized():
break
synchronousProc()
I have tried making terminalWasResized async but it ends up blocking and I can't make the synchronous proc async because it won't compile (memory safety warnings). The synchronous proc also takes some time to complete so I can't wait on it before checking terminal size again.
Any ideas?
Never heard of SIGWINCH before, but it seems to work at least in my URxvt terminal:
import posix
type
StdSignal* = enum
SIGHUP = 1 ## 1. Hangup
SIGINT ## 2. Terminal interrupt signal
# https://gist.github.com/dom96/908782
template onSignal*(sigs: varargs[StdSignal]; actions: untyped): untyped =
# https://www.gnu.org/software/libc/manual/html_node/Sigaction-Function-Example.html#Sigaction-Function-Example
proc newHandler(signum: cint) {.noconv.} =
echo "\n[Error] Standard signal " & $StdSignal(signum) & " detected"
`actions`
var
newAction: SigAction
newAction.sa_handler = newHandler
discard sigemptyset(newAction.sa_mask)
newAction.sa_flags = 0
for sig in sigs:
discard sigaction(cint(sig), newAction, nil)
when isMainModule:
onSignal(SIGFPE, SIGINT):
quit()
echo "Hit Ctrl-C to end this program .."
discard pause()
change
/etc/X11/app-defaults/XTerm
*allowWindowOps: true
*eightBitInput: false
with escape codes
example in https://github.com/AS400JPLPC/nim_termkey/blob/master/termkey.nim