终端:我在哪里?

发布于 2024-10-17 02:55:36 字数 425 浏览 3 评论 0原文

是否有一个变量或函数可以告诉我光标的实际位置?

#!/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一生独一 2024-10-24 02:55:36

您可以使用 curses 来代替。它有 getcurx()getcurx()。有一个 CPAN 模块(以及 libcurses-perl Debian 或 Ubuntu 中的软件包)。

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).

戒ㄋ 2024-10-24 02:55:36

我认为您无法使用 termcap 确定光标位置。

termutils 手册 说:

如果您计划在应用程序中使用相对光标移动命令,您必须知道起始光标位置是什么。为此,您必须跟踪光标位置,并在每次向终端输出任何内容(包括图形字符)时更新记录。

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.

音栖息无 2024-10-24 02:55:36

部分终端可能支持查询位置,如CSI 6 n。如果支持,位置将报告为CSI Pl;Pc R。例如,

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

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

这会将光标报告为位于第 4 行的第 1 列(从 1 开始计数)。

然而,这可能不应该依赖,因为实际上支持这一点的终端并不多。

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.

揽月 2024-10-24 02:55:36

在 ANSI 兼容终端上打印 ESC[6n 将为您提供当前光标位置 ESC[n;mR,其中 n 是行,m 是列,

因此请尝试使用终端转义字符读取它。像这样的东西:

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

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";'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文