Control Konsole toolbars from commandline

Hey y’all,

I’ve been trying to script Konsole, to moderate success. With dbus-send I’ve been able to get/set the profile, fullscreen status, and menubar. The one thing I’ve been unable to figure out how to do is toggle the various toolbars. I don’t see anything for it in qdbusviewer, it doesn’t seem to be associated with profiles so there’s nothing I can do from that angle, and it’s not even a flag I can pass to konsole when starting up (whereas something like --show-menubar does exist). I see no way to even interact with the toolbars outside of the GUI buttons.

The documentation on “Scripting Konsole” is very sparse, but it’s gotten me this far.

And for those why are curious why I’m doing this:
I like having the toolbars and whatnot normally, but when working on projects/editing files in neovim, I’d rather have it clean. That means fullscreen without any bells or whistles, and in a different font. I’ve grown tired of changing profiles, hiding all the bars, and hitting F11 every time I want to work on a project (and then undoing it all when I’m done), so I made my nvim alias n perform this setup/cleanup for me. It’s all working except the toolbars, which was the most tedious part in the first place. You can see what I’ve currently done in the code block at the end of this post. (…I woulda just linked to GitHub but it won’t let me, probably because I’m a new user).

Any thoughts?

Thanks,
-Zman

bash_cmds.sh
#!/bin/bash
# This file is intended to provide some QoL commands for working with Neovim in the Konsole terminal emulator
# Namely, it provides an alias `n` which starts up Neovim is the correct profile with the correct visual esttings, and then resets them to previous values upon exit
# (May also setup tmux in a future version, although not currently implemented)

# It also provides an `nvim-update` command, which updates my Neovim installation since I am running the nightly build


# HELPER FUNCTIONS (can be used as standalone commands in Konsole)
# Pass true/false to these functions to toggle their respective properties. Passing the `-o` flag will return the old value of the property

function profile()
{
    local OPTIND
    while getopts 'o' flag; do
        case "${flag}" in
            o)  dbus-send --dest=$KONSOLE_DBUS_SERVICE $KONSOLE_DBUS_SESSION --print-reply=literal --type=method_call org.kde.konsole.Session.profile | xargs ;;
            ?)  printf "Usage: %s: [-o] true|false\n" $0
                return 2 ;;
        esac
    done
    shift $(($OPTIND - 1))
    dbus-send --dest=$KONSOLE_DBUS_SERVICE $KONSOLE_DBUS_SESSION --type=method_call org.kde.konsole.Session.setProfile string:"$1"
}

function fullscreen()
{
    local OPTIND
    while getopts 'o' flag; do
        case "${flag}" in
            o)  local status=($(dbus-send --dest=$KONSOLE_DBUS_SERVICE /konsole/MainWindow_1 --print-reply=literal --type=method_call org.freedesktop.DBus.Properties.Get string:org.qtproject.Qt.QWidget string:fullScreen | xargs))
                echo ${status[2]} ;;
            ?)  printf "Usage: %s: [-o] true|false\n" $0
                return 2 ;;
        esac
    done
    shift $(($OPTIND - 1))
    dbus-send --dest=$KONSOLE_DBUS_SERVICE /konsole/MainWindow_1 --type=method_call org.kde.konsole.Konsole.MainWindow.viewFullScreen boolean:"$1"
}

# As a side-effect of how this is implemented, passing in something other than "true" or "false" causes the menubar to toggle states
# This includes calling `menubar` with no arguments (or just `menubar -o`)
# All other helper functions in this file simply leave the property unchanged
function menubar()
{
    local OPTIND
    local status=($(dbus-send --dest=$KONSOLE_DBUS_SERVICE /konsole/MainWindow_1/actions/options_show_menubar --print-reply=literal --type=method_call org.freedesktop.DBus.Properties.Get string:org.qtproject.Qt.QAction string:checked | xargs))
    while getopts 'o' flag; do
        case "${flag}" in
            o)  echo ${status[2]} ;;
            ?)  printf "Usage: %s: [-o] true|false\n" $0
                return 2 ;;
        esac
    done
    shift $(($OPTIND - 1))
    if [ "$1" != "${status[2]}" ]; then
        dbus-send --dest=$KONSOLE_DBUS_SERVICE /konsole/MainWindow_1/actions/options_show_menubar --type=method_call org.qtproject.Qt.QAction.trigger
    fi
}


# MAIN COMMANDS

# By default the command puts the terminal into fullscreen. Pass `-f` to disable this functionality
# NOTE: This does mess with flags intended to be passed to Neovim itself, so just run `nvim` if you need to use flags
function n()
{
    local disable_fullscreen_flag=''
    local OPTIND
    while getopts 'f' flag; do
        case "${flag}" in
            f)  disable_fullscreen_flag=1 ;;
            ?)  printf "Usage: %s: [-f] [nvim_args]\n" $0
                return 2 ;;
        esac
    done
    shift $(($OPTIND - 1))

    local prof=$(profile -o nvim)
    if [ -z "$disable_fullscreen_flag" ]; then
        local full=$(fullscreen -o true)
        local menu=$(menubar -o false)
    fi

    if [ ! -z "$*" ]; then
        nvim $*
    else
        nvim .
    fi

    profile "$prof"
    if [ -z "$disable_fullscreen_flag" ]; then
        fullscreen "$full"
        menubar "$menu"
    fi
}

function nvim-update()
{
    curl -LO https://github.com/neovim/neovim/releases/download/nightly/nvim-linux64.tar.gz
    sudo rm -rf /usr/local/bin/nvim-linux64
    sudo tar -C /usr/local/bin -xzvf nvim-linux64.tar.gz
    rm nvim-linux64.tar.gz
}
2 Likes

I decided to give it a whirl myself, and have now submitted a merge request on the subject (still can’t link to it). Hopefully anyone who stumbles onto this in the future will be able to use the solution without issue.