Skip to content
Download example

Simple run dialog with history

⚠ This example is a work in progress, and not well documented

First, hook the usual boilerplate

eval "$(gtk-stream --hook bash)"

Then create the main window.

GTK.send <<EOF
<application application-id="net.gtk-stream.example.hist-run">
  <window id="window" title="Run command" icon-name="applications/system" default-width="400" default-height="200">
    <box id="main-box" orientation="vertical">
      <box><icon icon-name="system-run-symbolic" pixel-size="48" /><label text="Enter a command to run" /></box>
      <entry id="command" placeholder-text="Command" />
      <box>
        <button id="close"><box><icon icon-name="window-close" /><label text="Close" /></box></button>
        <button id="run"><box><icon icon-name="system-run-symbolic" /><label text="Run" /></box></button>
      </box>
    </box>
  </window>
EOF

: ${HISTFILE:=${XDG_CACHE_HOME:-$HOME/.cache}/hist-run.history}
if [ ! -e "$HISTFILE" ]; then touch "$HISTFILE"; fi
COMMANDS=( )
OLD_IFS="$IFS" IFS=$'\n' COMMANDS=( $(< "$HISTFILE") ) IFS="$OLD_IFS"

function load_history() {
    GTK.send '<insert into="main-box"><box-prepend after="command"><dropdown id="history" enable-search="true">'
    for((i=0;i<${#COMMANDS[@]};i++)); do
        GTK.send '<item value="%d"><label text="%s" /></item>' "$i" "${COMMANDS[i]}"
    done
    GTK.send '</dropdown></box-prepend></insert>'
}
load_history

while GTK.receive ev; do
    case "$ev" in
        command:changed:*) command="${ev#command:changed:}";;
        history:selected:*)
            sel="${ev#history:selected:}"
            GTK.send '<set-prop id="command" name="text" value="%s" />' "${COMMANDS[sel]}"
            ;;

        close:clicked) GTK.send '<close-window id="window" />';;

        run:clicked)
            printf "%s\n" "$command" >> "$HISTFILE"
            ( eval "$command" ) &
            GTK.send '<close-window id="window" />'
            ;;
        window:close-request) break;;
    esac
done

GTK.send '</application>'