@nick-skriabin/glyph 0.1.47 → 0.1.48

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/dist/index.js CHANGED
@@ -4,7 +4,7 @@ import te,{createContext,forwardRef,useState,useContext,useRef,useEffect,useLayo
4
4
  `),M=ce(T,l,G),b=0,y=0;if(K){let x=o.position,V=0,z=x,O=0;for(let J=0;J<T.length;J++){let B=T[J].length;if(x<=O+B){V=J,z=x-O;break}O+=B+1;}let W=0;for(let J=0;J<V;J++)W+=ce([T[J]],l,G).length;let Y=ce([T[V]],l,G),X=0,_=0;for(let J=0;J<Y.length;J++){let B=Y[J];if(z<=X+B.length){_=J;break}X+=B.length;}b=W+_,y=ue(T[V].slice(X,X+(z-X)));}let C=Math.max(0,b-u+1);for(let x=0;x<u;x++){let V=C+x;if(V>=M.length)break;let z=M[V],O=0;for(let W of z){if(O>=l)break;let Y=ue(W);Y>0&&ie(t,n,i+O,c+x,W,P,a.bg,a.bold,j,a.italic,a.underline),O+=Y;}}if(K){let x=b-C;if(x>=0&&x<u){let V=Math.min(y,l-1),z=i+V,O=c+x;if(Xe(z,O,n)&&z<i+l)if(s)E={cursorPosition:{x:z,y:O,bg:a.bg}};else {let W=t.get(z,O),Y=W?.ch&&W.ch!==" "?W.ch:"\u258C",X=a.bg??"black",_=a.color??"white";t.setChar(z,O,Y,X,_,W?.bold,W?.dim,W?.italic,false);}}}}else {let G=0;for(let T of S){if(G>=l)break;let M=ue(T);M>0&&ie(t,n,i+G,c,T,P,a.bg,a.bold,j,a.italic,a.underline),G+=M;}if(K){let T=Math.min(o.position,l-1),M=i+T;if(Xe(M,c,n)&&M<i+l)if(s)E={cursorPosition:{x:M,y:c,bg:a.bg}};else {let b=t.get(M,c),y=b?.ch&&b.ch!==" "?b.ch:"\u258C",C=a.bg??"black",x=a.color??"white";t.setChar(M,c,y,C,x,b?.bold,b?.dim,b?.italic,false);}}}return E}var er="\x1B",me=`${er}[`;function tr(e,t){return `${me}${t+1};${e+1}H`}function nr(e){let t=`${me}0m`;return e.bold&&(t+=`${me}1m`),e.dim&&(t+=`${me}2m`),e.italic&&(t+=`${me}3m`),e.underline&&(t+=`${me}4m`),e.fg!=null&&(t+=Ot(e.fg)),e.bg!=null&&(t+=Dt(e.bg)),t}function Zt(e,t,n){let r="",o=-1,s=-1,i="";for(let c=0;c<t.height;c++)for(let l=0;l<t.width;l++){let u=t.get(l,c);if(!n){let m=e.get(l,c);if(m&&t.cellsEqual(u,m))continue}(s!==c||o!==l)&&(r+=tr(l,c));let f=nr(u);f!==i&&(r+=f,i=f),r+=u.ch,o=l+1,s=c;}return r.length>0&&(r+=`${me}0m`),r}var ir={row:FlexDirection.Row,column:FlexDirection.Column},lr={"flex-start":Justify.FlexStart,center:Justify.Center,"flex-end":Justify.FlexEnd,"space-between":Justify.SpaceBetween,"space-around":Justify.SpaceAround},cr={"flex-start":Align.FlexStart,center:Align.Center,"flex-end":Align.FlexEnd,stretch:Align.Stretch};function tn(e,t,n){n!==void 0&&(typeof n=="string"&&n.endsWith("%"),t(n));}function ae(e,t,n){n!==void 0&&(typeof n=="string"&&n.endsWith("%")?e.setPositionPercent(t,parseFloat(n)):e.setPosition(t,n));}function ur(e,t,n){tn(e,o=>e.setWidth(o),t.width),tn(e,o=>e.setHeight(o),t.height),t.minWidth!==void 0&&e.setMinWidth(t.minWidth),t.minHeight!==void 0&&e.setMinHeight(t.minHeight),t.maxWidth!==void 0&&e.setMaxWidth(t.maxWidth),t.maxHeight!==void 0&&e.setMaxHeight(t.maxHeight),t.padding!==void 0&&e.setPadding(Edge.All,t.padding),t.paddingX!==void 0&&e.setPadding(Edge.Horizontal,t.paddingX),t.paddingY!==void 0&&e.setPadding(Edge.Vertical,t.paddingY),t.paddingTop!==void 0&&e.setPadding(Edge.Top,t.paddingTop),t.paddingRight!==void 0&&e.setPadding(Edge.Right,t.paddingRight),t.paddingBottom!==void 0&&e.setPadding(Edge.Bottom,t.paddingBottom),t.paddingLeft!==void 0&&e.setPadding(Edge.Left,t.paddingLeft);let r=t.border!=null&&t.border!=="none";e.setBorder(Edge.All,r?1:0),t.flexDirection&&e.setFlexDirection(ir[t.flexDirection]??FlexDirection.Column),t.flexWrap&&e.setFlexWrap(t.flexWrap==="wrap"?Wrap.Wrap:Wrap.NoWrap),t.justifyContent&&e.setJustifyContent(lr[t.justifyContent]??Justify.FlexStart),t.alignItems&&e.setAlignItems(cr[t.alignItems]??Align.Stretch),t.flexGrow!==void 0&&e.setFlexGrow(t.flexGrow),t.flexShrink!==void 0&&e.setFlexShrink(t.flexShrink),t.gap!==void 0&&e.setGap(Gutter.All,t.gap),t.position==="absolute"?e.setPositionType(PositionType.Absolute):e.setPositionType(PositionType.Relative),t.inset!==void 0&&(ae(e,Edge.Top,t.inset),ae(e,Edge.Right,t.inset),ae(e,Edge.Bottom,t.inset),ae(e,Edge.Left,t.inset)),ae(e,Edge.Top,t.top),ae(e,Edge.Right,t.right),ae(e,Edge.Bottom,t.bottom),ae(e,Edge.Left,t.left),t.clip&&e.setOverflow(Overflow.Hidden);}function rn(e){let t=nn.Node.create();if(e.yogaNode=t,ur(t,e.style,e.type),e.type==="text"||e.type==="input")t.setMeasureFunc((n,r,o,s)=>{let i;return e.type==="input"?(i=e.props.value??e.props.defaultValue??e.props.placeholder??"",i.length===0&&(i=" ")):i=on(e),zt(i,n,r,e.style.wrap??"wrap")});else for(let n=0;n<e.children.length;n++){let r=e.children[n];r.hidden||(rn(r),t.insertChild(r.yogaNode,t.getChildCount()));}}function on(e){if(e.text!=null)return e.text;let t="";for(let n of e.children)t+=on(n);if(t===""&&e.props.children!=null){if(typeof e.props.children=="string")return e.props.children;if(typeof e.props.children=="number")return String(e.props.children)}return t}function sn(e,t,n){let r=e.yogaNode,o=r.getComputedLayout(),s=t+o.left,i=n+o.top,c=o.width,l=o.height,u=e.style.border&&e.style.border!=="none"?1:0,f=r.getComputedPadding(Edge.Top),m=r.getComputedPadding(Edge.Right),S=r.getComputedPadding(Edge.Bottom),R=r.getComputedPadding(Edge.Left),v=s+u+R,a=i+u+f,p=Math.max(0,c-u*2-R-m),h=Math.max(0,l-u*2-f-S);e.layout={x:s,y:i,width:c,height:l,innerX:v,innerY:a,innerWidth:p,innerHeight:h};for(let I of e.children)I.hidden||!I.yogaNode||sn(I,s,i);}function ln(e,t,n){let r=nn.Node.create();r.setWidth(t),r.setHeight(n),r.setFlexDirection(FlexDirection.Column);for(let o of e)o.hidden||(rn(o),r.insertChild(o.yogaNode,r.getChildCount()));r.calculateLayout(t,n,Direction.LTR);for(let o of e)o.hidden||!o.yogaNode||sn(o,0,0);r.freeRecursive(),cn(e);}function cn(e){for(let t of e)t.yogaNode=null,cn(t.children);}var Z=createContext(null),q=createContext(null),oe=createContext(null),Ce=createContext(null),Ye=createContext(null);function ar(e,t={}){let n=t.stdout??process.stdout,r=t.stdin??process.stdin,o=t.debug??false,s=t.useNativeCursor??true,i=new $e(n,r);i.setup();let c=false;i.queryPalette().then(g=>{_t(g),f=true,x();});let l=new ke(i.columns,i.rows),u=new ke(i.columns,i.rows),f=true,m=new Set,S=new Set,R=new Map,v={subscribe(g){return m.add(g),()=>m.delete(g)},subscribePriority(g){return S.add(g),()=>S.delete(g)},registerInputHandler(g,L){return R.set(g,L),()=>R.delete(g)}},a=null,p=new Map,h=[],I=new Set,P=[],j=new Set;function K(g){if(a!==g){a=g,x();for(let L of j)L(a);}}function E(){let g=[...h];if(P.length>0){let L=P[P.length-1];g=g.filter(N=>L.has(N));}return g=g.filter(L=>!I.has(L)),g.sort((L,N)=>{let w=p.get(L),k=p.get(N);if(!w||!k)return 0;let F=w.layout,$=k.layout;return F.y!==$.y?F.y-$.y:F.x-$.x}),g}let G={get focusedId(){return a},register(g,L){if(p.set(g,L),h.includes(g)||h.push(g),P.length>0&&P[P.length-1].add(g),a===null){let N=E();N.length>0&&K(N[0]);}return ()=>{p.delete(g);let N=h.indexOf(g);if(N!==-1&&h.splice(N,1),a===g){let w=E();K(w[0]??null);}}},requestFocus(g){K(g);},focusNext(){let g=E();if(g.length===0)return;let N=((a?g.indexOf(a):-1)+1)%g.length;K(g[N]);},focusPrev(){let g=E();if(g.length===0)return;let N=((a?g.indexOf(a):0)-1+g.length)%g.length;K(g[N]);},setSkippable(g,L){if(L){if(I.add(g),a===g){let N=E();N.length>0&&K(N[0]);}}else I.delete(g);},trapIds:null,pushTrap(g){return P.push(g),()=>{let L=P.indexOf(g);L!==-1&&P.splice(L,1);}},onFocusChange(g){return j.add(g),()=>{j.delete(g);}},getRegisteredElements(){let g=[];for(let L of h){if(I.has(L))continue;let N=p.get(L);N&&g.push({id:L,node:N});}return g},getActiveElements(){let g=E(),L=[];for(let N of g){if(I.has(N))continue;let w=p.get(N);w&&L.push({id:N,node:w});}return L}},T=new Map,M={getLayout(g){return g.layout},subscribe(g,L){return T.has(g)||T.set(g,new Set),T.get(g).add(L),()=>{let N=T.get(g);N&&(N.delete(L),N.size===0&&T.delete(g));}}},b={registerNode(){},unregisterNode(){},scheduleRender:x,exit(g){le.exit(g);},get columns(){return i.columns},get rows(){return i.rows}},y={type:"root",children:[],onCommit(){x();}},C=false;function x(){C||(C=true,queueMicrotask(()=>{C=false,V();}));}function V(){let g=i.columns,L=i.rows;(u.width!==g||u.height!==L)&&(u.resize(g,L),l.resize(g,L),f=true),ln(y.children,g,L),z(y.children);let N;if(a){let F=p.get(a);F?.type==="input"&&(N={nodeId:a,position:F.props.cursorPosition??F.props.value?.length??0});}let w=Jt(y.children,u,{cursorInfo:N,useNativeCursor:s}),k=Zt(l,u,f);if(k.length>0&&i.write(k),s)if(w.cursorPosition){let F=$t(w.cursorPosition.bg);i.setCursorColor(F),i.moveCursor(w.cursorPosition.x,w.cursorPosition.y),c||(i.showCursor(),c=true);}else c&&(i.hideCursor(),c=false);for(let F=0;F<u.cells.length;F++)l.cells[F]={...u.cells[F]};f=false;}function z(g){for(let L of g){let N=T.get(L);if(N)for(let w of N)w(L.layout);z(L.children);}}let O=i.onData(g=>{let L=jt(g);for(let N of L){if(N.ctrl&&N.name==="c"){le.exit();return}if(N.ctrl&&N.name==="z"){i.suspend(),process.kill(0,"SIGSTOP");return}let w=false;for(let k of S)if(k(N)){w=true;break}if(!w&&a){let k=R.get(a);k&&(w=k(N));}if(!w&&N.name==="tab"&&!N.ctrl&&!N.alt){N.shift?G.focusPrev():G.focusNext();continue}if(!w)for(let k of m)k(N);}}),W=i.onResize(()=>{f=true,x();}),Y=()=>{i.resume(),f=true,x();};process.on("SIGCONT",Y);let X=te.createElement(Ce.Provider,{value:b},te.createElement(Z.Provider,{value:v},te.createElement(q.Provider,{value:G},te.createElement(oe.Provider,{value:M},e)))),_=g=>{o&&console.error("Uncaught error:",g);},J=g=>{o&&console.error("Error caught by boundary:",g);},B=g=>{o&&console.error("Recoverable error:",g);},se=Ie.createContainer(y,0,null,false,null,"",_,J,B,null);Ie.updateContainer(X,se,null,null);let le={unmount(){Ie.updateContainer(null,se,null,null),O(),W(),process.off("SIGCONT",Y),i.cleanup();},exit(g){le.unmount(),process.exit(g??0);}};return le}var pr=forwardRef(function({children:t,style:n,focusable:r},o){return te.createElement("box",{style:n,focusable:r,ref:o},t)});var gr=forwardRef(function({children:t,style:n,wrap:r},o){let s=r?{...n,wrap:r}:n;return te.createElement("text",{style:s,ref:o},t)});function un(e,t,n){if(n<=0)return {visualLine:0,visualCol:t,totalVisualLines:1,lineStartOffset:0,lineLength:e.length};let r=e.split(`
5
5
  `),o=[],s=0;for(let l of r){let u=ce([l],n,"wrap"),f=0;for(let m of u)o.push({text:m,logicalOffset:s+f}),f+=m.length;s+=l.length+1;}let i=0;for(let l=0;l<o.length;l++){let u=o[l],f=u.text.length,m=l+1<o.length&&o[l+1].logicalOffset!==u.logicalOffset+f,S=f+(m?1:0);if(t<i+f||l===o.length-1)return {visualLine:l,visualCol:Math.min(t-i,f),totalVisualLines:o.length,lineStartOffset:i,lineLength:f};i+=S;}let c=o.length-1;return {visualLine:c,visualCol:o[c].text.length,totalVisualLines:o.length,lineStartOffset:i-o[c].text.length,lineLength:o[c].text.length}}function an(e,t,n,r){if(r<=0)return Math.min(n,e.length);let o=e.split(`
6
6
  `),s=[],i=0;for(let f of o){let m=ce([f],r,"wrap"),S=0;for(let R of m)s.push({text:R,startOffset:i+S}),S+=R.length;i+=f.length+1;}let c=Math.max(0,Math.min(t,s.length-1)),l=s[c],u=Math.min(n,l.text.length);return l.startOffset+u}function Ge(e,t){let n=e.split(`
7
- `),r=t;for(let s=0;s<n.length;s++){if(r<=n[s].length)return {line:s,col:r,lines:n};r-=n[s].length+1;}let o=n.length-1;return {line:o,col:n[o].length,lines:n}}function Be(e,t,n){let r=0;for(let o=0;o<t&&o<e.length;o++)r+=e[o].length+1;return r+Math.min(n,e[t]?.length??0)}function xr(e){let{value:t,defaultValue:n="",onChange:r,onKeyPress:o,onBeforeChange:s,placeholder:i,style:c,focusedStyle:l,multiline:u,autoFocus:f,type:m="text"}=e,[S,R]=useState(n),[v,a]=useState(n.length),[p,h]=useState(0),[I,P]=useState(false),[j,K]=useState(false),E=useContext(Z),G=useContext(q),T=useContext(oe),M=useRef(null),b=useRef(null),y=t!==void 0,C=y?t:S;useEffect(()=>{if(!T||!M.current)return;let _=T.getLayout(M.current);return h(_.innerWidth),T.subscribe(M.current,J=>{h(J.innerWidth);})},[T]);let x=useRef(C),V=useRef(v),z=useRef(C),O=useRef(v);useEffect(()=>{C!==z.current&&(x.current=C,z.current=C,V.current>C.length&&(V.current=C.length,O.current=C.length,a(C.length)));},[C]),useEffect(()=>{v!==O.current&&(V.current=v,O.current=v);},[v]);let W=useRef({isControlled:y,onChange:r,onKeyPress:o,onBeforeChange:s,multiline:u??false,innerWidth:p,type:m});W.current={isControlled:y,onChange:r,onKeyPress:o,onBeforeChange:s,multiline:u??false,innerWidth:p,type:m},useEffect(()=>{if(!(!G||!b.current||!M.current))return G.register(b.current,M.current)},[G,j]);let Y=useRef(false);useEffect(()=>{if(!j){Y.current=false;return}if(f&&!Y.current&&G&&b.current){Y.current=true;let _=b.current,J=setTimeout(()=>{G.requestFocus(_);},0);return ()=>clearTimeout(J)}},[f,G,j]),useEffect(()=>{if(!G||!b.current)return;let _=b.current;return P(G.focusedId===_),G.onFocusChange(J=>{P(J===_);})},[G,j]),useEffect(()=>{if(!E||!b.current)return;let _=b.current,J=B=>{let{isControlled:se,onChange:le,onKeyPress:g,onBeforeChange:L,multiline:N}=W.current;if(g?.(B)===true)return true;let w=x.current,k=V.current;if(B.name==="escape")return false;let F=(d,A)=>{let H=d,Q=A;if(L){let U=L(d,w);if(U===false)return;typeof U=="string"&&(H=U,Q=U.length);}x.current=H,V.current=Q,z.current=H,O.current=Q,se||R(H),le?.(H),a(Q);},$=d=>{V.current=d,O.current=d,a(d);};if(B.name==="return"){if(N){let d=w.slice(0,k)+`
7
+ `),r=t;for(let s=0;s<n.length;s++){if(r<=n[s].length)return {line:s,col:r,lines:n};r-=n[s].length+1;}let o=n.length-1;return {line:o,col:n[o].length,lines:n}}function Be(e,t,n){let r=0;for(let o=0;o<t&&o<e.length;o++)r+=e[o].length+1;return r+Math.min(n,e[t]?.length??0)}function xr(e){let{value:t,defaultValue:n="",onChange:r,onKeyPress:o,onBeforeChange:s,placeholder:i,style:c,focusedStyle:l,multiline:u,autoFocus:f,type:m="text"}=e,[S,R]=useState(n),[v,a]=useState(n.length),[p,h]=useState(0),[I,P]=useState(false),[j,K]=useState(false),E=useContext(Z),G=useContext(q),T=useContext(oe),M=useRef(null),b=useRef(null),y=t!==void 0,C=y?t:S;useEffect(()=>{if(!T||!M.current)return;let _=T.getLayout(M.current);return h(_.innerWidth),T.subscribe(M.current,J=>{h(J.innerWidth);})},[T]);let x=useRef(C),V=useRef(v),z=useRef(C),O=useRef(v);useEffect(()=>{C!==z.current&&(x.current=C,z.current=C,V.current=C.length,O.current=C.length,a(C.length));},[C]),useEffect(()=>{v!==O.current&&(V.current=v,O.current=v);},[v]);let W=useRef({isControlled:y,onChange:r,onKeyPress:o,onBeforeChange:s,multiline:u??false,innerWidth:p,type:m});W.current={isControlled:y,onChange:r,onKeyPress:o,onBeforeChange:s,multiline:u??false,innerWidth:p,type:m},useEffect(()=>{if(!(!G||!b.current||!M.current))return G.register(b.current,M.current)},[G,j]);let Y=useRef(false);useEffect(()=>{if(!j){Y.current=false;return}if(f&&!Y.current&&G&&b.current){Y.current=true;let _=b.current,J=setTimeout(()=>{G.requestFocus(_);},0);return ()=>clearTimeout(J)}},[f,G,j]),useEffect(()=>{if(!G||!b.current)return;let _=b.current;return P(G.focusedId===_),G.onFocusChange(J=>{P(J===_);})},[G,j]),useEffect(()=>{if(!E||!b.current)return;let _=b.current,J=B=>{let{isControlled:se,onChange:le,onKeyPress:g,onBeforeChange:L,multiline:N}=W.current;if(g?.(B)===true)return true;let w=x.current,k=V.current;if(B.name==="escape")return false;let F=(d,A)=>{let H=d,Q=A;if(L){let U=L(d,w);if(U===false)return;typeof U=="string"&&(H=U,Q=U.length);}x.current=H,V.current=Q,z.current=H,O.current=Q,se||R(H),le?.(H),a(Q);},$=d=>{V.current=d,O.current=d,a(d);};if(B.name==="return"){if(N){let d=w.slice(0,k)+`
8
8
  `+w.slice(k);return F(d,k+1),true}return false}if(B.ctrl){if(B.name==="w"){if(k>0){let d=k;for(;d>0&&w[d-1]===" ";)d--;for(;d>0&&w[d-1]!==" "&&(!N||w[d-1]!==`
9
9
  `);)d--;let A=w.slice(0,d)+w.slice(k);F(A,d);}return true}if(B.name==="a"){if(N){let{line:d,lines:A}=Ge(w,k);$(Be(A,d,0));}else $(0);return true}if(B.name==="e"){if(N){let{line:d,lines:A}=Ge(w,k);$(Be(A,d,A[d].length));}else $(w.length);return true}if(B.name==="k"){if(N){let{line:d,lines:A}=Ge(w,k),H=Be(A,d,A[d].length);if(k<H){let Q=w.slice(0,k)+w.slice(H);F(Q,k);}}else if(k<w.length){let d=w.slice(0,k);F(d,k);}return true}return false}if(B.alt){if(B.name==="left"||B.name==="b"){let d=k;for(;d>0&&w[d-1]===" ";)d--;for(;d>0&&w[d-1]!==" "&&w[d-1]!==`
10
10
  `;)d--;return $(d),true}if(B.name==="right"||B.name==="f"){let d=k;for(;d<w.length&&w[d]!==" "&&w[d]!==`