【问题标题】:How to capture the title of a terminal window in bash using ANSI escape sequences?如何使用 ANSI 转义序列在 bash 中捕获终端窗口的标题?
【发布时间】:2011-05-27 03:45:26
【问题描述】:

我在 OSX 中使用 bash 命令行。我知道 ANSI 转义序列 \033[21t 将检索当前终端窗口的标题。所以,例如:

$ echo -ne "\033[21t"
...sandbox... 
$ # Where "sandbox" is the title of the current terminal window
$ # and the ... are some extra control characters

我想做的是在脚本中以编程方式捕获这些信息,但我不知道该怎么做。脚本仅捕获原始 ANSI 转义序列的内容。所以,再举个例子,这个小 Ruby 脚本:

cmd = 'echo -ne "\033[21t"'
puts "Output from echo (directly to terminal):"
system(cmd)
terminal_name=`#{cmd}`
puts "\nOutput from echo to variable:"
puts terminal_name.inspect

产生以下输出:

Output from echo (directly to terminal):
^[]lsandbox^[\
Output from echo to variable:
"\e[21t"

我希望第二种情况下的信息与终端上显示的信息相匹配,但我得到的只是原始命令序列。 (我尝试过使用 system() 并将输出捕获到文件中——这也不起作用。)有人知道如何让它工作吗?

【问题讨论】:

    标签: ruby terminal


    【解决方案1】:

    详细here 你必须使用肮脏的技巧才能让它发挥作用。

    这是一个修改后的脚本:

    #!/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[21t" > /dev/tty
    read -r x
    stty $oldstty
    echo $x   
    

    【讨论】:

      猜你喜欢
      • 2020-05-08
      • 1970-01-01
      • 2021-06-30
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      相关资源
      最近更新 更多