UNPKG

@kotori-bot/tools

Version:
129 lines (128 loc) 4.49 kB
/** * @Package @kotori-bot/tools * @Version 1.5.2 * @Author Hotaru <me@hotaru.icu> * @Copyright 2024-2025 Hotaru. All rights reserved. * @License BAN-ZHINESE-USING * @Link https://github.com/kotorijs/kotori * @Date 17:26:22 */ "use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default")); var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var colors_exports = {}; __export(colors_exports, { Colors: () => Colors, default: () => colors_default }); module.exports = __toCommonJS(colors_exports); var import_utils = require("./utils"); var import_adapters = require("./adapters"); var import_browser = __toESM(require("./adapters/browser")); __reExport(colors_exports, require("./utils"), module.exports); __reExport(colors_exports, require("./adapters"), module.exports); class Colors { customRules; c; constructor(config) { if ("adapter" in config) { this.c = config.adapter; this.customRules = config.rules ?? {}; } else { this.c = config; this.customRules = {}; } } parse(text) { const allColors = [...import_utils.colorsIdentity, ...Object.keys(this.customRules), "clear"]; const colorStack = []; let result = ""; let currentText = ""; const applyColor = () => { if (currentText) { for (let i = colorStack.length - 1; i >= 0; i--) { currentText = this.dye(currentText, colorStack[i]); } result += currentText; currentText = ""; } }; const regex = new RegExp(`(<(${allColors.join("|")})>|</(${allColors.join("|")})>|([^<]+))`, "g"); let match = regex.exec(text); while (match !== null) { if (match[2]) { applyColor(); colorStack.push(match[2]); } else if (match[3]) { applyColor(); const lastColor = colorStack.pop(); if (lastColor !== match[3]) continue; } else if (match[4]) { currentText += match[4]; } match = regex.exec(text); } applyColor(); return result; } batch(batches) { return batches.map((batch) => this.parse(batch)); } dye(text, color) { if (color in this.c) return this.c[color](text); if (color in this.customRules) return this.customRules[color](text); return this.clear(text); } clear(text) { this.clear.toString(); return text.replace( /* html */ /<clear>(.*?)<\/clear>/g, "$1" ); } } ((Colors2) => { function createColor(config) { return new Colors2(config); } Colors2.createColor = createColor; const instance = createColor({ adapter: !globalThis.document ? new import_adapters.TerminalAdapter() : new import_browser.default() }); Colors2.parse = instance.parse.bind(instance); Colors2.batch = instance.batch.bind(instance); Colors2.dye = instance.dye.bind(instance); Colors2.clear = instance.clear.bind(instance); })(Colors || (Colors = {})); var colors_default = Colors; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Colors, ...require("./utils"), ...require("./adapters") });