I have used the 'terminal' module quite a bit now for writing to the console, but there is one thing I can not figure out: How to determine the current cursor position. I can set it anywhere I like, but I can not 'get' where it is at any given time.
I am hoping to have a proc that moves the cursor, writes some data to the screen, then returns it to wherever the cursor was when the proc was called. Is the only way to do this to manually track where it will be on screen myself? Or is there some equivalent to setCursorPos(...) that instead returns the position? Ideally something like getCursorPosX() and getCursorPosY()?
Any ideas are much appreciated :)
It has some limitations at the moment, but you might be interested in this: https://github.com/fowlmouth/nim-termbox
It's available via nimble (our package manager), and can be installed with this command:
$ nimble install termbox
The terminal library doesn't have getCursorPos() set as an exported proc.
Temporary fix is to edit lib/pure/terminal.nim, and add the asterix as follows:
proc getCursorPos*(): tuple [x,y: int] =
Is there anything I need to do after I modify the terminal.nim file? I have added the asterix but get: Error: undeclared identifier: 'getCursorPos' even after the change (and of course importing terminal). That seems like an excellent solution, if I can get it to work.
I will also look into termbox when I have some time, looks interesting.
add something like this
(warning: newbie code, so there is probably a more effficient way to code this)
when not defined(windows): proc getCursorPos(): tuple [x,y: int] = if isatty(stdout): # request cursor pos, and get \\E[y;xR => row=y-1, col=x-1 for 0,0 cord system stdout.write("\\E[6n") var x, y: int = 0 c = getch() # \\E c = getch() # [ c = getch() while c != ';': y = y*10 + (int(c) - int('0')) c = getch() while c != ';': x = x*10 + (int(c) - int('0')) c = getch() return (x,y) return (-1,-1)