I'm trying to write a shell in Ruby, and to implement tab completion I am using the WinAPI function getch
to read in a character at a time from the user, checking for tabs.
The problem with this is the backspace key:
- It moves the cursor further back than the prompt (eg, with prompt
hello>
, the user can backspace the cursor on to theh
. I would like it to stop at the final space. - When the user's text overflows on to the next row of the console, backspace will not move back up to the previous line.
(I know both of these behaviours are by design.)
My imagined solution to these problems involve controlling the cursor movement; I need to know where the cursor is, and be able to move it.
On Linux, I would use ANSI escape sequences, but these aren't supported by the Windows console.
I have looked into the WinAPI and tried to find functions that would let me do this, but all I could find was GetConsoleCursorInfo
function, which only returns the size and visibility of the cursor.
Examples would be appreciated, as I am hopeless at using the Win32API class for anything other than primitive functions.
Thanks.