toml
[tasks.monitors-home]
description = """Save or restore monitor layout by profile (e.g. standing, sitting) -
see [[CLI/Tool/displayplacer/How To/Save and Restore Monitor Configurations with mise]]
"""
usage = '''
arg "action" "Action to perform: list, save, restore, or edit"
arg "profile" "Profile name (e.g., standing, sitting)"
flag "-s --save" "Save current layout (alternative to 'save' action)"
flag "-l --list" "List available profiles (alternative to 'list' action)"
'''
run = """
#!/usr/bin/env bash
set -euo pipefail
# Create layouts directory if it doesn't exist
mkdir -p "$HOME/.config/mise-monitor-layouts"
# Determine action and profile
ACTION="{{arg(name='action')}}"
PROFILE="{{arg(name='profile')}}"
SAVE_FLAG="{{flag(name='save')}}"
LIST_FLAG="{{flag(name='list')}}"
# Handle flag-based actions
if [[ "$LIST_FLAG" == "true" ]]; then
ACTION="list"
fi
if [[ "$SAVE_FLAG" == "true" ]]; then
ACTION="save"
fi
# Handle list action
if [[ "$ACTION" == "list" ]]; then
LAYOUT_DIR="$HOME/.config/mise-monitor-layouts"
if [[ ! -d "$LAYOUT_DIR" ]] || [[ -z "$(ls -A "$LAYOUT_DIR" 2>/dev/null)" ]]; then
echo "No saved layouts found."
echo "Use 'mise run monitors-home save <profile>' to save a layout."
exit 0
fi
echo "Available profiles:"
for file in "$LAYOUT_DIR"/*.layout; do
if [[ -f "$file" ]]; then
basename "$file" .layout
fi
done
exit 0
fi
# Handle save action
if [[ "$ACTION" == "save" ]]; then
if [[ -z "$PROFILE" ]]; then
echo "Error: Profile name required for save action"
echo "Usage: mise run monitors-home save <profile>"
echo " or: mise run monitors-home <profile> --save"
exit 1
fi
LAYOUT_FILE="$HOME/.config/mise-monitor-layouts/${PROFILE}.layout"
displayplacer list > "$LAYOUT_FILE"
echo "Saved layout: $PROFILE"
exit 0
fi
# Handle restore action
if [[ "$ACTION" == "restore" ]]; then
if [[ -z "$PROFILE" ]]; then
echo "Error: Profile name required for restore action"
echo "Usage: mise run monitors-home restore <profile>"
exit 1
fi
LAYOUT_FILE="$HOME/.config/mise-monitor-layouts/${PROFILE}.layout"
if [[ ! -f "$LAYOUT_FILE" ]]; then
echo "No layout saved for profile: $PROFILE"
echo "Use 'mise run monitors-home list' to see available profiles"
exit 1
fi
# Read the layout file and extract the displayplacer command
LAYOUT_CONFIG="$(cat "$LAYOUT_FILE")"
echo "Applying layout for profile: $PROFILE"
# Extract the displayplacer command from the end of the file
# The command is on the last line that starts with "displayplacer"
DISPLAYPLACER_CMD="$(echo "$LAYOUT_CONFIG" | grep '^displayplacer ' | tail -n1)"
if [[ -z "$DISPLAYPLACER_CMD" ]]; then
echo "Error: Could not find displayplacer command in layout file"
echo "The layout file may be corrupted or in an unexpected format"
exit 1
fi
echo "Executing: $DISPLAYPLACER_CMD"
# Execute the displayplacer command
eval "$DISPLAYPLACER_CMD"
if [[ $? -eq 0 ]]; then
echo "Successfully restored layout: $PROFILE"
else
echo "Error: Failed to apply layout for profile: $PROFILE"
echo "The saved layout may be invalid or incompatible with current display setup"
exit 1
fi
exit 0
fi
# Handle edit action
if [[ "$ACTION" == "edit" ]]; then
if [[ -z "$PROFILE" ]]; then
echo "Error: Profile name required for edit action"
echo "Usage: mise run monitors-home edit <profile>"
exit 1
fi
LAYOUT_FILE="$HOME/.config/mise-monitor-layouts/${PROFILE}.layout"
if [[ ! -f "$LAYOUT_FILE" ]]; then
echo "No layout saved for profile: $PROFILE"
echo "Use 'mise run monitors-home list' to see available profiles"
exit 1
fi
# Determine which editor to use
EDITOR_CMD=""
if [[ -n "${VISUAL:-}" ]]; then
EDITOR_CMD="$VISUAL"
elif [[ -n "${EDITOR:-}" ]]; then
EDITOR_CMD="$EDITOR"
else
echo "Error: No editor found. Please set VISUAL or EDITOR environment variable."
exit 1
fi
echo "Opening layout file for profile '$PROFILE' with $EDITOR_CMD"
$EDITOR_CMD "$LAYOUT_FILE"
exit 0
fi
echo "Unknown action: $ACTION"
echo "Valid actions: list, save, restore, edit"
exit 1
"""