colored-table 0.0.2 → 0.1.0-alpha.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/README.md CHANGED
@@ -12,6 +12,10 @@
12
12
 
13
13
  ```bash
14
14
  npm install --save colored-table
15
+
16
+ # 亦可 (两包以后将同步版本发布)
17
+
18
+ npm install --save @qqi/table
15
19
  ```
16
20
 
17
21
  ## 使用
@@ -35,7 +39,7 @@ table.addRow(['我在第三行第一列', '我在第三行第二列']);
35
39
  table();
36
40
  ```
37
41
 
38
- 效果:
42
+ 格式效果:
39
43
 
40
44
  | 第一列 | 第二列 |
41
45
  | :--------------- | :--------------- |
@@ -43,6 +47,14 @@ table();
43
47
  | 我在第二行第一列 | 我在第二行第二列 |
44
48
  | 我在第三行第一列 | 我在第三行第二列 |
45
49
 
50
+ 在浏览器效果:
51
+
52
+ [![浏览器表格](https://raw.githubusercontent.com/earthnutDev/qqi/main/media/浏览器表格.png)](https://github.com/earthnutDev/qqi/blob/main/media/浏览器表格.png)
53
+
54
+ 带表情的效果
55
+
56
+ [![浏览器表格](https://raw.githubusercontent.com/earthnutDev/qqi/main/media/浏览器表格-with-emoji.png)](https://github.com/earthnutDev/qqi/blob/main/media/浏览器表格-with-emoji.png)
57
+
46
58
  ### setFontSize
47
59
 
48
60
  除了 `addRow` 和 `setHeader` 两个方法,实例上还存在 `setFontSize` 方法。该方法将控制最终渲染的表格的字符大小。当然,这仅可在浏览器环境使用
package/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var e=require("a-js-tools"),t=require("a-type-of-js"),o=require("color-pen"),r=require("@color-pen/static");const n=e.createConstructor((function(e){return this.data=[],Object.setPrototypeOf(this,e),this}));function l(e,o){if(o.align&&(e.align=o.align),o.bgColor&&(e.bgColor=o.bgColor),o.color&&(e.color=o.color),o.italic&&(e.italic=o.italic),o.underline&&(e.underline=o.underline),o.border)if(t.isType(o.border,(e=>t.isString(e)))){const t=o.border;e.border={left:{color:void 0,style:t},right:{color:void 0,style:t},top:{color:void 0,style:t},bottom:{color:void 0,style:t}}}else if(t.isType(o.border,(e=>["color","style"].some((o=>!t.isUndefined(e[o])))))){const t=o.border,r=t.style,n=t.color||void 0;e.border=JSON.parse(JSON.stringify({left:{color:n,style:r},right:{color:n,style:r},top:{color:n,style:r},bottom:{color:n,style:r}},((e,t)=>""===t?void 0:t)))}else t.isType(o.border,(e=>["left","right","top","bottom"].some((o=>!t.isUndefined(e[o])))))&&(e.border=o.border)}class i{}function s(e,o){t.isArray(e)&&(e={data:e});const r=new i;l(r,e);const s=new n(r);return Object.setPrototypeOf(r,o),e.data?.forEach((e=>{e&&s.data.push(function(e,o){if(t.isUndefined(e))return;t.isType(e,(e=>!t.isPlainObject(e)))&&(e={content:e});const r={content:e.content};return l(r,e),Object.setPrototypeOf(r,o),r}(e,r))})),s}class c extends i{border={left:{style:"bold",color:void 0},right:{style:"bold",color:void 0},top:{style:"bold",color:void 0},bottom:{style:"bold",color:void 0}}}function a(e){const t=new c;return Object.setPrototypeOf(t,e),t}const d=!e.isNode(),u={emojiLength:2.08,emojiRegex:/[\u{1F600}-\u{1F64F}\u{1F300}-\u{1F5FF}\u{1F680}-\u{1F6FF}\u{1F900}-\u{1F9FF}]/gu,chineseLength:1.8,chineseRegex:/[\u4E00-\u9FFF]/g};const f=new class{align="left";color="";bgColor="";bold=!1;underline=!1;italic=!1;border={left:{color:void 0,style:"simple"},right:{color:void 0,style:"simple"},top:{color:void 0,style:"simple"},bottom:{color:void 0,style:"simple"}}},h=e.createConstructor((function(e){return this.body=[],this.header=void 0,Object.setPrototypeOf(this,e),this}));class b{}function g(e){let r=o.pen;return t.isType(e)&&(e.bold&&(r=r.bold),t.isBusinessEmptyString(e.bgColor)||t.isUndefined(e.bgColor)||(r=r.bgColor(e.bgColor)),t.isBusinessEmptyString(e.color)||t.isUndefined(e.color)||(r=r.color(e.color)),e.italic&&(r=r.italic),e.underline&&(r=r.underline)),r}const y={fine:{lt:"┌",ct:"┬",rt:"┐",lc:"├",c:"┼",rc:"┤",lb:"└",cb:"┴",rb:"┘",l:"─",v:"│"}};function p(e){let t=0;return[...e].forEach((e=>{u.emojiRegex.lastIndex=0,u.chineseRegex.lastIndex=0,e.match(u.emojiRegex)?t+=u.emojiLength:e.match(u.chineseRegex)?t+=u.chineseLength:t++})),Math.ceil(t)}function m(e,n){const l=function(e){const t=[];return e.header&&e.header.data.forEach(((e,r)=>{const n=e?.content?.toString()??"";t[r]=Math.max(t[r]??0,d?p(n):o.strInTerminalLength(n))})),e.body&&e.body.forEach((e=>{e.data.forEach(((e,r)=>{const n=e?.content?.toString()??"";t[r]=Math.max(t[r]??0,d?p(n):o.strInTerminalLength(n))}))})),t}(e);let i=!1,s="";e.header&&(i=!0,s+=function(e,n){let l="";const i=n.length-1,{fine:s}=y,c=g(e);return n.forEach(((e,t)=>{0===t&&(l+=s.lt),l+=r.terminalResetStyle,l+=s.l.repeat(e+2),t===i?(l+=r.terminalResetStyle,l+=s.rt):(l+=r.terminalResetStyle,l+=s.ct)})),l+="\n",n.forEach(((n,a)=>{l+=r.terminalResetStyle,l+=s.v;const d=e.data[a];if(t.isUndefined(d)||t.isNull(d))l+=r.terminalResetStyle,l+=c(" ".repeat(n+2));else{const e=g(d);let t=d.content?.toString()??"";t=t.replace(/[\n]/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t"),l+=r.terminalResetStyle,l+=e`\u2002${o.cutoffStringWithChar(t,n)}\u2002`}a===i&&(l+=s.v)})),l+="\n",l}(e.header,l)),s+=function(e,n,l){let i="";const s=n.length-1,{fine:c}=y;return n.forEach(((e,t)=>{0===t&&(i+=r.terminalResetStyle,i+=l?c.lc:c.lt),i+=r.terminalResetStyle,i+=c.l.repeat(e+2),t===s?(i+=r.terminalResetStyle,i+=l?c.rc:c.rt):(i+=r.terminalResetStyle,i+=l?c.c:c.ct)})),i+="\n",e.forEach(((l,a)=>{const d=g(l);n.forEach(((e,n)=>{i+=r.terminalResetStyle,i+=c.v;const a=l.data[n];if(t.isUndefined(a)||t.isNull(a))i+=d`${" ".repeat(e+2)}`;else{const t=g(a);let n=a.content?.toString()||"";n=n.replace(/[\n]/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t"),i+=r.terminalResetStyle,i+=t`\u2002${o.cutoffStringWithChar(n,e)}\u2002`}n===s&&(i+=r.terminalResetStyle,i+=c.v)})),i+="\n";const u=e.length-1===a;n.forEach(((e,t)=>{0===t&&(i+=r.terminalResetStyle,i+=u?c.lb:c.lc),i+=r.terminalResetStyle,i+=c.l.repeat(e+2),t===s?(i+=r.terminalResetStyle,i+=u?c.rb:c.rc):(i+=r.terminalResetStyle,i+=u?c.cb:c.c)})),i+="\n"})),i}(e.body,l,i);const c=o.colorText(s);if(d){const e=c[0].split("%c"),t=c.map(((t,r)=>{if(r>0){const l=[...e[r]].reduce(((e,t)=>o.strInTerminalLength(t)>1?(u.emojiRegex.lastIndex=0,t.match(u.emojiRegex)?e-(u.emojiLength-2):e+(2-u.chineseLength)):e),0);return t+"font-family: Consolas,Monaco,Courier,'Courier New','等宽字体',monospace; font-size:"+n+"px;word-spacing:normal;padding:0;margin:0;padding-right:"+l*n+"px"}return t}));console.log(...t)}else console.log(...c)}const S=e.createConstructor((function(e){const[o,r]=function(e){t.isUndefined(e)&&(e={}),t.isArray(e)&&(e={body:e});const o=new b;l(o,e),Object.setPrototypeOf(o,f);const r=new h(o);return e.header&&(r.header=s(e.header,a(o))),e.body?.forEach((e=>r.body.push(s(e,o)))),[r,o]}(e);let n=12;const i=()=>m(o,n);return Object.setPrototypeOf(i,this),Object.defineProperties(i,{addRow:{value:e=>(o.body.push(s(e,r)),i),configurable:!1,writable:!1,enumerable:!1},setHeader:{value:e=>(o.header=s(e,a(r)),i),configurable:!1,writable:!1,enumerable:!1},setFontSize:{value:e=>(n=e,i)}}),i}));exports.ColoredTable=S,exports.ColoredTableGlobalData=f,exports.Table=S;
1
+ "use strict";var e=require("a-js-tools"),t=require("a-type-of-js"),o=require("color-pen"),r=require("@color-pen/static");const n=e.createConstructor(function(e){return this.data=[],Object.setPrototypeOf(this,e),this});function l(e,o){if(o.align&&(e.align=o.align),o.bgColor&&(e.bgColor=o.bgColor),o.color&&(e.color=o.color),o.italic&&(e.italic=o.italic),o.underline&&(e.underline=o.underline),o.border)if(t.isType(o.border,e=>t.isString(e))){const t=o.border;e.border={left:{color:void 0,style:t},right:{color:void 0,style:t},top:{color:void 0,style:t},bottom:{color:void 0,style:t}}}else if(t.isType(o.border,e=>["color","style"].some(o=>!t.isUndefined(e[o])))){const t=o.border,r=t.style,n=t.color||void 0;e.border=JSON.parse(JSON.stringify({left:{color:n,style:r},right:{color:n,style:r},top:{color:n,style:r},bottom:{color:n,style:r}},(e,t)=>""===t?void 0:t))}else t.isType(o.border,e=>["left","right","top","bottom"].some(o=>!t.isUndefined(e[o])))&&(e.border=o.border)}class i{}function s(e,o){t.isArray(e)&&(e={data:e});const r=new i;l(r,e);const s=new n(r);return Object.setPrototypeOf(r,o),e.data?.forEach(e=>{e&&s.data.push(function(e,o){if(t.isUndefined(e))return;t.isType(e,e=>!t.isPlainObject(e))&&(e={content:e});const r={content:e.content};return l(r,e),Object.setPrototypeOf(r,o),r}(e,r))}),s}class c extends i{border={left:{style:"bold",color:void 0},right:{style:"bold",color:void 0},top:{style:"bold",color:void 0},bottom:{style:"bold",color:void 0}}}function a(e){const t=new c;return Object.setPrototypeOf(t,e),t}const d=!e.isNode(),u={emojiLength:2.08,emojiRegex:/[\u{1F600}-\u{1F64F}\u{1F300}-\u{1F5FF}\u{1F680}-\u{1F6FF}\u{1F900}-\u{1F9FF}]/gu,chineseLength:1.8,chineseRegex:/[\u4E00-\u9FFF]/g};const f=new class{align="left";color="";bgColor="";bold=!1;underline=!1;italic=!1;border={left:{color:void 0,style:"simple"},right:{color:void 0,style:"simple"},top:{color:void 0,style:"simple"},bottom:{color:void 0,style:"simple"}}},h=e.createConstructor(function(e){return this.body=[],this.header=void 0,Object.setPrototypeOf(this,e),this});class b{}function g(e){let r=o.pen;return t.isType(e)&&(e.bold&&(r=r.bold),t.isBusinessEmptyString(e.bgColor)||t.isUndefined(e.bgColor)||(r=r.bgColor(e.bgColor)),t.isBusinessEmptyString(e.color)||t.isUndefined(e.color)||(r=r.color(e.color)),e.italic&&(r=r.italic),e.underline&&(r=r.underline)),r}const y={fine:{lt:"┌",ct:"┬",rt:"┐",lc:"├",c:"┼",rc:"┤",lb:"└",cb:"┴",rb:"┘",l:"─",v:"│"}};function p(e){let t=0;return[...e].forEach(e=>{u.emojiRegex.lastIndex=0,u.chineseRegex.lastIndex=0,e.match(u.emojiRegex)?t+=u.emojiLength:e.match(u.chineseRegex)?t+=u.chineseLength:t++}),Math.ceil(t)}function m(e,n){const l=function(e){const t=[];return e.header&&e.header.data.forEach((e,r)=>{const n=e?.content?.toString()??"";t[r]=Math.max(t[r]??0,d?p(n):o.strInTerminalLength(n))}),e.body&&e.body.forEach(e=>{e.data.forEach((e,r)=>{const n=e?.content?.toString()??"";t[r]=Math.max(t[r]??0,d?p(n):o.strInTerminalLength(n))})}),t}(e);let i=!1,s="";e.header&&(i=!0,s+=function(e,n){let l="";const i=n.length-1,{fine:s}=y,c=g(e);return n.forEach((e,t)=>{0===t&&(l+=s.lt),l+=r.terminalResetStyle,l+=s.l.repeat(e+2),t===i?(l+=r.terminalResetStyle,l+=s.rt):(l+=r.terminalResetStyle,l+=s.ct)}),l+="\n",n.forEach((n,a)=>{l+=r.terminalResetStyle,l+=s.v;const d=e.data[a];if(t.isUndefined(d)||t.isNull(d))l+=r.terminalResetStyle,l+=c(" ".repeat(n+2));else{const e=g(d);let t=d.content?.toString()??"";t=t.replace(/[\n]/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t"),l+=r.terminalResetStyle,l+=e`\u2002${o.cutoffStringWithChar(t,n)}\u2002`}a===i&&(l+=s.v)}),l+="\n",l}(e.header,l)),s+=function(e,n,l){let i="";const s=n.length-1,{fine:c}=y;return n.forEach((e,t)=>{0===t&&(i+=r.terminalResetStyle,i+=l?c.lc:c.lt),i+=r.terminalResetStyle,i+=c.l.repeat(e+2),t===s?(i+=r.terminalResetStyle,i+=l?c.rc:c.rt):(i+=r.terminalResetStyle,i+=l?c.c:c.ct)}),i+="\n",e.forEach((l,a)=>{const d=g(l);n.forEach((e,n)=>{i+=r.terminalResetStyle,i+=c.v;const a=l.data[n];if(t.isUndefined(a)||t.isNull(a))i+=d`${" ".repeat(e+2)}`;else{const t=g(a);let n=a.content?.toString()||"";n=n.replace(/[\n]/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t"),i+=r.terminalResetStyle,i+=t`\u2002${o.cutoffStringWithChar(n,e)}\u2002`}n===s&&(i+=r.terminalResetStyle,i+=c.v)}),i+="\n";const u=e.length-1===a;n.forEach((e,t)=>{0===t&&(i+=r.terminalResetStyle,i+=u?c.lb:c.lc),i+=r.terminalResetStyle,i+=c.l.repeat(e+2),t===s?(i+=r.terminalResetStyle,i+=u?c.rb:c.rc):(i+=r.terminalResetStyle,i+=u?c.cb:c.c)}),i+="\n"}),i}(e.body,l,i);const c=o.colorText(s);if(d){const e=c[0].split("%c"),t=c.map((t,r)=>{if(r>0){const l=[...e[r]].reduce((e,t)=>o.strInTerminalLength(t)>1?(u.emojiRegex.lastIndex=0,t.match(u.emojiRegex)?e-(u.emojiLength-2):e+(2-u.chineseLength)):e,0);return t+"font-family: Consolas,Monaco,Courier,'Courier New','等宽字体',monospace; font-size:"+n+"px;word-spacing:normal;padding:0;margin:0;padding-right:"+l*n+"px"}return t});console.log(...t)}else console.log(...c)}const S=e.createConstructor(function(e){const[o,r]=function(e){t.isUndefined(e)&&(e={}),t.isArray(e)&&(e={body:e});const o=new b;l(o,e),Object.setPrototypeOf(o,f);const r=new h(o);return e.header&&(r.header=s(e.header,a(o))),e.body?.forEach(e=>r.body.push(s(e,o))),[r,o]}(e);let n=12;const i=()=>m(o,n);return Object.setPrototypeOf(i,this),Object.defineProperties(i,{addRow:{value:e=>(o.body.push(s(e,r)),i),configurable:!1,writable:!1,enumerable:!1},setHeader:{value:e=>(o.header=s(e,a(r)),i),configurable:!1,writable:!1,enumerable:!1},setFontSize:{value:e=>(n=e,i)}}),i});exports.ColoredTable=S,exports.ColoredTableGlobalData=f,exports.Table=S;
package/index.mjs CHANGED
@@ -1 +1 @@
1
- import{createConstructor as o,isNode as e}from"a-js-tools";import{isType as t,isString as r,isUndefined as n,isPlainObject as l,isArray as c,isBusinessEmptyString as i,isNull as s}from"a-type-of-js";import{pen as a,cutoffStringWithChar as d,strInTerminalLength as b,colorText as u}from"color-pen";import{terminalResetStyle as f}from"@color-pen/static";const h=o((function(o){return this.data=[],Object.setPrototypeOf(this,o),this}));function g(o,e){if(e.align&&(o.align=e.align),e.bgColor&&(o.bgColor=e.bgColor),e.color&&(o.color=e.color),e.italic&&(o.italic=e.italic),e.underline&&(o.underline=e.underline),e.border)if(t(e.border,(o=>r(o)))){const t=e.border;o.border={left:{color:void 0,style:t},right:{color:void 0,style:t},top:{color:void 0,style:t},bottom:{color:void 0,style:t}}}else if(t(e.border,(o=>["color","style"].some((e=>!n(o[e])))))){const t=e.border,r=t.style,n=t.color||void 0;o.border=JSON.parse(JSON.stringify({left:{color:n,style:r},right:{color:n,style:r},top:{color:n,style:r},bottom:{color:n,style:r}},((o,e)=>""===e?void 0:e)))}else t(e.border,(o=>["left","right","top","bottom"].some((e=>!n(o[e])))))&&(o.border=e.border)}class p{}function m(o,e){c(o)&&(o={data:o});const r=new p;g(r,o);const i=new h(r);return Object.setPrototypeOf(r,e),o.data?.forEach((o=>{o&&i.data.push(function(o,e){if(n(o))return;t(o,(o=>!l(o)))&&(o={content:o});const r={content:o.content};return g(r,o),Object.setPrototypeOf(r,e),r}(o,r))})),i}class y extends p{border={left:{style:"bold",color:void 0},right:{style:"bold",color:void 0},top:{style:"bold",color:void 0},bottom:{style:"bold",color:void 0}}}function v(o){const e=new y;return Object.setPrototypeOf(e,o),e}const F=!e(),j={emojiLength:2.08,emojiRegex:/[\u{1F600}-\u{1F64F}\u{1F300}-\u{1F5FF}\u{1F680}-\u{1F6FF}\u{1F900}-\u{1F9FF}]/gu,chineseLength:1.8,chineseRegex:/[\u4E00-\u9FFF]/g};const x=new class{align="left";color="";bgColor="";bold=!1;underline=!1;italic=!1;border={left:{color:void 0,style:"simple"},right:{color:void 0,style:"simple"},top:{color:void 0,style:"simple"},bottom:{color:void 0,style:"simple"}}},O=o((function(o){return this.body=[],this.header=void 0,Object.setPrototypeOf(this,o),this}));class C{}function E(o){let e=a;return t(o)&&(o.bold&&(e=e.bold),i(o.bgColor)||n(o.bgColor)||(e=e.bgColor(o.bgColor)),i(o.color)||n(o.color)||(e=e.color(o.color)),o.italic&&(e=e.italic),o.underline&&(e=e.underline)),e}const w={fine:{lt:"┌",ct:"┬",rt:"┐",lc:"├",c:"┼",rc:"┤",lb:"└",cb:"┴",rb:"┘",l:"─",v:"│"}};function R(o){let e=0;return[...o].forEach((o=>{j.emojiRegex.lastIndex=0,j.chineseRegex.lastIndex=0,o.match(j.emojiRegex)?e+=j.emojiLength:o.match(j.chineseRegex)?e+=j.chineseLength:e++})),Math.ceil(e)}function P(o,e){const t=function(o){const e=[];return o.header&&o.header.data.forEach(((o,t)=>{const r=o?.content?.toString()??"";e[t]=Math.max(e[t]??0,F?R(r):b(r))})),o.body&&o.body.forEach((o=>{o.data.forEach(((o,t)=>{const r=o?.content?.toString()??"";e[t]=Math.max(e[t]??0,F?R(r):b(r))}))})),e}(o);let r=!1,l="";o.header&&(r=!0,l+=function(o,e){let t="";const r=e.length-1,{fine:l}=w,c=E(o);return e.forEach(((o,e)=>{0===e&&(t+=l.lt),t+=f,t+=l.l.repeat(o+2),e===r?(t+=f,t+=l.rt):(t+=f,t+=l.ct)})),t+="\n",e.forEach(((e,i)=>{t+=f,t+=l.v;const a=o.data[i];if(n(a)||s(a))t+=f,t+=c(" ".repeat(e+2));else{const o=E(a);let r=a.content?.toString()??"";r=r.replace(/[\n]/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t"),t+=f,t+=o`\u2002${d(r,e)}\u2002`}i===r&&(t+=l.v)})),t+="\n",t}(o.header,t)),l+=function(o,e,t){let r="";const l=e.length-1,{fine:c}=w;return e.forEach(((o,e)=>{0===e&&(r+=f,r+=t?c.lc:c.lt),r+=f,r+=c.l.repeat(o+2),e===l?(r+=f,r+=t?c.rc:c.rt):(r+=f,r+=t?c.c:c.ct)})),r+="\n",o.forEach(((t,i)=>{const a=E(t);e.forEach(((o,e)=>{r+=f,r+=c.v;const i=t.data[e];if(n(i)||s(i))r+=a`${" ".repeat(o+2)}`;else{const e=E(i);let t=i.content?.toString()||"";t=t.replace(/[\n]/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t"),r+=f,r+=e`\u2002${d(t,o)}\u2002`}e===l&&(r+=f,r+=c.v)})),r+="\n";const b=o.length-1===i;e.forEach(((o,e)=>{0===e&&(r+=f,r+=b?c.lb:c.lc),r+=f,r+=c.l.repeat(o+2),e===l?(r+=f,r+=b?c.rb:c.rc):(r+=f,r+=b?c.cb:c.c)})),r+="\n"})),r}(o.body,t,r);const c=u(l);if(F){const o=c[0].split("%c"),t=c.map(((t,r)=>{if(r>0){const n=[...o[r]].reduce(((o,e)=>b(e)>1?(j.emojiRegex.lastIndex=0,e.match(j.emojiRegex)?o-(j.emojiLength-2):o+(2-j.chineseLength)):o),0);return t+"font-family: Consolas,Monaco,Courier,'Courier New','等宽字体',monospace; font-size:"+e+"px;word-spacing:normal;padding:0;margin:0;padding-right:"+n*e+"px"}return t}));console.log(...t)}else console.log(...c)}const S=o((function(o){const[e,t]=function(o){n(o)&&(o={}),c(o)&&(o={body:o});const e=new C;g(e,o),Object.setPrototypeOf(e,x);const t=new O(e);return o.header&&(t.header=m(o.header,v(e))),o.body?.forEach((o=>t.body.push(m(o,e)))),[t,e]}(o);let r=12;const l=()=>P(e,r);return Object.setPrototypeOf(l,this),Object.defineProperties(l,{addRow:{value:o=>(e.body.push(m(o,t)),l),configurable:!1,writable:!1,enumerable:!1},setHeader:{value:o=>(e.header=m(o,v(t)),l),configurable:!1,writable:!1,enumerable:!1},setFontSize:{value:o=>(r=o,l)}}),l}));export{S as ColoredTable,x as ColoredTableGlobalData,S as Table};
1
+ import{createConstructor as o,isNode as e}from"a-js-tools";import{isType as t,isString as r,isUndefined as n,isPlainObject as l,isArray as c,isBusinessEmptyString as i,isNull as s}from"a-type-of-js";import{pen as a,cutoffStringWithChar as d,strInTerminalLength as b,colorText as u}from"color-pen";import{terminalResetStyle as f}from"@color-pen/static";const h=o(function(o){return this.data=[],Object.setPrototypeOf(this,o),this});function g(o,e){if(e.align&&(o.align=e.align),e.bgColor&&(o.bgColor=e.bgColor),e.color&&(o.color=e.color),e.italic&&(o.italic=e.italic),e.underline&&(o.underline=e.underline),e.border)if(t(e.border,o=>r(o))){const t=e.border;o.border={left:{color:void 0,style:t},right:{color:void 0,style:t},top:{color:void 0,style:t},bottom:{color:void 0,style:t}}}else if(t(e.border,o=>["color","style"].some(e=>!n(o[e])))){const t=e.border,r=t.style,n=t.color||void 0;o.border=JSON.parse(JSON.stringify({left:{color:n,style:r},right:{color:n,style:r},top:{color:n,style:r},bottom:{color:n,style:r}},(o,e)=>""===e?void 0:e))}else t(e.border,o=>["left","right","top","bottom"].some(e=>!n(o[e])))&&(o.border=e.border)}class p{}function m(o,e){c(o)&&(o={data:o});const r=new p;g(r,o);const i=new h(r);return Object.setPrototypeOf(r,e),o.data?.forEach(o=>{o&&i.data.push(function(o,e){if(n(o))return;t(o,o=>!l(o))&&(o={content:o});const r={content:o.content};return g(r,o),Object.setPrototypeOf(r,e),r}(o,r))}),i}class y extends p{border={left:{style:"bold",color:void 0},right:{style:"bold",color:void 0},top:{style:"bold",color:void 0},bottom:{style:"bold",color:void 0}}}function v(o){const e=new y;return Object.setPrototypeOf(e,o),e}const F=!e(),j={emojiLength:2.08,emojiRegex:/[\u{1F600}-\u{1F64F}\u{1F300}-\u{1F5FF}\u{1F680}-\u{1F6FF}\u{1F900}-\u{1F9FF}]/gu,chineseLength:1.8,chineseRegex:/[\u4E00-\u9FFF]/g};const x=new class{align="left";color="";bgColor="";bold=!1;underline=!1;italic=!1;border={left:{color:void 0,style:"simple"},right:{color:void 0,style:"simple"},top:{color:void 0,style:"simple"},bottom:{color:void 0,style:"simple"}}},O=o(function(o){return this.body=[],this.header=void 0,Object.setPrototypeOf(this,o),this});class C{}function E(o){let e=a;return t(o)&&(o.bold&&(e=e.bold),i(o.bgColor)||n(o.bgColor)||(e=e.bgColor(o.bgColor)),i(o.color)||n(o.color)||(e=e.color(o.color)),o.italic&&(e=e.italic),o.underline&&(e=e.underline)),e}const w={fine:{lt:"┌",ct:"┬",rt:"┐",lc:"├",c:"┼",rc:"┤",lb:"└",cb:"┴",rb:"┘",l:"─",v:"│"}};function R(o){let e=0;return[...o].forEach(o=>{j.emojiRegex.lastIndex=0,j.chineseRegex.lastIndex=0,o.match(j.emojiRegex)?e+=j.emojiLength:o.match(j.chineseRegex)?e+=j.chineseLength:e++}),Math.ceil(e)}function P(o,e){const t=function(o){const e=[];return o.header&&o.header.data.forEach((o,t)=>{const r=o?.content?.toString()??"";e[t]=Math.max(e[t]??0,F?R(r):b(r))}),o.body&&o.body.forEach(o=>{o.data.forEach((o,t)=>{const r=o?.content?.toString()??"";e[t]=Math.max(e[t]??0,F?R(r):b(r))})}),e}(o);let r=!1,l="";o.header&&(r=!0,l+=function(o,e){let t="";const r=e.length-1,{fine:l}=w,c=E(o);return e.forEach((o,e)=>{0===e&&(t+=l.lt),t+=f,t+=l.l.repeat(o+2),e===r?(t+=f,t+=l.rt):(t+=f,t+=l.ct)}),t+="\n",e.forEach((e,i)=>{t+=f,t+=l.v;const a=o.data[i];if(n(a)||s(a))t+=f,t+=c(" ".repeat(e+2));else{const o=E(a);let r=a.content?.toString()??"";r=r.replace(/[\n]/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t"),t+=f,t+=o`\u2002${d(r,e)}\u2002`}i===r&&(t+=l.v)}),t+="\n",t}(o.header,t)),l+=function(o,e,t){let r="";const l=e.length-1,{fine:c}=w;return e.forEach((o,e)=>{0===e&&(r+=f,r+=t?c.lc:c.lt),r+=f,r+=c.l.repeat(o+2),e===l?(r+=f,r+=t?c.rc:c.rt):(r+=f,r+=t?c.c:c.ct)}),r+="\n",o.forEach((t,i)=>{const a=E(t);e.forEach((o,e)=>{r+=f,r+=c.v;const i=t.data[e];if(n(i)||s(i))r+=a`${" ".repeat(o+2)}`;else{const e=E(i);let t=i.content?.toString()||"";t=t.replace(/[\n]/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t"),r+=f,r+=e`\u2002${d(t,o)}\u2002`}e===l&&(r+=f,r+=c.v)}),r+="\n";const b=o.length-1===i;e.forEach((o,e)=>{0===e&&(r+=f,r+=b?c.lb:c.lc),r+=f,r+=c.l.repeat(o+2),e===l?(r+=f,r+=b?c.rb:c.rc):(r+=f,r+=b?c.cb:c.c)}),r+="\n"}),r}(o.body,t,r);const c=u(l);if(F){const o=c[0].split("%c"),t=c.map((t,r)=>{if(r>0){const n=[...o[r]].reduce((o,e)=>b(e)>1?(j.emojiRegex.lastIndex=0,e.match(j.emojiRegex)?o-(j.emojiLength-2):o+(2-j.chineseLength)):o,0);return t+"font-family: Consolas,Monaco,Courier,'Courier New','等宽字体',monospace; font-size:"+e+"px;word-spacing:normal;padding:0;margin:0;padding-right:"+n*e+"px"}return t});console.log(...t)}else console.log(...c)}const S=o(function(o){const[e,t]=function(o){n(o)&&(o={}),c(o)&&(o={body:o});const e=new C;g(e,o),Object.setPrototypeOf(e,x);const t=new O(e);return o.header&&(t.header=m(o.header,v(e))),o.body?.forEach(o=>t.body.push(m(o,e))),[t,e]}(o);let r=12;const l=()=>P(e,r);return Object.setPrototypeOf(l,this),Object.defineProperties(l,{addRow:{value:o=>(e.body.push(m(o,t)),l),configurable:!1,writable:!1,enumerable:!1},setHeader:{value:o=>(e.header=m(o,v(t)),l),configurable:!1,writable:!1,enumerable:!1},setFontSize:{value:o=>(r=o,l)}}),l});export{S as ColoredTable,x as ColoredTableGlobalData,S as Table};
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "colored-table",
3
3
  "type": "module",
4
- "version": "0.0.2",
4
+ "version": "0.1.0-alpha.0",
5
5
  "description": "一个简单的在控制台及控制台构建彩色文本表格的工具 🔧",
6
6
  "license": "MIT",
7
7
  "dependencies": {
8
8
  "@color-pen/static": "^1.0.0",
9
- "a-js-tools": "^1.0.0",
10
- "a-type-of-js": "^1.0.1",
11
- "color-pen": "^2.0.10"
9
+ "a-js-tools": "^1.0.8",
10
+ "a-type-of-js": "^1.0.5",
11
+ "color-pen": "^2.0.12"
12
12
  },
13
13
  "author": {
14
14
  "name": "earthnut",
@@ -34,7 +34,10 @@
34
34
  }
35
35
  },
36
36
  "keywords": [
37
- "colored-table"
37
+ "colored-table",
38
+ "qqi",
39
+ "@qqi",
40
+ "@qqi/table"
38
41
  ],
39
42
  "homepage": "https://earthnut.dev/colored-table",
40
43
  "bugs": {