numerous changes

+ fish & bash completions
+ -w/--where option
M moved util_readf to _reve.sh
M linter-detected warnings fixed
M less variable clutter
This commit is contained in:
yigid balaban 2024-08-25 00:50:57 +03:00
parent f9b7112d1e
commit f34494b5ea
Signed by: fyb
GPG Key ID: E21FEB2C244CB7EB
4 changed files with 126 additions and 36 deletions

15
_reve.sh Normal file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env bash
# reve desktop environment framework
# Yigid BALABAN <fyb@fybx.dev> 2024
util_readf () {
local filename=$1
if [[ -f "$filename" ]]; then
cat "$filename"
else
echo "[reve] [E] util_readf: File not found: $filename" >&2
return 1
fi
}

34
completions/reve.bash Normal file
View File

@ -0,0 +1,34 @@
#!/bin/bash
_reve_completions() {
local cur prev opts modes reasons chores
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="-h --help -m --mode -r --reason -c --chore -w --where --shell-completion"
modes="dark light"
reasons="time network"
chores=$(ls ~/.config/reve/chores/mode | sed 's/\.sh$//')
case "${prev}" in
-m|--mode)
COMPREPLY=( $(compgen -W "${modes}" -- ${cur}) )
return 0
;;
-r|--reason)
COMPREPLY=( $(compgen -W "${reasons}" -- ${cur}) )
return 0
;;
-c|--chore)
COMPREPLY=( $(compgen -W "${chores}" -- ${cur}) )
return 0
;;
*)
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
}
complete -F _reve_completions reve

24
completions/reve.fish Normal file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env bash
# reve desktop environment framework
# Yigid BALABAN <fyb@fybx.dev> 2024
function __reve_complete_modes
echo "dark"
echo "light"
end
function __reve_complete_reasons
echo "time"
echo "network"
end
function __reve_complete_chores
ls ~/.config/reve/chores/mode | sed 's/\.sh$//'
end
complete -c reve -s m -l mode -a '(__reve_complete_modes)' -d 'Specify desktop mode (dark or light)'
complete -c reve -s r -l reason -a '(__reve_complete_reasons)' -d 'Specify the reason (time or network)'
complete -c reve -s c -l chore -a '(__reve_complete_chores)' -d 'Specify a chore to run'
complete -c reve -s w -l where -d 'Display the installation path of reve'
complete -c reve -s h -l help -d 'Show help message'

81
reve.sh
View File

