jal3/terminalUtils/terminalGetInfo.nim

35 lines
819 B
Nim

import terminal
import strutils
import escapeSequences
proc termGetCursorPos*(ouput: File): (int, int) =
# TODO, must only call this at the start of the program
ouput.write(escGetCursorPos)
var response = ""
if getch() != '\e':
raise newException(InvalidResponseError, "Unsupported terminal - can't get cursor position.")
if getch() != '[':
raise newException(InvalidResponseError, "Can't parse response in getCursorPos")
var newChar = getch()
while newChar != ';':
response &= newChar
newChar = getch()
let y = parseInt(response) - 1
response = ""
newChar = getch()
while newChar != 'R':
response &= newChar
newChar = getch()
let x = parseInt(response) - 1
return (x, y)
proc termGetWidth*: int =
terminalWidth()
proc termGetHeight*: int =
terminalHeight()