Skip to content

Commit

Permalink
refactor: invisible code optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
webdiscus committed Dec 26, 2024
1 parent 09e7bbb commit 48a7450
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 25 deletions.
14 changes: 11 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# Change log

## 3.5.1 (2024-12-26)

- refactor: invisible code optimisation

## 3.5.0 (2024-12-26)

- release: v3.5.0
- refactor: optimise npm package to reduce size by 3 kB, from 10.3 kB to 7.3 kB
- feat: add support the `COLORTERM` variable for values: `truecolor`, `24bit`, `ansi256`, `ansi` (16 colors)
- feat: add support the `xterm-direct` terminal to detect the truecolor
- fix: default import in TypeScript, compiled with `tsc`:
`import ansis from 'ansis'` now works so well as `import * as ansis from 'ansis'`

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

Expand Down Expand Up @@ -36,8 +44,8 @@

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

- feat: add support for terminals where the GPG is installed.
If GPG is installed, `GPG_TTY` will be set to `ENV` instead of `isTTY`.
- feat: add support environment variable `GPG_TTY` to detect it as `isTTY`.
NOTE: in release v3.5.1 was removed as needles.
- fix: default import in TypeScript, compiled with `tsc`:
`import ansis from 'ansis'` now works so well as `import * as ansis from 'ansis'`
- refactor: optimise npm package to reduce size by ~1.3 kB, from 10.3 kB to 8.9 kB
Expand Down
2 changes: 1 addition & 1 deletion compare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
"colors-cli": "1.0.33",
"kleur": "4.1.5",
"kolorist": "1.8.0",
"picocolors": "1.1.0"
"picocolors": "1.1.1"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ansis",
"version": "3.5.0",
"version": "3.5.1",
"description": "A small and fast Node.js library for applying ANSI colors and styles in terminal output",
"keywords": [
"ansi",
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",
"version": "3.5.1",
"description": "ANSI colors and styles in terminal output",
"keywords": [
"ansi",
Expand Down
19 changes: 8 additions & 11 deletions src/color-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ let detectColorSpace = (env, isTTY, isWin) => {

// Azure DevOps CI
// https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml
if ('TF_BUILD' in env) return SPACE_16COLORS;
if (!!env.TF_BUILD) return SPACE_16COLORS;

// JetBrains TeamCity support 256 colors since 2020.1.1 (2020-06-23)
if ('TEAMCITY_VERSION' in env) return SPACE_256COLORS;
if (!!env.TEAMCITY_VERSION) return SPACE_256COLORS;

// CI tools
// https://github.com/watson/ci-info/blob/master/vendors.json
if ('CI' in env) {
if (!!env.CI) {
// CI supports true colors
if (someEnv(['GITHUB_ACTIONS', 'GITEA_ACTIONS'])) return SPACE_TRUECOLOR;

Expand All @@ -59,7 +59,7 @@ let detectColorSpace = (env, isTTY, isWin) => {
// truecolor support starts from Windows 10 build 14931 (2016-09-21), in 2024 we assume modern Windows is used
if (isWin) return SPACE_TRUECOLOR;

// kitty is GPU based terminal emulator, xterm-direct indicates truecolor support
// kitty or KDE terminal emulator indicates truecolor support
if (/^xterm-(kitty|direct)$/i.test(term)) return SPACE_TRUECOLOR;

// JetBrains IDEA: JetBrains-JediTerm
Expand Down Expand Up @@ -123,7 +123,7 @@ export const getColorSpace = (mockThis) => {
let forceColorNum = parseInt(forceColorValue);
let forceColor = forceColorValue === 'false' ? SPACE_MONO : isNaN(forceColorNum) ? SPACE_TRUECOLOR : forceColorNum;

let isForceDisabled = 'NO_COLOR' in env
let isForceDisabled = !!env.NO_COLOR
|| forceColor === SPACE_MONO
// --no-color --color=false --color=never
|| hasFlag(/^-{1,2}(no-color|color=(false|never))$/);
Expand All @@ -133,16 +133,13 @@ export const getColorSpace = (mockThis) => {

// when Next.JS runtime is `edge`, process.stdout is undefined, but colors output is supported
// runtime values supported colors: `nodejs`, `edge`, `experimental-edge`
let isNextJS = (env.NEXT_RUNTIME || '').indexOf('edge') > -1;
let isNextJS = (env.NEXT_RUNTIME || '').includes('edge');

// PM2 does not set process.stdout.isTTY, but colors may be supported (depends on actual terminal)
let isPM2 = 'PM2_HOME' in env && 'pm_id' in env;

// is used in any shell if GPG is installed
let isGPG_TTY = 'GPG_TTY' in env;
let isPM2 = !!env.PM2_HOME && !!env.pm_id;

// whether the output is supported
let isTTY = isGPG_TTY || isPM2 || isNextJS || (isDeno ? Deno.isatty(1) : stdout && 'isTTY' in stdout);
let isTTY = isPM2 || isNextJS || (isDeno ? Deno.isatty(1) : stdout && !!stdout.isTTY);

// optimisation: placed here to reduce the size of the compiled bundle
if (isForceDisabled) return SPACE_MONO;
Expand Down
3 changes: 2 additions & 1 deletion test/ansi16.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { esc } from './utils/helpers.js';
// import env variables to simulate ANSI 16 color space
import './env/color-space.ansi16.js';

import { hex, green } from '../src/index.mjs';
//import { hex, green } from '../src/index.mjs'; // for debugging only
import { hex, green } from 'ansis';

describe('color space', () => {
test(`convert truecolor to ANSI 16 color space`, () => {
Expand Down
3 changes: 2 additions & 1 deletion test/ansi256.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { esc } from './utils/helpers.js';
// import env variables to simulate ANSI 256 color space
import './env/color-space.ansi256.js';

import { hex, ansi256 } from '../src/index.mjs';
//import { hex, ansi256 } from '../src/index.mjs'; // for debugging only
import { hex, ansi256 } from 'ansis';

describe('color space', () => {
test(`convert truecolor to ANSI 256 color space`, () => {
Expand Down
3 changes: 2 additions & 1 deletion test/cli/output.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node
import { red, rgb, bgRgb, hex, bgHex } from '../../src/index.mjs';
//import { red, rgb, bgRgb, hex, bgHex } from '../../src/index.mjs'; // for debugging only
import { red, rgb, bgRgb, hex, bgHex } from 'ansis';

// test output into terminal, depends on flags (--no-color, --color) and environment variables (NO_COLOR, FORCE_COLOR)

Expand Down
2 changes: 1 addition & 1 deletion test/env/color-space.ansi16.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// set env variable to simulate ANSI 16 color space
process.env.TF_BUILD = 'true';
process.env.COLORTERM = 'ansi';
2 changes: 1 addition & 1 deletion test/env/color-space.ansi256.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// set env variable to simulate ANSI 256 color space
process.env.TEAMCITY_VERSION = 'true';
process.env.COLORTERM = 'ansi256';
4 changes: 2 additions & 2 deletions test/functional.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { esc } from './utils/helpers.js';
// import env variables to simulate truecolor color space in CLI
import './env/color-space.truecolor.js';

//import ansis, { Ansis, red, yellow, green, bold, hex } from '../src/index.mjs'; // use it for debugging only
import ansis, { Ansis, red, grey, gray, green, yellow, bold, italic, underline, hex } from 'ansis'; // test build package
//import ansis, { Ansis, red, yellow, green, bold, hex } from '../src/index.mjs'; // // for debugging only
import ansis, { Ansis, red, grey, gray, green, yellow, bold, italic, underline, hex } from 'ansis'; // test npm package

describe('support colors', () => {
test(`ansis.isSupported()`, () => {
Expand Down
3 changes: 2 additions & 1 deletion test/no-color.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { expect, describe, test } from 'vitest';
// import env variables to simulate no color
import './env/color-space.mono.js';

import ansis, { red } from '../src/index.mjs';
//import ansis, { red } from '../src/index.mjs'; // for debugging only
import ansis, { red } from 'ansis';

describe('color space', () => {
test(`ansis.isSupported()`, () => {
Expand Down

0 comments on commit 48a7450

Please sign in to comment.