termark 2.1.0 → 3.0.0
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/LICENCE.txt +177 -177
- package/README.md +180 -313
- package/README.npm.md +57 -0
- package/dist/__internal/conversion.cjs +1 -0
- package/dist/__internal/conversion.mjs +1 -0
- package/dist/__internal/env.cjs +1 -0
- package/dist/__internal/env.d.mts +21 -0
- package/dist/__internal/env.mjs +1 -0
- package/dist/__internal/format.cjs +1 -0
- package/dist/__internal/format.mjs +1 -0
- package/dist/__internal/impl.cjs +1 -0
- package/dist/__internal/impl.mjs +1 -0
- package/dist/__internal/styling.cjs +1 -0
- package/dist/__internal/styling.d.mts +115 -0
- package/dist/__internal/styling.mjs +1 -0
- package/dist/__internal/util.cjs +1 -0
- package/dist/__internal/util.mjs +1 -0
- package/dist/index.cjs +4 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.mts +161 -0
- package/dist/index.mjs +4 -1
- package/dist/types.d.mts +279 -0
- package/dist/utilities.cjs +1 -0
- package/dist/utilities.d.mts +54 -0
- package/dist/utilities.mjs +1 -0
- package/package.json +33 -21
- package/dist/index.d.ts +0 -383
- package/dist/util.d.ts +0 -157
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
//#region src/__internal/styling.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Built-in styles.
|
|
4
|
+
*/
|
|
5
|
+
declare const BUILT_INS: {
|
|
6
|
+
readonly reset: readonly [0, 0];
|
|
7
|
+
readonly bold: readonly [1, 22];
|
|
8
|
+
readonly dim: readonly [2, 22];
|
|
9
|
+
readonly italic: readonly [3, 23];
|
|
10
|
+
readonly underline: readonly [4, 24];
|
|
11
|
+
readonly blink: readonly [5, 25];
|
|
12
|
+
readonly inverse: readonly [7, 27];
|
|
13
|
+
readonly overline: readonly [53, 55];
|
|
14
|
+
readonly hidden: readonly [8, 28];
|
|
15
|
+
readonly strike: readonly [9, 29];
|
|
16
|
+
readonly black: readonly [30, 39];
|
|
17
|
+
readonly red: readonly [31, 39];
|
|
18
|
+
readonly green: readonly [32, 39];
|
|
19
|
+
readonly yellow: readonly [33, 39];
|
|
20
|
+
readonly blue: readonly [34, 39];
|
|
21
|
+
readonly magenta: readonly [35, 39];
|
|
22
|
+
readonly cyan: readonly [36, 39];
|
|
23
|
+
readonly white: readonly [37, 39];
|
|
24
|
+
readonly bgBlack: readonly [40, 49];
|
|
25
|
+
readonly bgRed: readonly [41, 49];
|
|
26
|
+
readonly bgGreen: readonly [42, 49];
|
|
27
|
+
readonly bgYellow: readonly [43, 49];
|
|
28
|
+
readonly bgBlue: readonly [44, 49];
|
|
29
|
+
readonly bgMagenta: readonly [45, 49];
|
|
30
|
+
readonly bgCyan: readonly [46, 49];
|
|
31
|
+
readonly bgWhite: readonly [47, 49];
|
|
32
|
+
readonly brightBlack: readonly [90, 39];
|
|
33
|
+
readonly grey: readonly [90, 39];
|
|
34
|
+
readonly gray: readonly [90, 39];
|
|
35
|
+
readonly brightRed: readonly [91, 39];
|
|
36
|
+
readonly brightGreen: readonly [92, 39];
|
|
37
|
+
readonly brightYellow: readonly [93, 39];
|
|
38
|
+
readonly brightBlue: readonly [94, 39];
|
|
39
|
+
readonly brightMagenta: readonly [95, 39];
|
|
40
|
+
readonly brightCyan: readonly [96, 39];
|
|
41
|
+
readonly brightWhite: readonly [97, 39];
|
|
42
|
+
readonly bgBrightBlack: readonly [100, 49];
|
|
43
|
+
readonly bgGrey: readonly [100, 49];
|
|
44
|
+
readonly bgGray: readonly [100, 49];
|
|
45
|
+
readonly bgBrightRed: readonly [101, 49];
|
|
46
|
+
readonly bgBrightGreen: readonly [102, 49];
|
|
47
|
+
readonly bgBrightYellow: readonly [103, 49];
|
|
48
|
+
readonly bgBrightBlue: readonly [104, 49];
|
|
49
|
+
readonly bgBrightMagenta: readonly [105, 49];
|
|
50
|
+
readonly bgBrightCyan: readonly [106, 49];
|
|
51
|
+
readonly bgBrightWhite: readonly [107, 49];
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Styles for the borders of {@linkcode box}.
|
|
55
|
+
*/
|
|
56
|
+
declare const BOX_BORDERS: {
|
|
57
|
+
readonly sharp: {
|
|
58
|
+
readonly tl: "┌";
|
|
59
|
+
readonly tr: "┐";
|
|
60
|
+
readonly bl: "└";
|
|
61
|
+
readonly br: "┘";
|
|
62
|
+
readonly h: "─";
|
|
63
|
+
readonly v: "│";
|
|
64
|
+
};
|
|
65
|
+
readonly rounded: {
|
|
66
|
+
readonly tl: "╭";
|
|
67
|
+
readonly tr: "╮";
|
|
68
|
+
readonly bl: "╰";
|
|
69
|
+
readonly br: "╯";
|
|
70
|
+
readonly h: "─";
|
|
71
|
+
readonly v: "│";
|
|
72
|
+
};
|
|
73
|
+
readonly double: {
|
|
74
|
+
readonly tl: "╔";
|
|
75
|
+
readonly tr: "╗";
|
|
76
|
+
readonly bl: "╚";
|
|
77
|
+
readonly br: "╝";
|
|
78
|
+
readonly h: "═";
|
|
79
|
+
readonly v: "║";
|
|
80
|
+
};
|
|
81
|
+
readonly heavy: {
|
|
82
|
+
readonly tl: "┏";
|
|
83
|
+
readonly tr: "┓";
|
|
84
|
+
readonly bl: "┗";
|
|
85
|
+
readonly br: "┛";
|
|
86
|
+
readonly h: "━";
|
|
87
|
+
readonly v: "┃";
|
|
88
|
+
};
|
|
89
|
+
readonly dashed: {
|
|
90
|
+
readonly tl: "┌";
|
|
91
|
+
readonly tr: "┐";
|
|
92
|
+
readonly bl: "└";
|
|
93
|
+
readonly br: "┘";
|
|
94
|
+
readonly h: "⹀";
|
|
95
|
+
readonly v: "╎";
|
|
96
|
+
};
|
|
97
|
+
readonly dotted: {
|
|
98
|
+
readonly tl: "┌";
|
|
99
|
+
readonly tr: "┐";
|
|
100
|
+
readonly bl: "└";
|
|
101
|
+
readonly br: "┘";
|
|
102
|
+
readonly h: "╌";
|
|
103
|
+
readonly v: "┊";
|
|
104
|
+
};
|
|
105
|
+
readonly ascii: {
|
|
106
|
+
readonly tl: "+";
|
|
107
|
+
readonly tr: "+";
|
|
108
|
+
readonly bl: "+";
|
|
109
|
+
readonly br: "+";
|
|
110
|
+
readonly h: "-";
|
|
111
|
+
readonly v: "|";
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
//#endregion
|
|
115
|
+
export { BOX_BORDERS, BUILT_INS };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{formatForNesting as e,toFormatter as t}from"./format.mjs";import{getColourLevel as n,hasNoColour as r}from"./env.mjs";import{clamp as i,hexColourRegex as a,interpolate as o}from"./util.mjs";import{ansi256To4bit as s,hexToRgb as c,rgbToAnsi256 as l}from"./conversion.mjs";const u={reset:[0,0],bold:[1,22],dim:[2,22],italic:[3,23],underline:[4,24],blink:[5,25],inverse:[7,27],overline:[53,55],hidden:[8,28],strike:[9,29],black:[30,39],red:[31,39],green:[32,39],yellow:[33,39],blue:[34,39],magenta:[35,39],cyan:[36,39],white:[37,39],bgBlack:[40,49],bgRed:[41,49],bgGreen:[42,49],bgYellow:[43,49],bgBlue:[44,49],bgMagenta:[45,49],bgCyan:[46,49],bgWhite:[47,49],brightBlack:[90,39],grey:[90,39],gray:[90,39],brightRed:[91,39],brightGreen:[92,39],brightYellow:[93,39],brightBlue:[94,39],brightMagenta:[95,39],brightCyan:[96,39],brightWhite:[97,39],bgBrightBlack:[100,49],bgGrey:[100,49],bgGray:[100,49],bgBrightRed:[101,49],bgBrightGreen:[102,49],bgBrightYellow:[103,49],bgBrightBlue:[104,49],bgBrightMagenta:[105,49],bgBrightCyan:[106,49],bgBrightWhite:[107,49]},d={sharp:{tl:`┌`,tr:`┐`,bl:`└`,br:`┘`,h:`─`,v:`│`},rounded:{tl:`╭`,tr:`╮`,bl:`╰`,br:`╯`,h:`─`,v:`│`},double:{tl:`╔`,tr:`╗`,bl:`╚`,br:`╝`,h:`═`,v:`║`},heavy:{tl:`┏`,tr:`┓`,bl:`┗`,br:`┛`,h:`━`,v:`┃`},dashed:{tl:`┌`,tr:`┐`,bl:`└`,br:`┘`,h:`⹀`,v:`╎`},dotted:{tl:`┌`,tr:`┐`,bl:`└`,br:`┘`,h:`╌`,v:`┊`},ascii:{tl:`+`,tr:`+`,bl:`+`,br:`+`,h:`-`,v:`|`}};function f(e,r){let i;if(n()===3){let n=e.join(`;`);i=t(r===`fg`?`38;2;${n}`:`48;2;${n}`)}else if(n()===2){let n=l(e);i=t(r===`fg`?`38;5;${n}`:`48;5;${n}`)}else{let n=s(l(e));i=t(r===`fg`?n:n+10)}return i}function p(a,o){if(r()||n()===0)return e=>e;let s=f(a.map(e=>i(e,0,255)),o),c=t(o===`fg`?39:49);return t=>e(t,s,c)}function m(e,t){return a.test(e)?p(c(e),t):e=>e}function h(e,i){return r()||n()===0||e.length===0?e=>e:e.length===1?e.every(e=>typeof e==`string`)?m(e[0],i):p(e[0],i):n=>{let r=e.every(e=>typeof e==`string`)?e.map(e=>c(e)):e,a=r.length,s=Math.min(n.length/(a-1)),l=``,u=t(i===`fg`?39:49);for(let e=0;e<n.length;e++){let t=Math.min(Math.floor(e/s),a-2),c=e%s/s,d=r[t],p=r[t+1],[m,h,g]=o(d,p,c),_=f([m,h,g],i);l+=`${_}${n[e]}${u}`}return l}}function g(a,o){if(r()||n()===0)return e=>e;let c=i(a,0,255),l;if(n()>=2)l=t(o===`fg`?`38;5;${c}`:`48;5;${c}`);else{let e=s(c);l=t(o===`fg`?e:e+10)}let u=t(o===`fg`?39:49);return t=>e(t,l,u)}export{g as $ansi256,h as $gradient,m as $hex,p as $rgb,d as BOX_BORDERS,u as BUILT_INS};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=/^#?([a-f\d]{3}|[a-f\d]{6})$/i;function t(e,t,n){return Math.min(Math.max(e,t),n)}function n(e,t,n){return[Math.round(e[0]+n*(t[0]-e[0])),Math.round(e[1]+n*(t[1]-e[1])),Math.round(e[2]+n*(t[2]-e[2]))]}exports.clamp=t,exports.hexColourRegex=e,exports.interpolate=n;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=/^#?([a-f\d]{3}|[a-f\d]{6})$/i;function t(e,t,n){return Math.min(Math.max(e,t),n)}function n(e,t,n){return[Math.round(e[0]+n*(t[0]-e[0])),Math.round(e[1]+n*(t[1]-e[1])),Math.round(e[2]+n*(t[2]-e[2]))]}export{t as clamp,e as hexColourRegex,n as interpolate};
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`./__internal/env.cjs`),t=require(`./__internal/styling.cjs`),n=require(`./__internal/impl.cjs`),r=require(`./utilities.cjs`),i=n.createProxy(new n.TermarkImpl);function a(...e){let t={};for(let n of e)Object.assign(t,n(i));return n.createProxy(new n.TermarkImpl,t)}function o(e,n={}){let{kind:i=`sharp`,style:a=e=>e,padding:o=1,margin:s=0}=n,c=t.BOX_BORDERS[i],l=Math.floor(o/4),u=Math.floor(s/4),d=e.split(`
|
|
2
|
+
`),f=d.map(e=>r.stripAnsi(e).length),p=Math.max(...f),m=c.tl+c.h.repeat(p+o*2)+c.tr,h=c.bl+c.h.repeat(p+o*2)+c.br,g=` `.repeat(s),_=`
|
|
3
|
+
`.repeat(u),v=g+a(m)+g,y=g+a(h)+g,b=g+a(c.v),x=a(c.v)+g;return`${_}${v}\n${d.map(e=>{let t=r.stripAnsi(e),n=e+` `.repeat(p-t.length),i=b+` `.repeat(o*2+t.length)+x,a=l===0?``:`${i}\n`.repeat(l),s=l===0?``:`\n${i}`.repeat(l),c=` `.repeat(o);return`${a}${b}${c}${n}${c}${x}${s}`}).join(`
|
|
4
|
+
`)}\n${y}${_}`}function s(e,t){let n={};for(let t in e){let r=e[t];n[t]=i.hex(r)}if(t)for(let t in e){let r=e[t],a=`bg${t.slice(0,1).toUpperCase()}${t.slice(1)}`;n[a]=i.hex(r)}return a(()=>({...n}))}exports.ansiLowerCase=r.ansiLowerCase,exports.ansiUpperCase=r.ansiUpperCase,exports.areColoursSupported=r.areColoursSupported,exports.box=o,exports.extend=a,exports.fmt=i,exports.fromColObject=s,exports.getColourLevel=e.getColourLevel,exports.hasNoColour=e.hasNoColour,exports.stripAnsi=r.stripAnsi,exports.supports=r.supports,exports.supportsAtMost=r.supportsAtMost,exports.transformPreserveAnsi=r.transformPreserveAnsi;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type * from './index.d.mts'
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { ANSICode, BoxOptions, BuiltIn, ColourLevel, ColourObject, FromColObjectReturn, LinkFn, MiscellaneousFns, ParamStyling, RGB, StyleElement, StyleFn, TermarkAPI, TermarkToJSONReturn, Terminator } from "./types.mjs";
|
|
2
|
+
import { getColourLevel, hasNoColour } from "./__internal/env.mjs";
|
|
3
|
+
import { ansiLowerCase, ansiUpperCase, areColoursSupported, stripAnsi, supports, supportsAtMost, transformPreserveAnsi } from "./utilities.mjs";
|
|
4
|
+
|
|
5
|
+
//#region src/index.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Style some text for terminal output.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { fmt } from "termark";
|
|
12
|
+
*
|
|
13
|
+
* const styled = fmt.bold.green.italic("Text");
|
|
14
|
+
*
|
|
15
|
+
* console.log(styled);
|
|
16
|
+
* // => "Text" printed in bold, green (if colours are supported/enabled), and italic.
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* Each member of the chain can be called by passing a string, or can be accessed to apply further styling.
|
|
20
|
+
* Use `.text()` to finally terminate the styling.
|
|
21
|
+
*
|
|
22
|
+
* @example Terminating the chain with `.text()`
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { fmt } from "termark";
|
|
25
|
+
*
|
|
26
|
+
* const styled = fmt.underline.bgBlue.black.text("Text");
|
|
27
|
+
*
|
|
28
|
+
* console.log(styled);
|
|
29
|
+
* // => "Text" printed in black with a blue background (for both: if colours are supported/enabled) and underlined.
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* `.text()` is primarily for methods such as `hex`, `rgb`, or `gradient`, that would be inconvenient to use without it.
|
|
33
|
+
*
|
|
34
|
+
* @example `.text()` on `hex` vs. not using `.text()`
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { fmt } from "termark";
|
|
37
|
+
*
|
|
38
|
+
* const custom = fmt.hex("#ffa500").text("orange");
|
|
39
|
+
*
|
|
40
|
+
* // Not using `.text()` is also possible, but less elegant:
|
|
41
|
+
*
|
|
42
|
+
* const withoutTextCall = fmt.hex("#80b3ff")("Text");
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
declare const fmt: TermarkAPI;
|
|
46
|
+
/**
|
|
47
|
+
* Extend the functionality of Termark with custom methods.
|
|
48
|
+
*
|
|
49
|
+
* > **Note:** Methods created this way can be called as the last item in the chain only.
|
|
50
|
+
*
|
|
51
|
+
* @example Creating a `shout` method
|
|
52
|
+
* ```ts
|
|
53
|
+
* import { extend, ansiUpperCase } from "termark";
|
|
54
|
+
*
|
|
55
|
+
* const myLib = extend((base) => ({
|
|
56
|
+
* shout: (str) => base.bold.red(ansiUpperCase(str)),
|
|
57
|
+
* }));
|
|
58
|
+
*
|
|
59
|
+
* console.log(
|
|
60
|
+
* myLib.shout("this text is red, bold, and uppercase!"),
|
|
61
|
+
* myLib.italic.shout("works in the chain, too!")
|
|
62
|
+
* );
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* > **Note:** Methods that transform text instead of merely style it need to use {@linkcode transformAnsi} or a
|
|
66
|
+
* function derived from it to work with nesting properly.
|
|
67
|
+
*
|
|
68
|
+
* @example Creating a custom colour method
|
|
69
|
+
* ```ts
|
|
70
|
+
* import { extend } from "termark";
|
|
71
|
+
*
|
|
72
|
+
* const myLib = extend((base) => ({
|
|
73
|
+
* orange: (str) => base.hex("#ffa500").text(str),
|
|
74
|
+
* }));
|
|
75
|
+
*
|
|
76
|
+
* console.log(
|
|
77
|
+
* myLib.orange("This is orange text"),
|
|
78
|
+
* myLib.bold.italic.orange("Bold, italic and orange text")
|
|
79
|
+
* );
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* @param factories An array of functions returning custom methods that can be accessed from the return-value of
|
|
83
|
+
* `extend`.
|
|
84
|
+
*/
|
|
85
|
+
declare function extend<M extends Record<string, StyleFn>>(...factories: ((base: TermarkAPI) => M)[]): TermarkAPI<M>;
|
|
86
|
+
/**
|
|
87
|
+
* Create a box around some text.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* import { box } from "termark";
|
|
92
|
+
*
|
|
93
|
+
* const boxed = box("This text will be in a box")
|
|
94
|
+
*
|
|
95
|
+
* console.log(boxed);
|
|
96
|
+
*
|
|
97
|
+
* // Returned:
|
|
98
|
+
* // ┌────────────────────────────┐
|
|
99
|
+
* // │ This text will be in a box │
|
|
100
|
+
* // └────────────────────────────┘
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* @param text The text within the box.
|
|
104
|
+
* @param options Configuration options.
|
|
105
|
+
*/
|
|
106
|
+
declare function box(text: string, options?: BoxOptions): string;
|
|
107
|
+
/**
|
|
108
|
+
* Generate colour-styling from an object literal. This primarily exists for use with
|
|
109
|
+
* [`css-color-names`](https://github.com/bahamas10/css-color-names).
|
|
110
|
+
*
|
|
111
|
+
* Because this function uses {@linkcode extend} under the hood, colour methods created this way must be called as the
|
|
112
|
+
* last one in the chain.
|
|
113
|
+
*
|
|
114
|
+
* @example Using `css-color-names`
|
|
115
|
+
* ```ts
|
|
116
|
+
* import cssColours from "css-color-names";
|
|
117
|
+
* import { fromColObject } from "termark";
|
|
118
|
+
*
|
|
119
|
+
* const css = fromColObject(cssColours);
|
|
120
|
+
*
|
|
121
|
+
* console.log(
|
|
122
|
+
* css.lemonchiffron("This text uses a CSS colour!"),
|
|
123
|
+
* css.bold.aliceblue("Works with other styles")
|
|
124
|
+
* );
|
|
125
|
+
* ```
|
|
126
|
+
*
|
|
127
|
+
* @example Enabling background colours
|
|
128
|
+
* ```ts
|
|
129
|
+
* import cssColours from "css-color-names";
|
|
130
|
+
* import { fromColObject } from "termark";
|
|
131
|
+
*
|
|
132
|
+
* const cssWithBg = fromColObject(cssColours, true);
|
|
133
|
+
* ```
|
|
134
|
+
*
|
|
135
|
+
* @example Using [`color-names`](https://github.com/meodai/color-names)
|
|
136
|
+
* ```ts
|
|
137
|
+
* import { colornames } from "color-name-list";
|
|
138
|
+
* import { fromColObject } from "termark";
|
|
139
|
+
*
|
|
140
|
+
* const mapped = colornames.reduce((acc, { name, hex }) => {
|
|
141
|
+
* acc[name] = hex;
|
|
142
|
+
* return acc;
|
|
143
|
+
* }, {});
|
|
144
|
+
*
|
|
145
|
+
* const colours = fromColObject(mapped);
|
|
146
|
+
*
|
|
147
|
+
* console.log(
|
|
148
|
+
* colours["100 Mph"]("This colour is from color-names"),
|
|
149
|
+
* colours.italic.Alaska("...")
|
|
150
|
+
* );
|
|
151
|
+
* ```
|
|
152
|
+
*
|
|
153
|
+
* @param obj An object whose keys are colour names (which will be used as the method name) and whose values are
|
|
154
|
+
* hex colour codes.
|
|
155
|
+
* @param generateBg Whether to generate background styling for all supplied colours. These will have the format
|
|
156
|
+
* `bgColourName` where `ColourName` is the original name of the colour, capitalized. This name is not formatted,
|
|
157
|
+
* so a name like `100 Mph` will retain its spaces.
|
|
158
|
+
*/
|
|
159
|
+
declare function fromColObject<R extends ColourObject, GenBg extends boolean | undefined = undefined>(obj: R, generateBg?: GenBg): TermarkAPI<FromColObjectReturn<R, GenBg>>;
|
|
160
|
+
//#endregion
|
|
161
|
+
export { ANSICode, BoxOptions, BuiltIn, ColourLevel, ColourObject, FromColObjectReturn, LinkFn, MiscellaneousFns, ParamStyling, RGB, StyleElement, StyleFn, TermarkAPI, TermarkToJSONReturn, Terminator, ansiLowerCase, ansiUpperCase, areColoursSupported, box, extend, fmt, fromColObject, getColourLevel, hasNoColour, stripAnsi, supports, supportsAtMost, transformPreserveAnsi };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import{getColourLevel as e,hasNoColour as t}from"./__internal/env.mjs";import{BOX_BORDERS as n}from"./__internal/styling.mjs";import{TermarkImpl as r,createProxy as i}from"./__internal/impl.mjs";import{ansiLowerCase as a,ansiUpperCase as o,areColoursSupported as s,stripAnsi as c,supports as l,supportsAtMost as u,transformPreserveAnsi as d}from"./utilities.mjs";const f=i(new r);function p(...e){let t={};for(let n of e)Object.assign(t,n(f));return i(new r,t)}function m(e,t={}){let{kind:r=`sharp`,style:i=e=>e,padding:a=1,margin:o=0}=t,s=n[r],l=Math.floor(a/4),u=Math.floor(o/4),d=e.split(`
|
|
2
|
+
`),f=d.map(e=>c(e).length),p=Math.max(...f),m=s.tl+s.h.repeat(p+a*2)+s.tr,h=s.bl+s.h.repeat(p+a*2)+s.br,g=` `.repeat(o),_=`
|
|
3
|
+
`.repeat(u),v=g+i(m)+g,y=g+i(h)+g,b=g+i(s.v),x=i(s.v)+g;return`${_}${v}\n${d.map(e=>{let t=c(e),n=e+` `.repeat(p-t.length),r=b+` `.repeat(a*2+t.length)+x,i=l===0?``:`${r}\n`.repeat(l),o=l===0?``:`\n${r}`.repeat(l),s=` `.repeat(a);return`${i}${b}${s}${n}${s}${x}${o}`}).join(`
|
|
4
|
+
`)}\n${y}${_}`}function h(e,t){let n={};for(let t in e){let r=e[t];n[t]=f.hex(r)}if(t)for(let t in e){let r=e[t],i=`bg${t.slice(0,1).toUpperCase()}${t.slice(1)}`;n[i]=f.hex(r)}return p(()=>({...n}))}export{a as ansiLowerCase,o as ansiUpperCase,s as areColoursSupported,m as box,p as extend,f as fmt,h as fromColObject,e as getColourLevel,t as hasNoColour,c as stripAnsi,l as supports,u as supportsAtMost,d as transformPreserveAnsi};
|
package/dist/types.d.mts
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { BOX_BORDERS, BUILT_INS } from "./__internal/styling.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Array with red, green, and blue colour values.
|
|
6
|
+
*/
|
|
7
|
+
type RGB = [number, number, number];
|
|
8
|
+
/**
|
|
9
|
+
* A number representing the supported colour level.
|
|
10
|
+
*
|
|
11
|
+
* - `0` — No colours, just black-and-white text.
|
|
12
|
+
* - `1` — Basic 4-bit (16) colours.
|
|
13
|
+
* - `2` — 8-bit (256) colours.
|
|
14
|
+
* - `3` — 24-bit (16m; `truecolor`) colours.
|
|
15
|
+
*/
|
|
16
|
+
type ColourLevel = 0 | 1 | 2 | 3;
|
|
17
|
+
/**
|
|
18
|
+
* How an ANSI code (e.g., the code for making text bold) can be represented.
|
|
19
|
+
*
|
|
20
|
+
* `string | number` allow single numbers such as `21`, but also complex codes like `38;5;203`, which are
|
|
21
|
+
* not representable by a number alone.
|
|
22
|
+
*/
|
|
23
|
+
type ANSICode = string | number;
|
|
24
|
+
interface TermarkToJSONReturn {
|
|
25
|
+
text: string;
|
|
26
|
+
openingCodes: string[];
|
|
27
|
+
closingCodes: string[];
|
|
28
|
+
}
|
|
29
|
+
type StyleFn = (str: string) => string;
|
|
30
|
+
interface StyleElement {
|
|
31
|
+
open: string;
|
|
32
|
+
close: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Base built-in methods like `red` or `bold`.
|
|
36
|
+
*/
|
|
37
|
+
type BuiltIn = keyof typeof BUILT_INS;
|
|
38
|
+
/**
|
|
39
|
+
* Methods that terminate a styling sequence.
|
|
40
|
+
*/
|
|
41
|
+
interface Terminator {
|
|
42
|
+
/**
|
|
43
|
+
* Return a string that has been styled. Useful for methods like `hex` or `gradient`.
|
|
44
|
+
*
|
|
45
|
+
* @param str An input string.
|
|
46
|
+
*/
|
|
47
|
+
text(str: string): string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Styling functions that take parameters.
|
|
51
|
+
*/
|
|
52
|
+
interface ParamStyling {
|
|
53
|
+
/**
|
|
54
|
+
* Colour some text using a hex colour code.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* import { fmt } from "termark";
|
|
59
|
+
*
|
|
60
|
+
* const customHex = fmt.hex("#338cd1").text("Text");
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* @example Without leading hash (`#`)
|
|
64
|
+
* ```ts
|
|
65
|
+
* import { fmt } from "termark";
|
|
66
|
+
*
|
|
67
|
+
* const customHex = fmt.hex("008330").text("Text");
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* @param code A hex colour code.
|
|
71
|
+
*/
|
|
72
|
+
hex(code: string): TermarkAPI;
|
|
73
|
+
/**
|
|
74
|
+
* Colour some text using red, green, and blue colour values.
|
|
75
|
+
*
|
|
76
|
+
* Each passed value is clamped so that values less than `0` are treated as `0` and values greater than `255` are
|
|
77
|
+
* treated as `255`.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* import { fmt } from "termark";
|
|
82
|
+
*
|
|
83
|
+
* const rgbString = fmt.rgb([128, 5, 243]).text("Text");
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* @param colours An array of red, green, and blue colour values.
|
|
87
|
+
*/
|
|
88
|
+
rgb(colours: RGB): TermarkAPI;
|
|
89
|
+
/**
|
|
90
|
+
* Make some text have a gradient.
|
|
91
|
+
*
|
|
92
|
+
* @example With hex colour codes
|
|
93
|
+
* ```ts
|
|
94
|
+
* import { fmt } from "termark";
|
|
95
|
+
*
|
|
96
|
+
* const textWithGradient = fmt.gradient("#80b3ff", "#338cd1").text("Text");
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* @example With RGB values
|
|
100
|
+
* ```ts
|
|
101
|
+
* import { fmt } from "termark";
|
|
102
|
+
*
|
|
103
|
+
* const textWithGradient = fmt.gradient([128, 179, 255], [51, 140, 209]).text("Text");
|
|
104
|
+
* ```
|
|
105
|
+
*
|
|
106
|
+
* @param colours Colours for the gradient, either as hex colour codes or as RGB tuples (arrays).
|
|
107
|
+
*/
|
|
108
|
+
gradient(...colours: string[] | RGB[]): TermarkAPI;
|
|
109
|
+
/**
|
|
110
|
+
* Use an ANSI number code to apply a specific colour to some text.
|
|
111
|
+
*
|
|
112
|
+
* This value is clamped so that values less than `0` are treated as `0` while values greater than `255` are treated
|
|
113
|
+
* as `255`.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* import { fmt } from "termark";
|
|
118
|
+
*
|
|
119
|
+
* const ansi = fmt.fg(57).text("Text");
|
|
120
|
+
* ```
|
|
121
|
+
*
|
|
122
|
+
* @param code An ANSI code for 8-bit colours.
|
|
123
|
+
*
|
|
124
|
+
* @see https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
|
|
125
|
+
*/
|
|
126
|
+
fg(code: number): TermarkAPI;
|
|
127
|
+
/**
|
|
128
|
+
* Colour the background of some text using a hex colour code.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```ts
|
|
132
|
+
* import { fmt } from "termark";
|
|
133
|
+
*
|
|
134
|
+
* const customHex = fmt.bgHex("#338cd1").text("Text");
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* @example Without leading hash (`#`)
|
|
138
|
+
* ```ts
|
|
139
|
+
* import { fmt } from "termark";
|
|
140
|
+
*
|
|
141
|
+
* const customHex = fmt.bgHex("008330").text("Text");
|
|
142
|
+
* ```
|
|
143
|
+
*
|
|
144
|
+
* @param code A hex colour code.
|
|
145
|
+
*/
|
|
146
|
+
bgHex(code: string): TermarkAPI;
|
|
147
|
+
/**
|
|
148
|
+
* Colour the background of some text using red, green, and blue colour values.
|
|
149
|
+
*
|
|
150
|
+
* Each passed value is clamped so that values less than `0` are treated as `0` and values greater than `255` are
|
|
151
|
+
* treated as `255`.
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* ```ts
|
|
155
|
+
* import { fmt } from "termark";
|
|
156
|
+
*
|
|
157
|
+
* const rgbString = fmt.bgRgb([128, 5, 243]).text("Text");
|
|
158
|
+
* ```
|
|
159
|
+
*
|
|
160
|
+
* @param colours An array of red, green, and blue colour values.
|
|
161
|
+
*/
|
|
162
|
+
bgRgb(colours: RGB): TermarkAPI;
|
|
163
|
+
/**
|
|
164
|
+
* Make the background of some text have a gradient.
|
|
165
|
+
*
|
|
166
|
+
* @example With hex colour codes
|
|
167
|
+
* ```ts
|
|
168
|
+
* import { fmt } from "termark";
|
|
169
|
+
*
|
|
170
|
+
* const textWithGradient = fmt.bgGradient("#80b3ff", "#338cd1").text("Text");
|
|
171
|
+
* ```
|
|
172
|
+
*
|
|
173
|
+
* @example With RGB values
|
|
174
|
+
* ```ts
|
|
175
|
+
* import { fmt } from "termark";
|
|
176
|
+
*
|
|
177
|
+
* const textWithGradient = fmt.bgGradient([128, 179, 255], [51, 140, 209]).text("Text");
|
|
178
|
+
* ```
|
|
179
|
+
*
|
|
180
|
+
* @param colours Colours for the gradient, either as hex colour codes or as RGB tuples (arrays).
|
|
181
|
+
*/
|
|
182
|
+
bgGradient(...colours: string[] | RGB[]): TermarkAPI;
|
|
183
|
+
/**
|
|
184
|
+
* Use an ANSI number code to apply a specific colour to the background of some text.
|
|
185
|
+
*
|
|
186
|
+
* This value is clamped so that values less than `0` are treated as `0` while values greater than `255` are treated
|
|
187
|
+
* as `255`.
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* import { fmt } from "termark";
|
|
192
|
+
*
|
|
193
|
+
* const ansi = fmt.bg(57).text("Text");
|
|
194
|
+
* ```
|
|
195
|
+
*
|
|
196
|
+
* @param code An ANSI code for 8-bit colours.
|
|
197
|
+
*
|
|
198
|
+
* @see https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
|
|
199
|
+
*/
|
|
200
|
+
bg(code: number): TermarkAPI;
|
|
201
|
+
}
|
|
202
|
+
type LinkFn = (url: string, text?: string) => string;
|
|
203
|
+
/**
|
|
204
|
+
* Miscellaneous features that don't fit other category.
|
|
205
|
+
*/
|
|
206
|
+
interface MiscellaneousFns {
|
|
207
|
+
/**
|
|
208
|
+
* Construct an OSC 8 hyperlink by specifying a URL and optionally text to display.
|
|
209
|
+
*
|
|
210
|
+
* `link` acts as a style chain terminator and is called as the last element of such a chain.
|
|
211
|
+
*
|
|
212
|
+
* [OSC 8](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda) may not be supported in all terminal
|
|
213
|
+
* environments. See ["OSC 8 adoption in terminal emulators"](https://github.com/Alhadis/OSC8-Adoption/) for a list
|
|
214
|
+
* of terminals that support OSC 8 hyperlinks. In terminals where OSC 8 hyperlinks aren't supported, the URL will
|
|
215
|
+
* be shown as-is, along with the display text (if one was provided).
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```ts
|
|
219
|
+
* import { fmt } from "termark";
|
|
220
|
+
*
|
|
221
|
+
* const plainLink = fmt.link("https://q-file.com");
|
|
222
|
+
*
|
|
223
|
+
* const styledLink = fmt.red.underline.link("https://q-file.com", "Website");
|
|
224
|
+
* ```
|
|
225
|
+
*
|
|
226
|
+
* @param url A URL to some resource, which can be a webpage (`http://`, `https://`), a local file (`file://`), an
|
|
227
|
+
* email address (`mailto:`), an FTP connection (`ftp://`), etc.
|
|
228
|
+
* @param text An optional text to display. If omitted, `url` will be displayed.
|
|
229
|
+
*
|
|
230
|
+
* @see https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
|
|
231
|
+
* @see https://notes.billmill.org/programming/bash/Hyperlink_escape_codes.html
|
|
232
|
+
*/
|
|
233
|
+
link: LinkFn;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* The API provided by Termark, including all built-in functions as well as user-created extensions.
|
|
237
|
+
*/
|
|
238
|
+
type TermarkAPI<Ext extends Record<string, any> = {}> = { [K in BuiltIn]: TermarkAPI<Ext> } & Ext & StyleFn & Terminator & ParamStyling & MiscellaneousFns;
|
|
239
|
+
/**
|
|
240
|
+
* Configuration options for {@linkcode box}.
|
|
241
|
+
*/
|
|
242
|
+
interface BoxOptions {
|
|
243
|
+
/**
|
|
244
|
+
* @default "sharp"
|
|
245
|
+
*/
|
|
246
|
+
kind?: keyof typeof BOX_BORDERS;
|
|
247
|
+
/**
|
|
248
|
+
* Styling to apply to the border such as making it blue.
|
|
249
|
+
*
|
|
250
|
+
* @default (str) => str
|
|
251
|
+
*/
|
|
252
|
+
style?: StyleFn;
|
|
253
|
+
/**
|
|
254
|
+
* Padding to apply. Top-bottom padding will always be a quarter of left-right padding (that is, top-bottom padding
|
|
255
|
+
* will increase by one for every four padding units).
|
|
256
|
+
*
|
|
257
|
+
* @default 0
|
|
258
|
+
*/
|
|
259
|
+
padding?: number;
|
|
260
|
+
/**
|
|
261
|
+
* Spacing around the box. Top-bottom margin will always be a quarter of left-right margin (that is, top-bottom
|
|
262
|
+
* margin will increase by one for every four margin units).
|
|
263
|
+
*
|
|
264
|
+
* @default 0
|
|
265
|
+
*/
|
|
266
|
+
margin?: number;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Object type to be passed into {@linkcode fromColObject}.
|
|
270
|
+
*
|
|
271
|
+
* Represents an object literal whose keys are colour names and whose values are hex colour codes.
|
|
272
|
+
*/
|
|
273
|
+
type ColourObject = Record<string, string>;
|
|
274
|
+
/**
|
|
275
|
+
* The return-value of {@linkcode fromColObject}.
|
|
276
|
+
*/
|
|
277
|
+
type FromColObjectReturn<R extends ColourObject, GenBg extends boolean | undefined> = { [K in keyof R as K | (GenBg extends true ? `bg${Capitalize<K & string>}` : never)]: StyleFn };
|
|
278
|
+
//#endregion
|
|
279
|
+
export { ANSICode, BoxOptions, BuiltIn, ColourLevel, ColourObject, FromColObjectReturn, LinkFn, MiscellaneousFns, ParamStyling, RGB, StyleElement, StyleFn, TermarkAPI, TermarkToJSONReturn, Terminator };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=require(`./__internal/env.cjs`);function t(){return e.getColourLevel()!==0}function n(t){return e.getColourLevel()>=t}function r(t){return e.getColourLevel()<=t}function i(e){return t=>{let n=/(\u{001B}\[[0-9;]*[A-PRZ])|(.+?)/giu,r=``;for(let i of t.matchAll(n)){let t=i[0];r+=t.charCodeAt(0)===27?t:e(t)}return r}}const a=i(e=>e.toUpperCase()),o=i(e=>e.toLowerCase());function s(e){return e.replace(/\u{001B}\[[0-9;]*m/gu,``)}exports.ansiLowerCase=o,exports.ansiUpperCase=a,exports.areColoursSupported=t,exports.stripAnsi=s,exports.supports=n,exports.supportsAtMost=r,exports.transformPreserveAnsi=i;
|