From 9c8f540053cf9ac3d3f4aa07ea72d001ced17260 Mon Sep 17 00:00:00 2001 From: Charlotte Van Petegem Date: Sun, 17 Jan 2021 11:16:44 +0100 Subject: [PATCH] Extract launcher to separate file using substituteAll --- configurations/sway/launcher.nix | 149 +++++-------------------------- configurations/sway/launcher.zsh | 124 +++++++++++++++++++++++++ 2 files changed, 147 insertions(+), 126 deletions(-) create mode 100644 configurations/sway/launcher.zsh diff --git a/configurations/sway/launcher.nix b/configurations/sway/launcher.nix index 6f041902..821efcd3 100644 --- a/configurations/sway/launcher.nix +++ b/configurations/sway/launcher.nix @@ -7,7 +7,7 @@ let type = "gem"; version = "4.0.0.rc2"; }; - emoji_list = stdenv.mkDerivation { + emojiList = stdenv.mkDerivation { name = "emoji_list"; buildInputs = [ pkgs.ruby gemoji ]; unpackPhase = "true"; @@ -26,130 +26,27 @@ let cp emoji_list.txt $out ''; }; + script = pkgs.substituteAll { + src = ./launcher.zsh; + inherit (pkgs) + fzy + jq + kitty + libqalculate + pass + slurp + sway + tmuxinator + zsh + ; + inherit emojiList; + wfRecorder = pkgs.wf-recorder; + wlClipboard = pkgs.wl-clipboard; + xdgUserDirs = pkgs.xdg-user-dirs; + }; in -pkgs.writeScriptBin "launcher" '' - #!${pkgs.zsh}/bin/zsh - - _sighandler() { - kill -INT "$child" 2>/dev/null - } - - calc_options() { - echo "calc " - } - - calc() { - if [ -n "$1" ] - then - ${pkgs.libqalculate}/bin/qalc "$1" - sleep 5 - else - ${pkgs.libqalculate}/bin/qalc - fi - } - - emoji_options() { - cat ${emoji_list} | sed "s/^/emoji /" - } - - emoji() { - char=$(echo -n "$1" | sed "s/^\([^ ]*\) .*/\1/") - ${pkgs.sway}/bin/swaymsg exec -- "echo -n $char | ${pkgs.wl-clipboard}/bin/wl-copy --foreground" - } - - pass_options(){ - prefix=''${PASSWORD_STORE_DIR-~/.password-store} - password_files=( "$prefix"/**/*.gpg ) - printf 'password %s\n' ''${''${password_files%.gpg}#$prefix/} - printf 'username %s\n' ''${''${password_files%.gpg}#$prefix/} - printf 'otp %s\n' ''${''${password_files%.gpg}#$prefix/} - } - - username() { - swaymsg exec -- "${pkgs.pass}/bin/pass show '$@' | sed -n 's/^Username: *//p' | tr -d '\n' | ${pkgs.wl-clipboard}/bin/wl-copy --foreground" - } - password() { - swaymsg exec -- "${pkgs.pass}/bin/pass show -c0 '$@'" - } - otp() { - swaymsg exec -- "${pkgs.pass}/bin/pass otp -c '$@'" - } - - record_options() { - ${pkgs.sway}/bin/swaymsg -t get_outputs | ${pkgs.jq}/bin/jq -r '.[]["name"]' | sed "s/^/record /" - echo record select - } - - record() { - filename="$(${pkgs.xdg-user-dirs}/bin/xdg-user-dir VIDEOS)/$(date +'screenrecording_%y-%m-%d-%H%M%S.mp4')" - - trap _sighandler SIGINT - if [[ "$1" = "select" ]] - then - ${pkgs.wf-recorder}/bin/wf-recorder -g "$(${pkgs.slurp}/bin/slurp)" -f "$filename" & - else - wf-recorder -o $! -f "$filename" & - fi - child=$! - wait "$child" - # We wait two times, because the first wait exits when the process receives a signal. The process might have finished though, so we ignore errors. - wait "$child" 2>/dev/null - if [ -f "$filename" ] - then - echo "Saved as $filename" - else - echo "Something went wrong while recording" - fi - sleep 5 - } - - run_options() { - print -rl -- ''${(ko)commands} | sed "s/^/run /" - } - - run() { - ${pkgs.sway}/bin/swaymsg exec $1 - } - - ssh_options() { - cat $HOME/.ssh/config | grep "^Host [a-zA-Z]\+" | sed "s/Host /ssh /" - } - - ssh() { - ${pkgs.sway}/bin/swaymsg exec "${pkgs.kitty}/bin/kitty -e ssh $1" - } - - systemctl_options() { - echo systemctl hibernate - echo systemctl poweroff - echo systemctl reboot - echo systemctl suspend - } - - tmuxinator_options() { - ls ~/.config/tmuxinator | sed "s/\.yml$//" | sed "s/^/tmuxinator /" - } - - tmuxinator() { - ${pkgs.sway}/bin/swaymsg exec "${pkgs.kitty}/bin/kitty -e ${pkgs.tmuxinator}/bin/tmuxinator start $1" - } - - windows_options() { - ${pkgs.sway}/bin/swaymsg -t get_tree | ${pkgs.jq}/bin/jq -r 'recurse(.nodes[]?)|recurse(.floating_nodes[]?)|select(.layout=="none")|select(.app_id!="launcher")|select(.type=="con"),select(.type=="floating_con")|(if .app_id then .app_id else .window_properties.class end)+": "+.name+" ("+(.id|tostring)+")"' | sed "s/^/windows /" - } - - windows() { - window=$(echo $@ | sed 's/.* (\([^)]*\))$/\1/') - ${pkgs.sway}/bin/swaymsg \[con_id="$window"\] focus - } - - CHOSEN=$(cat <(windows_options) <(tmuxinator_options) <(ssh_options) <(systemctl_options) <(pass_options) <(run_options) <(record_options) <(calc_options) <(emoji_options) | ${pkgs.fzy}/bin/fzy --lines 36 | tail -n1) - - if [ -n "$CHOSEN" ] - then - PREFIX=$(echo $CHOSEN | sed "s/^\([^ ]*\) .*/\1/g") - WORD=$(echo $CHOSEN | sed "s/^[^ ]* \(.*\)/\1/g") - - $PREFIX $WORD - fi +pkgs.runCommandNoCC "launcher" {} '' + mkdir -p $out/bin + cp ${script} $out/bin/launcher + chmod +x $out/bin/launcher '' diff --git a/configurations/sway/launcher.zsh b/configurations/sway/launcher.zsh new file mode 100644 index 00000000..5820ef15 --- /dev/null +++ b/configurations/sway/launcher.zsh @@ -0,0 +1,124 @@ +#!@zsh@/bin/zsh + +_sighandler() { + kill -INT "$child" 2>/dev/null +} + +calc_options() { + echo "calc " +} + +calc() { + if [ -n "$1" ] + then + @libqalculate@/bin/qalc "$1" + sleep 5 + else + @libqalculate@/bin/qalc + fi +} + +emoji_options() { + cat @emojiList@ | sed "s/^/emoji /" +} + +emoji() { + char=$(echo -n "$1" | sed "s/^\([^ ]*\) .*/\1/") + @sway@/bin/swaymsg exec -- "echo -n $char | @wlClipboard@/bin/wl-copy --foreground" +} + +pass_options(){ + prefix=${PASSWORD_STORE_DIR-~/.password-store} + password_files=( "$prefix"/**/*.gpg ) + printf 'password %s\n' ${${password_files%.gpg}#$prefix/} + printf 'username %s\n' ${${password_files%.gpg}#$prefix/} + printf 'otp %s\n' ${${password_files%.gpg}#$prefix/} +} + +username() { + swaymsg exec -- "@pass@/bin/pass show '$@' | sed -n 's/^Username: *//p' | tr -d '\n' | @wlClipboard@/bin/wl-copy --foreground" +} +password() { + swaymsg exec -- "@pass@/bin/pass show -c0 '$@'" +} +otp() { + swaymsg exec -- "@pass@/bin/pass otp -c '$@'" +} + +record_options() { + @sway@/bin/swaymsg -t get_outputs | @jq@/bin/jq -r '.[]["name"]' | sed "s/^/record /" + echo record select +} + +record() { + filename="$(@xdgUserDirs@/bin/xdg-user-dir VIDEOS)/$(date +'screenrecording_%y-%m-%d-%H%M%S.mp4')" + + trap _sighandler SIGINT + if [[ "$1" = "select" ]] + then + @wfRecorder@/bin/wf-recorder -g "$(@slurp@/bin/slurp)" -f "$filename" & + else + @wfRecorder@/bin/wf-recorder -o $! -f "$filename" & + fi + child=$! + wait "$child" + # We wait two times, because the first wait exits when the process receives a signal. The process might have finished though, so we ignore errors. + wait "$child" 2>/dev/null + if [ -f "$filename" ] + then + echo "Saved as $filename" + else + echo "Something went wrong while recording" + fi + sleep 5 +} + +run_options() { + print -rl -- ''${(ko)commands} | sed "s/^/run /" +} + +run() { + @sway@/bin/swaymsg exec $1 +} + +ssh_options() { + cat $HOME/.ssh/config | grep "^Host [a-zA-Z]\+" | sed "s/Host /ssh /" +} + +ssh() { + @sway@/bin/swaymsg exec "@kitty@/bin/kitty -e ssh $1" +} + +systemctl_options() { + echo systemctl hibernate + echo systemctl poweroff + echo systemctl reboot + echo systemctl suspend +} + +tmuxinator_options() { + ls ~/.config/tmuxinator | sed "s/\.yml$//" | sed "s/^/tmuxinator /" +} + +tmuxinator() { + @sway@/bin/swaymsg exec "@kitty@/bin/kitty -e @tmuxinator@/bin/tmuxinator start $1" +} + +windows_options() { + @sway@/bin/swaymsg -t get_tree | @jq@/bin/jq -r 'recurse(.nodes[]?)|recurse(.floating_nodes[]?)|select(.layout=="none")|select(.app_id!="launcher")|select(.type=="con"),select(.type=="floating_con")|(if .app_id then .app_id else .window_properties.class end)+": "+.name+" ("+(.id|tostring)+")"' | sed "s/^/windows /" +} + +windows() { + window=$(echo $@ | sed 's/.* (\([^)]*\))$/\1/') + @sway@/bin/swaymsg \[con_id="$window"\] focus +} + +CHOSEN=$(cat <(windows_options) <(tmuxinator_options) <(ssh_options) <(systemctl_options) <(pass_options) <(run_options) <(record_options) <(calc_options) <(emoji_options) | @fzy@/bin/fzy --lines 36 | tail -n1) + +if [ -n "$CHOSEN" ] +then + PREFIX=$(echo $CHOSEN | sed "s/^\([^ ]*\) .*/\1/g") + WORD=$(echo $CHOSEN | sed "s/^[^ ]* \(.*\)/\1/g") + + $PREFIX $WORD +fi