Home: Rename home/modules to home/homemodules
This commit is contained in:
102
home/homemodules/color/applyColors.py
Normal file
102
home/homemodules/color/applyColors.py
Normal file
@ -0,0 +1,102 @@
|
||||
import fileinput
|
||||
import re
|
||||
from typing import Callable
|
||||
|
||||
# This dict gets defined before.
|
||||
# The code is templated by nix to fill the color values automatically.
|
||||
# colors: dict[str, str] = {
|
||||
# "rosewater": "f5e0dc",
|
||||
# "flamingo": "f2cdcd",
|
||||
# }
|
||||
colors: dict[str, str]
|
||||
|
||||
# We don't want the accent aliases here
|
||||
del colors["accent"]
|
||||
del colors["accentHl"]
|
||||
del colors["accentDim"]
|
||||
del colors["accentText"]
|
||||
|
||||
|
||||
def getRule(
|
||||
line: str,
|
||||
value: str,
|
||||
) -> Callable[[str, str, str], str] | None:
|
||||
"""Obtain a substitution rule for a line"""
|
||||
|
||||
# This contains each rule assigned to a pattern
|
||||
rules: dict[str, Callable[[str, str, str], str]] = {
|
||||
# "#ffffff" -> ${color.hexS.white}
|
||||
f'"#{value}"': lambda line, name, value: line.replace(
|
||||
f'"#{value}"', f"${{color.hexS.{name}}}"
|
||||
),
|
||||
# '#ffffff' -> ${color.hexS.white}
|
||||
f"'#{value}'": lambda line, name, value: line.replace(
|
||||
f"'#{value}'", f"${{color.hexS.{name}}}"
|
||||
),
|
||||
# #ffffff -> ${color.hexS.white}
|
||||
f"#{value}": lambda line, name, value: line.replace(
|
||||
f"#{value}", f"${{color.hexS.{name}}}"
|
||||
),
|
||||
# "ffffff" -> ${color.hex.white}
|
||||
f'"{value}"': lambda line, name, value: line.replace(
|
||||
f'"{value}"', f"${{color.hex.{name}}}"
|
||||
),
|
||||
# 'ffffff' -> ${color.hex.white}
|
||||
f"'{value}'": lambda line, name, value: line.replace(
|
||||
f"'{value}'", f"${{color.hex.{name}}}"
|
||||
),
|
||||
# ffffff -> ${color.hex.white}
|
||||
f"{value}": lambda line, name, value: line.replace(
|
||||
f"{value}", f"${{color.hex.{name}}}"
|
||||
),
|
||||
# #${color.hex.white} -> ${color.hexS.white}
|
||||
"#${color.hex.": lambda line, name, value: line.replace(
|
||||
"#${color.hex.", "${color.hexS."
|
||||
),
|
||||
# ff,ff,ff -> ${color.rgbS.white}
|
||||
f"{value[0:2]},{value[2:4]},{value[4:6]}": lambda line, name, value: line.replace(
|
||||
f"{value[0:2]},{value[2:4]},{value[4:6]}", f"${{color.rgbS.{name}}}"
|
||||
),
|
||||
}
|
||||
|
||||
for pattern, rule in rules.items():
|
||||
# If the line matches the pattern, use this rule
|
||||
if len(re.findall(pattern, line, re.IGNORECASE)) > 0:
|
||||
return rule
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def applyColorToLine(
|
||||
line: str,
|
||||
name: str,
|
||||
value: str,
|
||||
) -> str:
|
||||
"""Apply a single color to a single line"""
|
||||
result: str = line
|
||||
|
||||
rule: Callable[[str, str, str], str] | None = getRule(result, value)
|
||||
while rule is not None:
|
||||
result = rule(line, name, value)
|
||||
|
||||
# We apply rules until no rule can be applied anymore
|
||||
rule = getRule(result, value)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def applyColorsToLine(
|
||||
line: str,
|
||||
) -> None:
|
||||
"""Apply all defined colors to a single line"""
|
||||
result: str = line
|
||||
for name, value in colors.items():
|
||||
result = applyColorToLine(result, name, value)
|
||||
|
||||
# Print to stdout
|
||||
print(result)
|
||||
|
||||
|
||||
# Apply transformation to every line from stdin
|
||||
for line in fileinput.input():
|
||||
applyColorsToLine(line.rstrip())
|
||||
117
home/homemodules/color/default.nix
Normal file
117
home/homemodules/color/default.nix
Normal file
@ -0,0 +1,117 @@
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
mylib,
|
||||
pkgs,
|
||||
...
|
||||
}: let
|
||||
inherit (config.modules) color;
|
||||
in {
|
||||
options.modules.color = import ./options.nix {inherit lib mylib pkgs;};
|
||||
|
||||
config = {
|
||||
home.packages = let
|
||||
mkPythonColorDef = name: value: " '${name}': '${value}',";
|
||||
|
||||
# Helper script that processes a visual mode selection and replaces
|
||||
# referenced colors in-place with their counterparts in this module.
|
||||
# Usage: ":'<,'>!applyColors<cr>"
|
||||
applyColors =
|
||||
pkgs.writers.writePython3Bin
|
||||
"applyColors"
|
||||
{
|
||||
doCheck = false;
|
||||
}
|
||||
(builtins.concatStringsSep "\n" [
|
||||
"colors: dict[str, str] = {"
|
||||
(color.hex
|
||||
|> builtins.mapAttrs mkPythonColorDef
|
||||
|> builtins.attrValues
|
||||
|> builtins.concatStringsSep "\n")
|
||||
"}"
|
||||
(builtins.readFile ./applyColors.py)
|
||||
]);
|
||||
|
||||
mkBashColorEcho = let
|
||||
pastel = "${pkgs.pastel}/bin/pastel";
|
||||
in
|
||||
name: value: ''printf "%-12s" " ${name}:" && ${pastel} color "${value}" | ${pastel} format hex'';
|
||||
|
||||
# Helper script that prints the color scheme to the terminal
|
||||
printNixColors =
|
||||
pkgs.writeShellScriptBin
|
||||
"printNixColors"
|
||||
(builtins.concatStringsSep "\n" [
|
||||
''echo " ${color.scheme}:"''
|
||||
''echo ${lib.concatStrings (lib.replicate 20 "=")}''
|
||||
(color.hexS
|
||||
|> builtins.mapAttrs mkBashColorEcho
|
||||
|> builtins.attrValues
|
||||
|> builtins.concatStringsSep "\n")
|
||||
''echo ${lib.concatStrings (lib.replicate 20 "=")}''
|
||||
]);
|
||||
in
|
||||
[
|
||||
applyColors
|
||||
printNixColors
|
||||
]
|
||||
++ (lib.optionals color.installPackages [color.iconPackage color.cursorPackage])
|
||||
++ (lib.optionals color.installPackages color.extraPackages);
|
||||
|
||||
# This module sets its own options to the values specified in a colorscheme file.
|
||||
modules.color = let
|
||||
scheme = import ./schemes/${color.scheme}.nix;
|
||||
|
||||
# Add the aliases
|
||||
colorDefs =
|
||||
scheme
|
||||
// {
|
||||
accent = scheme.${color.accent};
|
||||
accentHl = scheme.${color.accentHl};
|
||||
accentDim = scheme.${color.accentDim};
|
||||
accentText = scheme.${color.accentText};
|
||||
};
|
||||
|
||||
mkColorAssignment = key: {
|
||||
${key} = colorDefs.${key};
|
||||
};
|
||||
mkStringColorAssignment = key: {
|
||||
${key} = "#${colorDefs.${key}}";
|
||||
};
|
||||
mkRgbColorAssignment = key: {
|
||||
${key} = mylib.color.hexToRGB colorDefs.${key};
|
||||
};
|
||||
mkRgbStringColorAssignment = key: {
|
||||
${key} = mylib.color.hexToRGBString "," colorDefs.${key};
|
||||
};
|
||||
in {
|
||||
# RRGGBB (0-F)
|
||||
hex =
|
||||
colorDefs
|
||||
|> builtins.attrNames
|
||||
|> builtins.map mkColorAssignment
|
||||
|> lib.mergeAttrsList;
|
||||
|
||||
# #RRGGBB (0-F)
|
||||
hexS =
|
||||
colorDefs
|
||||
|> builtins.attrNames
|
||||
|> builtins.map mkStringColorAssignment
|
||||
|> lib.mergeAttrsList;
|
||||
|
||||
# [RR GG BB] (0-255)
|
||||
rgb =
|
||||
colorDefs
|
||||
|> builtins.attrNames
|
||||
|> builtins.map mkRgbColorAssignment
|
||||
|> lib.mergeAttrsList;
|
||||
|
||||
# RR,GG,BB (0-255)
|
||||
rgbS =
|
||||
colorDefs
|
||||
|> builtins.attrNames
|
||||
|> builtins.map mkRgbStringColorAssignment
|
||||
|> lib.mergeAttrsList;
|
||||
};
|
||||
};
|
||||
}
|
||||
191
home/homemodules/color/options.nix
Normal file
191
home/homemodules/color/options.nix
Normal file
@ -0,0 +1,191 @@
|
||||
{
|
||||
lib,
|
||||
mylib,
|
||||
pkgs,
|
||||
...
|
||||
}: let
|
||||
colorKeys = [
|
||||
"rosewater"
|
||||
"flamingo"
|
||||
"pink"
|
||||
"mauve"
|
||||
"red"
|
||||
"maroon"
|
||||
"peach"
|
||||
"yellow"
|
||||
"green"
|
||||
"teal"
|
||||
"sky"
|
||||
"sapphire"
|
||||
"blue"
|
||||
"lavender"
|
||||
|
||||
"text"
|
||||
"subtext1"
|
||||
"subtext0"
|
||||
"overlay2"
|
||||
"overlay1"
|
||||
"overlay0"
|
||||
"surface2"
|
||||
"surface1"
|
||||
"surface0"
|
||||
"base"
|
||||
"mantle"
|
||||
"crust"
|
||||
];
|
||||
in rec {
|
||||
scheme = lib.mkOption {
|
||||
type = lib.types.enum [
|
||||
"catppuccin-latte"
|
||||
"catppuccin-mocha"
|
||||
];
|
||||
description = "The color scheme to use";
|
||||
example = "catppuccin-mocha";
|
||||
default = "catppuccin-mocha";
|
||||
};
|
||||
|
||||
font = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
description = "The font to use";
|
||||
example = "JetBrainsMono Nerd Font Mono";
|
||||
default = "JetBrainsMono Nerd Font Mono";
|
||||
};
|
||||
|
||||
cursor = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
description = "The cursor to use";
|
||||
example = "Bibata-Modern-Classic";
|
||||
default = "Bibata-Modern-Classic";
|
||||
};
|
||||
|
||||
cursorSize = lib.mkOption {
|
||||
type = lib.types.int;
|
||||
description = "The cursor size";
|
||||
example = 24;
|
||||
default = 24;
|
||||
};
|
||||
|
||||
cursorPackage = lib.mkOption {
|
||||
type = lib.types.package;
|
||||
description = "The cursor package";
|
||||
example = pkgs.bibata-cursors;
|
||||
default = pkgs.bibata-cursors;
|
||||
};
|
||||
|
||||
iconTheme = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
description = "The icon theme to use";
|
||||
example = "Papirus";
|
||||
default = "Papirus";
|
||||
};
|
||||
|
||||
iconPackage = lib.mkOption {
|
||||
type = lib.types.package;
|
||||
description = "The icon theme package";
|
||||
example = pkgs.papirus-icon-theme;
|
||||
default = pkgs.papirus-icon-theme;
|
||||
};
|
||||
|
||||
extraPackages = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.package;
|
||||
description = "Extra packages to install";
|
||||
example = ''
|
||||
[
|
||||
pkgs.bibata-cursors
|
||||
]
|
||||
'';
|
||||
default = [];
|
||||
};
|
||||
|
||||
installPackages = lib.mkEnableOption "Install cursor and icon themes";
|
||||
|
||||
# This option is set automatically
|
||||
wallpapers = let
|
||||
# Collect all the available wallpapers.
|
||||
# We can't do this in default.nix as the value
|
||||
# needs to be available during option evaluation.
|
||||
wallpapers = let
|
||||
rmFileExt = file: builtins.replaceStrings [".jpg"] [""] file;
|
||||
|
||||
rmBasePath = file: let
|
||||
matches = builtins.match "/.*/(.*)" file;
|
||||
in
|
||||
if matches == null
|
||||
then file
|
||||
else (builtins.head matches);
|
||||
in
|
||||
lib.filesystem.listFilesRecursive ../../../wallpapers
|
||||
|> builtins.map builtins.toString
|
||||
|> builtins.map rmFileExt
|
||||
|> builtins.map rmBasePath;
|
||||
in
|
||||
lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
readOnly = true;
|
||||
description = "The available wallpapers";
|
||||
default = wallpapers;
|
||||
};
|
||||
|
||||
wallpaper = lib.mkOption {
|
||||
type = lib.types.enum wallpapers.default;
|
||||
description = "The wallpaper to use";
|
||||
example = "Foggy-Lake";
|
||||
default = "Foggy-Lake";
|
||||
};
|
||||
|
||||
# Some semantic aliases for colors
|
||||
|
||||
accent = lib.mkOption {
|
||||
type = lib.types.enum colorKeys;
|
||||
description = "The accent color to use";
|
||||
example = "mauve";
|
||||
default = "mauve";
|
||||
};
|
||||
|
||||
accentHl = lib.mkOption {
|
||||
type = lib.types.enum colorKeys;
|
||||
description = "The accented accent color to use";
|
||||
example = "pink";
|
||||
default = "pink";
|
||||
};
|
||||
|
||||
accentDim = lib.mkOption {
|
||||
type = lib.types.enum colorKeys;
|
||||
description = "The dim accent color to use";
|
||||
example = "lavender";
|
||||
default = "lavender";
|
||||
};
|
||||
|
||||
accentText = lib.mkOption {
|
||||
type = lib.types.enum colorKeys;
|
||||
description = "The text color to use for accents";
|
||||
example = "base";
|
||||
default = "base";
|
||||
};
|
||||
|
||||
# These options will be populated automatically.
|
||||
|
||||
hex = lib.mkOption {
|
||||
type = lib.types.attrs;
|
||||
readOnly = true;
|
||||
description = "Colors in \"RRGGBB\" hexadecimal format";
|
||||
};
|
||||
|
||||
hexS = lib.mkOption {
|
||||
type = lib.types.attrs;
|
||||
readOnly = true;
|
||||
description = "Colors in \"#RRGGBB\" hexadecimal format";
|
||||
};
|
||||
|
||||
rgb = lib.mkOption {
|
||||
type = lib.types.attrs;
|
||||
readOnly = true;
|
||||
description = "Colors in [RR GG BB] decimal format";
|
||||
};
|
||||
|
||||
rgbS = lib.mkOption {
|
||||
type = lib.types.attrs;
|
||||
readOnly = true;
|
||||
description = "Colors in \"RR,GG,BB\" decimal format";
|
||||
};
|
||||
}
|
||||
29
home/homemodules/color/schemes/0_template.nix
Normal file
29
home/homemodules/color/schemes/0_template.nix
Normal file
@ -0,0 +1,29 @@
|
||||
{
|
||||
rosewater = "";
|
||||
flamingo = "";
|
||||
pink = "";
|
||||
mauve = "";
|
||||
red = "";
|
||||
maroon = "";
|
||||
peach = "";
|
||||
yellow = "";
|
||||
green = "";
|
||||
teal = "";
|
||||
sky = "";
|
||||
sapphire = "";
|
||||
blue = "";
|
||||
lavender = "";
|
||||
|
||||
text = "";
|
||||
subtext1 = "";
|
||||
subtext0 = "";
|
||||
overlay2 = "";
|
||||
overlay1 = "";
|
||||
overlay0 = "";
|
||||
surface2 = "";
|
||||
surface1 = "";
|
||||
surface0 = "";
|
||||
base = "";
|
||||
mantle = "";
|
||||
crust = "";
|
||||
}
|
||||
29
home/homemodules/color/schemes/catppuccin-latte.nix
Normal file
29
home/homemodules/color/schemes/catppuccin-latte.nix
Normal file
@ -0,0 +1,29 @@
|
||||
{
|
||||
rosewater = "dc8a78";
|
||||
flamingo = "dd7878";
|
||||
pink = "ea76cb";
|
||||
mauve = "8839ef";
|
||||
red = "d20f39";
|
||||
maroon = "e64553";
|
||||
peach = "fe640b";
|
||||
yellow = "df8e1d";
|
||||
green = "40a02b";
|
||||
teal = "179299";
|
||||
sky = "04a5e5";
|
||||
sapphire = "209fb5";
|
||||
blue = "1e66f5";
|
||||
lavender = "7287fd";
|
||||
|
||||
text = "4c4f69";
|
||||
subtext1 = "5c5f77";
|
||||
subtext0 = "6c6f85";
|
||||
overlay2 = "7c7f93";
|
||||
overlay1 = "8c8fa1";
|
||||
overlay0 = "9ca0b0";
|
||||
surface2 = "acb0be";
|
||||
surface1 = "bcc0cc";
|
||||
surface0 = "ccd0da";
|
||||
base = "eff1f5";
|
||||
mantle = "e6e9ef";
|
||||
crust = "dce0e8";
|
||||
}
|
||||
29
home/homemodules/color/schemes/catppuccin-mocha.nix
Normal file
29
home/homemodules/color/schemes/catppuccin-mocha.nix
Normal file
@ -0,0 +1,29 @@
|
||||
{
|
||||
rosewater = "f5e0dc";
|
||||
flamingo = "f2cdcd";
|
||||
pink = "f5c2e7";
|
||||
mauve = "cba6f7";
|
||||
red = "f38ba8";
|
||||
maroon = "eba0ac";
|
||||
peach = "fab387";
|
||||
yellow = "f9e2af";
|
||||
green = "a6e3a1";
|
||||
teal = "94e2d5";
|
||||
sky = "89dceb";
|
||||
sapphire = "74c7ec";
|
||||
blue = "89b4fa";
|
||||
lavender = "b4befe";
|
||||
|
||||
text = "cdd6f4";
|
||||
subtext1 = "bac2de";
|
||||
subtext0 = "a6adc8";
|
||||
overlay2 = "9399b2";
|
||||
overlay1 = "7f849c";
|
||||
overlay0 = "6c7086";
|
||||
surface2 = "585b70";
|
||||
surface1 = "45475a";
|
||||
surface0 = "313244";
|
||||
base = "1e1e2e";
|
||||
mantle = "181825";
|
||||
crust = "11111b";
|
||||
}
|
||||
Reference in New Issue
Block a user