终端:我在哪里?
是否有一个变量或函数可以告诉我光标的实际位置?
#!/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 = $termios->getospeed;
my $terminal = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed };
# some movement ...
# at which position (x/y) is the cursor now?
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 = $termios->getospeed;
my $terminal = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed };
# some movement ...
# at which position (x/y) is the cursor now?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
curses
来代替。它有getcurx()
和getcurx()
。有一个 CPAN 模块(以及libcurses-perl
Debian 或 Ubuntu 中的软件包)。You could use
curses
instead. It hasgetcurx()
andgetcurx()
. There is a CPAN module for it (and thelibcurses-perl
package in Debian or Ubuntu).我认为您无法使用
termcap
确定光标位置。termutils 手册 说:
I don't think you can determine the cursor position using
termcap
.The termutils manual says:
部分终端可能支持查询位置,如
CSI 6 n
。如果支持,位置将报告为CSI Pl;Pc R
。例如,这会将光标报告为位于第 4 行的第 1 列(从 1 开始计数)。
然而,这可能不应该依赖,因为实际上支持这一点的终端并不多。
Some terminals may support querying the position, as
CSI 6 n
. If supported, the position will be reported asCSI Pl;Pc R
. For exampleThis 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.
在 ANSI 兼容终端上打印 ESC[6n 将为您提供当前光标位置 ESC[n;mR,其中 n 是行,m 是列,
因此请尝试使用终端转义字符读取它。像这样的东西:
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: