All files / packages/colors/dist index.d.mts

0% Statements 0/0
0% Branches 1/1
0% Functions 1/1
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84                                                                                                                                                                       
import { Prettify } from '@https-enable/types';
 
// ================= 类型定义层 =================
interface StyleEntry { open: string, close: string }
 
// 基础颜色类型(支持是否包含亮色)
type ColorDefinition<HasBright extends boolean = true> = {
  normal: { fg: number, bg: number }
} & (HasBright extends true ? { bright: { fg: number, bg: number } } : unknown)
 
// 颜色配置模板
interface ColorScheme {
  black: ColorDefinition<false> // 示例:黑色没有亮色
  red: Prettify<ColorDefinition>
  green: Prettify<ColorDefinition>
  yellow: Prettify<ColorDefinition>
  blue: Prettify<ColorDefinition>
  magenta: Prettify<ColorDefinition>
  cyan: Prettify<ColorDefinition>
  white: Prettify<ColorDefinition>
  gray: ColorDefinition<false>
  grey: ColorDefinition<false>
}
 
// ================= 生成工具类型 =================
type ColorName = Prettify<keyof ColorScheme>
// 判断颜色是否包含亮色版本
type HasBright<T extends ColorName> = 'bright' extends keyof ColorScheme[T] ? true : false
 
// 生成颜色键名(自动处理亮色)
type ColorKeys<T extends ColorName> =
  T | (HasBright<T> extends true ? `bright${Capitalize<T>}` : never)
 
// 生成背景色键名(自动处理亮色)
type BackgroundKeys<T extends ColorName> =
  `bg${Capitalize<T>}` | (HasBright<T> extends true ? `bgBright${Capitalize<T>}` : never)
 
// 联合所有可能的键名
type AllColorKeys = Prettify<{
  [K in ColorName]: ColorKeys<K> | BackgroundKeys<K>;
}[ColorName]>
 
interface StyleType {
  reset: StyleEntry
  // 基础样式
  bold: StyleEntry
  dim: StyleEntry
  italic: StyleEntry
  underline: StyleEntry
  blink: StyleEntry
  inverse: StyleEntry
  hidden: StyleEntry
  strikethrough: StyleEntry
  doubleunderline: StyleEntry
  framed: StyleEntry
  overlined: StyleEntry
}
 
type ColorType = {
  // 动态颜色类型
  [K in AllColorKeys]: StyleEntry;
}
 
type FormatKeys = Prettify<keyof StyleType | AllColorKeys>
 
declare function ansiRegex({ onlyFirst }?: {
    onlyFirst?: boolean | undefined;
}): RegExp;
declare function stripAnsi(string: string): string;
 
declare const ansiRegStr: string;
 
declare const styles: Prettify<StyleType & ColorType>;
 
/**
 * 给文本添加 ANSI 转义序列样式(递归版本)
 * @param format - 单个样式或样式数组
 * @param text - 需要格式化的文本
 * @returns 带有 ANSI 转义序列的格式化文本
 */
declare function styleText(format: FormatKeys | FormatKeys[], text: string): string;
 
export { type AllColorKeys, type ColorName, type ColorScheme, type ColorType, type FormatKeys, type StyleEntry, type StyleType, ansiRegStr, ansiRegex, stripAnsi, styleText, styles };