Skip to content

Commit

Permalink
Added: Fzf theme script description; Fix: Proper exit on throw SUCCESS;
Browse files Browse the repository at this point in the history
  • Loading branch information
ss committed Oct 13, 2024
1 parent e8da500 commit c076879
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 44 deletions.
3 changes: 1 addition & 2 deletions src/globalConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ globalThis.SystemError = class SystemError extends Error {
globalThis.execAsync = execAsync;

const handleError = (error, blockName) => {
if (error instanceof SystemError) throw error;
if (!(error instanceof Error)) return;
if (error instanceof SystemError || (error === SUCCESS)) throw error;
if (error.stackTrace) {
error.stackTrace.unshift(blockName);
} else {
Expand Down
3 changes: 1 addition & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class WallWiz {
}

handleExecutionStatus(status) {
if (status === 0) return;
if (status === SUCCESS) STD.exit(0);
if (status instanceof SystemError) {
status.log(USER_ARGUMENTS.inspection);
} else if (USER_ARGUMENTS.inspection) {
Expand All @@ -227,7 +227,6 @@ class WallWiz {
}).join("\n");
print(stackTrace, "\n", status);
}
STD.exit(status);
}
}

Expand Down
108 changes: 68 additions & 40 deletions themeExtensionScripts/fzf@5hubham5ingh.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { loadfile, setenv } from "std";
/*
For: fzf, https://junegunn.github.io/fzf
Author: https://github.com/5hubham5ingh
Prerequisite: Shell configuration file. (default: .bashrc)
Note: The new theme only takes effect in a new shell, or requires
sourcing the shell configuration file in already running shell.
*/

import { getenv, loadFile, open, SEEK_SET } from "std";

function hexToRGB(hex) {
const r = parseInt(hex.slice(1, 3), 16);
Expand Down Expand Up @@ -37,38 +46,6 @@ function rgbToHSL(r, g, b) {
return [h * 360, s * 100, l * 100];
}

function hslToRGB(h, s, l) {
h /= 360;
s /= 100;
l /= 100;
let r, g, b;

if (s === 0) {
r = g = b = l;
} else {
const hue2rgb = (p, q, t) => {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1 / 6) return p + (q - p) * 6 * t;
if (t < 1 / 2) return q;
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
return p;
};

const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
const p = 2 * l - q;
r = hue2rgb(p, q, h + 1 / 3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1 / 3);
}

return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}

function rgbToHex(r, g, b) {
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}

function generateTheme(colors, isDark) {
const sortedColors = [...colors].sort((a, b) => {
const [, , lA] = rgbToHSL(...hexToRGB(a));
Expand All @@ -88,26 +65,77 @@ function generateTheme(colors, isDark) {
? sortedColors[Math.floor(midIndex / 2)]
: sortedColors[Math.floor(midIndex * 1.5)];

const secondary = sortedColors[Math.floor(midIndex * 0.75)];
const tertiary = sortedColors[Math.floor(midIndex * 1.25)];

return {
background,
foreground,
highlight,
accent,
secondary,
tertiary,
};
}

async function getThemeConf(colors, isDark = true) {
function getThemeConf(colors, isDark = true) {
const theme = generateTheme(colors, isDark);

const config =
`--color=fg:${theme.foreground},bg:${theme.background},hl:${theme.highlight},fg+:${theme.accent},bg+:${theme.background},hl+:${theme.highlight},info:${theme.accent},prompt:${theme.highlight},pointer:${theme.accent},marker:${theme.accent},spinner:${theme.highlight},header:${theme.highlight},border:${theme.highlight},gutter:${theme.background}`;
const config = `
fg:${theme.foreground},
bg:${theme.background},
hl:${theme.highlight},
fg+:${theme.accent},
bg+:${theme.secondary},
hl+:${theme.highlight},
info:${theme.tertiary},
prompt:${theme.highlight},
pointer:${theme.accent},
marker:${theme.accent},
spinner:${theme.secondary},
header:${theme.highlight},
border:${theme.tertiary},
gutter:${theme.background},
preview-fg:${theme.foreground},
preview-bg:${theme.background}
`.replace(/\n+|\s/g, "").trim();

return `--color ${config}`;
}

function updateOrAddEnvVar(filePath, variable, value) {
let fileContent = "";

const file = open(filePath, "r+");
fileContent = file.readAsString();

const regex = new RegExp(`^\\s*export\\s+${variable}="[^"]*"`, "m");
const newEnvLine = `export ${variable}="${value}"`;

return config;
if (regex.test(fileContent)) {
fileContent = fileContent.replace(regex, newEnvLine);
} else {
fileContent += `\n${newEnvLine}\n`;
}

file.seek(0, SEEK_SET);
file.puts(fileContent);
file.close();
}

function getDarkThemeConf(colours) {
return getThemeConf(colours, true);
}
function getLightThemeConf(colours) {
return getThemeConf(colours, false);
}
function setTheme(themeConfig) {
const fzfConfig = loadfile(themeConfig);
setenv("FZF_DEFAULT_OPTS", fzfConfig);
const fzfConfig = loadFile(themeConfig);
updateOrAddEnvVar(
getenv("HOME") + "/.bashrc",
"FZF_DEFAULT_OPTS",
fzfConfig,
);
}

export { getThemeConf, setTheme };
export { getDarkThemeConf, getLightThemeConf, setTheme };

0 comments on commit c076879

Please sign in to comment.