Skip to content

Instantly share code, notes, and snippets.

@maluta
Last active February 29, 2024 10:02
Show Gist options
  • Save maluta/40e614bb633c0c534896a16e0e0ac5d3 to your computer and use it in GitHub Desktop.
Save maluta/40e614bb633c0c534896a16e0e0ac5d3 to your computer and use it in GitHub Desktop.
Quick and dirty script to manage my monitor preferences on my computer
import subprocess
from rich.console import Console
from rich.markup import escape
from rich.prompt import Prompt
# Initialize Rich console
console = Console()
def run_xrandr_command(command):
"""
Runs an xrandr command and prints the output using Rich.
"""
try:
# Execute the xrandr command
result = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# Print the standard output
if result.stdout:
console.print(f"[bold green]Output:[/bold green] {escape(result.stdout)}")
# Print the standard error, if any
if result.stderr:
console.print(f"[bold red]Error:[/bold red] {escape(result.stderr)}", style="bold red")
except subprocess.CalledProcessError as e:
console.print(f"[bold red]An error occurred while running the command:[/bold red] {escape(str(e))}", style="bold red")
def display_menu():
console.print("[bold]Please select an option:[/bold]", style="bold blue")
console.print("[1] Ligar monitor")
console.print("[2] Desligar monitor")
console.print("[3] Rotacionar monitor")
console.print("[4] Voltar ao normal")
console.print("[5] Incluir monitor externo (HDMI-1)")
console.print("[6] Remover monitor externo (HDMI-1)")
console.print("[7] Sair")
choice = Prompt.ask("Enter your choice", choices=["1", "2", "3", "4", "5", "6"], default="6")
return choice
def main():
device_name = "eDP-1"
while True:
choice = display_menu()
if choice == "5":
command = f"xrandr --output HDMI-1 --auto --right-of {device_name}"
elif choice == "2":
command = f"xrandr --output {device_name} --off"
elif choice == "3":
command = f"xrandr --output {device_name} --rotate right"
elif choice == "4":
command = f"xrandr --output {device_name} --rotate normal"
elif choice == "5":
command = f"xrandr --output {device_name} --auto"
elif choice == "1":
command = f"xrandr --output {device_name} --auto"
elif choice == "6":
command = f"xrandr --output HDMI-1 --off"
elif choice == "7":
console.print("[bold green]Exiting...[/bold green]", style="bold green")
break
else:
console.print("[bold red]Invalid choice, please try again.[/bold red]", style="bold red")
continue
run_xrandr_command(command)
console.print("-" * 40) # Print a separator line
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment