获取垂直光标位置

Bra*_*one 13 shell terminal

这听起来可能很奇怪,但我知道如何在 Bash 中设置垂直光标位置,如下所示:

echo -e "\e[12H"
Run Code Online (Sandbox Code Playgroud)

这会将光标移动到第 12 行(从 1 开始)。

那么如何使用 linux bash 获取光标位置(行号)?如果我可以简单地将此值存储在一个变量中,以便我可以用它进行计算,那将会很有帮助。

编辑:

这是我得到的错误:

$ sh rowcol.sh
-en
    read: 9: Illegal option -d
                              test.sh: 12: Bad substitution
Run Code Online (Sandbox Code Playgroud)

use*_*015 22

使用该-p选项而不是echo我发现解决了脚本中的挂起问题。经测试GNU bash, version 3.00.16(1)-release (x86_64-redhat-linux-gnu)

IFS=';' read -sdR -p $'\E[6n' ROW COL;echo "${ROW#*[}"
Run Code Online (Sandbox Code Playgroud)

以交互方式或在脚本中工作:

#!/usr/bin/env bash
function pos
{
    local CURPOS
    read -sdR -p $'\E[6n' CURPOS
    CURPOS=${CURPOS#*[} # Strip decoration characters <ESC>[
    echo "${CURPOS}"    # Return position in "row;col" format
}
function row
{
    local COL
    local ROW
    IFS=';' read -sdR -p $'\E[6n' ROW COL
    echo "${ROW#*[}"
}
function col
{
    local COL
    local ROW
    IFS=';' read -sdR -p $'\E[6n' ROW COL
    echo "${COL}"
}
tput sc         # Save cursor position
tput cup 5 10   # Move to row 6 col 11
POS1=$(pos)     # Get the cursor position
ROW1=$(row)
COL1=$(col)
tput cup 25 15  # Move to row 25 col 15
POS2=$(pos)     # Get the cursor position
ROW2=$(row)
COL2=$(col)
tput rc # Restore cursor position
echo $BASH_VERSION
echo $POS1 $ROW1 $COL1
echo $POS2 $ROW2 $COL2
Run Code Online (Sandbox Code Playgroud)

输出:

$./cursor.sh
3.00.16(1)-发布
6;11 6 11
26;16 26 16


slm*_*slm 7

我能够使用关于 SO 的同一篇文章中的一些示例,标题为:如何在 bash 中获取光标位置?. 我在这里发布这个只是为了表明它们有效,并且解决方案的内容实际上也在 U&L 上。

Bash 解决方案

从脚本内部

#!/bin/bash
# based on a script from http://invisible-island.net/xterm/xterm.faq.html
exec < /dev/tty
oldstty=$(stty -g)
stty raw -echo min 0
# on my system, the following line can be replaced by the line below it
echo -en "\033[6n" > /dev/tty
# tput u7 > /dev/tty    # when TERM=xterm (and relatives)
IFS=';' read -r -d R -a pos
stty $oldstty
# change from one-based to zero based so they work with: tput cup $row $col
row=$((${pos[0]:2} - 1))    # strip off the esc-[
col=$((${pos[1]} - 1))

echo "(row,col): $row,$col"
Run Code Online (Sandbox Code Playgroud)

注意:我稍微改变了输出!

例子

$ ./rowcol.bash 
(row,col): 43,0
$ clear
$ ./rowcol.bash 
(row,col): 1,0
Run Code Online (Sandbox Code Playgroud)

交互式外壳

此命令链用于获取光标的行和列位置:

$ echo -en "\E[6n";read -sdR CURPOS; CURPOS=${CURPOS#*[};echo "${CURPOS}"
Run Code Online (Sandbox Code Playgroud)

例子

$ echo -en "\E[6n";read -sdR CURPOS; CURPOS=${CURPOS#*[};echo "${CURPOS}"
13;1
$ clear
$ echo -en "\E[6n";read -sdR CURPOS; CURPOS=${CURPOS#*[};echo "${CURPOS}"
2;1
Run Code Online (Sandbox Code Playgroud)

注意:此方法似乎不适用于任何类型的脚本。即使是交互式终端中的简单命令也不适合我。例如:

$ pos=$(echo -en "\E[6n";read -sdR CURPOS; CURPOS=${CURPOS#*[};echo "${CURPOS}")
Run Code Online (Sandbox Code Playgroud)

只是无限期地挂起。

破折号/sh 解决方案

从脚本内部

此解决方案适用于 Ubuntu/Debian 系统,该系统与dashPOSIX 兼容。因此,该read命令不支持-d其他差异之间的切换。

为了解决这个问题,有一个使用 asleep 1代替-d开关的解决方案。这并不理想,但至少提供了一个可行的解决方案。

#!/bin/sh

exec < /dev/tty
oldstty=$(stty -g)
stty raw -echo min 0
tput u7 > /dev/tty
sleep 1
IFS=';' read -r row col
stty $oldstty

row=$(expr $(expr substr $row 3 99) - 1)        # Strip leading escape off
col=$(expr ${col%R} - 1)                        # Strip trailing 'R' off

echo "(row,col): $col,$row"
Run Code Online (Sandbox Code Playgroud)

例子

$ ./rowcol.sh 
(row,col): 0,24
$ clear
$ ./rowcol.sh 
(row,col): 0,1
Run Code Online (Sandbox Code Playgroud)

交互式外壳

我找不到仅适用sh于交互式 shell的可行解决方案。