gnome 终端_将GNOME终端变为弹出终端

gnome 终端

A pop-up terminal is great and handy on Linux and similar OS. On KDE, Yakuake is great. On Gnome or GTK, I ever tried Guake. It is quite good. However, it has not been as mature, stable and figure-rich as gnome-terminal. One day, I got this idea: why not using a script/program to manage the gnome-terminal and take action upon hotkey hits? The top-down effect is non-important to me. What I want is simple: use a hotkey (like F12) to activate/deactivate the terminal.

Linux和类似的OS上,弹出式终端非常方便。 在KDE上,Yakuake很棒。 在Gnome或GTK上,我曾经尝试过Guake 。 很好 但是,它还没有像gnome-terminal那样成熟,稳定和富于图形。 有一天,我有了一个主意:为什么不使用脚本/程序来管理gnome终端并在热键击中时采取措施? 自上而下的效果对我而言并不重要。 我想要的很简单:使用热键(如F12)激活/停用终端。

gnome-terminal.png

It turns out the Linux software have sufficient tools for this: bash for scripting, Gnome/Cinnamon native keyboard tools to catch hotkey (F12 is my favorite for this) and xdotool for checking/searching/activating/minimizing X windows. What I need to do is to write the bash script to connect them together.

事实证明Linux 软件为此提供了足够的工具:用于脚本编写的bash,用于捕获热键的Gnome / Cinnamon本地键盘工具(我最喜欢F12)和用于检查/搜索/激活/最小化X窗口的xdotool 。 我需要做的是编写bash脚本将它们连接在一起。

For those who are in hurry to try: install the xdotool, download the script at here, map a hotkey in your desktop environment to invoke this script and just hit that key.

对于那些在急于尝试 :安装xdotool,在下载脚本在这里 ,在你的桌面环境来调用这个脚本映射热键,只是打的关键。

gnome-terminal-quake-like-shortcut.png

In the remaining part, I will introduce the design of the script and why it is like this.

在剩下的部分中,我将介绍脚本的设计以及为什么会这样。

The script is invoked multiple times. Each time it is invoked, it may 1) Check whether the terminal is opened. If not, open the terminal. 2) Check whether the terminal is currently activated. If it is, minimize it to hide it. Otherwise, activate the terminal.

该脚本被多次调用。 每次调用它时,可能是:1)检查终端是否打开。 如果没有,请打开终端。 2)检查终端当前是否处于激活状态。 如果是这样,请将其最小化以隐藏它。 否则,激活终端。

The first problem is how to track the state? The state is the ID of the terminal managed by this script. And the state should be persistent across different runs of the script. The solution is to save it into a file. Causing disk I/O each time we activate/deactivate the terminal is not a good choice. Hence, I use the ramdisk /dev/shm/. The “$USER” prefix is to avoid multiple users conflict with each other.

第一个问题是如何跟踪状态? 状态是此脚本管理的终端的ID。 并且状态应该在脚本的不同运行之间保持不变。 解决方案是将其保存到文件中。 每次我们激活/停用终端都会导致磁盘I / O并不是一个好的选择。 因此,我使用ramdisk /dev/shm/ 。 “ $ USER”前缀是为了避免多个用户相互冲突。

state_file="/dev/shm/actiavte-termianl.term.$USER"
...
    # save the state
    echo "$term" >$state_file
...
# read the state
term=$(cat $state_file)

The next problem: what if the state file exists while the terminal is closed? The solution is to heck whether it exists and create it if it does not exist:

下一个问题:如果终端关闭时状态文件存在,该怎么办? 解决方案是检查它是否存在,如果不存在则创建它:

# check whether it exists
term_exists "$term"
exists=$?
if [[ "$exists" != "0" ]]; then
    create_terminal
    exit 0
fi

The term_exists and create_terminal is tricky at several parts. I leave the readers to figure it out by yourselves.

term_existscreate_terminal在几部分上很棘手。 我让读者自己弄清楚。

term_exists () {
    allterms=`xdotool search --class "$termtype"`
    for e in $allterms; do [[ "$e" == "$1" ]] && return 0; done
    return 1
}

create_terminal () {
    echo "Create new terminal"
    gnome-terminal --maximize &

    exists=1
    wait_cnt=0
    until [ "$exists" == "0" ]; do
        # sleep a while
        sleep $wait_sec

        # Note that the focus window may be from a terminal that
        # already exists; the makes create_terminal choose the existing terminal
        # Making the wait_sec large enough so that the terminal can be created and
        # displayed can make the false choosing more unlikely.

        term=$(xdotool getwindowfocus)
        term_exists "$term"
        exists=$?
        # avoid infinite wait
        let wait_cnt=wait_cnt+1
        if [ $wait_cnt -gt $max_wait_cnt ]; then
            echo "Wait for too long. Give up."
            term=""
            exists=0
        fi
    done

    echo "Created terminal window $term"
    # save the state
    echo "$term" >$state_file
}

The create_terminal may possibly fail by choosing another terminal that it is not created by this script (why?). In practice, I never find this since I usually use one terminal with many tabs. Please comment if you have a better solution.

通过选择该脚本未创建的另一个终端, create_terminal可能会失败(为什么?)。 实际上,我从来没有发现这一点,因为我通常使用一个带有许多选项卡的终端。 如果您有更好的解决方案,请发表评论。

Now, we ensured that there is a terminal created by the script exists. The last step is to check whether it is activated and activate/deactivate it accordingly using xdotool.

现在,我们确保该脚本创建的终端存在。 最后一步是检查它是否已激活,并使用xdotool相应地激活/停用它。

# check whether it is already activated
curwin=$(xdotool getwindowfocus)

if [ "$term" == "$curwin" ]; then
    # deactivate (minimize) the terminal if it is currently activated
    xdotool windowminimize $curwin
else
    # activate the terminal if it is not currently activated
    xdotool windowactivate $term
fi

翻译自: https://www.systutorials.com/turning-gnome-terminal-to-a-pop-up-terminal/

gnome 终端

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值