简易版chalk

2023-12-21

简易版chalk

  • chalk/chalk: ???? Terminal string styling done right (github.com)

  • 【MINI 系列】五颜六色的控制台)

  • ANSI转义序列 - 维基百科,自由的百科全书 (wikipedia.org)

贴一下项目地址:https://github.com/xumeng03/chalk

“ANSI 转义序列(ANSI escape sequences)是一种带内信号的转义序列标准,用于控制视频文本终端上的光标位置、颜色和其他选项。在文本中嵌入确定的字节序列,大部分以 ESC 转义字符和"["字符开始,终端会把这些字节序列解释为相应的指令,而不是普通的字符编码。”

根据描述我们知道需要使用一些特殊的转义字符来实现不同字体风格、颜色、背景色。

1、字体风格

根据wiki 选择图形再现(SGR)参数 的描述编写 Ansi 码表,同时获取一下码表的key

在这里插入图片描述

/**
 * ESC 按键的 Ansi 码
 */
export const ESC_CODE: string = "\u001B"

/**
 * 图形再现参数的 Ansi 码表1(开启)
 */
const Style1 = {
    // 前景色
    'foreground': '38;2;',
    // 背景色
    'background': '48;2;',
    // 加粗
    'bold': '1',
    // 暗淡
    'dim': '2',
    // 倾斜
    'italic': '3',
    // 下划线
    'underline': '4',
    // 闪烁
    'flick': '5',
    // 快速闪烁
    'fast_flick': '6',
    // 反转
    'inverse': '7',
    // 隐藏
    'hidden': '8',
    // 删除线
    'strikethrough': '9',
    // 上划线
    'overline': '53',
};

/**
 * 图形再现参数的 Ansi 码表2(关闭)
 */
const Style2 = {
    // 前景色
    'foreground': '39',
    // 背景色
    'background': '49',
    // 加粗
    'bold': '22',
    // 暗淡
    'dim': '22',
    // 倾斜
    'italic': '23',
    // 下划线
    'underline': '24',
    // 闪烁
    'flick': '25',
    // 快速闪烁
    'fast_flick': '25',
    // 反转
    'inverse': '27',
    // 隐藏
    'hidden': '28',
    // 删除线
    'strikethrough': '29',
    // 上划线
    'overline': '55',
};

export type Style1 = keyof typeof Style1
export const EscapeSequence1 = (key: Style1, ...args: any[]) => {
    const code = Style1[key];
    // return String.raw`${ESC_CODE}[${code}${args.join(';')}m`;
    return `${ESC_CODE}[${code}${args.join(';')}m`;
}

export type Style2 = keyof typeof Style2
export const EscapeSequence2 = (key: Style2) => {
    const code = Style2[key]
    // return String.raw`${ESC_CODE}[${code}m`;
    return `${ESC_CODE}[${code}m`;
}

export type Styles = Style1 & Style2

2、颜色

这里不再像chalk一样支持3/4位颜色、8位颜色,而是仅仅支持真彩色(TrueColor),根据wiki 颜色 的描述补充 Ansi 码表的 foreground background

在这里插入图片描述

这里需要先判断一下终端是否支持真彩色(并未给出所有平台的判断方案)。

import process from "node:process";
import os from "node:os";
import {execSync} from 'child_process';

/**
 * 判断是否支持当前环境是否支持 TrueColor
 * @returns {boolean} 是否支持 TrueColor
 */
export default (): boolean => {
    if (process.platform === 'win32') {
        // Windows 10 build 14931 is the first release that supports 16m/TrueColor.
        const osRelease = os.release().split('.');
        return Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 14_931;
    } else if (process.platform === 'darwin') {
        const result = execSync('tput colors');
        const numColors = parseInt(result.toString().trim(), 10);
        return numColors >= 256;
    }
    return false;
}

颜色名称根据 CSS3 种的 147 个名称去定义。

/**
 * CSS3 标准中的 147 中颜色定义
 */
