Skip to content

Commit

Permalink
release v3.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
webdiscus committed Dec 25, 2024
1 parent 0274117 commit 09e7bbb
Show file tree
Hide file tree
Showing 20 changed files with 620 additions and 391 deletions.
32 changes: 31 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
# Change log

## 3.5.0 (2024-12-26)

- release: v3.5.0

## 3.5.0-beta.6 (2024-12-25)

- refactor: optimise npm package to reduce the size
- chore: update benchmarks with newest version
- test: add more use cases

## 3.5.0-beta.5 (2024-12-24)

- feat: detect `xterm-direct` terminal as supported the truecolor
- feat: add support the `COLORTERM` variable for values: `truecolor`, `24bit`, `ansi256`, `ansi` (16 colors)
- docs: update readme for using `COLORTERM` with examples

## 3.5.0-beta.4 (2024-12-23)

- docs: remove badges in readme for npm package to reduce package size

## 3.5.0-beta.3 (2024-12-23)

- docs: fix badge urls in readme for npm package

## 3.5.0-beta.2 (2024-12-23)

- refactor: optimise npm package to reduce the size by ~0.5 kB, from 8.0 kB to 7.5 kB
- test: fix swc configuration for compiling into CJS
- test: fix handling line endings on windows

## 3.5.0-beta.1 (2024-12-23)

- refactor: optimise npm package to reduce the size by ~1.5 kB, from 8.9 kB to 7.5 kB
- refactor: optimise npm package to reduce the size by ~1 kB, from 8.9 kB to 8.0 kB

## 3.5.0-beta.0 (2024-12-21)

Expand Down
487 changes: 295 additions & 192 deletions README.md

Large diffs are not rendered by default.

8 changes: 2 additions & 6 deletions README.npm.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
<h1 align="center"><img src="docs/img/logo.png"></h1>

