dots

git clone git://mattcarlson.org/repos/dots.git
Log | Files | Refs

commit 8aa79f5129e12d0fc5f99317bf60f2ab309985cf
parent e6bb52b09e1a0a280305468fa0e7ab10a14e4284
Author: Matthew Carlson <matt@mcarlson.xyz>
Date:   Wed, 18 Aug 2021 17:15:58 -0400

scripting

Diffstat:
M.config/vim/vimrc | 4++--
M.local/bin/bar/music.sh | 2+-
A.local/bin/bar/pkgs.sh | 112+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
M.local/bin/compile.sh | 9+++++++--
4 files changed, 122 insertions(+), 5 deletions(-)

diff --git a/.config/vim/vimrc b/.config/vim/vimrc @@ -186,5 +186,5 @@ nnoremap <CR> :noh<CR><CR> " basic compiler map <leader>c :w! \| !compile.sh <c-r>%<CR> -map <leader>o :!compile.sh -o <c-r>%<CR><CR> -map <leader>x :!compile.sh -c %<CR> +map <leader>o :w! \| !compile.sh -o <c-r>%<CR><CR> +map <leader>x :w! \| !compile.sh -c %<CR> diff --git a/.local/bin/bar/music.sh b/.local/bin/bar/music.sh @@ -26,7 +26,7 @@ toggle() { rest="$(printf '%s\n' "${state}" | cut -c 2-)" state="${first_letter}${rest}" - env HERBE_ID=/0 herbe "${state} ${song}" & + herbe "${state} ${song}" & } diff --git a/.local/bin/bar/pkgs.sh b/.local/bin/bar/pkgs.sh @@ -0,0 +1,112 @@ +#!/bin/sh +# +# pkgs + +ICON='' +UPGRADE_ICON='' +TMP='/tmp/upgrade-pkgs' +AUR_HELPER='paru' +PKGS="${XDG_CACHE_HOME:-${HOME}/.cache}/bar/pkgs" + +get_os() { + command -v pacman >/dev/null && os='arch' + { command -v apt || command -v apt-get ; } >/dev/null && os='debian' + command -v emerge >/dev/null && os='gentoo' + command -v xbps-install >/dev/null && os='void' +} + +upgrade_pkgs() { + trap "rm -f ${TMP}; exit 1" INT TERM + + upgrade_arch_pkgs() { + upgrade_repo_pkgs() { + while :; do + get_pkgs + + case "${1}" in + 'pacman') repo_type='Official' + available_upgrades=$(($(printf '%s\n' "${PACKAGES}" | wc -l)-1)) + cmd='pacman' + ;; + 'aur') repo_type='AUR' + available_upgrades=$(($(printf '%s\n' "${aur_pkgs}" | wc -l)-1)) + cmd="${AUR_HELPER} -a" + ;; + *) return 1 ;; + esac + + if [ "${available_upgrades}" -eq 0 ]; then + [ "${repo_type}" = 'Official' ] && repo_type="$(printf '%s\n' "${repo_type}" | tr '[:upper:]' '[:lower:]')" + printf '%s\n' "No ${repo_type} packages are available for upgrade at the moment. Press any key to continue." + read -r input + break + else + if ! doas "${cmd}" -Syu; then + printf '%s\n' "${repo_type} packages failed to upgrade. Retry? [Y/n] " + read -r input + input="$(printf '%s\n' "${input}" | tr '[:upper:]' '[:lower:]')" + if [ "${input}" = 'y' ]; then continue + else return 1 + fi + fi + fi + done + } + + upgrade_repo_pkgs 'pacman' + upgrade_repo_pkgs 'aur' + } + + touch "${TMP}" + + get_os + + case "${os}" in 'arch') upgrade_arch_pkgs ;; esac + + rm -f "${TMP}" +} + +get_pkgs() { + get_os + + case "${os}" in + 'arch') pacman_pkgs="$(printf '%s\n' "$(pacman -Qu 2>/dev/null)")" + if command -v "${AUR_HELPER}" >/dev/null; then + aur_pkgs="$(printf '%s\n' "$("${AUR_HELPER}" -Qum --devel 2>/dev/null)")" + fi + pkgs="$(printf '%s\n%s\n' "${pacman_pkgs}" "${aur_pkgs}")" + ;; + esac + + if [ -f "${PKGS}" ]; then + printf '%s\n' "${pkgs}" > "${PKGS}" + else touch "${PKGS}" + fi +} + +bar() { + [ -f "${PKGS}" ] && pkgs=$(($(cat "${PKGS}" | wc -l)-1)) + + [ -f '/tmp/upgrade-pkgs' ] && printf '%s\n' "${UPGRADE_ICON}" \ + || printf '%s\n' "${ICON} ${pkgs}" +} + +main() { + # called from bar + [ ${#} -eq 0 ] && bar + + # bar usage + case ${BLOCK_BUTTON} in esac + + while getopts 'gu' opt; do + case "${opt}" in + # get upgradable pkgs + g) get_pkgs ;; + # upgrade if called with u flag + u) upgrade_pkgs ;; + *) return ;; + esac + done +} + +main "${@}" diff --git a/.local/bin/compile.sh b/.local/bin/compile.sh @@ -2,7 +2,7 @@ # # compile -. "${HOME:-/home/${USER}/}/.local/bin/symlink.sh" +. "${HOME}/.local/bin/symlink.sh" main() { # use POSIX-compliant way to follow symlink @@ -23,7 +23,12 @@ main() { while getopts 'c:o:' opt; do case "${opt}" in # shell check if called with c flag and shell script - c) [ "${OPTARG##*.}" = 'sh' ] && shellcheck -x "${OPTARG}" + c) if [ "${OPTARG##*.}" = 'sh' ]; then + if [ -z "$(shellcheck -x "${OPTARG}")" ]; then printf 'No issues detected!' + else shellcheck -x "${OPTARG}" + fi + else printf 'Not a shell script.' + fi return ;; # open same file name with different extension if called with o flag