repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

hypr/scripts/theme.lua

Raw
File outline
#!/usr/bin/env lua

local source = debug.getinfo(1, "S").source:gsub("^@", "")
local script_dir = source:match("(.+)/[^/]+$") or "."
local hypr_dir = script_dir:match("(.+)/[^/]+$") or "."
package.path = hypr_dir .. "/?.lua;" .. hypr_dir .. "/?/init.lua;" .. package.path

local nugu = require("generated.nugu")

local function trim(value)
  return (value or ""):gsub("^%s+", ""):gsub("%s+$", "")
end

local function shell_quote(value)
  value = tostring(value)
  return "'" .. value:gsub("'", "'\\''") .. "'"
end

local function run(command)
  os.execute(command .. " >/dev/null 2>&1")
end

local function mkdir(path)
  run("mkdir -p " .. shell_quote(path))
end

local function read_file(path)
  local file = io.open(path, "r")
  if not file then return nil end
  local data = file:read("*a")
  file:close()
  return data
end

local function write_file(path, content)
  local file = assert(io.open(path, "w"))
  file:write(content)
  file:close()
end

local home = assert(os.getenv("HOME"), "HOME unset")
local config_home = os.getenv("XDG_CONFIG_HOME") or home .. "/.config"
local cache_dir = home .. "/.cache/nugu"
local mode_file = cache_dir .. "/mode"
local auto_file = cache_dir .. "/auto"

local function active_mode()
  local mode = trim(read_file(mode_file))
  if mode == "light" then return "light" end
  return "dark"
end

local function auto_mode()
  local hour = tonumber(os.date("%H")) or 0
  if hour >= 7 and hour < 19 then return "light" end
  return "dark"
end

local function auto_enabled()
  local value = read_file(auto_file)
  return value == nil or trim(value) ~= "0"
end

local function set_auto(enabled)
  mkdir(cache_dir)
  write_file(auto_file, enabled and "1" or "0")
end

local function write_desktop_color_scheme(mode)
  local scheme = mode == "dark" and "prefer-dark" or "prefer-light"
  run("gsettings set org.gnome.desktop.interface color-scheme " .. shell_quote(scheme))
end

local function write_mako(p)
  local dir = config_home .. "/mako"
  mkdir(dir)
  write_file(dir .. "/nugu.conf", string.format([[# Generated by hypr/scripts/theme.lua from Nugu palette.
background-color=%see
text-color=%sff
border-color=%sff
progress-color=over %sff

[urgency=low]
background-color=%se6
text-color=%sff
border-color=%sff
progress-color=over %sff

[urgency=normal]
border-color=%sff
progress-color=over %sff

[urgency=critical]
background-color=%sff
text-color=%sff
border-color=%sff
progress-color=over %sff

[summary~="[Ee]rror|[Ff]ailed|[Ff]ailure|[Cc]rash"]
border-color=%sff
progress-color=over %sff

[summary~="[Ww]arn|[Ww]arning"]
border-color=%sff
progress-color=over %sff

[summary~="[Ii]nfo|[Nn]otice"]
border-color=%sff
progress-color=over %sff
]],
    p.ui.backdrop, p.ui.normal, p.ui.accent, p.ui.focus,
    p.content.backdrop, p.content.minor, p.ui.unfocus, p.content.minor,
    p.ui.accent, p.ui.focus,
    p.ui.backdrop, p.ui.important_global, p.error, p.error,
    p.error, p.error,
    p.warning, p.warning,
    p.info, p.info))
end

local function write_ironbar(p)
  local dir = config_home .. "/ironbar"
  mkdir(dir)
  write_file(dir .. "/nugu.css", string.format([[/* Generated by hypr/scripts/theme.lua from Nugu palette. */
:root {
  --bg: %s;
  --bg-transparent: %see;
  --surface: %s;
  --fg: %s;
  --accent: %s;
  --focus: %s;
  --on-focus: %s;
  --muted: %s;
  --unfocus: %s;
  --error: %s;
  --warning: %s;
  --info: %s;
  --debug: %s;
  --border: %s;
}
]],
    p.ui.backdrop,
    p.ui.backdrop,
    p.ui.minor,
    p.ui.normal,
    p.ui.accent,
    p.ui.focus,
    p.content.backdrop,
    p.ui.minor,
    p.ui.unfocus,
    p.error,
    p.warning,
    p.info,
    p.debug,
    p.ui.accent))
end

local function apply_mode(mode)
  mkdir(cache_dir)
  write_file(mode_file, mode)

  local p = assert(nugu.get(mode), "unknown mode: " .. tostring(mode))
  write_desktop_color_scheme(mode)
  write_mako(p)
  write_ironbar(p)

  run("hyprctl hyprpaper wallpaper " .. shell_quote(",~/.config/hypr/wallpapers/nugu-" .. mode .. ".png"))
  run("hyprctl reload")
  if os.getenv("NUGU_NO_RESTART_MAKO") ~= "1" then
    run("systemctl --user restart mako.service")
  end
  run("systemctl --user restart ironbar.service")
  if os.getenv("NUGU_NO_NOTIFY") ~= "1" then
    run("notify-send --expire-time 1500 nugu " .. shell_quote(mode .. " mode"))
  end
end

local function next_mode()
  return active_mode() == "dark" and "light" or "dark"
end

local function watch()
  while true do
    if auto_enabled() then
      local target = auto_mode()
      if target ~= active_mode() then apply_mode(target) end
    end
    os.execute("sleep 300")
  end
end

local mode = arg[1] or "toggle"

if mode == "dark" or mode == "light" or mode == "toggle" then
  set_auto(false)
elseif mode == "auto" then
  set_auto(true)
end

if mode == "dark" or mode == "light" then
  apply_mode(mode)
elseif mode == "toggle" then
  apply_mode(next_mode())
elseif mode == "auto" then
  apply_mode(auto_mode())
elseif mode == "current" then
  apply_mode(active_mode())
elseif mode == "watch" then
  watch()
else
  io.stderr:write("mode must be dark|light|toggle|auto|current|watch\n")
  os.exit(1)
end