UNPKG

estilo

Version:

Create color schemes for Vim, NeoVim, Airline and Lightline

93 lines (92 loc) 3.83 kB
import * as dntShim from "../_dnt.shims.js"; import { parse as yamlParse } from "../deps/jsr.io/@std/yaml/1.0.5/mod.js"; import { basename, resolve } from "../deps/jsr.io/@std/path/1.0.8/mod.js"; import { hexterm } from "../deps/jsr.io/@jacoborus/hexterm/2.1.1/src/hexterm.js"; import { formatStatusStyles, formatSyntax, formatTerminal, } from "./format-project.js"; import { assertIsList, assertIsObject, crash, existsSync, isHexColor, } from "./util.js"; export function loadProjectFiles(projectUrl) { const config = loadYml(projectUrl, "estilo.yml").content; const airlineFiles = loadYmlsInFolder(projectUrl, "estilos/airline"); const lightlineFiles = loadYmlsInFolder(projectUrl, "estilos/lightline"); const baseSyntaxFile = loadYml(projectUrl, "estilos/syntax/base.yml"); const syntaxFiles = loadYmlsInFolder(projectUrl, "estilos/syntax", "base.yml"); const paletteFiles = loadYmlsInFolder(projectUrl, "estilos/palettes"); const terminalFile = loadYml(projectUrl, "estilos/terminal.yml"); return { projectUrl, config, palettes: buildPalettes(paletteFiles, config.common), baseSyntax: formatSyntax([baseSyntaxFile]), syntax: formatSyntax(syntaxFiles), terminalSyntax: formatTerminal(terminalFile.content), airlineStyles: formatStatusStyles(airlineFiles, "airline"), lightlineStyles: formatStatusStyles(lightlineFiles, "lightline"), }; } function loadYmlsInFolder(projectUrl, folder, omit) { const folderUrl = resolve(projectUrl, folder); if (!existsSync(folderUrl)) return []; return Array.from(dntShim.Deno.readDirSync(folderUrl)) .filter((file) => file.name.endsWith(".yml")) .filter((file) => file.name !== omit) .map((file) => resolve(folderUrl, file.name)) .map((filepath) => loadYml(filepath)); } function loadYml(folderPath, filename) { const filepath = resolve(folderPath, filename || ""); const content = yamlParse(dntShim.Deno.readTextFileSync(filepath)); assertIsObject(content, filepath); return { filepath, content }; } function buildPalettes(paletteFiles, common = {}) { const commonPalette = buildMainPalette(common); const palettes = {}; paletteFiles.forEach((paletteFile) => { const palette = buildPalette(paletteFile, commonPalette); palettes[palette.name] = palette; }); return palettes; } function buildMainPalette(content) { const colors = {}; for (const name of Object.keys(content)) { const hexcolor = content[name].trim(); if (!isHexColor(hexcolor)) { crash("Wrong color in common palette", { name }); } colors[name] = { hex: hexcolor.startsWith("#") ? hexcolor : "#" + hexcolor, xterm: hexterm(hexcolor).toString(), }; } return colors; } export function buildPalette(paletteFile, common) { const { filepath, content } = paletteFile; assertIsList(content, filepath); const palette = { filepath, name: basename(filepath, ".yml"), colors: structuredClone(common), }; Object.entries(content).forEach(([name, value]) => { const hexcolor = value.trim(); if (hexcolor.startsWith("@")) { const propName = hexcolor.slice(1); const color = common[propName]; if (!color) crash("Missing common color", { color: propName }); palette.colors[name] = color; return; } if (!isHexColor(hexcolor)) { crash("Wrong color", { filepath, name, hexcolor }); } palette.colors[name] = { hex: hexcolor.startsWith("#") ? hexcolor : "#" + hexcolor, xterm: hexterm(hexcolor).toString(), }; }); return palette; }