Created
October 23, 2025 20:38
-
-
Save myrkvi/bb118eb02a08f0be55e510ec745c3653 to your computer and use it in GitHub Desktop.
Bash script for doing things with monitors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env bash | |
| function verify_deps() { | |
| command -v niri >/dev/null 2>&1 || { echo >&2 "niri is required but it's not installed. Aborting."; exit 1; } | |
| command -v jq >/dev/null 2>&1 || { echo >&2 "jq is required but it's not installed. Aborting."; exit 1; } | |
| command -v fuzzel >/dev/null 2>&1 || { echo >&2 "fuzzel is required but it's not installed. Aborting."; exit 1; } | |
| command -v wl-mirror >/dev/null 2>&1 || { echo >&2 "wl-mirror is required but it's not installed. Aborting."; exit 1; } | |
| } | |
| function select_monitor() { | |
| niri msg -j outputs | jq 'keys | .[]' --raw-output | fuzzel --dmenu -p "Select monitor: " | |
| } | |
| function get_monitor_modes() { | |
| local monitor | |
| monitor=$1 | |
| niri msg -j outputs \ | |
| | jq '."'"$monitor"'".modes.[] | (.width | tostring) + "x" + (.height | tostring) + "@" + (.refresh_rate / 1000 | tostring)' --raw-output \ | |
| | fuzzel --dmenu -p "Select mode: " | |
| } | |
| function select_action_with_monitor() { | |
| local monitor | |
| monitor=$1 | |
| action="$(echo -e "Turn on\nTurn off\nSet mode (resolution)\nSet scale\nSet transform\nSet position\nSet VRR\nMirror this display" | fuzzel --dmenu -p "Select action for $monitor: ")" | |
| case $action in | |
| "Turn on") | |
| niri msg output "$monitor" on | |
| ;; | |
| "Turn off") | |
| niri msg output "$monitor" off | |
| ;; | |
| "Set mode (resolution)") | |
| local mode | |
| mode=$(get_monitor_modes "$monitor") | |
| niri msg output "$monitor" mode "$mode" | |
| ;; | |
| "Set scale") | |
| local scale | |
| scale="$(echo -e "1.0\n1.25\n1.5\n2.0" | fuzzel --dmenu -p "Select scale (or enter custom): ")" | |
| niri msg output "$monitor" scale "$(printf "%.2f" "$scale")" | |
| ;; | |
| "Set transform") | |
| local transform | |
| transform="$(echo -e "normal\n90\n180\n270\nflipped\nflipped-90\nflipped-180\nflipped-270" | fuzzel --dmenu -p "Select transform: ")" | |
| niri msg output "$monitor" transform "$transform" | |
| ;; | |
| "Set position") | |
| local position x y | |
| position="$(fuzzel --dmenu --prompt "Enter position as 'x y'")" | |
| x="$(echo $position | cut -d' ' -f1)" | |
| y="$(echo $position | cut -d' ' -f2)" | |
| niri msg output "$monitor" position "${x}x${y}" | |
| ;; | |
| "Set VRR") | |
| local vrr | |
| vrr="$(echo -e "on\noff" | fuzzel --dmenu -p "Set VRR: ")" | |
| niri msg output "$monitor" vrr "$vrr" | |
| ;; | |
| "Mirror this display") | |
| local target_monitor mirror_windows | |
| target_monitor="$(select_monitor)" | |
| wl-mirror $monitor & | |
| sleep 1 | |
| mirror_windows=$(niri msg -j windows | jq '.[] | select(.app_id == "at.yrlf.wl_mirror") | .id' --raw-output) | |
| for window in $mirror_windows; do | |
| niri msg action move-window-to-monitor --id "$window" "$target_monitor" | |
| niri msg action fullscreen-window --id "$window" | |
| done | |
| ;; | |
| esac | |
| } | |
| verify_deps | |
| monitor="$(select_monitor)" | |
| if [ -n "$monitor" ]; then | |
| select_action_with_monitor "$monitor" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment