Terminal: where am I?

Is there a variable or function that can tell me the actual cursor position?

#!/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? 
+4
source share
4 answers

I do not think that you can determine the position of the cursor using termcap .

The termutils manual says:

If you plan to use the relative cursor move commands in an application, you need to know what the position of the original cursor is. To do this, you must monitor the position of the cursor and update records every time something is displayed on the terminal, including graphic symbols.

+3
source

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

+4
source

Some terminals may support position request, like CSI 6 n . If supported, the position will be displayed as CSI Pl;Pc R for instance

 $ echo -e "\e[6n"; xxd ^[[4;1R 0000000: 1b5b 343b 3152 0a .[4;1R. 

This indicates that the cursor is in the first column of the 4th row (counting from 1).

However, this probably should not be relied on, since in reality it is not so many terminals.

+1
source

Printing ESC [6n on 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
source

Source: https://habr.com/ru/post/1339221/


All Articles