repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

hypr/scripts/theme.nu

Raw
#!/usr/bin/env nu

# Nugu desktop theme switcher. Writes active runtime configs, then reloads clients.

const modes = [dark light]

def config-home [] { $env.XDG_CONFIG_HOME? | default ($env.HOME | path join ".config") }
def cache-dir [] { $env.HOME | path join ".cache" "nugu" }
def mode-file [] { cache-dir | path join "mode" }
def auto-file [] { cache-dir | path join "auto" }
def palette-file [] { config-home | path join "hypr" "generated" "nugu.json" }

def active-mode [] {
  let path = (mode-file)
  if ($path | path exists) {
    let mode = (open --raw $path | str trim)
    if $mode in $modes { return $mode }
  }
  "dark"
}

def auto-mode [] {
  let hour = (date now | format date "%H" | into int)
  if $hour >= 7 and $hour < 19 { "light" } else { "dark" }
}

def hex-no-hash [color: string] { $color | str replace --all "#" "" }
def argb [color: string] { $"0xFF(hex-no-hash $color)" }

def palette [mode: string] {
  open (palette-file) | get $mode
}

def auto-enabled [] {
  let path = (auto-file)
  if not ($path | path exists) { return true }
  (open --raw $path | str trim) != "0"
}

def set-auto [enabled: bool] {
  (if $enabled { "1" } else { "0" }) | save --raw --force (auto-file)
}

def write-desktop-color-scheme [mode: string] {
  let scheme = if $mode == "dark" { "prefer-dark" } else { "prefer-light" }
  try { gsettings set org.gnome.desktop.interface color-scheme $scheme | ignore }
}

def write-hyprtoolkit [mode: string, p: record] {
  let out = (config-home | path join "hypr" "hyprtoolkit.conf")
  $"background = (argb $p.content.backdrop)
base = (argb $p.ui.backdrop)
alternate_base = (argb $p.ui.unfocus)
text = (argb $p.content.normal)
bright_text = (argb $p.ui.important_global)
accent = (argb $p.ui.accent)
accent_secondary = (argb $p.content.important_local)
h1_size = 20
h2_size = 16
h3_size = 13
font_size = 11
small_font_size = 10
font_family = IBM Plex Sans
font_family_monospace = IBM Plex Mono
icon_theme = Adwaita
" | save --raw --force $out
}

def write-mako [mode: string, p: record] {
  mkdir (config-home | path join "mako")
  let out = (config-home | path join "mako" "nugu.conf")
  $"# Generated by hypr/scripts/theme.nu from Nugu palette.
background-color=($p.ui.backdrop)ee
text-color=($p.ui.normal)ff
border-color=($p.ui.accent)ff
progress-color=over ($p.ui.focus)ff

[urgency=low]
background-color=($p.content.backdrop)e6
text-color=($p.content.minor)ff
border-color=($p.ui.unfocus)ff
progress-color=over ($p.content.minor)ff

[urgency=normal]
border-color=($p.ui.accent)ff
progress-color=over ($p.ui.focus)ff

[urgency=critical]
background-color=($p.ui.backdrop)ff
text-color=($p.ui.important_global)ff
border-color=($p.error)ff
progress-color=over ($p.error)ff

[summary~=\"[Ee]rror|[Ff]ailed|[Ff]ailure|[Cc]rash\"]
border-color=($p.error)ff
progress-color=over ($p.error)ff

[summary~=\"[Ww]arn|[Ww]arning\"]
border-color=($p.warning)ff
progress-color=over ($p.warning)ff

[summary~=\"[Ii]nfo|[Nn]otice\"]
border-color=($p.info)ff
progress-color=over ($p.info)ff
" | save --raw --force $out
}

def write-ironbar [mode: string, p: record] {
  mkdir (config-home | path join "ironbar")
  let out = (config-home | path join "ironbar" "nugu.css")
  let surface = $p.ui.minor
  let on_focus = $p.content.backdrop
  $"/* Generated by hypr/scripts/theme.nu from Nugu palette. */
:root {
  --bg: ($p.ui.backdrop);
  --bg-transparent: ($p.ui.backdrop)ee;
  --surface: ($surface);
  --fg: ($p.ui.normal);
  --accent: ($p.ui.accent);
  --focus: ($p.ui.focus);
  --on-focus: ($on_focus);
  --muted: ($p.ui.minor);
  --unfocus: ($p.ui.unfocus);
  --error: ($p.error);
  --border: ($p.ui.accent);
}
" | save --raw --force $out
}

def apply-mode [mode: string] {
  mkdir (cache-dir)
  let p = (palette $mode)
  $mode | save --raw --force (mode-file)
  write-desktop-color-scheme $mode
  # Disabled until Hyprtoolkit handles live config rewrites without busy-looping:
  # https://github.com/hyprwm/hyprtoolkit/issues/92
  # write-hyprtoolkit $mode $p
  write-mako $mode $p
  write-ironbar $mode $p

  try { hyprctl hyprpaper wallpaper $",~/.config/hypr/wallpapers/nugu-($mode).png" | ignore }
  try { hyprctl reload | ignore }
  if (($env.NUGU_NO_RESTART_MAKO? | default "") != "1") {
    try { systemctl --user restart mako.service | ignore }
  }
  try { systemctl --user restart ironbar.service | ignore }
  if (($env.NUGU_NO_NOTIFY? | default "") != "1") {
    try { notify-send --expire-time 1500 "nugu" $"($mode) mode" | ignore }
  }
}

def next-mode [] { if (active-mode) == "dark" { "light" } else { "dark" } }

def main [mode: string = "toggle"] {
  let manual = $mode in [dark light toggle]
  if $manual { set-auto false }
  if $mode == "auto" { set-auto true }

  let target = match $mode {
    "dark" => "dark",
    "light" => "light",
    "toggle" => (next-mode),
    "auto" => (auto-mode),
    "current" => (active-mode),
    "watch" => { watch; return },
    _ => { error make { msg: "mode must be dark|light|toggle|auto|current|watch" } },
  }
  apply-mode $target
}

def watch [] {
  loop {
    if (auto-enabled) {
      let target = (auto-mode)
      if $target != (active-mode) { apply-mode $target }
    }
    sleep 5min
  }
}