Last active
January 29, 2025 15:06
-
-
Save xentec/d6101917829c98058af7930e660726a3 to your computer and use it in GitHub Desktop.
Generates a kanshi profile from current monitor configuration with the help of Nushell. (https://git.sr.ht/~emersion/kanshi)
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 nu | |
def ind [level: int = 2, symbol: string = " "] { | |
mut id = '' | |
if $level > 0 { # fix for single runs of 1..0 | |
for i in 1..$level { | |
$id += $symbol | |
} | |
} | |
$id + $in | |
} | |
# Generate a kanshi profile from current monitor configuration | |
def main [ | |
profile: string = "default" # Profile name | |
--include: string # Make the profile include the path/glob | |
--exec: string # Add 'exec' command to the profile | |
] { | |
if (which wlr-randr | length) == 0 { | |
print --stderr "command 'wlr-randr' not found!" | |
return | |
} | |
let df = wlr-randr --json | from json | flatten modes | where modes.current | |
mut ln = [] | |
if ($include | is-not-empty) { | |
$ln = $ln | append $"include ($include)" | |
} | |
$ln = $ln | append $'profile ($profile) {' | |
$ln ++= $df | reduce --fold [] {|r,acc| | |
mut ln = $acc | |
$ln = $ln | append $"output \"($r.make) ($r.model) ($r.serial)\" {" | |
$ln = $ln | append $"# alias: ($r.name)" | |
$ln = $ln | append (if ($r.enabled) { "enable" } else { "disable" }) | |
$ln = $ln | append $"position ($r.position.x),($r.position.y)" | |
$ln = $ln | append $"mode ($r.modes.width)x($r.modes.height)" | |
if ($r.transform != "normal") { | |
$ln = $ln | append $"transform ($r.transform)" | |
} | |
if ($r.scale != 1.0) { | |
$ln = $ln | append $"scale ($r.scale)" | |
} | |
$ln = $ln | append "}" | |
$ln | |
} | |
if ($exec | is-not-empty) { | |
$ln = $ln | append $"exec ($exec)" | |
} | |
$ln = $ln | append "}" | |
# Stringify and add indentation | |
mut indent = 0 | |
for line in $ln { | |
if ($line | str index-of '}') >= 0 { | |
$indent -= 1 | |
} | |
print ($line | ind ($indent * 2)) | |
if ($line | str index-of '{') >= 0 { | |
$indent += 1 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment