Made scripts more readable by moving most logic to functions.

This commit is contained in:
cronyakatsuki 2022-11-04 14:54:08 +01:00
parent 86e3a3a79c
commit 5c738fdc77
11 changed files with 234 additions and 133 deletions

View File

@ -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 $@

View File

@ -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,10 +11,9 @@ get_category () {
[ -z "$category" ] && exit 0
}
get_category
while [ -z "$game" ]
do
menu () {
while [ -z "$game" ]
do
if [ -z "$1" ]; then
game=$(game-run list "$category" | sed '/-18+/d' | $DMENU -p "Choose game to run:")
@ -25,10 +25,25 @@ do
fi
[ -z "$game" ] && get_category
done
done
}
if game-run launch "$game"; then
launch_game () {
if game-run launch "$game"; then
quick-notify "Game run" "Launching $game"
else
else
quick-notify "Game run" "Failed to launch $game"
fi
fi
}
main () {
load_config
get_category
menu
launch_game
}
main $@

View File

@ -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 $@

View File

@ -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
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
esac
}
main () {
load_config
menu $@
}
main $@

View File

@ -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
if [ "$instances" = "0" ]; then
exit
elif [ "$instances" -lt "2" ]; then
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
exit
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
fi
}
main $@

View File

@ -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
menu () {
case "$(printf "shutdown\\nreboot" | $DMENU -p "Choose your poison")" in
"shutdown") poweroff ;;
"reboot") reboot ;;
esac
esac
}
main () {
load_config
menu
}
main $@

View File

@ -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 $@

View File

@ -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"
while [ -z "$profile" ]
do
[ "$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:")
[ -z "$profile" ] && get_category
done
done
}
if sudo ryzenset set "$profile"; then
launch_profile () {
if sudo ryzenset set "$profile"; then
quick-notify "Ryzenset" "Setting $profile"
else
else
quick-notify "Ryzenset" "Failed to set $game"
fi
fi
}
main () {
load_config
get_category
get_current_profile
menu
launch_profile
}
main $@

View File

@ -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
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
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
esac
}
main () {
dir="$HOME/pics/screenshots"
[ ! -d "$dir" ] && mkdir $dir -p
load_config
menu
}
main $@

View File

@ -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,11 +27,10 @@ move () {
else
quick-notify "Transmission" "Couldn't move $1 to $choice"
fi
}
case "$(printf "Start\\nStop\\nMove\\nRemove" | $DMENU -p "Torrent Action: ")" in
menu () {
case "$(printf "Start\\nStop\\nMove\\nRemove" | $DMENU -p "Torrent Action: ")" in
Remove) transmission-remote -t $id -rad
quick-notify "Transmission" "Removed $id"
;;
@ -47,4 +44,17 @@ case "$(printf "Start\\nStop\\nMove\\nRemove" | $DMENU -p "Torrent Action: ")" i
;;
*) quick-notify "Transmission" "No action chosen"
;;
esac
esac
}
main () {
load_config
id=$(transmission-remote -l | $DMENU -l 10 | awk '{print $1}'| sed 's/*//')
[ -z "$id" ] && exit 0
menu
}
main $@

View File

@ -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)"
@ -47,7 +49,17 @@ unmount(){
fi
}
case $(printf "mount\\nunmount" | $DMENU -p "Chose your usb action") in
menu () {
case $(printf "mount\\nunmount" | $DMENU -p "Chose your usb action") in
"mount") mount ;;
"unmount") unmount ;;
esac
esac
}
main () {
load_config
menu
}
main $@