gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 7.36 kB
JavaScript
/*! Copyright 2023-2024 the gnablib contributors MPL-1.1 */
var t,e=this&&this.__classPrivateFieldGet||function(t,e,r,n){if("a"===r&&!n)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!n:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?n:"a"===r?n.call(t):n?n.value:e.get(t)};import{padStart as r}from"../primitive/xtString.js";import{config as n}from"../runtime/Config.js";import{sNum as o}from"../safe/safe.js";n.define("color",!0).importAvailEnv("NO_COLOR",((t,e)=>{t.length>0&&e(!1)})).importEnv("NODE_DISABLE_COLORS",((t,e)=>{"1"===t&&e(!1)})).importEnv("TERM",((t,e)=>{"dumb"===t&&e(!1)})),n.define("ttyStyle",!0).importEnv("TERM",((t,e)=>{"dumb"===t&&e(!1)}));const l=n.getBool("color");function s(t,e){return l?`[${e.join(";")}${t}`:""}function i(t){return"number"==typeof t?s("m",[t]):t.length>0?s("m",t):""}class c{constructor(t,e=[]){this.off=t,this.on=e}now(){return this.on.length>0?i(this.on):i(this.off)}}export class Ctrl{constructor(t,e){this.end=t,this.by=e}exec(){return s(this.end,this.by)}static up(t){return new Ctrl("A",t?[t]:[])}static down(t){return new Ctrl("B",t?[t]:[])}static right(t){return new Ctrl("C",t?[t]:[])}static left(t){return new Ctrl("D",t?[t]:[])}static jump(t=1,e=1){return new Ctrl("H",[t,e])}static get clearScreen_toEnd(){return new Ctrl("J",[])}static get clearScreen_toStart(){return new Ctrl("J",[1])}static get clearScreen_all(){return new Ctrl("J",[2])}static get clearScreen_allAndScroll(){return new Ctrl("J",[3])}static get clearLine_toEnd(){return new Ctrl("K",[])}static get clearLine_toStart(){return new Ctrl("K",[1])}static get clearLine_all(){return new Ctrl("K",[2])}}class u{constructor(){t.set(this,[])}add(r){return 0==r.on.length?delete e(this,t,"f")[r.off]:e(this,t,"f")[r.off]=r.on,r.now()}cleanup(){const r=[];for(const n in e(this,t,"f"))r.push(+n);return i(r)}}t=new WeakMap;export class Underline extends c{constructor(t){super(24,t?[t]:void 0)}static get off(){return new Underline}static get single(){return new Underline(4)}static get double(){return new Underline(21)}}export class Weight extends c{constructor(t){super(22,t?[t]:void 0)}static get regular(){return new Weight}static get bold(){return new Weight(1)}static get faint(){return new Weight(2)}}export class LetterStyle extends c{constructor(t){super(23,t?[t]:void 0)}static get regular(){return new LetterStyle}static get italic(){return new LetterStyle(3)}static get gothic(){return new LetterStyle(20)}}export class Blink extends c{constructor(t){super(25,t?[t]:void 0)}static get none(){return new Blink}static get slow(){return new Blink(5)}static get fast(){return new Blink(6)}}export class Invert extends c{constructor(t){super(27,t?[t]:void 0)}static get off(){return new Invert}static get on(){return new Invert(7)}}export class Hide extends c{constructor(t){super(28,t?[t]:void 0)}static get off(){return new Hide}static get on(){return new Hide(8)}}export class StrikeThrough extends c{constructor(t){super(29,t?[t]:void 0)}static get off(){return new StrikeThrough}static get on(){return new StrikeThrough(9)}}export class Overline extends c{constructor(t){super(55,t?[t]:void 0)}static get off(){return new Overline}static get on(){return new Overline(53)}}export class Color extends c{constructor(t){super(39,t)}static get default(){return new Color}static get black(){return new Color([30])}static get red(){return new Color([31])}static get green(){return new Color([32])}static get yellow(){return new Color([33])}static get blue(){return new Color([34])}static get magenta(){return new Color([35])}static get cyan(){return new Color([36])}static get white(){return new Color([37])}static get gray(){return new Color([90])}static grey24(t){o("value",t).unsigned().lt(24).throwNot();return new Color([38,5,232+t])}static rgb6(t,e,r){o("r",t).unsigned().lt(6).throwNot(),o("g",e).unsigned().lt(6).throwNot(),o("b",r).unsigned().lt(6).throwNot();return new Color([38,5,16+36*t+6*e+r])}static rgb(t,e,r){return new Color([38,2,255&t,255&e,255&r])}}export class BgColor extends c{constructor(t){super(49,t)}static get default(){return new BgColor}static get black(){return new BgColor([40])}static get red(){return new BgColor([41])}static get green(){return new BgColor([42])}static get yellow(){return new BgColor([43])}static get blue(){return new BgColor([44])}static get magenta(){return new BgColor([45])}static get cyan(){return new BgColor([46])}static get white(){return new BgColor([47])}static grey24(t){o("value",t).unsigned().lt(24).throwNot();return new BgColor([48,5,232+t])}static rgb6(t,e,r){o("r",t).unsigned().lt(6).throwNot(),o("g",e).unsigned().lt(6).throwNot(),o("b",r).unsigned().lt(6).throwNot();return new BgColor([48,5,16+36*t+6*e+r])}static rgb(t,e,r){return new BgColor([48,2,255&t,255&e,255&r])}}export function reset(){return i(0)}export function tty(t,...e){const r=[],n=new u;for(let o=0;o<e.length;o++){r.push(t[o]);const l=e[o];l instanceof c?r.push(n.add(l)):l instanceof Ctrl?r.push(l.exec()):r.push(l)}return r.push(t[t.length-1]),r.push(n.cleanup()),r.join("")}export function demo(){const t="◀▶",e="◁▷",n=16;const[o,l]=function(){const o=[],l=[];let s=tty`${Underline.single}Foreground colors (named)`+"\n",i=tty`${Underline.single}Background colors (named)`+"\n";const c=["black","red","green","yellow","blue","magenta","cyan","white"];for(let t=0;t<c.length;t++)s+=tty` [${Color[c[t]]}${r(c[t],n)}`+"]",i+=tty` [${BgColor[c[t]]}${r(c[t],n)}`+"]",3&~t||(s+="\n",i+="\n");o.push(s),l.push(i),s=tty`${Underline.single}Foreground colors (grey-24)`+"\n",i=tty`${Underline.single}Background colors (grey-24)`+"\n";for(let r=0;r<24;r++)s+=tty`${Color.grey24(r)}${t}`,i+=tty`${BgColor.grey24(r)}${e}`,r%6==5&&(s+=" ",i+=" ");o.push(s+"\n"),l.push(i+"\n"),s=tty`${Underline.single}Foreground colors (rgb-6)`+"\n",i=tty`${Underline.single}Background colors (rgb-6)`+"\n";for(let r=0;r<6;r++){for(let n=0;n<6;n++){for(let o=0;o<6;o++)s+=tty`${Color.rgb6(r,n,o)}${t}`,i+=tty`${BgColor.rgb6(r,n,o)}${e}`;s+=" ",i+=" "}s+="\n",i+="\n"}o.push(s),l.push(i),s=tty`${Underline.single}Foreground colors (rgb +=32)`+"\n",i=tty`${Underline.single}Background colors (rgb +=32)`+"\n";for(let r=0;r<256;r+=32){for(let n=0;n<128;n+=32){for(let o=0;o<256;o+=32)s+=tty`${Color.rgb(r,n,o)}${t}`,i+=tty`${BgColor.rgb(r,n,o)}${e}`;s+=" ",i+=" "}s+="\n",i+="\n"}for(let r=0;r<256;r+=32){for(let n=128;n<256;n+=32){for(let o=0;o<256;o+=32)s+=tty`${Color.rgb(r,n,o)}${t}`,i+=tty`${BgColor.rgb(r,n,o)}${e}`;s+=" ",i+=" "}s+="\n",i+="\n"}return o.push(s),l.push(i),[o,l]}();return[function(){let t=tty`${Underline.single}Text styles (*=hide)`+"\n";return t+=tty` [${Underline.single}${r("underline",n)}`+"]",t+=tty` [${Underline.double}${r("doubleUnder",n)}`+"]",t+=tty` [${StrikeThrough.on}${r("strikeThrough",n)}`+"]",t+=tty` [${Overline.on}${r("overline",n)}`+"]\n",t+=tty` [${Weight.bold}${r("bold",n)}`+"]",t+=tty` [${Weight.faint}${r("faint",n)}`+"]",t+=tty` [${LetterStyle.italic}${r("italic",n)}`+"]",t+=tty` [${LetterStyle.gothic}${r("gothic",n)}`+"]\n",t+=tty` [${Blink.slow}${r("blinkSlow",n)}`+"]",t+=tty` [${Blink.fast}${r("blinkFast",n)}`+"]",t+=tty` [${Invert.on}${r("invert",n)}`+"]",t+=tty` [${Hide.on}${r("hide",15)}`+"]*\n",t}(),...o,...l].join("\n")}