export const ColorMapping = {
    "aliceblue": "#f0f8ff",
    "antiquewhite": "#faebd7",
    "aqua": "#00ffff",
    "aquamarine": "#7fffd4",
    "azure": "#f0ffff",
    "beige": "#f5f5dc",
    "bisque": "#ffe4c4",
    "black": "#000000",
    "blanchedalmond": "#ffebcd",
    "blue": "#0000ff",
    "blueviolet": "#8a2be2",
    "brown": "#a52a2a",
    "burlywood": "#deb887",
    "cadetblue": "#5f9ea0",
    "chartreuse": "#7fff00",
    "chocolate": "#d2691e",
    "coral": "#ff7f50",
    "cornflowerblue": "#6495ed",
    "cornsilk": "#fff8dc",
    "crimson": "#dc143c",
    "cyan": "#00ffff",
    "darkblue": "#00008b",
    "darkcyan": "#008b8b",
    "darkgoldenrod": "#b8860b",
    "darkgray": "#a9a9a9",
    "darkgreen": "#006400",
    "darkgrey": "#a9a9a9",
    "darkkhaki": "#bdb76b",
    "darkmagenta": "#8b008b",
    "darkolivegreen": "#556b2f",
    "darkorange": "#ff8c00",
    "darkorchid": "#9932cc",
    "darkred": "#8b0000",
    "darksalmon": "#e9967a",
    "darkseagreen": "#8fbc8f",
    "darkslateblue": "#483d8b",
    "darkslategray": "#2f4f4f",
    "darkslategrey": "#2f4f4f",
    "darkturquoise": "#00ced1",
    "darkviolet": "#9400d3",
    "deeppink": "#ff1493",
    "deepskyblue": "#00bfff",
    "dimgray": "#696969",
    "dimgrey": "#696969",
    "dodgerblue": "#1e90ff",
    "firebrick": "#b22222",
    "floralwhite": "#fffaf0",
    "forestgreen": "#228b22",
    "fuchsia": "#ff00ff",
    "gainsboro": "#dcdcdc",
    "ghostwhite": "#f8f8ff",
    "gold": "#ffd700",
    "goldenrod": "#daa520",
    "gray": "#808080",
    "green": "#008000",
    "greenyellow": "#adff2f",
    "grey": "#808080",
    "honeydew": "#f0fff0",
    "hotpink": "#ff69b4",
    "indianred": "#cd5c5c",
    "indigo": "#4b0082",
    "ivory": "#fffff0",
    "khaki": "#f0e68c",
    "lavender": "#e6e6fa",
    "lavenderblush": "#fff0f5",
    "lawngreen": "#7cfc00",
    "lemonchiffon": "#fffacd",
    "lightblue": "#add8e6",
    "lightcoral": "#f08080",
    "lightcyan": "#e0ffff",
    "lightgoldenrodyellow": "#fafad2",
    "lightgray": "#d3d3d3",
    "lightgreen": "#90ee90",
    "lightgrey": "#d3d3d3",
    "lightpink": "#ffb6c1",
    "lightsalmon": "#ffa07a",
    "lightseagreen": "#20b2aa",
    "lightskyblue": "#87cefa",
    "lightslategray": "#778899",
    "lightslategrey": "#778899",
    "lightsteelblue": "#b0c4de",
    "lightyellow": "#ffffe0",
    "lime": "#00ff00",
    "limegreen": "#32cd32",
    "linen": "#faf0e6",
    "magenta": "#ff00ff",
    "maroon": "#800000",
    "mediumaquamarine": "#66cdaa",
    "mediumblue": "#0000cd",
    "mediumorchid": "#ba55d3",
    "mediumpurple": "#9370db",
    "mediumseagreen": "#3cb371",
    "mediumslateblue": "#7b68ee",
    "mediumspringgreen": "#00fa9a",
    "mediumturquoise": "#48d1cc",
    "mediumvioletred": "#c71585",
    "midnightblue": "#191970",
    "mintcream": "#f5fffa",
    "mistyrose": "#ffe4e1",
    "moccasin": "#ffe4b5",
    "navajowhite": "#ffdead",
    "navy": "#000080",
    "oldlace": "#fdf5e6",
    "olive": "#808000",
    "olivedrab": "#6b8e23",
    "orange": "#ffa500",
    "orangered": "#ff4500",
    "orchid": "#da70d6",
    "palegoldenrod": "#eee8aa",
    "palegreen": "#98fb98",
    "paleturquoise": "#afeeee",
    "palevioletred": "#db7093",
    "papayawhip": "#ffefd5",
    "peachpuff": "#ffdab9",
    "peru": "#cd853f",
    "pink": "#ffc0cb",
    "plum": "#dda0dd",
    "powderblue": "#b0e0e6",
    "purple": "#800080",
    "red": "#ff0000",
    "rosybrown": "#bc8f8f",
    "royalblue": "#4169e1",
    "saddlebrown": "#8b4513",
    "salmon": "#fa8072",
    "sandybrown": "#f4a460",
    "seagreen": "#2e8b57",
    "seashell": "#fff5ee",
    "sienna": "#a0522d",
    "silver": "#c0c0c0",
    "skyblue": "#87ceeb",
    "slateblue": "#6a5acd",
    "slategray": "#708090",
    "slategrey": "#708090",
    "snow": "#fffafa",
    "springgreen": "#00ff7f",
    "steelblue": "#4682b4",
    "tan": "#d2b48c",
    "teal": "#008080",
    "thistle": "#d8bfd8",
    "tomato": "#ff6347",
    "turquoise": "#40e0d0",
    "violet": "#ee82ee",
    "wheat": "#f5deb3",
    "white": "#ffffff",
    "whitesmoke": "#f5f5f5",
    "yellow": "#ffff00",
    "yellowgreen": "#9acd32"
}

