UNPKG

@visulima/cerebro

Version:

A delightful toolkit for building cross-runtime CLIs for Node.js, Deno, and Bun.

7 lines (5 loc) 4.89 kB
var m=Object.defineProperty;var s=(r,e)=>m(r,"name",{value:e,configurable:!0});import{createRequire as b}from"node:module";import{createTable as p}from"@visulima/tabular";import{NO_BORDER as u}from"@visulima/tabular/style";import $ from"@visulima/colorize/template";import{d as O}from"./runtime-process-G-n-wOub.js";import{bold as R}from"@visulima/colorize";const v=b(import.meta.url),l=typeof globalThis<"u"&&typeof globalThis.process<"u"?globalThis.process:process,w=s(r=>{if(typeof l<"u"&&l.versions&&l.versions.node){const[e,t]=l.versions.node.split(".").map(Number);if(e>22||e===22&&t>=3||e===20&&t>=16)return l.getBuiltinModule(r)}return v(r)},"__cjs_getBuiltinModule"),A=w("node:os");var j=Object.defineProperty,_=s((r,e)=>j(r,"name",{value:e,configurable:!0}),"s");const c=new Map,E=500,o=_(r=>{if(!r||r==="")return"";const e=c.get(r);if(e!==void 0)return e;const t=$(Object.assign([],{raw:[r]}));if(c.size>=E){const n=c.keys().next().value;n!==void 0&&c.delete(n)}return c.set(r,t),t},"templateFormat");var L=Object.defineProperty,N=s((r,e)=>L(r,"name",{value:e,configurable:!0}),"t$1");const g=N(()=>{const r=O().CEREBRO_TERMINAL_WIDTH;if(r===void 0)return;const e=Number.parseInt(r,10);if(!(Number.isNaN(e)||e<=0))return e},"getTerminalWidth");var S=Object.defineProperty,W=s((r,e)=>S(r,"name",{value:e,configurable:!0}),"t");class h{static{s(this,"n")}static{W(this,"BaseSection")}lines;constructor(){this.lines=[]}add(e){this.lines.push(e)}toString(){return this.lines.join(A.EOL)}header(e){this.add(R(e)),this.lines.push("")}}var T=Object.defineProperty,B=s((r,e)=>T(r,"name",{value:e,configurable:!0}),"f");class P extends h{static{s(this,"l")}static{B(this,"ContentSection")}constructor(e){if(super(),e.header&&this.header(o(e.header)),e.content){if(e.raw)if(Array.isArray(e.content)&&e.content.every(t=>typeof t=="string"))e.content.forEach(t=>{Array.isArray(t)?t.forEach(n=>this.add(o(n))):this.add(o(t))});else if(typeof e.content=="string")this.add(o(e.content));else throw new TypeError("Invalid raw content, must be a string or array of strings.");else this.add(this.getContentLines(e.content));this.add("")}}getContentLines(e){if(typeof e=="string"){const t=p({showHeader:!1,style:{border:u,paddingLeft:4,paddingRight:1},terminalWidth:g(),truncateOverflow:!1,wordWrap:!0});return t.addRow([o(e)]),t.toString()}if(Array.isArray(e)&&e.every(t=>typeof t=="string"||Array.isArray(t)&&t.every(n=>typeof n=="string"))){const t=p({showHeader:!1,style:{border:u,paddingLeft:4,paddingRight:1},terminalWidth:g(),truncateOverflow:!1,wordWrap:!0});return e.forEach(n=>{Array.isArray(n)?t.addRow(n.map(i=>o(i))):t.addRow([o(n)])}),t.toString()}if(typeof e=="object"){const t=e;if(!t.options||!t.data)throw new Error(`Must have an "options" or "data" property ${JSON.stringify(e)}`);const n=p({showHeader:!1,style:{border:u,paddingLeft:4,paddingRight:1},terminalWidth:g(),truncateOverflow:!1,wordWrap:!0});return t.data.forEach(i=>{Array.isArray(i)?n.addRow(i.map(d=>o(d))):n.addRow([o(i)])}),n.toString()}throw new Error(`invalid input - 'content' must be a string, array of strings or a object: ${JSON.stringify(e)}`)}}var M=Object.defineProperty,C=s((r,e)=>M(r,"name",{value:e,configurable:!0}),"a");class H extends h{static{s(this,"b")}static{C(this,"OptionListSection")}constructor(e){super();let t=e.optionList??[];const n=Array.isArray(e.hide)?e.hide:[e.hide].filter(Boolean),i=Array.isArray(e.group)?e.group:[e.group].filter(Boolean);n.length>0&&(t=t.filter(a=>!n.includes(a.name))),e.header&&this.header(o(e.header)),i.length>0&&(t=t.filter(a=>{const f=i.includes("_none")&&!a.group,y=this.intersect(Array.isArray(a.group)?a.group:[a.group],i);return f||y?a:void 0}));const d=p({showHeader:!1,style:{border:u,paddingLeft:2,paddingRight:1,...e.tableOptions?.style},terminalWidth:g()??e.tableOptions?.terminalWidth,truncateOverflow:!1,wordWrap:e.tableOptions?.wordWrap??!0,...e.tableOptions});t.forEach(a=>d.addRow([this.getOptionNames(a,e.reverseNameOrder??!1,e.isArgument??!1),o(a.description)])),this.add(d.toString()),this.lines.push("")}getOptionNames(e,t,n){if(!e.name)throw new TypeError("Invalid option definition, name is required.");let i=e.type?e.type.name.toLowerCase():"string";const d=e.multiple||e.lazyMultiple?"[]":"";i=o(e.typeLabel??`{underline ${i}${d}}`);let a;if(e.alias)if(e.name){const f=n?e.name:`{yellow --${e.name}}`;a=o(t?`{bold ${f}}, {bold -${e.alias}} ${i}`:`{bold -${e.alias}}, {bold ${f}} ${i}`)}else t?a=o(`{bold -${e.alias}} ${i}`):a=o(`{bold -${e.alias}} ${i}`);else a=o(`{bold ${n?e.name:`{yellow --${e.name}}`}} ${i}`);return a}intersect(e,t){return e.some(n=>t.includes(n))}}var I=Object.defineProperty,q=s((r,e)=>I(r,"name",{value:e,configurable:!0}),"o");const G=q(r=>(Array.isArray(r)?r:[r]).length===0?"":` ${r.map(e=>e.optionList?new H(e).toString():new P(e).toString()).join(` `)}`,"commandLineUsage");export{o as f,G as p};