current cursor position
Loïc Grenié
loic.grenie at gmail.com
Mon Jul 19 10:10:44 UTC 2010
2010/7/19 Christopher Barry <christopher.barry at rackwareinc.com>:
> On Sun, 2010-07-18 at 21:55 +0200, Denys Vlasenko wrote:
>> On Saturday 17 July 2010 19:12, Christopher Barry wrote:
>> > All,
>> >
>> > looking for an easy way to capture the current cursor position. found a
>> > reference to 'ESC [ 6 n', but not sure how that works. I tried
>> > # echo -e '\e[6n'
>> > but that does not do what I expect.
>>
>> It makes terminal talk:
>>
>> shadow:~# echo -ne '\e[6n'; hexdump -vC
>> ^[[6;1R (here I press ^D^D to let hexdump see EOF) 00000000 1b 5b 36 3b 31 52 |.[6;1R|
>> 00000006
>>
>
> Denys, I have no idea what the heck that means! :)
When you echo 'ESC [ 6 n' the terminal puts
ESC [ <row> ; <col> R
on the standard input (where <row> and <col> are numbers, as
above). If you think about it, that's the only way it can provide
this information (remember that the terminal could be a text
terminal at the end of a serial cable). It is not extremely convenient,
but you can devise a kind of get_cursor_position program which
would look like (in C):
main()
{
int row, col;
fprintf(stderr, "^[[6n");
scanf("^[[%d;%dR",&row,&col);
printf("%d:%d\n",row,col);
exit(0);
}
(you have to clean it up, add some #include, change the last
printf to suit your need, etc).
> I was actually looking for something to put in a script, kinda like:
>
> cp=$(get_cursor_position)
> [[ ${cp} -gt 0 ]] && echo
>
> so my logger would not be writing at the end of an unrelated line.
Denys is probably right, it would be better if programs ouputted
only full lines.
Remember that there is no warranty that you are the only program
to write to the terminal: 1) a program could mess the ouput between
"get_cursor_position" and your output and 2) another program could
write *together* with yours.
Hope this helps,
Loïc
More information about the busybox
mailing list