-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add monochrome icons configuration option (#87)
- Loading branch information
Showing
17 changed files
with
294 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const CONFIG_ROOT = 'catppuccin-icons' as const | ||
|
||
export enum CONFIG_KEYS { | ||
HidesExplorerArrows = 'hidesExplorerArrows', | ||
SpecificFolders = 'specificFolders', | ||
Monochrome = 'monochrome', | ||
Associations = 'associations', | ||
} | ||
|
||
export enum COMMANDS { | ||
Reset = 'reset', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,40 @@ | ||
import defu from 'defu' | ||
import { ConfigurationTarget, workspace } from 'vscode' | ||
import { defaultConfig } from '~/defaults' | ||
import { CONFIG_KEYS, CONFIG_ROOT } from '~/constants' | ||
import type { Config } from '~/types' | ||
|
||
/** | ||
* Get user catppuccin-icons configuration | ||
* @returns explicitly set configuration keys | ||
*/ | ||
export function getConfig(): Partial<Config> { | ||
const config = workspace.getConfiguration('catppuccin-icons') | ||
const config = workspace.getConfiguration(CONFIG_ROOT) | ||
|
||
return { | ||
hidesExplorerArrows: config.get('hidesExplorerArrows'), | ||
specificFolders: config.get('specificFolders'), | ||
associations: config.get('associations'), | ||
monochrome: config.get('monochrome'), | ||
hidesExplorerArrows: config.get(CONFIG_KEYS.HidesExplorerArrows), | ||
specificFolders: config.get(CONFIG_KEYS.SpecificFolders), | ||
associations: config.get(CONFIG_KEYS.Associations), | ||
monochrome: config.get(CONFIG_KEYS.Monochrome), | ||
} | ||
} | ||
|
||
/** | ||
* Reset catppuccin-icons configuration | ||
* Deletes keys from `settings.json` | ||
*/ | ||
export async function resetConfig() { | ||
const config = workspace.getConfiguration('catppuccin-icons') | ||
await config.update('hidesExplorerArrows', undefined, ConfigurationTarget.Global) | ||
await config.update('specificFolders', undefined, ConfigurationTarget.Global) | ||
await config.update('associations', undefined, ConfigurationTarget.Global) | ||
await config.update('monochrome', undefined, ConfigurationTarget.Global) | ||
const config = workspace.getConfiguration(CONFIG_ROOT) | ||
for (const k in Object.values(CONFIG_KEYS)) | ||
await config.update(k, undefined, ConfigurationTarget.Global) | ||
} | ||
|
||
/** | ||
* Compares current user config to factory defaults | ||
* @returns `true` if parsed config === defaults | ||
*/ | ||
export function isDefaultConfig() { | ||
const config = defu(getConfig(), defaultConfig) | ||
|
||
return JSON.stringify(config) === JSON.stringify(defaultConfig) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,56 @@ | ||
import { Buffer } from 'node:buffer' | ||
import type { ExtensionContext, Uri } from 'vscode' | ||
import { window, workspace } from 'vscode' | ||
import type { ExtensionContext } from 'vscode' | ||
import { Uri, window, workspace } from 'vscode' | ||
import { flavorEntries } from '@catppuccin/palette' | ||
import { promptToReload } from './interactions' | ||
import { readFile, writeFile, writeJsonFile } from '~/hooks/interactions' | ||
import { getConfig } from '~/hooks/configuration' | ||
import { getIconDefinitions } from '~/hooks/iconDefinitions' | ||
import { compileTheme } from '~/utils/themes' | ||
import { getThemePaths } from '~/hooks/paths' | ||
import { getThemePaths, getUnflavoredPath } from '~/hooks/paths' | ||
import { compileIcon, hashedSvgPath, iconHash } from '~/utils/icons' | ||
|
||
export async function updateThemes(context: ExtensionContext) { | ||
/** | ||
* Update themes and icons according to configuration | ||
* @param context current extension context | ||
* @param icons should icon files be regenerated | ||
*/ | ||
export async function updateThemes(context: ExtensionContext, icons = false) { | ||
const iconDefinitions = await getIconDefinitions(context) | ||
const paths = getThemePaths(context) | ||
const config = getConfig() | ||
const theme = compileTheme(config, iconDefinitions) | ||
const hash = iconHash(config) | ||
const flavors = flavorEntries.map(([f]) => f) | ||
|
||
return Promise.all(flavors.map(async flavor => | ||
workspace.fs.writeFile( | ||
paths[flavor].theme, | ||
Buffer.from(JSON.stringify(theme, null, 2)), | ||
), | ||
)).then(async () => { | ||
await promptToReload() | ||
}).catch((e: Error) => { | ||
if (icons) { | ||
const unflavored = getUnflavoredPath(context) | ||
const unflavoredIcons = await workspace.fs.readDirectory(unflavored) | ||
|
||
await Promise.all(flavors.map(async (flavor) => { | ||
// delete flavored icons | ||
await workspace.fs.delete(paths[flavor].icons, { recursive: true }) | ||
// recreate flavored icon folder | ||
await workspace.fs.createDirectory(paths[flavor].icons) | ||
// recreate flavored icons with hashed paths | ||
await Promise.all(unflavoredIcons.map(async ([i]) => { | ||
const icon = await readFile(Uri.joinPath(unflavored, i)) | ||
await writeFile( | ||
Uri.joinPath(paths[flavor].icons, hashedSvgPath(i, hash)), | ||
compileIcon(icon, flavor, { monochrome: config.monochrome }), | ||
) | ||
})) | ||
})).catch((e: Error) => { | ||
window.showErrorMessage(`Failed to save re-compiled icons: \n${e.message}`) | ||
}) | ||
} | ||
|
||
// add hashed paths to iconDefs | ||
for (const i in iconDefinitions) | ||
iconDefinitions[i].iconPath = hashedSvgPath(iconDefinitions[i].iconPath, hash) | ||
|
||
// create and write `theme.json` files | ||
const theme = compileTheme(config, iconDefinitions) | ||
await Promise.all(flavors.map(async (flavor) => { | ||
await writeJsonFile(paths[flavor].theme, theme) | ||
})).catch((e: Error) => { | ||
window.showErrorMessage(`Failed to save re-compiled theme: \n${e.message}`) | ||
}) | ||
} | ||
|
||
export async function writeFile(uri: Uri, content: unknown) { | ||
return workspace.fs | ||
.writeFile(uri, Buffer.from(JSON.stringify(content, null, 2))) | ||
.then( | ||
() => {}, | ||
(error: Error) => { | ||
window.showErrorMessage(error.message) | ||
}, | ||
) | ||
} |
Oops, something went wrong.