diff --git a/env/.local/scripts/hypr/close-windows b/env/.local/scripts/hypr/close-windows index 0edf1e7..add7e29 100755 --- a/env/.local/scripts/hypr/close-windows +++ b/env/.local/scripts/hypr/close-windows @@ -1,12 +1,15 @@ #!/usr/bin/env bash +THIS_FILE=${BASH_SOURCE[0]} +THIS=$(basename "$THIS_FILE") + usage() { cat </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 diff --git a/env/.local/scripts/hypr/install-webapp b/env/.local/scripts/hypr/install-webapp index c45f4ae..857f257 100755 --- a/env/.local/scripts/hypr/install-webapp +++ b/env/.local/scripts/hypr/install-webapp @@ -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 <\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. diff --git a/env/.local/scripts/hypr/launch b/env/.local/scripts/hypr/launch index 13cada8..59ee750 100755 --- a/env/.local/scripts/hypr/launch +++ b/env/.local/scripts/hypr/launch @@ -1,5 +1,8 @@ #!/usr/bin/env bash +THIS_FILE=${BASH_SOURCE[0]} +THIS=$(basename "$THIS_FILE") + usage() { cat </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 diff --git a/env/.local/scripts/hypr/launch-webapp b/env/.local/scripts/hypr/launch-webapp index d7abcc6..94d2eb7 100755 --- a/env/.local/scripts/hypr/launch-webapp +++ b/env/.local/scripts/hypr/launch-webapp @@ -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 < [ARGS...] + $ $THIS [OPTIONS] [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" diff --git a/env/.local/scripts/hypr/logging b/env/.local/scripts/hypr/logging index 6273039..73b4183 100755 --- a/env/.local/scripts/hypr/logging +++ b/env/.local/scripts/hypr/logging @@ -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 } diff --git a/env/.local/scripts/hypr/switch-to-workspace b/env/.local/scripts/hypr/switch-to-workspace index fac7ea7..b501f9a 100755 --- a/env/.local/scripts/hypr/switch-to-workspace +++ b/env/.local/scripts/hypr/switch-to-workspace @@ -1,5 +1,8 @@ #!/usr/bin/env bash +THIS_FILE=${BASH_SOURCE[0]} +THIS=$(basename "$THIS_FILE") + usage() { cat < + $ $THIS [OPTIONS] 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 diff --git a/env/.local/scripts/hypr/uninstall-desktop-app b/env/.local/scripts/hypr/uninstall-desktop-app index 872e463..e0c622b 100755 --- a/env/.local/scripts/hypr/uninstall-desktop-app +++ b/env/.local/scripts/hypr/uninstall-desktop-app @@ -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 <>$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" diff --git a/env/.local/scripts/hypr/waybarctl b/env/.local/scripts/hypr/waybarctl index fad2a42..b6b9c8c 100755 --- a/env/.local/scripts/hypr/waybarctl +++ b/env/.local/scripts/hypr/waybarctl @@ -1,6 +1,7 @@ #!/usr/bin/env bash -THIS=$(basename ${BASH_SOURCE[0]}) +THIS_FILE=${BASH_SOURCE[0]} +THIS=$(basename $THIS_FILE) usage() { cat </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