All files / packages/colors/dist index.mjs

93.87% Statements 92/98
80% Branches 8/10
80% Functions 4/5
93.87% Lines 92/98

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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 1041x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 92x 92x 92x 92x 92x 92x 2x 2x 20x 20x 20x 20x 14x 14x 14x 20x 20x 20x 14x 14x 14x 20x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x   2x 2x 2x 1x               38x 38x 38x 38x      
const defaultFG = 39;
const defaultBG = 49;
const colors = {
  black: {
    normal: { fg: 30, bg: 40 }
  },
  red: {
    normal: { fg: 31, bg: 41 },
    bright: { fg: 91, bg: 101 }
  },
  green: {
    normal: { fg: 32, bg: 42 },
    bright: { fg: 92, bg: 102 }
  },
  yellow: {
    normal: { fg: 33, bg: 43 },
    bright: { fg: 93, bg: 103 }
  },
  blue: {
    normal: { fg: 34, bg: 44 },
    bright: { fg: 94, bg: 104 }
  },
  magenta: {
    normal: { fg: 35, bg: 45 },
    bright: { fg: 95, bg: 105 }
  },
  cyan: {
    normal: { fg: 36, bg: 46 },
    bright: { fg: 96, bg: 106 }
  },
  white: {
    normal: { fg: 37, bg: 47 },
    bright: { fg: 97, bg: 107 }
  },
  gray: {
    normal: { fg: 90, bg: 100 }
  },
  grey: {
    normal: { fg: 90, bg: 100 }
  }
};
function createEntry(code, resetCode) {
  return {
    open: `\x1B[${code}m`,
    close: `\x1B[${resetCode}m`
  };
}
function buildColorStyles() {
  return Object.entries(colors).reduce((acc, [name, variants]) => {
    const colorName = name;
    const capitalized = colorName[0]?.toUpperCase() + colorName.slice(1);
    acc[colorName] = createEntry(variants.normal.fg, defaultFG);
    if ("bright" in variants) {
      const brightKey = `bright${capitalized}`;
      acc[brightKey] = createEntry(variants.bright.fg, defaultFG);
    }
    const bgKey = `bg${capitalized}`;
    acc[bgKey] = createEntry(variants.normal.bg, defaultBG);
    if ("bright" in variants) {
      const bgBrightKey = `bgBright${capitalized}`;
      acc[bgBrightKey] = createEntry(variants.bright.bg, defaultBG);
    }
    return acc;
  }, {});
}
const styles = {
  // 基础样式
  reset: createEntry(0, 0),
  bold: createEntry(1, 22),
  dim: createEntry(2, 22),
  italic: createEntry(3, 23),
  underline: createEntry(4, 24),
  blink: createEntry(5, 25),
  // Swap foreground and background colors
  inverse: createEntry(7, 27),
  hidden: createEntry(8, 28),
  strikethrough: createEntry(9, 29),
  doubleunderline: createEntry(21, 24),
  framed: createEntry(51, 54),
  overlined: createEntry(53, 55),
  // 动态生成颜色样式
  ...buildColorStyles()
};
 
const ansiRegStr = "[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-\\w/#&.:=?%@~]+)+|[a-zA-Z\\d]+(?:;[-\\w/#&.:=?%@~]*)*)?(?:\\u0007|\\u001B\\u005C|\\u009C))|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))";
 
function ansiRegex({ onlyFirst = false } = {}) {
  return new RegExp(ansiRegStr, onlyFirst ? void 0 : "g");
}
const regex = ansiRegex();
function stripAnsi(string) {
  if (typeof string !== "string") {
    throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``);
  }
  return string.replace(regex, "");
}
 
function styleText(format, text) {
  const formats = Array.isArray(format) ? format : [format];
  return formats.reverse().reduce((str, style) => `${styles[style].open}${str}${styles[style].close}`, text);
}
 
export { ansiRegStr, ansiRegex, stripAnsi, styleText, styles };