export type Colors = keyof typeof ColorMapping

export const isColorName = (str: string) => {
    return (str in ColorMapping);
}

根据wiki上的描述,需要把16进制的颜色转换为RGB才能正确的设置样式,这里需要写一个工具类。

/**
 * 将16进制的颜色转换为RGB返回
 * @param hex - 16进制的颜色
 * @returns {number, number, number} 16进制的颜色的RGB表示
 */
export const hexToRgb = (hex: string) => {
    const matches = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)!;
    return {
        r: parseInt(matches[1], 16),
        g: parseInt(matches[2], 16),
        b: parseInt(matches[3], 16)
    }
}

3、实现简易版chalk

import supportTrueColor from "./supportTrueColor.js";
import {EscapeSequence1, EscapeSequence2, Styles} from "./ansi.js";
import {ColorMapping, Colors, isColorName} from "./color.js";
import {hexToRgb} from "./utils.js";

type colorType = "foreground" | "background"

export class Chalk {
    private prefix: string
    private suffix: string

    constructor() {
        if (!supportTrueColor()) {
            throw new Error("Not Supported TrueColor!")
        }
        this.prefix = ''
        this.suffix = ''
    }

    private escapeSequence(key: Styles, ...args: any[]) {
        this.prefix += EscapeSequence1(key, ...args)
        this.suffix += EscapeSequence2(key)
    }

    private rgb(t: colorType, r: number, g: number, b: number): this;
    private rgb(t: colorType, r: number, g: number, b: number, ...args: string[]): string;
    private rgb(t: colorType, r: number, g: number, b: number, ...args: string[]): string | this {
        this.escapeSequence(t as Styles, r, g, b);
        if (args.length > 0) {
            return this.build(...args);
        }
        return this;
    }

    build(...args: string[]) {
        const text = this.prefix + args.join(' ') + this.suffix;
        this.prefix = ''
        this.suffix = ''
        return text;
    }

    // style
    style(s: Styles): this;
    style(s: Styles, ...args: string[]): string;
    style(s: Styles, ...args: string[]): string | this {
        this.escapeSequence(s)
        if (args.length > 0) {
            return this.build(...args);
        }
        return this;
    }

