nushell/bin/x

128 lines
4.1 KiB
Plaintext
Raw Normal View History

2024-03-31 13:01:16 +01:00
#!/usr/bin/env nu
# default view: only main screen
def "main d" [] {
2024-03-31 13:10:15 +01:00
let res = main | where name == $env.PRIMARY_SCREEN | get resolutions | get 0 | get 0
xrandr --output $env.PRIMARY_SCREEN --mode $res --primary --output $env.SECONDARY_SCREEN --off --output HDMI-0 --off
2024-03-31 13:01:16 +01:00
}
# bureau view: both screens side to side
def "main b" [] {
2024-03-31 15:55:50 +01:00
let screens = main
let secondary_screen_connected = $screens | where name == $env.SECONDARY_SCREEN | get 0 | get connected
if not $secondary_screen_connected {
notify-send "Couldn't set screen" "Secondary screen disconnected"
return
}
let res = $screens | where name == $env.PRIMARY_SCREEN | get resolutions | get 0 | get 0
2024-03-31 13:10:15 +01:00
xrandr --output $env.PRIMARY_SCREEN --mode $res --primary --output $env.SECONDARY_SCREEN --mode 1920x1080 --right-of $env.PRIMARY_SCREEN --output HDMI-0 --off
2024-03-31 13:01:16 +01:00
}
# television view: only TV on (720p so that menus are readable)
def "main t" [] {
2024-03-31 15:55:50 +01:00
let screens = main
let secondary_screen_connected = $screens | where name == "HDMI-0" | get 0 | get connected
if not $secondary_screen_connected {
notify-send "Couldn't set screen" "TV disconnected"
return
}
2024-03-31 13:10:15 +01:00
xrandr --output $env.PRIMARY_SCREEN --off --output $env.SECONDARY_SCREEN --off --output HDMI-0 --mode 1280x720 --primary
2024-03-31 13:01:16 +01:00
}
# stream view: only secondary screen
def "main s" [] {
2024-03-31 15:55:50 +01:00
let screens = main
let secondary_screen_connected = $screens | where name == $env.SECONDARY_SCREEN | get 0 | get connected
if not $secondary_screen_connected {
notify-send "Couldn't set screen" "Secondary screen disconnected"
return
}
2024-03-31 13:10:15 +01:00
xrandr --output $env.SECONDARY_SCREEN --mode 1920x1080 --primary --output $env.PRIMARY_SCREEN --off
}
# laptop view: only secondary screen
def "main l" [] {
2024-03-31 15:55:50 +01:00
let screens = main
let secondary_screen_connected = $screens | where name == $env.SECONDARY_SCREEN | get 0 | get connected
if not $secondary_screen_connected {
notify-send "Couldn't set screen" "Secondary screen disconnected"
return
}
2024-03-31 13:10:15 +01:00
xrandr --output $env.SECONDARY_SCREEN --mode 1920x1080 --primary --output $env.PRIMARY_SCREEN --off
2024-03-31 13:01:16 +01:00
}
def main [] {
2023-12-22 16:09:35 +00:00
mut output = []
mut current_screen = {
name: ""
connected: false
on: false
primary: false
resolution: null
position: null
resolutions: []
}
let lines = ^xrandr | lines
let length = $lines | length
# We skip the first line
for i in 1..<$length {
let line = $lines | get $i
let split = $line | split row -r '\s+'
# This will be true whether the screen is connected or not
if $line =~ "connected" {
if $current_screen.name != "" {
$output = ($output | append $current_screen)
$current_screen.resolutions = []
$current_screen.on = false
$current_screen.resolution = null
$current_screen.position = null
}
$current_screen.name = ($split | get 0)
$current_screen.connected = (($split | get 1) != "disconnected")
$current_screen.primary = (($split | get 2) == "primary")
let resolution = if $current_screen.primary {
($split | get 3 | split column -c '+' | get column1 | get 0)
} else {
($split | get 2 | split column -c '+' | get column1 | get 0)
}
if not ($resolution | str starts-with "(") {
$current_screen.on = true
$current_screen.resolution = $resolution
$current_screen.position = if $current_screen.primary {
($split | get 3 | split column -c '+' | each { "+" + $in.column2 + "+" + $in.column3 } | get 0)
} else {
($split | get 2 | split column -c '+' | each { "+" + $in.column2 + "+" + $in.column3 } | get 0)
}
}
} else {
$current_screen.resolutions = ($current_screen.resolutions | append ($split | get 1))
}
}
if $current_screen.name != "" {
$output = ($output | append $current_screen)
}
$output
}