From c356b1765b88bb8d6cd294a084c2be9464539779 Mon Sep 17 00:00:00 2001 From: Yigid BALABAN Date: Wed, 2 Oct 2024 17:14:59 +0300 Subject: [PATCH] simplified and refactored chore/single chore workflow --- _reve.sh | 4 +- chores/mode/swww_single.sh | 47 +++++++++-------------- reve.sh | 79 +++++++++++++++----------------------- 3 files changed, 53 insertions(+), 77 deletions(-) diff --git a/_reve.sh b/_reve.sh index 3a09373..d7df8e2 100644 --- a/_reve.sh +++ b/_reve.sh @@ -51,8 +51,8 @@ util_delete_config() { util_run_chore() { local chore_path="$reve_installation/chores/$1" if [ -x "$chore_path.sh" ]; then - error I "util_run_chore" "Running single chore: $(basename "$1")" - bash "$chore_path.sh" + error I "util_run_chore" "Running chore: $(basename "$1")" + REVE_FOLDER="$reve_config" RV_CURRENT_MODE=$2 bash "$chore_path.sh" else error E "util_run_chore" "$chore_path is not executable" fi diff --git a/chores/mode/swww_single.sh b/chores/mode/swww_single.sh index dca167e..dd6a289 100755 --- a/chores/mode/swww_single.sh +++ b/chores/mode/swww_single.sh @@ -1,45 +1,36 @@ -#!/usr/bin/env bash +#!/bin/bash # reve desktop environment framework # Yigid BALABAN 2024 # chore script -# type mode -# name swww_single.sh -# desc changes the background depending on the desktop_mode -# vars bg_dark, bg_light +# type mode +# name swww_single.sh +# desc changes the background depending on the desktop_mode +# vars bg_dark, bg_light +# reload asus_kbd_light.sh -if ! command -v swww &> /dev/null; then - echo "swww is not installed. Please install it and try again." - exit 1 +if ! command -v swww &>/dev/null; then + echo "swww is not installed. Please install it and try again." + exit 1 fi -reve_folder="$HOME/.config/reve" -reve_desktop_mode="$reve_folder/desktop_mode" -bg_dark="$reve_folder/chore/bg_dark" -bg_light="$reve_folder/chore/bg_light" +bg_dark="$REVE_FOLDER/chore/bg_dark" +bg_light="$REVE_FOLDER/chore/bg_light" -util_readf () { - local filename=$1 +source "$(reve where)/_reve" - if [[ -f "$filename" ]]; then - cat "$filename" - else - echo "util_readf: File not found: $filename" >&2 - return 1 - fi -} - -current_mode=$( util_readf "$reve_desktop_mode" ) types=("left" "right" "top" "bottom" "wipe" "wave" "grow" "outer") ltypes=${#types[@]} rindex=$((RANDOM % ltypes)) rtype=${types[rindex]} -if [[ $current_mode == "dark" ]]; then - swww img --transition-type "$rtype" --transition-pos 1,1 --transition-step 90 "$bg_dark" - notify-send --urgency=low --expire-time=1450 --icon="$bg_dark" --app-name="reve: swww_single" "Wallpaper changed" "Wallpaper changed and saved on dark mode." +if [[ $RV_CURRENT_MODE == "dark" ]]; then + swww img --transition-type "$rtype" --transition-pos 1,1 --transition-step 90 "$bg_dark" + notify-send --urgency=low --expire-time=1450 --icon="$bg_dark" --app-name="reve: swww_single" "Wallpaper changed" "Wallpaper changed and saved on dark mode." else - swww img --transition-type "$rtype" --transition-pos 1,1 --transition-step 90 "$bg_light" - notify-send --urgency=low --expire-time=1450 --icon="$bg_light" --app-name="reve: swww_single" "Wallpaper changed" "Wallpaper changed and saved on light mode." + swww img --transition-type "$rtype" --transition-pos 1,1 --transition-step 90 "$bg_light" + notify-send --urgency=low --expire-time=1450 --icon="$bg_light" --app-name="reve: swww_single" "Wallpaper changed" "Wallpaper changed and saved on light mode." fi + +reload misc/asus_kbd_light.sh diff --git a/reve.sh b/reve.sh index d7c01be..301a70a 100755 --- a/reve.sh +++ b/reve.sh @@ -3,17 +3,19 @@ # reve desktop environment framework # Yigid BALABAN 2024 +# shellcheck disable=SC2086 +# SC2086 is disabled here, but all user inputs must be quoted + in_desktop_mode="" in_reason="" in_chore_name="" rt_script_dir=$(realpath "$(dirname "$0")") rt_has_mode_changed=0 +rt_current_mode="unset" reve_folder="$HOME/.config/reve" reve_desktop_mode="$reve_folder/desktop_mode" -reve_time_day="$reve_folder/time_day" -reve_time_night="$reve_folder/time_night" reve_chores_mode="$rt_script_dir/chores/mode" @@ -54,10 +56,6 @@ util_help () { esac } -util_mkdirs () { - mkdir -p "$reve_folder" -} - f_shell_completion () { if [ "$in_shell_comp" == "fish" ]; then cp "$rt_script_dir/completions/reve.fish" "$HOME/.config/fish/completions/reve.fish" @@ -71,10 +69,6 @@ f_shell_completion () { if ! grep -q "_reve_completions" "$HOME/.bash_completion"; then echo "$_reve_completions" >> "$HOME/.bash_completion" fi - - if [ -n "$BASH_SOURCE" ]; then - source "$HOME/.bash_completion" - fi fi } @@ -84,44 +78,34 @@ set_desktop_mode () { return 1 # since mode has changed fi - local current_mode="unset" - local previous_mode day_start night_start num_day num_night current_time - previous_mode=$( util_readf "$reve_desktop_mode" ) - day_start=$( util_readf "$reve_time_day" ) - night_start=$( util_readf "$reve_time_night" ) - num_day=$( awk -F: '{print $1 * 60 + $2}' <<< "$day_start" ) - num_night=$( awk -F: '{print $1 * 60 + $2}' <<< "$night_start" ) + local previous_mode num_day num_night current_time + previous_mode=$( util_read_config base.desktop_mode ) + num_day=$( awk -F: '{print $1 * 60 + $2}' <<< "$( util_read_config base.time_day )" ) + num_night=$( awk -F: '{print $1 * 60 + $2}' <<< "$( util_read_config base.time_night )" ) current_time=$( awk -F: '{print $1 * 60 + $2}' <<< "$(date +%H:%M)" ) if ((num_night > current_time && current_time >= num_day)); then - current_mode="light" + rt_current_mode="light" else - current_mode="dark" + rt_current_mode="dark" fi - echo "[reve] [I] Setting the mode: $current_mode" - echo "$current_mode" > "$reve_desktop_mode" + echo "[reve] [I] Setting the mode: $rt_current_mode" + echo "$rt_current_mode" > "$reve_desktop_mode" - if [ "$current_mode" == "$previous_mode" ]; then + if [ "$rt_current_mode" == "$previous_mode" ]; then return 0 # since mode did not change else return 1 fi } -prepare () { - util_mkdirs - set_desktop_mode - rt_has_mode_changed="$?" - echo $rt_has_mode_changed -} - # Called when the mode (the default state, is either dark or light) changes chores_mode () { for file in "$reve_chores_mode"/*; do if [ -x "$file" ]; then echo "[reve] [I] Running chore: $( basename "$file" )" - bash "$file" + util_run_chore "$file" $rt_current_mode else echo "[reve] [E] chores_mode: $file is not executable" fi @@ -129,7 +113,9 @@ chores_mode () { } util_handle_pos () { - local forced_mode + # args: $@ -- handles positionals + # returns: 'light' or 'dark' depending on positionals or $rt_current_mode + local forced_mode=$rt_current_mode for arg in "$@"; do if [[ "$arg" == "-d" || "$arg" == "--dark" ]]; then forced_mode="dark" @@ -137,10 +123,7 @@ util_handle_pos () { forced_mode="light" fi done - - if [[ $( util_read_config base.desktop_mode ) != "$forced_mode" ]]; then - util_write_config "$forced_mode" - fi + echo $forced_mode } sub_config () { @@ -158,18 +141,24 @@ sub_config () { util_help config ;; *) - echo "[reve] [E] in subcommand config: '$1' is not a valid command" + echo "reve: in subcommand config: '$1' is not a valid command" ;; esac } main () { - if (( rt_has_mode_changed == 1 )) || [[ "$in_reason" == "chores_mode" ]]; then - chores_mode - fi + mkdir -p "$reve_folder" if [[ "$in_chore_name" != "" ]]; then - util_run_chore "$in_chore_name" + forced_mode=$(util_handle_pos "$@") + util_run_chore "$in_chore_name" $forced_mode + return + fi + + set_desktop_mode + rt_has_mode_changed="$?" + if (( rt_has_mode_changed == 1 )) || [[ "$in_reason" == "chores_mode" ]]; then + chores_mode fi } @@ -202,10 +191,7 @@ case "$1" in in_reason="$2" ;; chore) - util_handle_pos "$@" - util_run_chore "$2" - util_toggle_dm - exit 0 + in_chore_name="$2" ;; poll) ;; @@ -214,10 +200,9 @@ case "$1" in exit 0 ;; *) - echo "Invalid command or subcommand: $1" + echo "reve: invalid command or subcommand: $1" exit 1 ;; esac -prepare "$@" -main +main "$@"