@travetto/terminal 7.0.3 → 7.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -30,22 +30,24 @@ These palettes then are usable at runtime, with the module determining light or
30
30
 
31
31
  **Code: CLI Color Palette**
32
32
  ```typescript
33
- import { StyleUtil } from '@travetto/terminal';
34
-
35
- export const cliTpl = StyleUtil.getTemplate({
36
- input: '#6b8e23', // Olive drab
37
- output: '#ffc0cb', // Pink
38
- path: '#008080', // Teal
39
- success: '#00ff00', // Green
40
- failure: '#ff0000', // Red
33
+ import { StyleUtil, type TermStyledTemplate } from '@travetto/terminal';
34
+
35
+ const input = {
36
+ input: ['#6b8e23'], // Olive drab
37
+ output: ['#ffc0cb'], // Pink
38
+ path: ['#008080'], // Teal
39
+ success: ['#00ff00'], // Green
40
+ failure: ['#ff0000'], // Red
41
41
  param: ['#ffff00', '#daa520'], // Yellow / Goldenrod
42
- type: '#00ffff', // Teal
42
+ type: ['#00ffff'], // Teal
43
43
  description: ['#e5e5e5', '#808080'], // White / Gray
44
44
  title: ['#ffffff', '#000000'], // Bright white / black
45
- identifier: '#1e90ff', // Dodger blue
45
+ identifier: ['#1e90ff'], // Dodger blue
46
46
  subtitle: ['#d3d3d3', '#a9a9a9'], // Light gray / Dark Gray
47
- subsubtitle: '#a9a9a9' // Dark gray
48
- });
47
+ subsubtitle: ['#a9a9a9'] // Dark gray
48
+ } as const;
49
+
50
+ export const cliTpl: TermStyledTemplate<keyof typeof input> = StyleUtil.getTemplate(input);
49
51
  ```
50
52
 
