I would like to offer the following code ,which I modified from a previous post
a few years ago my apologies to the original poster). This is far from elegant
or efficient, but it does the job.
import terminal
type Cursor* = tuple[x,y: int] # for the getcursorpos procedure
var cursor*: Cursor # for the getcursorpos procedure
proc getcursorpos*: Cursor =
> # This version returns the x,y values of the cursor position, erases the
>
> # side-effect text ";xR", and brings the cursor back to its proper position.
>
> stdout.write "e[6n" # get cursor coordinates discard getch() # ignore e
> discard getch() # ignore [ var c = getch() stdout.write "e[y;xR" # read y,x
> values of cursor position
>
> while c != ';':
> result.y = result.y * 10 + (int(c) - int('0')) c = getch()
>
> c = getch()
>
> while c != 'R':
> result.x = result.x * 10 + (int(c) - int('0')) c = getch()
>
> result.x -= 1 result.y -= 1 setcursorpos(result.x, result.y) # place cursor
> at proper position stdout.write " " # overwrite side-effect text ";xR"
> setcursorpos(result.x, result.y) # return cursor to proper position