mirror of
https://github.com/m-housh/dotfiles.git
synced 2026-02-14 14:12:41 +00:00
feat: Renames window-table to windowctl, adds preview data to utils-launcher and windowctl actions.
This commit is contained in:
233
env/.local/scripts/hypr/windowctl
vendored
Executable file
233
env/.local/scripts/hypr/windowctl
vendored
Executable file
@@ -0,0 +1,233 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
THIS_FILE=${BASH_SOURCE[0]}
|
||||
THIS=$(basename $THIS_FILE)
|
||||
|
||||
usage() {
|
||||
cat <<EOF
|
||||
|
||||
Show a table with details about the currently active windows. You can choose a window and perform
|
||||
an action. This script will prompt for what you would like to do depending on the options passed.
|
||||
There are no required options for this script to work.
|
||||
|
||||
USAGE:
|
||||
|
||||
$ $THIS [OPTIONS]
|
||||
|
||||
OPTIONS:
|
||||
|
||||
--launch: Launch in a new terminal window, user will be prompted what to do with selected window.
|
||||
-c | --clip: Copy selected window's address to the system clipboard.
|
||||
-x | --close: Close the selected window.
|
||||
-f | --focus: Focus the selected window.
|
||||
-i | --ignore: Ignore the selected window.
|
||||
-m | --move: Move the selected window to another workspace and switch to that workspace.
|
||||
-s | --silent-move: Move the selected window to another workspace and remain on current workspace.
|
||||
-t | --to-workspace: Used with one of the move options to set which workspace to move to. If not supplied
|
||||
then we will prompt for a workspace to move to.
|
||||
-h | --help: Show this page.
|
||||
|
||||
NOTES:
|
||||
|
||||
If using the 'launch' option then flags passed after the '--launch' flag will be passed into the
|
||||
launched terminal, allowing you to launch with a specific mode turned on, any flags passed in prior to
|
||||
'--launch' will be ignored.
|
||||
|
||||
EOF
|
||||
}
|
||||
|
||||
window_class="com.ghostty.$THIS"
|
||||
window_padding_x="10"
|
||||
|
||||
clipboard_flag="0"
|
||||
close_flag="0"
|
||||
focus_flag="0"
|
||||
ignore_flag="0"
|
||||
launch_flag="0"
|
||||
move_flag="0"
|
||||
move_silent_flag="0"
|
||||
move_to_workspace_name=""
|
||||
|
||||
launch_args=()
|
||||
selected_value=""
|
||||
|
||||
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
if [[ $launch_flag == "1" ]]; then
|
||||
launch_args+=("$1")
|
||||
else
|
||||
if [[ $1 =~ ^-c ]] || [[ $1 =~ ^--clip ]]; then
|
||||
clipboard_flag="1"
|
||||
elif [[ $1 =~ ^-x ]] || [[ $1 =~ ^--close ]]; then
|
||||
close_flag="1"
|
||||
elif [[ $1 =~ ^-f ]] || [[ $1 =~ ^--focus ]]; then
|
||||
focus_flag="1"
|
||||
elif [[ $1 =~ ^-h ]] || [[ $1 =~ ^--help ]]; then
|
||||
usage && exit 0
|
||||
elif [[ $1 =~ ^-i ]] || [[ $1 =~ ^--ignore ]]; then
|
||||
ignore_flag="1"
|
||||
elif [[ $1 =~ ^-m ]] || [[ $1 =~ ^--move ]]; then
|
||||
move_flag="1"
|
||||
elif [[ $1 =~ ^-s ]] || [[ $1 =~ ^--silent-move ]]; then
|
||||
move_flag="1"
|
||||
move_silent_flag="1"
|
||||
elif [[ $1 =~ ^-t ]] || [[ $1 =~ ^--to-workspace ]]; then
|
||||
shift
|
||||
move_to_workspace_name=$1
|
||||
elif [[ $1 =~ ^--launch ]]; then
|
||||
launch_flag="1"
|
||||
fi
|
||||
fi
|
||||
shift
|
||||
done
|
||||
|
||||
action_footer() {
|
||||
cat <<'EOF'
|
||||
___ __ _
|
||||
/ _ |____/ /_(_)__ ___
|
||||
/ __ / __/ __/ / _ \/ _ \
|
||||
/_/ |_\__/\__/_/\___/_//_/
|
||||
|
||||
EOF
|
||||
}
|
||||
|
||||
log() {
|
||||
logging log --source "$THIS_FILE" "$@"
|
||||
}
|
||||
|
||||
ask_what_to_do_with_selection() {
|
||||
local choices=(
|
||||
"Focus the selected window.:Focus window"
|
||||
"Close the selected window.:Close window"
|
||||
"Move the selected window to another workspace, focusing the window.\n\nA workspace picker will be presented to choose which workspace to move to.:Move to workspace"
|
||||
"Move the selected window to another workspace, without focusing the window.\n\nA workspace picker will be presented to choose which workspace to move to.:Move to workspace - silent"
|
||||
"Copy the window address to the system clipboard:Copy to clipboard"
|
||||
"Move back to window picker and reload windows.:Back"
|
||||
"Quit:Quit"
|
||||
)
|
||||
choice=$(
|
||||
printf "%s\n" "${choices[@]}" |
|
||||
fzf --style=full --footer="$(action_footer)" \
|
||||
--delimiter=':' --with-nth=2 \
|
||||
--header="What should we do with the selected window?" \
|
||||
--preview-label="[ Description ]" \
|
||||
--preview='echo -e {1}'
|
||||
)
|
||||
log "Choice: $choice"
|
||||
if [[ $choice == "Quit" ]]; then
|
||||
exit 0
|
||||
elif [[ $choice == "Close window" ]]; then
|
||||
close_flag="1"
|
||||
elif [[ $choice == "Copy to clipboard" ]]; then
|
||||
clipboard_flag="1"
|
||||
elif [[ $choice == "Focus window" ]]; then
|
||||
focus_flag="1"
|
||||
elif [[ $choice == "Move to workspace" ]]; then
|
||||
move_flag="1"
|
||||
elif [[ $choice == "Move to workspace - silent" ]]; then
|
||||
move_silent_flag="1"
|
||||
move_flag="1"
|
||||
elif [[ $choice == "Refresh window list" ]]; then
|
||||
eval exec ${BASH_SOURCE[0]}
|
||||
exit 0
|
||||
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=""
|
||||
|
||||
if [[ -z $move_to_workspace_name ]]; then
|
||||
move_to_workspace_name=$(
|
||||
$SCRIPTS/hypr/workspace-picker --return-name-if-special \
|
||||
--header="Select a workspace to move window to:"
|
||||
)
|
||||
fi
|
||||
|
||||
if [[ -z $move_to_workspace_name ]]; then
|
||||
log --error "No workspace set to move window to."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if moving to a special workspace to pass the appropriate args in to hyprctl.
|
||||
if [[ $move_to_workspace_name =~ ^special ]]; then
|
||||
workspace_id=$move_to_workspace_name
|
||||
else
|
||||
workspace_id=$(hyprctl workspaces -j | jq -r ".[] | select(.name | contains(\"$move_to_workspace_name\")) | .id")
|
||||
fi
|
||||
|
||||
if [[ -z $workspace_id ]]; then
|
||||
log --error "No workspace id found for: '$move_to_workspace_name'"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
action="movetoworkspace"
|
||||
if [[ $move_silent_flag == "1" ]]; then
|
||||
action="movetoworkspacesilent"
|
||||
fi
|
||||
|
||||
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
|
||||
log "Ignore flag set, selection: '$selected_value'"
|
||||
exit 0
|
||||
elif [[ $clipboard_flag == "1" ]]; then
|
||||
log "Copying to clipboard, selection: $selected_value"
|
||||
wl-copy $selected_value
|
||||
exit 0
|
||||
elif [[ $focus_flag == "1" ]]; then
|
||||
log "Focusing window, selection: $selected_value"
|
||||
hypr_dispatch focuswindow "address:$selected_value"
|
||||
exit 0
|
||||
elif [[ $close_flag == "1" ]]; then
|
||||
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.
|
||||
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" \
|
||||
-e "${BASH_SOURCE[0]}" "${launch_args[@]}"
|
||||
else
|
||||
|
||||
# Load colors if they haven't been loaded already.
|
||||
[[ -z ${FZF_DEFAULT_OPTS} ]] &&
|
||||
[[ -f $SCRIPTS/catppuccin-colors ]] &&
|
||||
source $SCRIPTS/catppuccin-colors
|
||||
|
||||
selected_value=$("$SCRIPTS/hypr/window-picker")
|
||||
if [[ -n $selected_value ]]; then
|
||||
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.
|
||||
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.
|
||||
log "Giving up without a selection."
|
||||
fi
|
||||
fi
|
||||
Reference in New Issue
Block a user