@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 2.9 kB
JavaScript
const ANSI_COLORS=["black","red","green","yellow","blue","magenta","cyan","gray"],ANSI_BRIGHT_COLORS=["darkgray","lightred","lightgreen","lightyellow","lightblue","lightmagenta","lightcyan","white"];export const format=(e,t)=>{const s=[],a=new Map;function o(e){const t=a.get("text-decoration")??"";t.includes(e)||a.set("text-decoration",`${t} ${e}`)}function r(e){const t=a.get("text-decoration")?.replace(` ${e}`,"");t?a.set("text-decoration",t):a.delete("text-decoration")}function n(e){e&&(s.length&&"string"===s[s.length-1].type?s[s.length-1].value+=e:s.push({type:"string",value:e}))}let c=0;const l=/%([%_Oocsdfi])|\x1B\[([\d;]*)m/;for(let i=l.exec(e);null!==i;i=l.exec(e)){let l;n(i.input.substring(0,i.index));const g=i[1];switch(g){case"%":n("%"),l="";break;case"s":if(c<t.length){const{description:e}=t[c++];l=e??""}break;case"c":if(c<t.length){const e="style",a=t[c++].description??"";s.push({type:e,value:a}),l=""}break;case"o":case"O":if(c<t.length){const e="O"===g?"generic":"optimal",a=t[c++];s.push({type:e,value:a}),l=""}break;case"_":c<t.length&&(c++,l="");break;case"d":case"f":case"i":if(c<t.length){const{value:e}=t[c++];l="number"!=typeof e?NaN:e,"f"!==g&&(l=Math.floor(l))}break;case void 0:{const e=(i[2]||"0").split(";").map((e=>e?parseInt(e,10):0));for(;e.length;){const t=e.shift();switch(t){case 0:a.clear();break;case 1:a.set("font-weight","bold");break;case 2:a.set("font-weight","lighter");break;case 3:a.set("font-style","italic");break;case 4:o("underline");break;case 9:o("line-through");break;case 22:a.delete("font-weight");break;case 23:a.delete("font-style");break;case 24:r("underline");break;case 29:r("line-through");break;case 38:case 48:if(2===e.shift()){const s=e.shift()??0,o=e.shift()??0,r=e.shift()??0;a.set(38===t?"color":"background-color",`rgb(${s},${o},${r})`)}break;case 39:case 49:a.delete(39===t?"color":"background-color");break;case 53:o("overline");break;case 55:r("overline");break;default:{const e=ANSI_COLORS[t-30]??ANSI_BRIGHT_COLORS[t-90];if(void 0!==e)a.set("color",`var(--console-color-${e})`);else{const e=ANSI_COLORS[t-40]??ANSI_BRIGHT_COLORS[t-100];void 0!==e&&a.set("background-color",`var(--console-color-${e})`)}break}}}const t=[...a.entries()].map((([e,t])=>`${e}:${t.trimStart()}`)).join(";"),n="style";s.push({type:n,value:t}),l="";break}}void 0===l&&(n(i[0]),l=""),e=l+i.input.substring(i.index+i[0].length)}return n(e),{tokens:s,args:t.slice(c)}};export const updateStyle=(e,t)=>{const s=["background","border","color","font","line","margin","padding","text"],a=/url\([\'\"]?([^\)]*)/g;e.clear();const o=document.createElement("span");o.setAttribute("style",t);for(const t of o.style){if(!s.some((e=>t.startsWith(e)||t.startsWith(`-webkit-${e}`))))continue;const r=o.style.getPropertyValue(t);[...r.matchAll(a)].map((e=>e[1])).some((e=>!e.startsWith("data:")))||e.set(t,{value:r,priority:o.style.getPropertyPriority(t)})}};