51
53
  When the color palette is combined with [Runtime](https://github.com/travetto/travetto/tree/main/module/runtime#readme "Runtime for travetto applications.")'s Util.makeTemplate, you produce a string template function that will automatically colorize:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/terminal",
3
- "version": "7.0.3",
3
+ "version": "7.0.4",
4
4
  "type": "module",
5
5
  "description": "General terminal support",
6
6
  "keywords": [
@@ -25,7 +25,7 @@
25
25
  "directory": "module/terminal"
26
26
  },
27
27
  "dependencies": {
28
- "@travetto/runtime": "^7.0.3",
28
+ "@travetto/runtime": "^7.0.4",
29
29
  "chalk": "^5.6.2"
30
30
  },
31
31
  "travetto": {
package/src/style.ts CHANGED
@@ -5,19 +5,48 @@ import { Env, TypedObject } from '@travetto/runtime';
5
5
  type TemplatePrim = number | string | bigint | boolean | RegExp;
6
6
  type Color = `#${string}`;
7
7
  export type TermStyleInput = Color | { text: Color, background?: Color, inverse?: boolean, bold?: boolean, italic?: boolean, underline?: boolean };
8
- type TermStylePairInput = TermStyleInput | [dark: TermStyleInput, light: TermStyleInput];
8
+ type TermStylePairInput = [dark: TermStyleInput, light?: TermStyleInput] | readonly [dark: TermStyleInput, light?: TermStyleInput];
9
9
  export type TermStyleFn = (input: TemplatePrim) => string;
10
- type TermStyledTemplate<T extends string> = (values: TemplateStringsArray, ...keys: (Partial<Record<T, TemplatePrim>> | string)[]) => string;
10
+ export type TermStyledTemplate<T extends string> = (values: TemplateStringsArray, ...keys: (Partial<Record<T, TemplatePrim>> | string)[]) => string;
11
11
  export type ColorLevel = 0 | 1 | 2 | 3;
12
12
 
13
- const DARK_ANSI_256 = new Set([
14
- 0, 1, 2, 3, 4, 5, 6, 16, 17, 18, 19, 20, 22, 23, 24, 25, 26, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38, 40, 41, 42, 43, 44, 52, 53, 54,
15
- 55, 56, 58, 59, 60, 64, 65, 66, 70, 76, 88, 89, 90, 91, 92, 94, 95, 96, 100, 101, 106, 112, 124, 125, 126, 127, 128, 130, 136, 142,
16
- 148, 160, 161, 162, 163, 164, 166, 172, 178, 184, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243
17
- ]);
13
+ const ANSI_16_RGB: [number, number, number][] = [
14
+ [0, 0, 0], [128, 0, 0], [0, 128, 0], [128, 128, 0],
15
+ [0, 0, 128], [128, 0, 128], [0, 128, 128], [192, 192, 192],
16
+ [128, 128, 128], [255, 0, 0], [0, 255, 0], [255, 255, 0],
17
+ [0, 0, 255], [255, 0, 255], [0, 255, 255], [255, 255, 255]
18
+ ];
19
+
20
+ const toLinear = (v: number): number => {
21
+ const s = v / 255;
22
+ return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
23
+ };
18
24
 
19
25
  export class StyleUtil {
20
26
 
27
+ /** Compute RGB values for ANSI 256 color code */
28
+ static computeRGBForAnsi256(code: number): [number, number, number] {
29
+ if (code < 16) {
30
+ return ANSI_16_RGB[code];
31
+ } else if (code <= 231) {
32
+ const cubeIdx = code - 16;
33
+ const levels = [0, 95, 135, 175, 215, 255];
34
+ const ri = Math.floor(cubeIdx / 36);
35
+ const gi = Math.floor((cubeIdx % 36) / 6);
36
+ const bi = cubeIdx % 6;
37
+ return [levels[ri], levels[gi], levels[bi]];
38
+ } else {
39
+ const gray = 8 + (code - 232) * 10;
40
+ return [gray, gray, gray];
41
+ }
42
+ }
43
+
44
+ /** Compute Luminosity for ANSI 256 color code */
45
+ static computeAnsi256Luminosity(code: number): number {
46
+ const [r, g, b] = this.computeRGBForAnsi256(code);
47
+ return 0.2126 * toLinear(r) + 0.7152 * toLinear(g) + 0.0722 * toLinear(b);
48
+ }
49
+
21
50
  static #scheme: { key: string, dark: boolean } = { key: '', dark: true };
22
51
 
23
52
  /**
@@ -50,7 +79,7 @@ export class StyleUtil {
50
79
  }
51
80
 
52
81
  const [, bg = '0'] = key.split(';');
53
- const dark = DARK_ANSI_256.has(+bg);
82
+ const dark = this.computeAnsi256Luminosity(+bg) < 0.4;
54
83
  Object.assign(this.#scheme, { key, dark });
55
84
  return dark;
56
85
  }
@@ -59,7 +88,7 @@ export class StyleUtil {
59
88
  * Create renderer from input source
60
89
  */
61
90
  static getThemedStyle(input: TermStylePairInput): TermStyleFn {
62
- const [dark, light] = (Array.isArray(input) ? input : [input]);
91
+ const [dark, light] = input;
63
92
  const isDark = this.isBackgroundDark();
64
93
  return isDark ? this.getStyle(dark) : this.getStyle(light ?? dark);
65
94
  }
package/src/terminal.ts CHANGED
@@ -1,4 +1,4 @@
1
- import tty from 'node:tty';
1
+ import type tty from 'node:tty';
2
2
 
3
3
  import { Env, Util } from '@travetto/runtime';
4
4
 
package/src/util.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { StyleUtil, TermStyleFn, TermStyleInput } from './style.ts';
2
- import { Terminal, WAIT_TOKEN } from './terminal.ts';
1
+ import { StyleUtil, type TermStyleFn, type TermStyleInput } from './style.ts';
2
+ import { type Terminal, WAIT_TOKEN } from './terminal.ts';
3
3
 
4
4
  type ProgressEvent<T> = { total?: number, idx: number, value: T };
5
5
  type ProgressStyle = { complete: TermStyleFn, incomplete?: TermStyleFn };
package/src/writer.ts CHANGED
@@ -1,4 +1,4 @@
1
- import tty from 'node:tty';
1
+ import type tty from 'node:tty';
2
2
 
3
3
  import { ShutdownManager } from '@travetto/runtime';
4
4