Skip to content

Commit

Permalink
Added: Dunst theme handler script; Updated: Todo in Readme;
Browse files Browse the repository at this point in the history
  • Loading branch information
ss committed Oct 13, 2024
1 parent c076879 commit e770ae5
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 3 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ WallWiz -e -a -d ~/Pictures
- `swww` wallpaper daemon handler script.
- VS Code theme extension script.
- Firefox theme extension script.
- Dunst notification theme extension script.
- `fzf` theme extension script.

## Contributing

Expand Down
2 changes: 1 addition & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class WallWiz {
"-d ~/Pics/wallpapers -s 42x10",
"-l -p 4x4",
])
.ver("0.0.1-alpha.6")
.ver("0.0.1-alpha.7")
.parse();

// Convert parsed arguments to a more convenient object format
Expand Down
134 changes: 134 additions & 0 deletions themeExtensionScripts/dunst@5hubham5ingh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { getenv, loadFile, open } from "std";
import { exec } from "os";

function hexToRGB(hex) {
const r = parseInt(hex.slice(1, 3), 16);
const g = parseInt(hex.slice(3, 5), 16);
const b = parseInt(hex.slice(5, 7), 16);
return [r, g, b];
}

function rgbToHSL(r, g, b) {
r /= 255;
g /= 255;
b /= 255;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
let h, s, l = (max + min) / 2;

if (max === min) {
h = s = 0;
} else {
const d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r:
h = (g - b) / d + (g < b ? 6 : 0);
break;
case g:
h = (b - r) / d + 2;
break;
case b:
h = (r - g) / d + 4;
break;
}
h /= 6;
}

return [h * 360, s * 100, l * 100];
}

function generateTheme(colors, isDark) {
const sortedColors = [...colors].sort((a, b) => {
const [, , lA] = rgbToHSL(...hexToRGB(a));
const [, , lB] = rgbToHSL(...hexToRGB(b));
return isDark ? lA - lB : lB - lA;
});

const backgroundIndex = isDark ? 0 : sortedColors.length - 1;
const foregroundIndex = isDark ? sortedColors.length - 1 : 0;

const background = sortedColors[backgroundIndex];
const foreground = sortedColors[foregroundIndex];

const midIndex = Math.floor(sortedColors.length / 2);
const frameColor = sortedColors[midIndex];
const criticalColor = sortedColors[Math.floor(midIndex * 0.75)];

return {
background,
foreground,
frameColor,
criticalColor,
};
}

function getThemeConf(colours, isDark) {
const theme = generateTheme(colours, isDark);

const config = `
# WallWiz theme
[global]
frame_color = "${theme.frameColor}"
separator_color = frame
[urgency_low]
background = "${theme.background}"
foreground = "${theme.foreground}"
[urgency_normal]
background = "${theme.background}"
foreground = "${theme.foreground}"
[urgency_critical]
background = "${theme.background}"
foreground = "${theme.foreground}"
frame_color = "${theme.criticalColor}"
# end
`.trim();

return config;
}

function getDarkThemeConf(colours) {
return getThemeConf(colours, true);
}

function getLightThemeConf(colours) {
return getThemeConf(colours, false);
}

function setTheme(newThemeConfigPath) {
const dunstConfigPath = getenv("HOME").concat("/.config/dunst/dunstrc");
const dunstOldConfig = loadFile(dunstConfigPath);
const newThemeConfig = loadFile(newThemeConfigPath);
let filterOut = false;
let updated = false;
const updatedConfig = dunstOldConfig.split("\n")
.filter((line) => {
if (line.includes("# WallWiz theme")) {
filterOut = true;
updated = true;
return false;
}
if (filterOut) {
if (line.includes("# end")) {
filterOut = false;
return false;
}
return false;
}
return true;
})
.join("\n") + "\n" + newThemeConfig;

if (!updated) dunstOldConfig.concat("\n", newThemeConfig);
const dunstConfig = open(dunstConfigPath, "w+");

dunstConfig.puts(updatedConfig);
dunstConfig.close();

exec(["pkill", "dunst"]);
}

export { getDarkThemeConf, getLightThemeConf, setTheme };

0 comments on commit e770ae5

Please sign in to comment.