@ -7,8 +7,8 @@ in_desktop_mode=""
in_reason="" in_reason=""
in_chore_name="" in_chore_name=""
SHORTOPTS="h,m:r:c:" SHORTOPTS="h,m:r:c:w,"
LONGOPTS="help,mode:,reason:,chore:" LONGOPTS="help,mode:,reason:,chore:,where"
PARSED_OPTS=$(getopt --options $SHORTOPTS --longoptions $LONGOPTS --name "$0" -- "$@") PARSED_OPTS=$(getopt --options $SHORTOPTS --longoptions $LONGOPTS --name "$0" -- "$@")
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
@ -18,7 +18,7 @@ fi
eval set -- "$PARSED_OPTS" eval set -- "$PARSED_OPTS"
rt_script_dir=$(realpath "$(dirname $0)") rt_script_dir=$(realpath "$(dirname "$0")")
rt_has_mode_changed=0 rt_has_mode_changed=0
reve_folder="$HOME/.config/reve" reve_folder="$HOME/.config/reve"
@ -28,27 +28,19 @@ reve_time_night="$reve_folder/time_night"
reve_chores_mode="$rt_script_dir/chores/mode" reve_chores_mode="$rt_script_dir/chores/mode"
source "$rt_script_dir/_reve.sh"
util_help () { util_help () {
echo "Usage: $0 [options]" echo "Usage: $0 [options]"
echo "Options:" echo "Options:"
echo "desktop_mode (-m, --mode): dark, light" echo "desktop_mode (-m, --mode): dark, light"
echo "reason (-r, --reason): time, network" echo "reason (-r, --reason): time, network"
echo "chore (-c, --chore): chore_name" echo "chore (-c, --chore): chore_name"
} echo "where (-w, --where): returns where reve's installed"
util_readf () {
local filename=$1
if [[ -f "$filename" ]]; then
cat "$filename"
else
echo "[reve] [E] util_readf: File not found: $filename" >&2
return 1
fi
} }
util_mkdirs () { util_mkdirs () {
mkdir -p $reve_folder mkdir -p "$reve_folder"
} }
util_run_single_chore () { util_run_single_chore () {
@ -61,20 +53,40 @@ util_run_single_chore () {
fi fi
} }
f_shell_completion () {
if [ "$in_shell_comp" == "fish" ]; then
cp "$rt_script_dir/completions/reve.fish" "$HOME/.config/fish/completions/reve.fish"
elif [ "$in_shell_comp" == "bash" ]; then
_reve_completions=$( util_readf "$rt_script_dir/completions/reve.bash" )
if [ ! -f "$HOME/.bash_completion" ]; then
touch "$HOME/.bash_completion"
fi
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
}
set_desktop_mode () { set_desktop_mode () {
if [[ -n "$forced_mode" ]]; then if [[ -n "$in_desktop_mode" ]]; then
echo $forced_mode > $reve_desktop_mode echo "$in_desktop_mode" > "$reve_desktop_mode"
return 1 # since mode has changed return 1 # since mode has changed
fi fi
local current_mode="unset" local current_mode="unset"
local previous_mode=$( util_readf $reve_desktop_mode ) local previous_mode, day_start, night_start, num_day, num_night, current_time
local day_start=$( util_readf $reve_time_day ) previous_mode=$( util_readf "$reve_desktop_mode" )
local night_start=$( util_readf $reve_time_night ) day_start=$( util_readf "$reve_time_day" )
night_start=$( util_readf "$reve_time_night" )
local num_day=$( awk -F: '{print $1 * 60 + $2}' <<< "$day_start" ) num_day=$( awk -F: '{print $1 * 60 + $2}' <<< "$day_start" )
local num_night=$( awk -F: '{print $1 * 60 + $2}' <<< "$night_start" ) num_night=$( awk -F: '{print $1 * 60 + $2}' <<< "$night_start" )
local current_time=$( awk -F: '{print $1 * 60 + $2}' <<< "$(date +%H:%M)" ) current_time=$( awk -F: '{print $1 * 60 + $2}' <<< "$(date +%H:%M)" )
if ((num_night > current_time && current_time >= num_day)); then if ((num_night > current_time && current_time >= num_day)); then
current_mode="light" current_mode="light"
@ -83,7 +95,7 @@ set_desktop_mode () {
fi fi
echo "[reve] [I] Setting the mode: $current_mode" echo "[reve] [I] Setting the mode: $current_mode"
echo "$current_mode" > $reve_desktop_mode echo "$current_mode" > "$reve_desktop_mode"
if [ "$current_mode" == "$previous_mode" ]; then if [ "$current_mode" == "$previous_mode" ]; then
return 0 # since mode did not change return 0 # since mode did not change
@ -101,19 +113,17 @@ prepare () {
# Called when the mode (the default state, is either dark or light) changes # Called when the mode (the default state, is either dark or light) changes
chores_mode () { chores_mode () {
for file in "$reve_chores_mode"/*; do for file in "$reve_chores_mode"/*; do
if [ "$item" != "$reve_chores_mode/." ] && [ "$item" != "$reve_chores_mode/.." ]; then
if [ -x "$file" ]; then if [ -x "$file" ]; then
echo "[reve] [I] Running chore: $( basename $file )" echo "[reve] [I] Running chore: $( basename "$file" )"
bash "$file" bash "$file"
else else
echo "[reve] [E] chores_mode: $file is not executable" echo "[reve] [E] chores_mode: $file is not executable"
fi fi
fi
done done
} }
main () { main () {
if (( $rt_has_mode_changed == 1 )) || [[ "$reason" == "chores_mode" ]]; then if (( rt_has_mode_changed == 1 )) || [[ "$in_reason" == "chores_mode" ]]; then
chores_mode chores_mode
fi fi
@ -127,7 +137,6 @@ while true; do
-h|--help) -h|--help)
util_help util_help
exit 0 exit 0
shift
;; ;;
-m|--mode) -m|--mode)
in_desktop_mode="$2" in_desktop_mode="$2"
@ -141,6 +150,16 @@ while true; do
in_chore_name="$2" in_chore_name="$2"
shift 2 shift 2
;; ;;
-w|--where)
which reve
exit 0
;;
--shell-completion)
in_shell_comp="$2"
shift 2
f_shell_completion
exit 0
;;
--) --)
shift shift
break break
@ -152,7 +171,5 @@ while true; do
esac esac
done done
forced_mode=$in_desktop_mode
reason=$in_reason
prepare prepare
main main