feat: Integrates logging into scripts that need it.

This commit is contained in:
2025-10-05 16:35:19 -04:00
parent f729bedc99
commit dccb1ca0a3
11 changed files with 185 additions and 137 deletions

View File

@@ -1,12 +1,15 @@
#!/usr/bin/env bash
THIS_FILE=${BASH_SOURCE[0]}
THIS=$(basename "$THIS_FILE")
usage() {
cat <<EOF
Close window(s) by address or pattern mode.
USAGE:
$ $(basename ${BASH_SOURCE[0]}) [OPTIONS] [MODE] [ARG...]
$ $THIS [OPTIONS] [MODE] [ARG...]
MODE:
--all: Close all windows in all workspaces, any arguments are ignored.
@@ -41,6 +44,7 @@ dry_run_flag="0"
special_flag="0"
args=()
addresses=()
SCRIPTS="${SCRIPTS:-$HOME/.local/scripts}"
while [[ $# -gt 0 ]]; do
if [[ $1 =~ ^-a ]] || [[ $1 =~ ^--active-workspace ]]; then
@@ -62,11 +66,7 @@ while [[ $# -gt 0 ]]; do
done
log() {
if [[ $dry_run_flag == "1" ]]; then
echo "[DRY RUN]: $1"
else
echo "$1"
fi
logging log --source "$THIS_FILE" "$@"
}
_select_addresses() {
@@ -92,10 +92,15 @@ get_special_addresses() {
close() {
log "Closing window address: $1"
if [[ $dry_run_flag == "0" ]]; then
hyprctl dispatch closewindow "address:$1"
hyprctl dispatch closewindow "address:$1" >/dev/null 2>&1
fi
}
# Setup logging file and label
source "$SCRIPTS/hypr/logging"
setup-logging "/tmp/$THIS.log" "$THIS"
export LOG_ENABLE_DRY_RUN="$dry_run_flag"
if [[ $active_workspace_flag == "1" ]]; then
# Set addresses to active workspace windows.
id=$(hyprctl activeworkspace -j | jq -r '.id')
@@ -123,7 +128,7 @@ else
fi
if [[ ${#addresses} == 0 ]]; then
log "No windows found."
log --warning "No windows found."
exit 0
fi

View File

@@ -2,7 +2,8 @@
# Adapted from https://github.com/basecamp/omarchy/tree/master?tab=readme-ov-file
THIS=$(basename ${BASH_SOURCE[0]})
THIS_FILE=${BASH_SOURCE[0]}
THIS=$(basename "$THIS_FILE")
function usage() {
cat <<EOF
@@ -88,21 +89,9 @@ launch_flag="0"
interactive_flag="0" # This is an internal flag, to not log some things when launch is used.
interactive_mode=false
no_interactive_flag="0"
SCRIPTS="${SCRIPTS}"
SCRIPTS="${SCRIPTS:-$HOME/.local/scripts}"
XDG_DATA_HOME=${XDG_DATA_HOME}
if [[ -z "$SCRIPTS" ]]; then
echo "SCRIPTS not set"
echo "using ~/.local/scripts"
SCRIPTS=$HOME/.local/scripts
fi
if [[ -z "$XDG_DATA_HOME" ]]; then
echo "XDG_DATA_HOME not set"
echo "using ~/.local/share"
XDG_DATA_HOME=$HOME/.local/share
fi
while [[ $# -gt 0 ]]; do
if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
usage && exit 0
@@ -138,15 +127,7 @@ while [[ $# -gt 0 ]]; do
done
log() {
if [[ $dry_run == "1" ]]; then
echo -e "\e[34m[DRY_RUN]=>\e[0m $1"
else
echo -e "$1"
fi
}
log_error() {
log "\e[31m[ERROR]:\e[0m $1"
logging log --source "$THIS_FILE" "$@"
}
launch() {
@@ -164,7 +145,7 @@ check_properties() {
load_from_file() {
if [[ ! -f $1 ]]; then
log_error "File '$1' is not found or readable." && exit 1
log --error "File '$1' is not found or readable." && exit 1
fi
file=$(cat $1)
app_name=$(echo $file | jq -r '.name // ""')
@@ -213,7 +194,7 @@ set_icon_ref() {
if curl -sL -o "$icon_path" "$icon_ref"; then
icon_path="$icon_dir/$app_name.png"
else
log_error "Failed to download icon." && exit 1
log --error "Failed to download icon." && exit 1
fi
fi
else
@@ -254,6 +235,17 @@ EOF
# MAIN
################################################################################
# Setup logging file and label
source "$SCRIPTS/hypr/logging"
setup-logging "/tmp/$THIS.log" "$THIS"
export LOG_ENABLE_DRY_RUN="$dry_run"
if [[ -z "$XDG_DATA_HOME" ]]; then
log "XDG_DATA_HOME not set"
log "using ~/.local/share"
XDG_DATA_HOME=$HOME/.local/share
fi
if [[ $launch_flag == "1" ]]; then
launch && exit 0
fi
@@ -268,7 +260,7 @@ if [[ "$?" == "1" ]]; then
# Check if the '--no-interactive' flag was passed and exit with error.
[[ $no_interactive_flag == "1" ]] &&
log_error "Required properties not set and '--no-interactive' flag was passed." &&
log --error "Required properties not set and '--no-interactive' flag was passed." &&
exit 1
# Only log this if not in interactive mode.
@@ -281,7 +273,7 @@ if [[ "$?" == "1" ]]; then
check_properties
if [[ "$?" == "1" ]]; then
# Exit if they were not set during interactive mode.
log_error "You must set app name, app URL, and icon URL!" && exit 1
log --error "You must set app name, app URL, and icon URL!" && exit 1
fi
# Set flag that we are in interactive mode.

View File

@@ -1,5 +1,8 @@
#!/usr/bin/env bash
THIS_FILE=${BASH_SOURCE[0]}
THIS=$(basename "$THIS_FILE")
usage() {
cat <<EOF
@@ -10,7 +13,7 @@ This is used in keybinds and by other scripts as a general entrypoint for managi
USAGE:
$ $(basename ${BASH_SOURCE[0]}) [OPTIONS] PATTERN [LAUNCH_CMD...]
$ $THIS [OPTIONS] PATTERN [LAUNCH_CMD...]
OPTIONS:
@@ -47,6 +50,7 @@ launch_cmd=()
pattern=""
special_flag="0"
special=""
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
while [[ $# -gt 0 ]]; do
if [[ $1 == "-c" ]] || [[ $1 == "--or-close" ]]; then
@@ -77,17 +81,26 @@ while [[ $# -gt 0 ]]; do
shift
done
log() {
logging log --source "$THIS_FILE" "$@"
}
# Redirects all output of hyprctl dispatch commands.
hypr_dispatch() {
hyprctl dispatch "$@" >/dev/null 2>&1
}
toggle_special() {
if [[ -z $special ]]; then
echo "[ERROR]: No name supplied for special workspace."
log --error " No name supplied for special workspace."
exit 1
fi
hyprctl dispatch togglespecialworkspace $special
hypr_dispatch togglespecialworkspace $special
}
launch_application() {
echo "Launching..."
echo "'${launch_cmd[@]}'"
log "Launching..."
log "'${launch_cmd[@]}'"
eval exec ${launch_cmd[@]}
}
@@ -95,15 +108,19 @@ launch_application() {
# MAIN
################################################################################
# Setup logging file and label
source "$SCRIPTS/hypr/logging"
setup-logging "/tmp/$THIS.log" "$THIS"
if [[ -z $pattern ]]; then
echo "[ERROR]: Must supply a pattern to match the window class."
log --error "Must supply a pattern to match the window class."
usage && exit 1
elif [[ -z $launch_cmd ]]; then
echo "[ERROR]: Must supply a launch command to match the window class."
log --error "Must supply a launch command to match the window class."
usage && exit 1
fi
echo "Pattern: $pattern"
log "Pattern: $pattern"
addresses=$(hyprctl clients -j | jq ".[] | select(.class | contains(\"$pattern\")) | .address")
# If no addresses, then launch the application.
@@ -127,14 +144,14 @@ fi
# Check if both close and focus flags were passed, so we don't do the
# wrong thing.
if [[ $focus_flag == "1" ]] && [[ $close_flag == "1" ]]; then
echo "[ERROR]: Both focus and close flag were passed."
log --error "Both focus and close flag were passed."
exit 1
fi
for address in ${addresses[@]}; do
# Clean the address of quotes.
address=${address//\"/}
echo "Handling address: '$address'"
log "Handling address: '$address'"
if [[ $focus_active_only_flag == "1" ]] || [[ $close_active_only_flag == "1" ]]; then
# get the workspace name for the address.
@@ -142,19 +159,19 @@ for address in ${addresses[@]}; do
# check that the window is on the active workspace.
if [[ $active_window_workspace == $workspace ]]; then
echo "Performing action: '$action', on window: '$address'"
hyprctl dispatch $action "address:$address"
log "Performing action: '$action', on window: '$address'"
hypr_dispatch $action "address:$address"
# early out if focusing a window.
[[ $focus_active_only_flag ]] && exit 0
else
# the window is not on the active workspace, so skip it.
echo "Skipping window: $address"
log "Skipping window: $address"
fi
else
# We don't have the focus_active_only_flag or close_active_only_flag set, so we perform
# the action on the window.
echo "Performing action: '$action', on window: '$address'"
hyprctl dispatch $action "address:$address"
log "Performing action: '$action', on window: '$address'"
hypr_dispatch $action "address:$address"
fi
done

View File

@@ -1,6 +1,8 @@
# /usr/bin/env bash
#! /usr/bin/env bash
# Adapted from https://github.com/basecamp/omarchy/tree/master?tab=readme-ov-file
THIS_FILE=${BASH_SOURCE[0]}
THIS=$(basename "$THIS_FILE")
usage() {
cat <<EOF
@@ -10,7 +12,7 @@ essentially just generates the pattern and launch command to pass into that scri
USAGE:
$ launch-webapp [OPTIONS] <url> [ARGS...]
$ $THIS [OPTIONS] <url> [ARGS...]
OPTIONS:
@@ -43,13 +45,7 @@ browser="chromium.desktop"
url=""
launch_args=()
app_args=""
SCRIPTS="${SCRIPTS}"
if [[ -z $SCRIPTS ]]; then
echo "scripts directory not set"
echo "using ~/.local/scripts"
SCRIPTS=~/.local/scripts
fi
SCRIPTS="${SCRIPTS:-$HOME/.local/scripts}"
while [[ $# -gt 0 ]]; do
if [[ $1 =~ ^--special ]] || [[ $1 =~ ^-s ]]; then
@@ -77,20 +73,28 @@ pattern() {
echo $pattern
}
log() {
logging log --source "$THIS_FILE" "$@"
}
##################################################
# MAIN
##################################################
# setup logging file and label
source "$SCRIPTS/hypr/logging"
setup-logging "/tmp/$THIS.log" "$THIS"
if [[ -z $url ]]; then
echo "[ERROR]: Must supply a url." && usage && exit 1
log --error "Must supply a url." && usage && exit 1
fi
# Any left over args after "--"
app_args="$@"
echo "URL: $url"
echo "Launch args: ${launch_args[@]}"
echo "App args: ${app_args}"
log "URL: $url"
log "Launch args: ${launch_args[@]}"
log "App args: ${app_args}"
$SCRIPTS/hypr/launch "${launch_args[@]}" "$(pattern)" \
setsid uwsm app -- $(sed -n 's/^Exec=\([^ ]*\).*/\1/p' {~/.local,~/.nix-profile,/usr}/share/applications/$browser 2>/dev/null | head -1) --app="$url" "$app_args"

View File

@@ -31,6 +31,9 @@
LOG_FILE=(${LOG_FILE:-})
LOG_INVOCATION_ID=${LOG_INVOCATION_ID:-}
LOG_LABEL=(${LOG_LABEL:-})
# Run in dry run mode, which just prints to the console and does
# not log to the files.
LOG_ENABLE_DRY_RUN=${LOG_ENABLE_DRY_RUN:-"0"}
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
warn_flag="0"
@@ -96,17 +99,23 @@ logging() {
exit 1
fi
msg=""
# Loop over log files logging message to each file.
for i in "${!LOG_FILE[@]}"; do
prefix="[id: $LOG_INVOCATION_ID][time: $($SCRIPTS/isosec)][label: ${LOG_LABEL[i]}][source: $source_file] : "
msg="$prefix $(__msg ${args[@]})"
echo "$msg" >>${LOG_FILE[i]}
done
msg="$(__msg ${args[@]})"
# Also log errors and warning messages to the console.
if [[ $error_flag == "1" ]] || [[ $warn_flag == "1" ]]; then
echo "${msg##* : }"
if [[ $LOG_ENABLE_DRY_RUN == "0" ]]; then
# Loop over log files logging message to each file.
for i in "${!LOG_FILE[@]}"; do
prefix="[id: $LOG_INVOCATION_ID][time: $($SCRIPTS/isosec)][label: ${LOG_LABEL[i]}][source: $source_file] : "
m="$prefix $msg"
echo -e "$m" >>${LOG_FILE[i]}
done
# Also log errors and warning messages to the console.
if [[ $error_flag == "1" ]] || [[ $warn_flag == "1" ]]; then
echo -e "$msg"
fi
else
# Dry run mode, so just log to the console
echo -e "\e[34m[DRY RUN]:\e[0m $msg"
fi
}

View File

@@ -1,5 +1,8 @@
#!/usr/bin/env bash
THIS_FILE=${BASH_SOURCE[0]}
THIS=$(basename "$THIS_FILE")
usage() {
cat <<EOF
@@ -12,7 +15,7 @@ the workspace.
USAGE:
$ $(basename ${BASH_SOURCE[0]}) [OPTIONS] <workspace>
$ $THIS [OPTIONS] <workspace>
OPTIONS:
@@ -23,6 +26,7 @@ EOF
target_workspace=""
active_workspace=$(hyprctl activewindow -j | jq -r '.workspace.name')
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
while [[ $# -gt 0 ]]; do
if [[ $1 =~ ^-h ]] || [[ $1 =~ ^--help ]]; then
@@ -33,15 +37,22 @@ while [[ $# -gt 0 ]]; do
shift
done
log() {
logging log --source "$THIS_FILE" "$@"
}
source "$SCRIPTS/hypr/logging"
setup-logging "/tmp/$THIS.log" "$THIS"
if [[ -z target_workspace ]]; then
echo "[ERROR]: Must supply a workpsace to switch to."
log --error "Must supply a workpsace to switch to."
usage && exit 1
fi
# If active window is on a special workspace, then toggle the special workspace off.
if [[ $active_workspace =~ ^special ]]; then
echo "Toggling special workspace: '$active_workspace'"
hyprctl dispatch togglespecialworkspace ${active_workspace#special:}
log "Toggling special workspace: '$active_workspace'"
hyprctl dispatch togglespecialworkspace ${active_workspace#special:} >/dev/null 2>&1
# Only toggle the special workspace if trying to switch to previous and we're currently
# on a special workspace.
@@ -50,5 +61,5 @@ if [[ $active_workspace =~ ^special ]]; then
fi
fi
echo "Switching to workspace: $target_workspace"
hyprctl dispatch workspace $target_workspace
log "Switching to workspace: $target_workspace"
hyprctl dispatch workspace $target_workspace >/dev/null 2>&1

View File

@@ -1,10 +1,7 @@
#!/usr/bin/env bash
# Uninstalls '.desktop' applications, including their icon.
#
# This is primarily used for uninstalling web app's, if a
# desktop app was installed via the package manager, then the
# package manager should be used to uninstall the application.
THIS_FILE=${BASH_SOURCE[0]}
THIS=$(basename "$THIS_FILE")
usage() {
cat <<EOF
@@ -15,11 +12,12 @@ package manager, then it should be used to uninstall the application.
Usage:
uninstall-desktop-app [OPTIONS] [FILE...]
$ $THIS [OPTIONS] [FILE...]
OPTIONS:
--dry-run: Perform but don't actually remove anything.
-h | --help: Show the help page.
OPTIONS:
--dry-run: Perform but don't actually remove anything.
-h | --help: Show the help page.
If no files are supplied, then an interactive session will be
started that allows you to choose the applications to remove.
@@ -32,27 +30,31 @@ interactive_mode="0"
dry_run="0"
help_flag="0"
XDG_DATA_HOME=${XDG_DATA_HOME}
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
while [[ $# -gt 0 ]]; do
if [[ $1 =~ ^--dry ]]; then
dry_run="1"
elif [[ $1 =~ ^-h ]] || [[ $1 =~ ^--h ]]; then
help_flag="1"
usage && exit 0
else
files+=("$1")
fi
shift
done
# Early out for help option.
if [[ $help_flag == "1" ]]; then
usage
exit 0
fi
log() {
logging log --source "$THIS_FILE" "$@"
}
############################## MAIN ##############################
source "$SCRIPTS/hypr/logging"
setup-logging "/tmp/$THIS.log" "$THIS"
if [[ -z $XDG_DATA_HOME ]]; then
echo "xdg data home is not set"
echo "using: ~/.local/share"
log "xdg data home is not set"
log "using: ~/.local/share"
XDG_DATA_HOME=$HOME/.local/share
fi
@@ -65,16 +67,6 @@ if [[ ${#files} == 0 ]]; then
)
fi
log() {
if [[ $dry_run == "1" ]]; then
echo "[DRY RUN]: $1"
else
echo "$1"
fi
}
############################## MAIN ##############################
for f in ${files[@]}; do
icon=""

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env bash
THIS=$(basename ${BASH_SOURCE[0]})
THIS_FILE=${BASH_SOURCE[0]}
THIS=$(basename $THIS_FILE)
usage() {
cat <<EOF
@@ -27,7 +28,6 @@ launch_flag="0"
rows=()
invocation_id=${RANDOM}
LOG_FILE=${LOG_FILE:-/tmp/$THIS.log}
XDG_CONFIG_HOME=${XDG_CONFIG_HOME}
SCRIPTS=${SCRIPTS}
@@ -42,15 +42,9 @@ while [[ $# -gt 0 ]]; do
done
log() {
echo "[$invocation_id]:[$($SCRIPTS/isosec)]:[$THIS]: $1" >>$LOG_FILE
logging log --source "$THIS_FILE" "$@"
}
if [[ -z $XDG_CONFIG_HOME ]]; then
log "XDG_CONFIG_HOME not set"
log "using ~/.config"
XDG_CONFIG_HOME=$HOME/.config
fi
launch() {
ghostty --class=$window_class --window-padding-x=$window_padding_x \
--keybind="ctrl+c=quit" \
@@ -81,6 +75,16 @@ generate_rows() {
# MAIN
################################################################################
# Setup logging file and label.
source "$SCRIPTS/hypr/logging"
setup-logging "/tmp/$THIS.log" "$THIS"
if [[ -z $XDG_CONFIG_HOME ]]; then
log "XDG_CONFIG_HOME not set"
log "using ~/.config"
XDG_CONFIG_HOME=$HOME/.config
fi
if [[ -z $config_file ]]; then
log "No config file set."
log "Using ~/.config/utils-launcher/config.json"

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env bash
THIS=$(basename ${BASH_SOURCE[0]})
THIS_FILE=${BASH_SOURCE[0]}
THIS=$(basename $THIS_FILE)
usage() {
cat <<EOF

View File

@@ -24,7 +24,7 @@ uses_supplied_footer="0"
fzf_opts=()
rows=()
window_data=$(hyprctl clients -j | jq 'sort_by(.workspace.id)')
SCRIPTS=${SCRIPTS}
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
while [[ $# -gt 0 ]]; do
if [[ $1 =~ ^--footer ]]; then
@@ -36,10 +36,6 @@ while [[ $# -gt 0 ]]; do
shift
done
if [[ -z $SCRIPTS ]]; then
SCRIPTS=$HOME/.local/scripts
fi
footer() {
cat <<'EOF'
_ ___ __

View File

@@ -1,5 +1,8 @@
#!/usr/bin/env bash
THIS_FILE=${BASH_SOURCE[0]}
THIS=$(basename $THIS_FILE)
usage() {
cat <<EOF
@@ -9,7 +12,7 @@ There are no required options for this script to work.
USAGE:
$ $(basename ${BASH_SOURCE[0]}) [OPTIONS]
$ $THIS [OPTIONS]
OPTIONS:
@@ -50,7 +53,7 @@ show_window_class_flag="0"
launch_args=()
selected_value=""
SCRIPTS=${SCRIPTS}
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
while [[ $# -gt 0 ]]; do
if [[ $launch_flag == "1" ]]; then
@@ -93,6 +96,10 @@ action_footer() {
EOF
}
log() {
logging log --source "$THIS_FILE" "$@"
}
show_table_return_choice() {
local opts=()
if [[ $show_window_class_flag == "1" ]]; then
@@ -115,7 +122,7 @@ ask_what_to_do_with_selection() {
printf "%s\n" "${choices[@]}" |
fzf --style=full --footer="$(action_footer)" --header="What should we do with the selected window?"
)
echo "Choice: $choice"
log "Choice: $choice"
if [[ $choice == "Quit" ]]; then
exit 0
elif [[ $choice == "Close window" ]]; then
@@ -135,6 +142,12 @@ ask_what_to_do_with_selection() {
fi
}
# Prevent hyprctl dispatch calls from printing to the console.
hypr_dispatch() {
hyprctl dispatch "$@" >/dev/null 2>&1
return $?
}
move_to_workspace() {
local workspace_id=""
@@ -146,7 +159,7 @@ move_to_workspace() {
fi
if [[ -z $move_to_workspace_name ]]; then
echo "[ERROR]: No workspace set to move window to."
log --error "No workspace set to move window to."
exit 1
fi
@@ -158,7 +171,7 @@ move_to_workspace() {
fi
if [[ -z $workspace_id ]]; then
echo "[ERROR]: No workspace id found for: '$move_to_workspace_name'"
log --error "No workspace id found for: '$move_to_workspace_name'"
exit 1
fi
@@ -167,37 +180,41 @@ move_to_workspace() {
action="movetoworkspacesilent"
fi
echo "Moving window: '$selected_value' to workspace: '$workspace_id'"
hyprctl dispatch $action "$workspace_id,address:$selected_value"
log "Moving window: '$selected_value' to workspace: '$workspace_id'"
hypr_dispatch $action "$workspace_id,address:$selected_value"
}
handle_selected_value() {
if [[ $ignore_flag == "1" ]]; then
echo "Ignore flag set, selection: '$selected_value'"
log "Ignore flag set, selection: '$selected_value'"
exit 0
elif [[ $clipboard_flag == "1" ]]; then
echo "Copying to clipboard, selection: $selected_value"
log "Copying to clipboard, selection: $selected_value"
wl-copy $selected_value
exit 0
elif [[ $focus_flag == "1" ]]; then
echo "Focusing window, selection: $selected_value"
hyprctl dispatch focuswindow "address:$selected_value"
log "Focusing window, selection: $selected_value"
hypr_dispatch focuswindow "address:$selected_value"
exit 0
elif [[ $close_flag == "1" ]]; then
echo "Closing window, selection: $selected_value"
hyprctl dispatch closewindow "address:$selected_value"
log "Closing window, selection: $selected_value"
hypr_dispatch closewindow "address:$selected_value"
exit 0
elif [[ $move_flag == "1" ]] || [[ $move_silent_flag == "1" ]]; then
move_to_workspace && exit 0
fi
# TODO: Choose from list of what to do with the selected_value.
echo "No flag set, selection: '$selected_value'"
log "No flag set, selection: '$selected_value'"
}
##################################################
# MAIN
##################################################
# Setup logging file and label.
source "$SCRIPTS/hypr/logging"
setup-logging "/tmp/$THIS.log" "$THIS"
if [[ $launch_flag == "1" ]]; then
ghostty --class="$window_class" --window-padding-x="$window_padding_x" \
--keybind="ctrl+c=quit" \
@@ -214,11 +231,11 @@ else
handle_selected_value
# If we got here then no flag was passed in initially on how to handle the
# selected window, so ask what they'd like to do. Then handle it.
echo "Asking what to do with selction."
log "Asking what to do with selction."
ask_what_to_do_with_selection
[[ -n $selected_value ]] && handle_selected_value
# If you make it here, We just give up... Don't start an endless loop.
echo "Giving up without a selection."
log "Giving up without a selection."
fi
fi