eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 3.98 kB
JavaScript
import{graphemes}from"#shared/text-boundaries.js";const ansiPattern=RegExp(`\x1B\\[[0-?]*[ -/]*[@-~]`,`g`),ansiPrefixPattern=RegExp(`^\x1B\\[[0-?]*[ -/]*[@-~]`),emojiPresentationPattern=/\p{Emoji_Presentation}/u,extendedPictographicPattern=/\p{Extended_Pictographic}/u,keycapPattern=/^[#*0-9]\u{fe0f}?\u{20e3}$/u;function stripAnsi(e){return stripTerminalControls(e.replaceAll(ansiPattern,``))}function stripTerminalControls(e){let t=``,n=0;for(;n<e.length;){let r=e.codePointAt(n);if(r==null)break;let i=String.fromCodePoint(r);n+=i.length,!isUnsafeTerminalControlCodePoint(r)&&(t+=i)}return t}function visibleLength(e){let t=0;for(let n of terminalTextUnits(e))t+=n.width;return t}function sliceVisible(e,t){if(t<=0)return``;let n=``,r=0,i=terminalTextUnits(e),a=0;for(;a<i.length&&r<t;){let e=i[a];if(e.width>0&&r+e.width>t)break;n+=e.text,r+=e.width,a+=1}for(;i[a]?.ansi===!0;)n+=i[a].text,a+=1;return n}function clipVisible(e,t){if(visibleLength(e)<=t)return e;let n=sliceVisible(e,t);return n.includes(`\x1B`)?`${n}[0m`:n}function inputTextWidth(t){let n=0;for(let r of graphemes(t))n+=terminalGraphemeWidth(r.text);return n}function renderInputText(e){return e.replaceAll(` `,` `)}function renderInputWithBlockCursor(e){let t=e.render??renderInputText,n=e.under.length>0?e.under:` `,r=e.visible?e.inverse(renderInputText(n)):t(n);return`${t(e.before)}${r}${t(e.after)}`}function offsetAtVisibleColumn(t,n){if(n<=0)return 0;let r=0;for(let i of graphemes(t)){let e=r+terminalGraphemeWidth(i.text);if(e>n)return i.start;if(e===n)return i.end;r=e}return t.length}function terminalGraphemeWidth(e){let t=0;for(let n of e){let e=n.codePointAt(0);e!==void 0&&(t=Math.max(t,codePointWidth(e)))}return emojiPresentationPattern.test(e)||keycapPattern.test(e)||e.includes(`️`)&&extendedPictographicPattern.test(e)?Math.max(2,t):t}function terminalTextUnits(r){let i=[],a=0;for(;a<r.length;){let o=r.slice(a),s=o.match(ansiPrefixPattern);if(s!==null){i.push({text:s[0],width:0,ansi:!0}),a+=s[0].length;continue}let c=o.search(ansiPattern),l=o.slice(0,c===-1?o.length:c);for(let t of graphemes(l))i.push({text:t.text,width:terminalGraphemeWidth(t.text),ansi:!1});a+=l.length}return i}function wrapVisibleLine(e,t){if(t<=0)return[e];if(e.length===0)return[``];let n=[],r=e;for(;visibleLength(r)>t;){let e=findVisibleBreakPoint(r,t);n.push(r.slice(0,e).trimEnd()),r=r.slice(e).trimStart()}return(r.length>0||n.length===0)&&n.push(r),n}function findVisibleBreakPoint(e,t){let n=sliceVisible(e,t+1).lastIndexOf(` `);if(n>0)return n;let r=sliceVisible(e,t);if(visibleLength(r)>0)return r.length;let i=0,a=!1;for(let t of terminalTextUnits(e)){if(a&&t.width>0)return i;i+=t.text.length,t.width>0&&(a=!0)}return i}function codePointWidth(e){return e===9?4:e<32||e>=127&&e<160||isZeroWidthCodePoint(e)?0:isWideCodePoint(e)?2:1}function isUnsafeTerminalControlCodePoint(e){return e>=0&&e<=8||e>=11&&e<=31||e>=127&&e<=159}function isZeroWidthCodePoint(e){return e>=768&&e<=879||e>=1155&&e<=1161||e>=1425&&e<=1469||e===1471||e>=1473&&e<=1474||e>=1476&&e<=1477||e===1479||e>=1552&&e<=1562||e>=1611&&e<=1631||e===1648||e>=1750&&e<=1756||e>=1759&&e<=1764||e>=1767&&e<=1768||e>=1770&&e<=1773||e===1809||e>=1840&&e<=1866||e>=1958&&e<=1968||e>=2027&&e<=2035||e>=2070&&e<=2073||e>=2075&&e<=2083||e>=2085&&e<=2087||e>=2089&&e<=2093||e>=2137&&e<=2139||e>=2259&&e<=2306||e===2362||e===2364||e>=2369&&e<=2376||e===2381||e>=2385&&e<=2391||e===8205||e>=65024&&e<=65039||e>=917760&&e<=917999}function isWideCodePoint(e){return e>=4352&&(e<=4447||e===9001||e===9002||e>=11904&&e<=42191&&e!==12351||e>=44032&&e<=55203||e>=63744&&e<=64255||e>=65040&&e<=65049||e>=65072&&e<=65135||e>=65280&&e<=65376||e>=65504&&e<=65510||e>=127744&&e<=128591||e>=129280&&e<=129535||e>=131072&&e<=262141)}export{ansiPattern,ansiPrefixPattern,clipVisible,codePointWidth,inputTextWidth,offsetAtVisibleColumn,renderInputText,renderInputWithBlockCursor,sliceVisible,stripAnsi,stripTerminalControls,visibleLength,wrapVisibleLine};