@visulima/string
Version:
Functions for manipulating strings.
2 lines (1 loc) • 4.93 kB
JavaScript
import{d as I,e as k,R as L}from"./packem_shared/constants-aPzGW29R.js";import{getStringWidth as w}from"./get-string-width.js";import{i as v}from"./packem_shared/lru-cache-BtMSrYQJ.js";const R=new v(100),N=new Intl.Segmenter("en",{granularity:"grapheme"}),O=/^[\u0000-\u007F]*$/,j=new Set(["0","22","23","24","27","28","29","39","49"]),y=new Set(["1","3","4","7","8","9"]),A=new Map([["22","1"],["23","3"],["24","4"],["27","7"],["28","8"],["29","9"]]),b=t=>t>="30"&&t<="37"||t>="90"&&t<="97"||t.startsWith("38;"),S=t=>t>="40"&&t<="47"||t>="100"&&t<="107"||t.startsWith("48;"),M=[{close:"\x1B[22m",open:"\x1B[1m"},{close:"\x1B[23m",open:"\x1B[3m"},{close:"\x1B[24m",open:"\x1B[4m"},{close:"\x1B[27m",open:"\x1B[7m"},{close:"\x1B[28m",open:"\x1B[8m"},{close:"\x1B[29m",open:"\x1B[9m"}],E=(t,h)=>{let l=-1;for(const u of h){const c=t.indexOf(u);c!==-1&&(l===-1||c<l)&&(l=c)}return l},F=(t,h)=>{if(!t.includes("\x1B"))return[{after:"",before:"",content:t,visibleLength:w(t,h.width)}];const l=R.get(t);if(l)return l;const u=[],c=[];let W=0,B;for(L.lastIndex=0;(B=L.exec(t))!==null;){const r=B[0];u.push(t.slice(W,B.index)),c.push(r),W=B.index+r.length}W<t.length&&u.push(t.slice(W));const m=[];for(const r of c){if(r==="\x1B[0m"||r==="\x1B]8;;\x07"){m.push(r);continue}if(r.startsWith("\x1B[")&&r.endsWith("m")){const x=r.slice(2,-1);if(j.has(x)){m.push(r);continue}}}const s=new Set,a=[];for(const[r,x]of u.entries()){if(r>0&&c[r-1]){const n=c[r-1];if(n==="\x1B[0m")s.clear();else if(n==="\x1B]8;;\x07"){for(const i of s)if(i.startsWith("\x1B]8;;")&&!i.endsWith("\x1B]8;;\x07")){s.delete(i);break}}else if(n.startsWith("\x1B[")&&n.endsWith("m")){const i=n.slice(2,-1);if(i==="39"){for(const e of s)if(e.startsWith("\x1B[")&&e.endsWith("m")){const f=e.slice(2,-1);if(b(f)){s.delete(e);break}}}else if(i==="49"){for(const e of s)if(e.startsWith("\x1B[")&&e.endsWith("m")){const f=e.slice(2,-1);if(S(f)){s.delete(e);break}}}else if(A.has(i)){const e=A.get(i);for(const f of s)if(f===`\x1B[${e}m`){s.delete(f);break}}else{if(b(i)){for(const e of s)if(e.startsWith("\x1B[")&&e.endsWith("m")){const f=e.slice(2,-1);if(b(f)){s.delete(e);break}}}else if(S(i)){for(const e of s)if(e.startsWith("\x1B[")&&e.endsWith("m")){const f=e.slice(2,-1);if(S(f)){s.delete(e);break}}}else if(y.has(i)){const e=`\x1B[${i}m`;s.has(e)&&s.delete(e)}s.add(n)}}else if(n.startsWith("\x1B]8;;")&&!n.endsWith("\x1B]8;;\x07")){for(const i of s)if(i.startsWith("\x1B]8;;")&&!i.endsWith("\x1B]8;;\x07")){s.delete(i);break}s.add(n)}}if(x==="")continue;let d="";if(s.size>0)if(m.includes("\x1B[0m"))d="\x1B[0m";else{const n=[];let i=!1;for(const o of s)if(o.startsWith("\x1B]8;;")&&!o.endsWith("\x1B]8;;\x07")){i=!0;break}i&&m.includes("\x1B]8;;\x07")&&n.push("\x1B]8;;\x07");let e=!1,f=!1;for(const o of s)if(o.startsWith("\x1B[")&&o.endsWith("m")){const g=o.slice(2,-1);b(g)?e=!0:S(g)&&(f=!0)}if(e&&f){const o=E(t,["\x1B[3","\x1B[9","\x1B[38;"]),g=E(t,["\x1B[4","\x1B[10","\x1B[48;"]);o>=0&&g>=0?o>g?n.push("\x1B[39m","\x1B[49m"):n.push("\x1B[49m","\x1B[39m"):n.push("\x1B[39m","\x1B[49m")}else e&&n.push("\x1B[39m"),f&&n.push("\x1B[49m");e&&f&&t.includes("\x1B[39m")&&t.includes("\x1B[49m")&&(t.indexOf("\x1B[39m")<t.indexOf("\x1B[49m")?(n.length-=2,n.push("\x1B[39m","\x1B[49m")):t.indexOf("\x1B[49m")<t.indexOf("\x1B[39m")&&(n.length-=2,n.push("\x1B[49m","\x1B[39m")));const _=M.filter(o=>s.has(o.open)).map(o=>({position:t.indexOf(o.open),style:o})).filter(o=>o.position>=0).toSorted((o,g)=>g.position-o.position);for(const o of _){const g=o;m.includes(g.style.close)&&n.push(g.style.close)}d=n.join("")}const p=[...s].join("");a.push({after:d,before:p,content:x,visibleLength:w(x,{fullWidth:1,wideWidth:1})})}return R.set(t,a),a},P=(t,h,l,u)=>{const c=[...u.segmenter.segment(t)],W=[];let B=0,m=!1;for(const s of c){const a=w(s.segment,u.width),r=B,x=B+a;if(!m)if(r<h){B=x;continue}else m=!0;if(r>=l||x>l)break;W.push(s.segment),B=x}return W.join("")},T=(t,h=0,l=Number.MAX_SAFE_INTEGER,u={})=>{const c={segmenter:N,...u};if(h>=l||t==="")return"";const W=w(t,c.width);if(h===0&&l>=W)return t;if(h<0||l<0)throw new RangeError("Negative indices aren't supported");if(!t.includes("\x1B"))return O.test(t)?t.slice(h,l):P(t,h,l,c);if(I.lastIndex=0,k.lastIndex=0,!I.test(t)&&!k.test(t))return t;const B=F(t,c);if(B.length===0)return"";let m=0;const s=[];for(const[x,d]of B.entries()){const p=m,n=m+d.visibleLength;if(n>h&&p<l){const i=Math.max(0,h-p),e=Math.min(d.visibleLength,l-p);s.push({end:e,index:x,segment:d,start:i})}m=n}if(s.length===0)return"";const a=[],r=s[0].segment;a.push(r.before);for(let x=0;x<s.length;x++){const{end:d,segment:p,start:n}=s[x];if(n===0&&d===p.visibleLength)a.push(p.content);else{const i=[...c.segmenter.segment(p.content)];a.push(i.slice(n,d).map(e=>e.segment).join(""))}if(x<s.length-1){const i=s[x+1].segment,e=p.after,f=i.before;e!==f&&(e||f)&&a.push(e,f)}}return a.push(s.at(-1).segment.after),a.join("")};export{T as slice};