[![](bit.ly/3ZS2ynW)](https://codecov.io/gh/webdiscus/ansis)
[![](bit.ly/49SsuV0)](https://www.npmjs.com/package/ansis)
[![](bit.ly/3P7e1Lp)](https://bundlephobia.com/package/ansis)

Colorize terminal with ANSI colors & styles.

📖 [Docs on GitHub](https://github.com/webdiscus/ansis)
---
## 📖 [Docs on GitHub](https://github.com/webdiscus/ansis)

![](docs/img/npm.png)
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ansis",
"version": "3.5.0-beta.1",
"description": "ANSI colors and styles in terminal output",
"version": "3.5.0",
"description": "A small and fast Node.js library for applying ANSI colors and styles in terminal output",
"keywords": [
"ansi",
"colour",
Expand Down Expand Up @@ -79,6 +79,7 @@
"test:cjs": "node ./test/package/cjs/test.cjs",
"test:esm": "node ./test/package/esm/test.mjs",
"test:tsc": "vitest run ./test/ts-compiler.test.js",
"test:levels": "vitest run ./test/color-levels.test.js",
"test:coverage": "vitest run --coverage",
"publish:public": "(npm run build) && npm publish ./dist --access public",
"publish:beta": "(npm run build) && npm publish ./dist --tag beta"
Expand All @@ -92,9 +93,9 @@
"@types/node": "^22.10.2",
"@vitest/coverage-v8": "^2.1.8",
"ansis": "file:dist",
"del-cli": "^6.0.0",
"esbuild": "^0.24.2",
"prettier": "^3.4.2",
"rimraf": "^6.0.1",
"rollup": "^4.29.1",
"rollup-plugin-copy": "^3.5.0",
"swc": "^1.0.11",
Expand Down
2 changes: 1 addition & 1 deletion package.npm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ansis",
"version": "3.5.0-beta.1",
"version": "3.5.0",
"description": "ANSI colors and styles in terminal output",
"keywords": [
"ansi",
Expand Down
30 changes: 21 additions & 9 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,26 @@ import copy from 'rollup-plugin-copy';
import { minify } from 'terser';

// last ECMA version compatible with node.js 12
const ecma = 2019;
//const ecma = 2019;
const ecma = 2021;

const terserOptions = {
ecma,
// https://github.com/terser/terser#compress-options
compress: {
ecma,
passes: 2,
//module: true, // omit 'use strict'
},
toplevel: true,
}

// use this options only for debugging
const debugTerserOptions = {
ecma,
compress: false,
keep_fnames: true,
}

function removeComments(string){
//Takes a string of code, not an actual function.
Expand Down Expand Up @@ -32,14 +51,7 @@ export default [
// `ansis.default = ansis` is needed for tsc using default import, e.g. `import ansis from 'ansis'`
'exports.default = ansis': 'module.exports.Ansis = Ansis, ansis.default = ansis',
}),
terser({
ecma,
compress: {
ecma,
passes: 2,
},
toplevel: true,
}),
terser(terserOptions),
copy({
targets: [
{
Expand Down
68 changes: 36 additions & 32 deletions src/ansi-codes.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
import { hexToRgb, rgbToAnsi256, rgbToAnsi16, ansi256To16 } from './utils.js';
import { getColorSpace } from './color-support.js';
import { SPACE_MONO, SPACE_16COLORS, SPACE_256COLORS } from './color-spaces.js';

const colorSpace = getColorSpace();
export const isSupported = colorSpace > 0;
const mono = { open: '', close: '' };
const monoFn = () => mono;
const esc = isSupported ? (open, close) => ({ open: `\x1b[${open}m`, close: `\x1b[${close}m` }) : monoFn;
const closeCode = 39;
const bgCloseCode = 49;
const bgOffset = 10;
let colorSpace = getColorSpace();
let isSupported = colorSpace > SPACE_MONO;
let mono = { open: '', close: '' };
let monoFn = () => mono;
let esc = isSupported ? (open, close) => ({ open: `\x1b[${open}m`, close: `\x1b[${close}m` }) : monoFn;
let closeCode = 39;
let bgCloseCode = 49;
let bgOffset = 10;

const createRgbFn = (fn) => (r, g, b) => fn(rgbToAnsi256(r, g, b));
let createRgbFn = (fn) => (r, g, b) => fn(rgbToAnsi256(r, g, b));

const createHexFn = (fn) => (hex) => {
let createHexFn = (fn) => (hex) => {
// note: the `...` operator is too slow
let [r, g, b] = hexToRgb(hex);
return fn(r, g, b);
};

// defaults, true color
// defaults, truecolor
let fnAnsi256 = (code) => esc(`38;5;${code}`, closeCode);
let fnBgAnsi256 = (code) => esc(`48;5;${code}`, bgCloseCode);
export let fnRgb = (r, g, b) => esc(`38;2;${r};${g};${b}`, closeCode);
let fnRgb = (r, g, b) => esc(`38;2;${r};${g};${b}`, closeCode);
let fnBgRgb = (r, g, b) => esc(`48;2;${r};${g};${b}`, bgCloseCode);

if (colorSpace === 1) {
if (colorSpace === SPACE_16COLORS) {
// ANSI 16 colors
fnAnsi256 = (code) => esc(ansi256To16(code), closeCode);
fnBgAnsi256 = (code) => esc(ansi256To16(code) + bgOffset, bgCloseCode);
fnRgb = (r, g, b) => esc(rgbToAnsi16(r, g, b), closeCode);
fnBgRgb = (r, g, b) => esc(rgbToAnsi16(r, g, b) + bgOffset, bgCloseCode);
} else if (colorSpace === 2) {
} else if (colorSpace === SPACE_256COLORS) {
// ANSI 256 colors
fnRgb = createRgbFn(fnAnsi256);
fnBgRgb = createRgbFn(fnBgAnsi256);
}

export let styleData = {
let styleData = {
// color functions
ansi256: fnAnsi256, // alias for compatibility with chalk
bgAnsi256: fnBgAnsi256, // alias for compatibility with chalk
Expand All @@ -47,31 +48,19 @@ export let styleData = {
hex: createHexFn(fnRgb),
bgHex: createHexFn(fnBgRgb),

// misc
// styles
visible: mono,
reset: esc(0, 0),
inverse: esc(7, 27),
hidden: esc(8, 28),

// styles
bold: esc(1, 22),
dim: esc(2, 22),
italic: esc(3, 23),
underline: esc(4, 24),
strikethrough: esc(9, 29),
strike: esc(9, 29), // alias for strikethrough

// foreground colors
grey: esc(90, closeCode), // UK spelling alias for blackBright
gray: esc(90, closeCode), // US spelling alias for blackBright

// background colors
bgGrey: esc(100, bgCloseCode), // UK spelling alias for bgBlackBright
bgGray: esc(100, bgCloseCode), // US spelling alias for bgBlackBright
inverse: esc(7, 27),
hidden: esc(8, 28),
};

// generate ANSI 16 colors dynamically and save ~450 bytes
let styles = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'];
// generate ANSI 16 colors dynamically to reduce the code
let styles = 'black,red,green,yellow,blue,magenta,cyan,white'.split(',');
let bright = 'Bright';
let code = 30;
let name, bgName;
Expand All @@ -86,3 +75,18 @@ for (name of styles) {

code++;
}

// UK and US spelling alias for blackBright
styleData.grey = styleData.gray = esc(90, closeCode);

// UK and US spelling alias for bgBlackBright
styleData.bgGrey = styleData.bgGray = esc(100, bgCloseCode);

// alias for strikethrough
styleData.strikethrough = styleData.strike = esc(9, 29);

export {
isSupported,
styleData,
fnRgb,
}
6 changes: 3 additions & 3 deletions src/color-spaces.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// color spaces
export const SPACE_MONO = 0;
export const SPACE_16_COLORS = 1;
export const SPACE_256_COLORS = 2;
export const SPACE_TRUE_COLORS = 3;
export const SPACE_16COLORS = 1;
export const SPACE_256COLORS = 2;
export const SPACE_TRUECOLOR = 3;
Loading

0 comments on commit 09e7bbb

Please sign in to comment.