From 5c738fdc77a8b811624d29698fa099b0050de305 Mon Sep 17 00:00:00 2001 From: cronyakatsuki <64900606+cronyakatsuki@users.noreply.github.com> Date: Fri, 4 Nov 2022 14:54:08 +0100 Subject: [PATCH] Made scripts more readable by moving most logic to functions. --- dmenu-configs | 23 ++++++++++++------- dmenu-games | 51 ++++++++++++++++++++++++++--------------- dmenu-kill | 22 ++++++++++++------ dmenu-link-handler | 28 ++++++++++++++++------- dmenu-playerctl | 48 +++++++++++++++------------------------ dmenu-power-menu | 22 +++++++++++++----- dmenu-runner | 14 +++++++++--- dmenu-ryzenadj | 56 ++++++++++++++++++++++++++++++---------------- dmenu-screenshot | 25 +++++++++++++++------ dmenu-transmission | 54 ++++++++++++++++++++++++++------------------ dmenu-usb-man | 24 +++++++++++++++----- 11 files changed, 234 insertions(+), 133 deletions(-) diff --git a/dmenu-configs b/dmenu-configs index 5ef0073..b19a1aa 100755 --- a/dmenu-configs +++ b/dmenu-configs @@ -1,9 +1,6 @@ #!/bin/env bash # a script to quickly open up one of my many configs - -source $HOME/.config/dmenu/config - editor="$TERMINAL -e $EDITOR" declare -a configs=( @@ -29,11 +26,21 @@ declare -a configs=( "xinitrc - $HOME/.config/X11/xinitrc" ) -choice=$(printf '%s\n' "${configs[@]}" | $DMENU -l 10 -p 'Edit config:') +load_config () { + source $HOME/.config/dmenu/config +} -[ -z "$choice" ] && exit +main () { + load_config -[ "$choice" = "Quit" ] && exit + choice=$(printf '%s\n' "${configs[@]}" | $DMENU -l 10 -p 'Edit config:') -cfg=$(printf '%s\n' "${choice}" | awk '{print $NF}') -$editor "$cfg" + [ -z "$choice" ] && exit + + [ "$choice" = "Quit" ] && exit + + cfg=$(printf '%s\n' "${choice}" | awk '{print $NF}') + $editor "$cfg" +} + +main $@ diff --git a/dmenu-games b/dmenu-games index 6ba7dbe..33dcb68 100755 --- a/dmenu-games +++ b/dmenu-games @@ -1,8 +1,9 @@ #!/bin/sh # dmenu script to open up my games - -. $HOME/.config/dmenu/config +load_config () { + . $HOME/.config/dmenu/config +} get_category () { category=$( game-run list | $DMENU -p "Choose game category:") @@ -10,25 +11,39 @@ get_category () { [ -z "$category" ] && exit 0 } -get_category +menu () { + while [ -z "$game" ] + do -while [ -z "$game" ] -do + if [ -z "$1" ]; then + game=$(game-run list "$category" | sed '/-18+/d' | $DMENU -p "Choose game to run:") + elif [ "$1" = "-a" ]; then + game=$(game-run list "$category" | $DMENU -p "Choose game to run:") + else + printf '%s\n' "Option $1 doesn't exit!!" + exit 1 + fi - if [ -z "$1" ]; then - game=$(game-run list "$category" | sed '/-18+/d' | $DMENU -p "Choose game to run:") - elif [ "$1" = "-a" ]; then - game=$(game-run list "$category" | $DMENU -p "Choose game to run:") + [ -z "$game" ] && get_category + done +} + +launch_game () { + if game-run launch "$game"; then + quick-notify "Game run" "Launching $game" else - printf '%s\n' "Option $1 doesn't exit!!" - exit 1 + quick-notify "Game run" "Failed to launch $game" fi +} - [ -z "$game" ] && get_category -done +main () { + load_config -if game-run launch "$game"; then - quick-notify "Game run" "Launching $game" -else - quick-notify "Game run" "Failed to launch $game" -fi + get_category + + menu + + launch_game +} + +main $@ diff --git a/dmenu-kill b/dmenu-kill index 15c6a53..0eb28e0 100755 --- a/dmenu-kill +++ b/dmenu-kill @@ -2,16 +2,24 @@ # kill those pesky proceses -. $HOME/.config/dmenu/config +load_config () { + . $HOME/.config/dmenu/config +} -pid=$(ps -u $USER -o pid,%mem,%cpu,comm,cmd | sort -b -k3 -r | $DMENU -l 15 -i -p "Choose procces to kill: " | awk '{print $1}') +main () { + load_config -[ -n "$pid" ] || exit + pid=$(ps -u $USER -o pid,%mem,%cpu,comm,cmd | sort -b -k3 -r | $DMENU -l 15 -i -p "Choose procces to kill: " | awk '{print $1}') -answer=$(printf "yes\\nno" | $DMENU -p "Are you sure?") + [ -n "$pid" ] || exit -[ $answer = "yes" ] || exit + answer=$(printf "yes\\nno" | $DMENU -p "Are you sure?") -kill -15 $pid 2>/dev/null + [ $answer = "yes" ] || exit -quick-notify "Killed Procces" "$pid" + kill -15 $pid 2>/dev/null + + quick-notify "Killed Procces" "$pid" +} + +main $@ diff --git a/dmenu-link-handler b/dmenu-link-handler index d3a2db2..bb36723 100755 --- a/dmenu-link-handler +++ b/dmenu-link-handler @@ -3,12 +3,24 @@ # Feed this script a link or it will get it from your clipboard and it will give dmenu # with some choice of programs to open the link with. -. $HOME/.config/dmenu/config +load_config () { + . $HOME/.config/dmenu/config +} -case "$(printf "mpv\\nbrowser\\ncopy url [X11]\\ncopy url [Wayland]\\nw3m" | $DMENU -p "Open link with what program?")" in - mpv) "$VIDEO" "$@" ;; - browser) "$BROWSER" "$@" > /dev/null;; - "copy url [X11]") echo "$@" | xclip -selection clipboard ;; - "copy url [Wayland]") echo "$@" | wl-copy -n ;; - w3m) readable "$@" | w3m -T text/html ;; -esac +menu () { + case "$(printf "mpv\\nbrowser\\ncopy url [X11]\\ncopy url [Wayland]\\nw3m" | $DMENU -p "Open link with what program?")" in + mpv) "$VIDEO" "$@" ;; + browser) "$BROWSER" "$@" > /dev/null;; + "copy url [X11]") echo "$@" | xclip -selection clipboard ;; + "copy url [Wayland]") echo "$@" | wl-copy -n ;; + w3m) readable "$@" | w3m -T text/html ;; + esac +} + +main () { + load_config + + menu $@ +} + +main $@ diff --git a/dmenu-playerctl b/dmenu-playerctl index 288d0a0..5415279 100755 --- a/dmenu-playerctl +++ b/dmenu-playerctl @@ -1,40 +1,28 @@ -#!/bin/bash +#!/bin/env bash # script to run a menu prompt when having more than 2 players since # playerctl is bad at managing more than one player at the same time -source $HOME/.config/dmenu/config +load_config () { + source $HOME/.config/dmenu/config +} -command="$@" -[ -z "$command" ] && exit -instances=$(playerctl -l | wc -l) +main () { + load_config -slock=$(ps -C slock | sed -n '1!p' | awk '{ print $4 }' | sed 's/slock/running/') + command="$@" + [ -z "$command" ] && exit + instances=$(playerctl -l | wc -l) -if [ "$instances" = "0" ]; then - exit -elif [ "$instances" -lt "2" ]; then - playerctl $command -elif [ "$slock" = "running" ]; then - if [ "$command" = "play-pause" ]; then - if playerctl status -a | grep -i "Playing" > /dev/null; then - playerctl pause -a - else - playerctl -p mpd $command - fi + if [ "$instances" = "0" ]; then exit + elif [ "$instances" -lt "2" ]; then + playerctl $command + else + choice=$(playerctl -l | $DMENU -p 'Manage:') + [ -z "$choice" ] && exit + playerctl -p $choice $command fi +} - players=$(paste <(playerctl -l) <(playerctl status -a) | grep -v -i mpd | awk '{print $1}') - if [ ! -z "$players" ]; then - for player in $players; do - playerctl -p $player pause - done - fi - - playerctl -p mpd $command -else - choice=$(playerctl -l | $DMENU -p 'Manage:') - [ -z "$choice" ] && exit - playerctl -p $choice $command -fi +main $@ diff --git a/dmenu-power-menu b/dmenu-power-menu index 5b8ccab..ed67ada 100755 --- a/dmenu-power-menu +++ b/dmenu-power-menu @@ -2,9 +2,21 @@ # simple power menu script -. $HOME/.config/dmenu/config +load_config () { + . $HOME/.config/dmenu/config +} -case "$(printf "shutdown\\nreboot" | $DMENU -p "Choose your poison")" in - "shutdown") poweroff ;; - "reboot") reboot ;; -esac +menu () { + case "$(printf "shutdown\\nreboot" | $DMENU -p "Choose your poison")" in + "shutdown") poweroff ;; + "reboot") reboot ;; + esac +} + +main () { + load_config + + menu +} + +main $@ diff --git a/dmenu-runner b/dmenu-runner index b589813..514766f 100755 --- a/dmenu-runner +++ b/dmenu-runner @@ -1,7 +1,15 @@ #!/bin/sh -. $HOME/.config/dmenu/config +load_config () { + . $HOME/.config/dmenu/config +} -choice=$(dmenu_path | grep dmenu- | $DMENU -p "Choose script to run:") +main () { + load_config -$choice + choice=$(dmenu_path | grep dmenu- | $DMENU -p "Choose script to run:") + + $choice +} + +main $@ diff --git a/dmenu-ryzenadj b/dmenu-ryzenadj index 5fc863f..8477056 100755 --- a/dmenu-ryzenadj +++ b/dmenu-ryzenadj @@ -1,8 +1,9 @@ #!/bin/sh # dmenu script for quickly setting ryzenadj profiles with my other script easily - -. $HOME/.config/dmenu/config +load_config () { + . $HOME/.config/dmenu/config +} get_category () { category=$( sudo ryzenset list | $DMENU -p "Choose profile category:") @@ -10,26 +11,43 @@ get_category () { [ -z "$category" ] && exit 0 } -get_category +get_current_profile () { + current_temp=$(sudo get-tctl-limit) -current_temp=$(sudo get-tctl-limit) + [ "$current_temp" = "60" ] && current_mode="normal mode" + [ "$current_temp" = "65" ] && current_mode="light gaming" + [ "$current_temp" = "70" ] && current_mode="heavy gaming" + [ "$current_temp" = "75" ] && current_mode="heavy programs" + [ "$current_temp" = "85" ] && current_mode="heavy gaming pro" +} -[ "$current_temp" = "60" ] && current_mode="normal mode" -[ "$current_temp" = "65" ] && current_mode="light gaming" -[ "$current_temp" = "70" ] && current_mode="heavy gaming" -[ "$current_temp" = "75" ] && current_mode="heavy programs" -[ "$current_temp" = "85" ] && current_mode="heavy gaming pro" +menu () { + while [ -z "$profile" ] + do + profile=$(sudo ryzenset list $category | $DMENU -p "Current: $current_mode:") -while [ -z "$profile" ] -do + [ -z "$profile" ] && get_category + done +} - profile=$(sudo ryzenset list $category | $DMENU -p "Current: $current_mode:") +launch_profile () { + if sudo ryzenset set "$profile"; then + quick-notify "Ryzenset" "Setting $profile" + else + quick-notify "Ryzenset" "Failed to set $game" + fi +} - [ -z "$profile" ] && get_category -done +main () { + load_config -if sudo ryzenset set "$profile"; then - quick-notify "Ryzenset" "Setting $profile" -else - quick-notify "Ryzenset" "Failed to set $game" -fi + get_category + + get_current_profile + + menu + + launch_profile +} + +main $@ diff --git a/dmenu-screenshot b/dmenu-screenshot index c59ebc8..c0d74ae 100755 --- a/dmenu-screenshot +++ b/dmenu-screenshot @@ -2,13 +2,24 @@ # It lets you choose the kind of screenshot to take, including # copying the image or even highlighting an area to copy. +load_config () { + . $HOME/.config/dmenu/config +} -. $HOME/.config/dmenu/config +menu () { + case "$(printf "a selected area\\nfull screen" | $DMENU -l 6 -p "Screenshot which area?")" in + "a selected area") sleep 0.5; shotgun -g $(hacksaw) $dir/pic-selected-"$(date '+%y%m%d-%H%M-%S').png" ;; + "full screen") sleep 0.5; shotgun $dir/pic-full-"$(date '+%y%m%d-%H%M-%S').png" ;; + esac +} -dir="$HOME/pics/screenshots" -[ ! -d "$dir" ] && mkdir $dir -p +main () { + dir="$HOME/pics/screenshots" + [ ! -d "$dir" ] && mkdir $dir -p -case "$(printf "a selected area\\ncurrent window\\nfull screen\\na selected area (copy)\\ncurrent window (copy)\\nfull screen (copy)" | $DMENU -l 6 -p "Screenshot which area?")" in - "a selected area") sleep 0.5; shotgun -g $(hacksaw) $dir/pic-selected-"$(date '+%y%m%d-%H%M-%S').png" ;; - "full screen") sleep 0.5; shotgun $dir/pic-full-"$(date '+%y%m%d-%H%M-%S').png" ;; -esac + load_config + + menu +} + +main $@ diff --git a/dmenu-transmission b/dmenu-transmission index 18d4f34..97ba87d 100755 --- a/dmenu-transmission +++ b/dmenu-transmission @@ -1,10 +1,8 @@ #!/bin/env bash -source $HOME/.config/dmenu/config - -id=$(transmission-remote -l | $DMENU -l 10 | awk '{print $1}'| sed 's/*//') - -[ -z "$id" ] && exit 0 +load_config () { + source $HOME/.config/dmenu/config +} move () { declare -a paths=( @@ -29,22 +27,34 @@ move () { else quick-notify "Transmission" "Couldn't move $1 to $choice" fi - - } -case "$(printf "Start\\nStop\\nMove\\nRemove" | $DMENU -p "Torrent Action: ")" in - Remove) transmission-remote -t $id -rad - quick-notify "Transmission" "Removed $id" - ;; - Start) transmission-remote -t $id -s - quick-notify "Transmission" "Started $id" - ;; - Stop) transmission-remote -t $id -S - quick-notify "Transmission" "Stoped $id" - ;; - Move) move $id - ;; - *) quick-notify "Transmission" "No action chosen" - ;; -esac +menu () { + case "$(printf "Start\\nStop\\nMove\\nRemove" | $DMENU -p "Torrent Action: ")" in + Remove) transmission-remote -t $id -rad + quick-notify "Transmission" "Removed $id" + ;; + Start) transmission-remote -t $id -s + quick-notify "Transmission" "Started $id" + ;; + Stop) transmission-remote -t $id -S + quick-notify "Transmission" "Stoped $id" + ;; + Move) move $id + ;; + *) quick-notify "Transmission" "No action chosen" + ;; + esac +} + +main () { + load_config + + id=$(transmission-remote -l | $DMENU -l 10 | awk '{print $1}'| sed 's/*//') + + [ -z "$id" ] && exit 0 + + menu +} + +main $@ diff --git a/dmenu-usb-man b/dmenu-usb-man index a6fa087..abb8e2d 100755 --- a/dmenu-usb-man +++ b/dmenu-usb-man @@ -2,7 +2,9 @@ # a simple dmenu usb managment script -. $HOME/.config/dmenu/config +load_config () { + . $HOME/.config/dmenu/config +} driveCount(){ count="$(printf '%s\n' "$1" | wc -l)" @@ -30,7 +32,7 @@ mount(){ unmount(){ mounted="$(lsblk -lp | awk '/run/ { print $1 " ("$4")" }')" - + if [ "$mounted" = "" ]; then quick-notify "Dmenu Usb Manager" "No drives to unmount" exit @@ -47,7 +49,17 @@ unmount(){ fi } -case $(printf "mount\\nunmount" | $DMENU -p "Chose your usb action") in - "mount") mount ;; - "unmount") unmount ;; -esac +menu () { + case $(printf "mount\\nunmount" | $DMENU -p "Chose your usb action") in + "mount") mount ;; + "unmount") unmount ;; + esac +} + +main () { + load_config + + menu +} + +main $@