开发者

terminal: where am I?

Is there a variable or a function, which can tell me the actual position of the cursor?

#!/usr/bin/env perl
use warnings;
use 5.012;

use Term::ReadKey;
use Term::Cap;
use POSIX;

my( $col, $row ) = GetTerminalSize();

my $termios = new POSIX::Termios;
$termios->getattr;
my $ospeed = $term开发者_StackOverflowios->getospeed;

my $terminal = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed };

# some movement ...

# at which position (x/y) is the cursor now?


You could use curses instead. It has getcurx() and getcurx(). There is a CPAN module for it (and the libcurses-perl package in Debian or Ubuntu).


I don't think you can determine the cursor position using termcap.

The termutils manual says:

If you plan to use the relative cursor motion commands in an application program, you must know what the starting cursor position is. To do this, you must keep track of the cursor position and update the records each time anything is output to the terminal, including graphic characters.


Some terminals may support querying the position, as CSI 6 n. If supported, the position will be reported as CSI Pl;Pc R. For example

$ echo -e "\e[6n"; xxd

^[[4;1R
0000000: 1b5b 343b 3152 0a                      .[4;1R.

This reports the cursor as being at the 1st column of the 4th line (counting from 1).

However, this probably ought not be relied upon, as not very many terminals actually support this.


Printing ESC[6n at ANSI compatible terminals will give you the current cursor position as ESC[n;mR, where n is the row and m is the column

So try reading it with terminal escape characters. Something like that:

perl -e '$/ = "R";' -e 'print "\033[6n";my $x=<STDIN>;my($n, $m)=$x=~m/(\d+)\;(\d+)/;print "Current position: $m, $n\n";'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