feat: Adds monitorctl script to handle monitor subcommands.

This commit is contained in:
2025-10-08 10:38:05 -04:00
parent 0922f7578f
commit 18fce8e2bb
5 changed files with 200 additions and 18 deletions

View File

@@ -17,7 +17,7 @@
{
"name": "Monitors - stats / picker",
"description": "View information from hyprctl about currently connected monitors.",
"exec": "$SCRIPTS/hypr/monitor-picker"
"exec": "$SCRIPTS/hypr/monitorctl picker"
},
{
"name": "Waybar - restart",

111
env/.local/scripts/hypr/monitorctl vendored Executable file
View File

@@ -0,0 +1,111 @@
#!/usr/bin/env bash
set -e
set -o nounset
set -o pipefail
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
THIS_FILE=${BASH_SOURCE[0]}
LOG_LABEL=$(basename "$THIS_FILE")
THIS=${THIS:-$LOG_LABEL}
LOG_FILE=${LOG_FILE:-"/tmp/$LOG_LABEL.log"}
window_class="com.ghostty.$THIS"
window_padding_x="10"
usage() {
cat <<EOF
Utility for monitor controls.
USAGE:
$ $THIS <command> <flags>
FLAGS:
-h | --help: Show this help page.
COMMANDS:
launch: Launch in a new terminal window.
picker: Shows information about the monitor(s).
toggle: Enable/disable a connected monitor.
Run "$THIS <command> --help" for more information about a command.
EOF
}
# Logging utility function, use in place of echo.
log() {
logging log --source "$THIS_FILE" "$@"
}
launch_usage() {
cat <<EOF
Launches a new terminal window that runs the given '$THIS' command. If no command
is supplied, then it will launch the picker command.
USAGE:
$ $THIS launch <command> <flags>
FLAGS:
-h | --help: Show this help page.
Run "$THIS --help" to show available commands that can ba launched in the new window.
EOF
}
launch() {
if [[ $@ =~ ^-h ]] || [[ $@ =~ ^--help ]]; then
launch_usage && exit 0
fi
log "Launching terminal..."
ghostty --class=$window_class --window-padding-x=$window_padding_x \
--keybind="ctrl+c=quit" \
-e $THIS_FILE "$@"
}
################################################################################
# MAIN
################################################################################
# Setup logging file and label.
source "$SCRIPTS/hypr/logging"
setup-logging "$LOG_FILE" "$LOG_LABEL"
log "Starting args: '$@'"
while [[ $# -gt 0 ]]; do
if [[ $1 == "launch" ]]; then
shift
if [[ -z "$@" ]]; then
launch picker && exit $?
else
launch "$@" && exit $?
fi
elif [[ $1 == "picker" ]]; then
shift
log "Showing picker..."
THIS="$THIS picker" "$SCRIPTS/hypr/utils/monitors/monitor-picker" "$@"
exit $?
elif [[ $1 == "toggle" ]]; then
shift
THIS="$THIS picker" "$SCRIPTS/hypr/utils/monitors/monitor-toggle" "$@"
exit $?
elif [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
usage && exit 0
else
log --error "Unhandle command: $1" && exit 1
fi
shift
done
# If we've reached here then no commands were passed / handled.
usage && exit 1

View File

@@ -1,12 +0,0 @@
#!/bin/zsh
#
# Toggles the state of the internal laptop monitor, which is useful
# when I'm connected to an external monitor / docks.
monitor="eDP-1"
if hyprctl monitors | grep -q "$monitor"; then
hyprctl keyword monitor "$monitor,disable" 1>/dev/null
else
hyprctl keyword monitor "$monitor,enable" 1>/dev/null
fi

View File

@@ -1,8 +1,15 @@
#!/usr/bin/env bash
set -e
set -o nounset
set -o pipefail
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
THIS_FILE=${BASH_SOURCE[0]}
THIS=$(basename $THIS_FILE)
LOG_LABEL=$(basename "$THIS_FILE")
THIS=${THIS:-$LOG_LABEL}
LOG_FILE=${LOG_FILE:-"/tmp/$LOG_LABEL.log"}
FZF_DEFAULT_OPTS=${FZF_DEFAULT_OPTS:-""}
usage() {
cat <<EOF
@@ -12,13 +19,13 @@ like to pass. The id of the monitor is returned from this script upon selection.
USAGE:
$ $THIS [OPTIONS] [FZF_OPTIONS...]
OPTIONS:
$ $THIS [flags] [FZF_OPTIONS...]
FLAGS:
--no-default-footer: Disable the 'Monitors' footer (supplying your own footer disables default as well).
--no-preview: Disables the monitor stats preview window.
-h | --help: Show this help page.
EOF
}
@@ -76,7 +83,7 @@ generate_rows() {
# Setup logging file and label.
source "$SCRIPTS/hypr/logging"
setup-logging "$THIS"
setup-logging "$LOG_FILE" "$LOG_LABEL"
[[ -z ${FZF_DEFAULT_OPTS} ]] &&
[[ -f $SCRIPTS/catppuccin-colors ]] &&

View File

@@ -0,0 +1,76 @@
#!/usr/bin/env bash
# Toggles the state of the internal laptop monitor, which is useful
# when I'm connected to an external monitor / docks.
set -o nounset
set -o pipefail
set -e
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
THIS_FILE=${BASH_SOURCE[0]}
LOG_LABEL=$(basename "$THIS_FILE")
THIS=${THIS:-$LOG_LABEL}
LOG_FILE=${LOG_FILE:-"/tmp/$LOG_LABEL.log"}
usage() {
cat <<EOF
Toggles the state (enable/disable) of a monitor.
USAGE:
$ $THIS <flags> <id>
FLAGS:
-i | --internal: Toggle the internal monitor.
-h | --help: Show this help page.
EOF
}
# Logging utility function, use in place of echo.
log() {
logging log --source "$THIS_FILE" "$@"
}
toggle() {
local monitor="${1:-""}"
if [[ -z $monitor ]]; then
log --error "No monitor supplied." && exit 1
fi
if hyprctl monitors | grep -q "$monitor"; then
hyprctl keyword monitor "$monitor,disable" 1>/dev/null
else
hyprctl keyword monitor "$monitor,enable" 1>/dev/null
fi
}
################################################################################
# MAIN
################################################################################
# Setup logging file and label.
source "$SCRIPTS/hypr/logging"
setup-logging "$LOG_FILE" "$LOG_LABEL"
monitor=""
while [[ $# -gt 0 ]]; do
if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
usage && exit 0
elif [[ $1 == "-i" ]] || [[ $1 == "--internal" ]]; then
monitor="eDP-1"
else
log --error "Unhandled option: $1" && exit 1
fi
shift
done
if [[ -z $monitor ]]; then
read -p "Monitor id: " monitor
fi
toggle $monitor