    // color
    color(name: Colors): this;
    color(name: Colors, ...args: string[]): string;
    color(name: string, ...args: string[]): string;
    color(name: Colors, ...args: string[]): string | this {
        const {r, g, b} = isColorName(name) ? hexToRgb(ColorMapping[name as Colors]) : hexToRgb(name);
        return this.rgb("foreground", r, g, b, ...args)
    }

    // bgColor
    bgColor(name: Colors | string): this;
    bgColor(name: Colors | string, ...args: string[]): string;
    bgColor(name: Colors | string, ...args: string[]): string | this {
        const {r, g, b} = isColorName(name) ? hexToRgb(ColorMapping[name as Colors]) : hexToRgb(name);
        return this.rgb("background", r, g, b, ...args)
    }
}

export default new Chalk();

// const chalk = new Chalk();
// console.log(chalk.color("blue").bgColor("skyblue").style("bold", "hello chalk!"));
// console.log(chalk.color("red").bgColor("pink").style("bold").build("hello chalk!"));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

简易版chalk 的相关文章

随机推荐

  • 产品经理和项目经理怎么区分?看完你也会

    产品经理的英文名叫Product Manager 项目经理的英文名叫Project Manager 两个都简称为PM 在工作中 这两种角色的工作内容常常有相同的地方 一些小公司甚至是产品经理和项目经理由同一个人承担 那今天我就给大家讲讲他们
  • 在线客服系统中的全渠道服务:多渠道整合与无缝沟通体验

    很多采购人员在了解在线客服系统的时候都会遇到一个名词 全渠道 很多人第一次接触可能并不理解它是什么意思 也不知道自己的企业是否需要这个 全渠道 今天这篇文章就为大家解答一二 一 全渠道是什么 全渠道 Omni Channel 就是企业为了满
  • LeetCode经典150题.274.H指数

    题目 274 H 指数 给你一个整数数组 citations 其中 citations i 表示研究者的第 i 篇论文被引用的次数 计算并返回该研究者的 h 指数 根据维基百科上 h 指数的定义 h 代表 高引用次数 一名科研人员的 h 指
  • ubuntu git: ‘lfs‘ is not a git command. See ‘git --help‘.

    sudo apt get install git lfs
  • EXCEL VLOOKUP函数

    参考资料 Excel 史上最全的VLOOKUP应用教程 VLOOKUP函数最全面最详细的讲解大全 涵盖17个重要和常见用法 目录 零 前提条件 一 单条件查找 1 1 顺向查找 1 2 逆向查找 二 多条件查找 2 1 顺向查找
  • 让你的查询更快——11个数据库优化技术

    数据库往往成为软件性能的瓶颈 好的数据对于高性能系统至关重要 以下是 11 种有效的数据库优化技术 1 索引 索引 索引是提供快速查找机制的数据结构 可显著提高查询性能 通过创建排序的数据结构来工作 该结构允许数据库引擎快速定位满足 WHE
  • 电子学会C/C++编程等级考试2022年06月(六级)真题解析

    C C 等级考试 1 8级 全部真题 点这里 第1题 小白鼠再排队2 N只小白鼠 1 lt N lt 100 每只鼠头上戴着一顶有颜色的帽子 现在称出每只白鼠的重量 要求按照白鼠重量从小到大的顺序输出它们头上帽子的颜色 帽子的颜色用 red
  • 内网穿透工具frp安装使用

    摘要 之前使用的 nps 目前没有维护更新了 和在使用的过程中做内网穿透的的网速应该有限制 不论云服务器带宽是多少 下载速度都比较慢 这里切换到 frp 试试 对安装和使用简单记录 其和 nps 有很大的操作配置不同之处 相关文章 内网穿透
  • 内网穿透工具frp安装使用

    摘要 之前使用的 nps 目前没有维护更新了 和在使用的过程中做内网穿透的的网速应该有限制 不论云服务器带宽是多少 下载速度都比较慢 这里切换到 frp 试试 对安装和使用简单记录 其和 nps 有很大的操作配置不同之处 相关文章 内网穿透
  • 【广州华锐互动】AR变电站交互仿真实训系统让你学生掌握专业技能

    随着科技的不断发展 智能变电站已经成为了电力系统的重要组成部分 为了提高电力系统的运行效率和安全性 培养高素质的电力工程技术人才 越来越多的高校和职业院校开始开设AR仿真实训课程 本文将为大家简单介绍一下广州华锐互动为知名电力集团开发的AR
  • Python教程84:程序主动退出进程有哪些方法?5种方式总有一种适合你

    在Python中 有多种方法可以主动退出程序进程 这里介绍5种方法 给大家参考一下 1 sys exit 这是最常见的方式 它将引发SystemExit异常 如果这个异常没有被捕获 那么Python解释器将会退出 你可以选择传递一个退出状态
  • 京东岗位吼哔多,具体有啥牛牛说!5类方向近20个岗位等你选~

    字节 测试开发 日常实习 三面面经 已发offer 字节测开秋招面经 华为上海青浦研究所现状 而我 落荒而逃 得物实习小记 运营岗位爆料 干了5年运营后我才知道的事 华为海思麒麟开奖清蒸白菜新鲜出炉 真的难啊 华为上海青浦研究所现状 华为上
  • 测试开发 | 智能农业引领农业革新,人工智能携手农业改写未来

    互联网40的包值得去吗 回暖分析 战绩结算 on 赛文X 软件技术就业单位分析 山东大厂浪潮集团 国家电网研究院VS杭州华为 华为跟银行怎么选 别焦虑 计算机的同学就业率也很低 华为 薪资爆料 字节电商运营实习面经分享 京东 Java OC
  • ESP8266 烧录 (关于BearPi扩展Wifi模块的烧录方式)

    简介 ESP 12F 模块是BearPI IOT购买的一个套餐所带的扩展模块 用来接通网络 但是默认电路不支持重新烧录 下面就是可支持重新烧录的方式 ESP 12F 电路原理图 如上图 GPIO15 gt GND gt 高电平 GPIO2
  • ros2 学习07 rclpy包 详解

    rclpy 是 python 操作ros2 封装的一个工具包 rclpy 源码路径 https github com ros2 rclpy 文档说明地址 https docs ros2 org latest api rclpy index
  • 题解 | #返回购买价格为 10 美元或以上产品的顾客列表#

    脚气怎样治能够根除 2022腾讯秋招面经导航汇总 测试篇 华为上海青浦研究所现状 华为上海青浦研究所现状 华为上海青浦研究所现状 TP普联嵌入式一面 HC还有一千多个 招不到就浪费了 求求大家投下小米吧 吃透计算机网络八股文 年薪40万 实
  • <img src=“x“ onerror=“alert(1)“>

    华为上海青浦研究所现状 华为上海青浦研究所现状 华孝子的胜利 xdm西安交行软开和电信西分怎么选 系表情包 一 旷视科技 一面 求问各位嵌入式Linux有什么好的项目 备战春招了 计软转嵌入式经验分享 嵌入式项目 华为上海青浦研究所现状 字
  • 拼多多temu招人,有较多hc,年前发起流程,拿完年终再入职

    海尔集团 海尔智家 嵌入式硬件工程师面经 MySQL高频面试题 整理牛客网100份面经的C C 校招 社招面试题总结 哇为 再次入池 敏你 华为华子 上海 青浦 第一批 薪资统计 大疆车载 嵌入式软件实习生 底层软件方向 冬季招聘专场感想
  • 基于vue-cli快速发布vue npm 包

    一 编写组件 1 初始化项目并运行 vue create vue digital count npm run serve 2 组件封装 新建package文件夹 因为我们可能会封装多个组件 所以在src下面新建一个 package 文件夹用
  • 简易版chalk

    简易版chalk chalk chalk Terminal string styling done right github com MINI 系列 五颜六色的控制台 ANSI转义序列 维基百科 自由的百科全书 wikipedia org