diff --git a/env/.config/utils-launcher/config.json b/env/.config/utils-launcher/config.json index b02cd13..20c9da8 100644 --- a/env/.config/utils-launcher/config.json +++ b/env/.config/utils-launcher/config.json @@ -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", diff --git a/env/.local/scripts/hypr/monitorctl b/env/.local/scripts/hypr/monitorctl new file mode 100755 index 0000000..32a8a7d --- /dev/null +++ b/env/.local/scripts/hypr/monitorctl @@ -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 < + +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 --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 < + +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 diff --git a/env/.local/scripts/hypr/toggle-internal-monitor b/env/.local/scripts/hypr/toggle-internal-monitor deleted file mode 100755 index f645e3e..0000000 --- a/env/.local/scripts/hypr/toggle-internal-monitor +++ /dev/null @@ -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 diff --git a/env/.local/scripts/hypr/monitor-picker b/env/.local/scripts/hypr/utils/monitors/monitor-picker similarity index 90% rename from env/.local/scripts/hypr/monitor-picker rename to env/.local/scripts/hypr/utils/monitors/monitor-picker index 9d4cbc1..b0ab793 100755 --- a/env/.local/scripts/hypr/monitor-picker +++ b/env/.local/scripts/hypr/utils/monitors/monitor-picker @@ -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 < + +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