diff options
author | Suleyman Farajli <suleyman@farajli.net> | 2025-06-17 22:11:51 +0400 |
---|---|---|
committer | Suleyman Farajli <suleyman@farajli.net> | 2025-06-17 22:11:51 +0400 |
commit | 4c03a6cd2038e3175e03c489845791b606c1cc06 (patch) | |
tree | 4f2943838b26d1eed5ab71ce0264aa236f968d04 | |
parent | 8a4659fa081773fed35975267f01f8a7c0acf3a4 (diff) |
scripts modularized
-rwxr-xr-x | scripts/br | 18 | ||||
-rwxr-xr-x | scripts/nsend | 21 | ||||
-rwxr-xr-x | scripts/qw | 50 | ||||
-rwxr-xr-x | scripts/sdev | 39 | ||||
-rwxr-xr-x | scripts/shot | 53 | ||||
-rwxr-xr-x | scripts/slib | 12 | ||||
-rwxr-xr-x | scripts/slight | 54 | ||||
-rwxr-xr-x | scripts/svol | 56 | ||||
-rwxr-xr-x | scripts/swall | 48 |
9 files changed, 122 insertions, 229 deletions
@@ -1,5 +1,7 @@ #!/bin/sh +. slib + help() { cat << EOF ${0}: Open links from bookmarks through dmenu @@ -9,13 +11,8 @@ options: NOTE: bookmarks file is located at ~/.config/sites/bookmarks EOF -} -err() { - for line in "${@}"; do - echo "${line}" >&2 - done - exit 1 +exit 0 } alias dmenucmd="dmenu -bw 1 -c -g 1 -l 25" @@ -24,21 +21,22 @@ alias dmenucmd="dmenu -bw 1 -c -g 1 -l 25" if [ "${#}" -eq 0 ]; then + # FIXME: Use XDG_CONFIG link_file="${HOME}/.config"/sites/bookmarks.txt - command -v dmenu > /dev/null 2>&1 || err "${0}: dmenu must be installed" + check_program "dmenu" [ -z "${XDG_HOME_CONFIG}" ] || link_file="${XDG_HOME_CONFIG}"/sites/bookmarks.txt - [ -e "${link_file}" ] || err "${0}: Couldn't find bookmarks file" + [ -e "${link_file}" ] || err "Couldn't find bookmarks file" link=$(< "${link_file}" dmenucmd) [ -z "${link}" ] || "${browser}" "${link}" elif [ "${#}" -eq 1 ]; then - [ "${1}" = "-h" ] && help && exit 0 + [ "${1}" = "-h" ] && help "${browser}" "${1}" else - err "${0}: Invalid usage" "Try '${0} -h' for help." + invalid_use fi diff --git a/scripts/nsend b/scripts/nsend index b8f9a00..7337b4d 100755 --- a/scripts/nsend +++ b/scripts/nsend @@ -1,5 +1,7 @@ #!/bin/sh +. slib + help() { cat << EOF ${0}: Wrapper script to send notifications @@ -9,29 +11,22 @@ options: -s [Msg] Send Msg Without Name -h Print this message and exit EOF -} -err() { - for line in "${@}"; do - echo "${line}" >&2 - done - exit 1 +exit 0 } -if ! command -v notify-send > /dev/null 2>&1; then - err "${0}: dunst must be installed" -fi +check_program "notify-send" "dunst must be installed" case "${1}" in "-s") - [ ${#} -gt 3 ] && err "${0}: Invalid usage" "Try '${0} -h' for help." + [ "${#}" -gt 3 ] && invalid_use - notify-send "${2}" "${3}" > /dev/null 2>&1 || err "${0}: Failed to send notification" + run "notify-send ${2} ${3}" exit 0 ;; -"-h") help; exit 0 ;; +"-h") help ;; -*) err "${0}: Invalid usage" "Try '${0} -h' for help." ;; +*) invalid_use ;; esac @@ -1,5 +1,7 @@ #!/bin/sh +. slib + help() { cat << EOF ${0}: Qemu Wrapper, very simple script for abriviating \ @@ -10,53 +12,31 @@ options: -r [input.iso] run input.iso -x [input.qcow2] run input.qcow2 EOF -} - -err() { - for line in "${@}"; do - echo "${line}" >&2 - done - exit 1 -} - -run() { - if ! ${1} > /dev/null 2>&1; then - err "${2}" - fi - [ -n "${3}" ] && echo "${3}" - exit 0 +exit 0 } -if ! command -v qemu-img > /dev/null 2>&1 || \ - ! command -v qemu-system-x86_64 > /dev/null 2>&1; then - err "${0}: qemu must be properly installed" -fi +check_program "qemu-img" +check_program "qemu-system-x86_64" -if [ $# != 2 ] && [ $# != 1 ]; then - err "${0}: Invalid usage" "Try '${0} -h' for help." -fi +[ $# != 2 ] && [ $# != 1 ] && invalid_use while getopts "cr:x:h" option ;do case "${option}" in - c) - run "qemu-img create -f qcow2 Image.img 10G" \ - "${0}: Failed to create Image.img" - ;; + c) run "qemu-img create -f qcow2 Image.img 10G" ;; + + x) run "qemu-system-x86_64 -drive file=${OPTARG},format=qcow2 -enable-kvm" ;; + r) run "qemu-system-x86_64 -enable-kvm -cdrom ${OPTARG} \ - -boot menu=on -drive file=Image.img -m 4G" \ - "${0}: Failed to run image" - ;; - x) - run "qemu-system-x86_64 -drive file=${OPTARG},format=qcow2 -enable-kvm" \ - "${0}: Failed to run image" + -boot menu=on -drive file=Image.img -m 4G" ;; - h) help; exit 0;; + h) help ;; - *) err "Try '${0} -h' for help." ;; + *) invalid_use -h ;; esac done -err "${0}: Invalid usage" "Try '${0} -h' for help." +# Unreachable +invalid_use diff --git a/scripts/sdev b/scripts/sdev index c53ce93..22c1af5 100755 --- a/scripts/sdev +++ b/scripts/sdev @@ -1,5 +1,7 @@ #!/bin/sh +. slib + help() { cat << EOF ${0}: Disable and Enable devices @@ -13,18 +15,13 @@ options: Note: Nondescriptive inputs may disable (or enable) unwanted devices. EOF -} -err() { - for line in "${@}"; do - echo "${line}" >&2 - done - exit 1 +exit 0 } get_id() { if ! dev=$(xinput list --name-only | grep -i -m1 "${1}" ); then - err "${0}: Couldn't get device" + err "Couldn't get device" fi id="${dev#∼ }" } @@ -33,7 +30,7 @@ xenable() { echo "${id}" if ! xinput enable "${1}" > /dev/null 2>&1; then - err "${0}: Failed to enable dev." + err "Failed to enable dev." else exit 0 fi @@ -43,22 +40,24 @@ xdisable() { echo "${id}" if ! xinput disable "${1}" > /dev/null 2>&1; then - err "${0}: Failed to disable dev." + err "Failed to disable dev." else exit 0 fi } -if ! command -v xinput > /dev/null 2>&1; then - err "${0}: xinput must be installed" -fi +check_program "xinput" while getopts "e:d:t:lh" option; do case "${option}" in - e) get_id "${OPTARG}"; xenable "${id}" ;; - - d) get_id "${OPTARG}"; xdisable "${id}" ;; - + e) + get_id "${OPTARG}" + xenable "${id}" + ;; + d) + get_id "${OPTARG}" + xdisable "${id}" + ;; t) get_id "${OPTARG}" @@ -67,16 +66,16 @@ while getopts "e:d:t:lh" option; do ;; l) if ! xinput list 2>/dev/null; then - err "${0}: Listing Failed" + err "Listing Failed" else exit 0 fi ;; - h) help; exit ;; + h) help ;; - *) err "Try '${0} -h' for help." ;; + *) invalid_use -h ;; esac done -err "${0}: Invalid usage" "Try '${0} -h' for help." +invalid_use diff --git a/scripts/shot b/scripts/shot index 67ad849..4a58dd2 100755 --- a/scripts/shot +++ b/scripts/shot @@ -1,5 +1,7 @@ #!/bin/sh +. slib + help() { cat << EOF ${0}: Wrapper script to take screenshots @@ -9,30 +11,13 @@ options: -s Select the area with the cursor -h Print this message and exit -NOTE: save directory is ~/pic/screenshots +NOTE: save directory is ~/pics/screenshots EOF -} - -err() { - for line in "$@"; do - echo "${line}" >&2 - done - exit 1 +exit 0 } -run() { - if ! ${1} > /dev/null 2>&1; then - err "${2}" - fi - - [ -n "${3}" ] && echo "${3}" - exit 0 -} - -if ! command -v scrot > /dev/null 2>&1; then - err "${0}: scrot must be installed" -fi +check_program "scrot" default_dir="${HOME}"/pics/screenshots @@ -43,34 +28,30 @@ while getopts "d:sh" option; do s) s_flg="1" ;; - h) help; exit 0 ;; + h) help ;; - *) err "Try '${0} -h' for help" ;; + *) invalid_use -h ;; esac done shift $((OPTIND - 1)) -[ ${#} != 0 ] && err "${0}: Invalid usage" "Try '${0} -h' for help." +[ ${#} != 0 ] && invalid_use -if [ -z "${input}" ]; then - outfile="${default_dir}"/%b%d::%H%M%S.png -else - if [ -d "${input}" ]; then - outfile="${input}"/%b%d::%H%M%S.png - else - outfile="${input}" - fi -fi +[ -z "${input}" ] && input="${default_dir}" + +outfile="${input}" + +[ -d "${input}" ] && outfile="${input}/$(date '+%b%d::%H%M%S')" if [ ! -e "$(dirname "${outfile}")" ]; then - mkdir -pv "$(dirname "${outfile}")" > /dev/null 2>&1 \ - || err "${0}: Failed to create directory" + mkdir -p "$(dirname "${outfile}")" > /dev/null 2>&1 \ + || err "Failed to create directory" fi if [ "${s_flg}" = "1" ]; then - run "scrot -zs ${outfile}" "Failed to screenshot" + run "scrot -zs ${outfile}" "${outfile}" else - run "scrot -z ${outfile}" "Failed to screenshot" + run "scrot -z ${outfile}" "${outfile}" fi diff --git a/scripts/slib b/scripts/slib index 8eba48b..f55acb8 100755 --- a/scripts/slib +++ b/scripts/slib @@ -1,7 +1,13 @@ #!/bin/sh +argv0=$(basename "${0}") + err() { - [ "${1}" != "-x" ] && printf "${0}: " || shift + if [ "${1}" != "-x" ]; then + printf "%s: " "${argv0}" + else + shift + fi for line in "${@}"; do echo "${line}" >&2 @@ -10,8 +16,8 @@ err() { } invalid_use() { - [ "${1}" == "-h" ] && err -x "Try '${0} -h' for help." - err "Invalid usage" "Try '$0 -h' for help." + [ "${1}" = "-h" ] && err -x "Try '${argv0} -h' for help." + err "Invalid usage" "Try '${argv0} -h' for help." } check_program() { diff --git a/scripts/slight b/scripts/slight index 7f1b03a..0f8cba0 100755 --- a/scripts/slight +++ b/scripts/slight @@ -1,5 +1,7 @@ #!/bin/sh +. slib + help() { cat << EOF ${0}: Wrapper script to change backlight @@ -13,61 +15,35 @@ options: NOTE: Script interprets values as percentages EOF -} -err() { - for line in "${@}"; do - echo "${line}" >&2 - done - exit 1 +exit 0 } -run() { - if ! ${1} > /dev/null 2>&1; then - err "${2}" - fi - - [ -n "${3}" ] && echo "${3}" +check_program "brightnessctl" - slreload || echo "Warning: Failed to reload slstatus" >&2 +[ ${#} != 1 ] && [ ${#} != 2 ] && invalid_use - exit 0 -} +while getopts "i:d:s:ph" option; do + case "${option}" in + i) run --reload-status "brightnessctl set +${OPTARG}%" ;; -if ! command -v brightnessctl > /dev/null 2>&1; then - err "${0}: brightnessctl must be installed" -fi + d) run --reload-status "brightnessctl set ${OPTARG}-%" ;; -if [ ${#} != 1 ] && [ ${#} != 2 ]; then - err "${0}: Invalid usage" "Try '${0} -h' for help." -fi + s) run --reload-status "brightnessctl set ${OPTARG}%" ;; -while getopts "i:d:s:ph" option; do - case "${option}" in - i) - run "brightnessctl set +${OPTARG}%" \ - "${0}: Failed to increase brightness" - ;; - d) - run "brightnessctl set ${OPTARG}-%" \ - "${0}: Failed to decrease brightness" - ;; - s) - run "brightnessctl set ${OPTARG}%" \ - "${0}: Failed to set brightness" - ;; p) - if ! echo $(( (`brightnessctl g` * 100) / `brightnessctl m` )); then + if ! echo $(( ($(brightnessctl g) * 100) / $(brightnessctl m) )); then err "Failed to get current brightness" else exit 0 fi ;; - h) help; exit 0 ;; + h) help ;; - *) err "Try '${0} -h' for help." ;; + *) invalid_use -h ;; esac done -err "${0}: Invalid usage" "Try '${0} -h' for help." +# Unreachable +invalid_use diff --git a/scripts/svol b/scripts/svol index 25024e7..f0765bb 100755 --- a/scripts/svol +++ b/scripts/svol @@ -1,5 +1,7 @@ #!/bin/sh +. slib + help() { cat << EOF ${0}: Wrapper script to change volume @@ -12,53 +14,24 @@ options: -t Toggle between mute and unmute -h Print this message and exit EOF -} -err() { - for line in "${@}"; do - echo "${line}" >&2 - done - exit 1 +exit 0 } -run() { - if ! ${1} > /dev/null 2>&1; then - err "${2}" - fi +check_program "pactl" "pulseaudio must be installed" - [ -n "${3}" ] && echo "${3}" +[ $# != 1 ] && [ $# != 2 ] && invalid_use - slreload || echo "Warning: Failed to reload slstatus" >&2 +while getopts "i:d:s:pth" option; do + case "${option}" in + i) run --reload-status "pactl set-sink-volume @DEFAULT_SINK@ +${OPTARG}%" ;; - exit 0 -} + d) run --reload-status "pactl set-sink-volume @DEFAULT_SINK@ -${OPTARG}%" ;; -if ! command -v pactl > /dev/null 2>&1; then - err "${0}: pulseaudio must be installed" -fi + s) run --reload-status "pactl set-sink-volume @DEFAULT_SINK@ ${OPTARG}%" ;; -if [ $# != 1 ] && [ $# != 2 ]; then - err "${0}: Invalid usage" "Try '$0 -h' for help." -fi + t) run --reload-status "pactl set-sink-mute @DEFAULT_SINK@ toggle" ;; -while getopts "i:d:s:pth" option; do - case "${option}" in - i) - run "pactl set-sink-volume @DEFAULT_SINK@ +${OPTARG}%" \ - "${0}: Failed to increase volume" - ;; - d) - run "pactl set-sink-volume @DEFAULT_SINK@ -${OPTARG}%" \ - "${0}: Failed to decrease volume" - ;; - s) - run "pactl set-sink-volume @DEFAULT_SINK@ ${OPTARG}%" \ - "${0}: Failed to set volume" - ;; - t) - run "pactl set-sink-mute @DEFAULT_SINK@ toggle" \ - "${0}: Failed to toggle volume" - ;; p) if ! pactl get-sink-volume @DEFAULT_SINK@ \ | sed -e 's,.* \([0-9][0-9]*\)%.*,\1,' | head -n1 2>/dev/null; then @@ -67,11 +40,12 @@ while getopts "i:d:s:pth" option; do exit 0 fi ;; - h) help; exit 0 ;; + h) help ;; - *) err "Try '${0} -h' for help" ;; + *) invalid_use -h ;; esac done -err "${0}: Invalid usage" "Try '$0 -h' for help." +# Unreachable +invalid_use diff --git a/scripts/swall b/scripts/swall index bc288b8..34c4bc8 100755 --- a/scripts/swall +++ b/scripts/swall @@ -1,5 +1,7 @@ #!/bin/sh +. slib + help() { cat << EOF ${0}: Wrapper script to set wallpapers @@ -11,53 +13,35 @@ options: NOTE: default directory is ~/.config/wallpapers EOF -} -err() { - for line in "${@}"; do - echo "${line}" >&2 - done - exit 1 +exit 0 } -run() { - if ! ${1} > /dev/null 2>&1; then - err "${2}" - fi - - [ -n "${3}" ] && echo "${3}" - exit 0 -} +check_program "xwallpaper" -if ! command -v xwallpaper > /dev/null 2>&1; then - err "${0}: xwallpaper must be installed" -fi - -if [ ${#} = 0 ]; then +if [ "${#}" = 0 ]; then + #FIXME: use XDG_CONFIG input="${HOME}/.config/wallpapers" -elif [ ${#} != 1 ] && [ $# != 2 ]; then - err "${0}: Invalid usage" \ - "Try '$0 -h' for help." +elif [ "${#}" != 1 ] && [ "${#}" != 2 ]; then + invalid_use fi while getopts "hcd:" option; do case "${option}" in - c) - run "xwallpaper --clear" \ - "${0}: Failed to clear wallpaper" - ;; + c) run "xwallpaper --clear" ;; + d) input="${OPTARG}" ;; - h) help; exit 0 ;; + h) help ;; - *) err "Try '${0} -h' for help" ;; + *) invalid_use -h ;; esac done shift $((OPTIND - 1)) -[ ${#} != 0 ] && err "${0}: Invalid usage" "Try '${0} -h' for help." +[ "${#}" != 0 ] && invalid_use if [ -n "${input}" ]; then case $(file -L -b --mime-type "${input}") in @@ -65,7 +49,7 @@ if [ -n "${input}" ]; then inode/directory) waldir="${input}" ;; - *) err "${0}: Couldn't read given file" ;; + *) err "Couldn't read given file" ;; esac fi @@ -74,6 +58,6 @@ if [ -n "${waldir}" ]; then | shuf -n 1 ) fi -[ -z "${image}" ] && err "${0}: No image file found" +[ -z "${image}" ] && err "No image file found" -run "xwallpaper --zoom ${image}" "${0}: Failed to set wallpaper" "${image}" +run "xwallpaper --zoom ${image}" "${image}" |