UNPKG

todo-txt-cli

Version:

A CLI for todo.txt files - http://todotxt.org/

137 lines (122 loc) 2.88 kB
import { createColors, isColorSupported } from 'colorette' /** * @typedef {{ * priA?: string * priB?: string * priC?: string * priD?: string * project?: string * context?: string * date?: string * done?: string * error?: string * useColor?: boolean * }} ColorConfig */ /** * @typedef {(s: string, overwrite?: (s: string) => string) => string} ColorFn * @typedef {{ * priA: ColorFn * priB: ColorFn * priC: ColorFn * priD: ColorFn * project: ColorFn * context: ColorFn * date: ColorFn * done: ColorFn * none: ColorFn * error: ColorFn * bold: ColorFn, * underline: ColorFn * }} Colors */ /** @type {ColorConfig} */ export const defaultColors = { priA: 'yellow', priB: 'green', priC: 'blueBright', priD: '', project: 'red', context: 'green', date: '', done: 'lightGray', error: 'red' } export const modifiers = ['light', 'bold', 'dim', 'underline', 'inverse'] const capitalize = (s = '') => s.charAt(0).toUpperCase() + s.slice(1) const deCapitalize = (s = '') => s.charAt(0).toLowerCase() + s.slice(1) export const getColorNames = () => // filter modifiers Object.keys(createColors()).filter( (color) => ![ 'reset', 'bold', 'dim', 'italic', 'underline', 'inverse', 'hidden', 'strikethrough' ].includes(color) ) /** * @param {string} name * @returns {{name: string, modifiers: string[]|[]}} */ export const getColorNameModifier = (name = '') => { const lc = name.toLowerCase() const found = new Set() for (let m of modifiers) { if (lc.includes(m)) { name = name.replace(m, '').replace(capitalize(m), '') if (m === 'light') { m = 'dim' } found.add(m) } } return { name: deCapitalize(name), modifiers: [...found] } } /** * @param {import('colorette').Colorette|{}} allColors * @param {string} name * @param {string[]} [modifiers] * @returns {ColorFn} */ const colorFn = (allColors, name, modifiers = []) => (s, overwrite) => { if (overwrite) { return overwrite(s) } let _s = (allColors[name] || String)(s) for (const mod of modifiers) { _s = (allColors[mod] || String)(_s) } return _s } /** * @param {ColorConfig} [config] * @returns {Colors} */ export const getColors = (config) => { const { useColor = isColorSupported, ..._config } = config || {} /** @type {ColorConfig} */ const _colors = { ...defaultColors, ..._config } const allColors = createColors({ useColor }) const colors = { none: colorFn({}, ''), bold: allColors.bold, underline: allColors.underline } for (const key of Object.keys(defaultColors)) { const { name, modifiers } = getColorNameModifier(_colors[key]) colors[key] = colorFn(allColors, name, modifiers) } // @ts-expect-error return colors }