Saturday 22 September 2018

ssh - change i3wm workspace / windows status to urgent


I have a shell script that I run on a distant server, and I'd like the i3 window status to become urgent so that the workspace button becomes red. How can I achieve that ?



Answer



Many terminals can set the urgent flag on their windows if the bell character - \a - is printed. So


me-runs-long-time.sh ; echo -e '\a'

Should do the trick. Remember that i3 will immediately remove the urgent flag when the window is focused or when it is already focused. So you can test if this works by running


sleep 3; echo -e '\a'

and immediately focusing another window.


Note: The terminal might need to be configured for this behavior:




  • For rxvt-unicode the resource urgentOnBell needs to be set to True. For xterm it is the resource bellIsUrgent. You can set resources in ~/.Xresources or ~/.Xdefaults, depending on your distribution.


    XTerm*bellIsUrgent: True
    URxvt.urgentOnBell: True

    Either run xrdb ~/.Xresources or restart your X session to enable the changes (for new terminals).




  • For termite this can be enabled by setting the urgent_on_bell option to true in its configuration - ~/.config/termite/config:


    [options]
    urgent_on_bell = true

    This will immediately work on any new termite window.






Of course, the above solution depends on you remembering to append ; echo -e '\a' every time. There are a few ways to automate this. For example:




  • An easy way out would be to just output \a at the appropriate time in the script itself. This of course requires write access and would need to be done for every script separately.




  • If you use zsh, you could add the following to your ~/.zshrc (on the remote machine):


    # this may already be in your ~/.zshrc
    autoload -Uz add-zsh-hook
    # duration in seconds after which a bell should be sent
    typeset -i LONGRUNTIME=60
    # function to save time at which a command was started
    save_starttime () {
    starttime=$SECONDS
    }
    # function to print \a if the command took longer than LONGRUNTIME
    set_longrunning_alert () {
    if ((LONGRUNTIME > 0 && SECONDS - starttime >= LONGRUNTIME)); then
    print "\a"
    fi
    }
    # run save_starttime before a command is executed
    add-zsh-hook preexec save_starttime
    # run set_longrunning_alert after a command finishes (before the prompt)
    add-zsh-hook precmd set_longrunning_alert

    This will automatically print a bell character if a command took longer than one minute.




  • If you use bash you could use the following on the remote machine.


    PROMPT_COMMAND='echo -e "\a"'

    This will run echo -e "\a" every time before the primary prompt is issued. While this will mark the window urgent after every command, it should not really be noticable in most cases because will not keep the urgent flag on focused windows.




If you use the settings for bash or zsh also on your local machine, you will also be notified in case of the SSH connection dying (in the case of zsh only if it dies after LONGRUNTIME seconds). Assuming of course that you start ssh from your shell.


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...