eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 4.19 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){return sliceUnits(terminalTextUnits(e),0,t)}function sliceUnits(e,t,n){if(n<=0)return``;let r=``,i=0,a=t;for(;a<e.length&&i<n;){let t=e[a];if(t.width>0&&i+t.width>n)break;r+=t.text,i+=t.width,a+=1}for(;e[a]?.ansi===!0;)r+=e[a].text,a+=1;return r}function clipVisible(e,t){if(visibleLength(e)<=t)return e;let n=sliceVisible(e,t);return n.includes(`\x1B`)?`${n}[0m`:n}function inputTextWidth(e){let t=0;for(let n of graphemes(e))t+=terminalGraphemeWidth(n.text);return t}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(e,t){if(t<=0)return 0;let n=0;for(let r of graphemes(e)){let e=n+terminalGraphemeWidth(r.text);if(e>t)return r.start;if(e===t)return r.end;n=e}return e.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(e){let t=[],n=0;for(;n<e.length;){let r=e.slice(n),i=r.match(ansiPrefixPattern);if(i!==null){t.push({text:i[0],width:0,ansi:!0}),n+=i[0].length;continue}let a=r.search(ansiPattern),o=r.slice(0,a===-1?r.length:a);for(let e of graphemes(o))t.push({text:e.text,width:terminalGraphemeWidth(e.text),ansi:!1});n+=o.length}return t}function wrapVisibleLine(e,t){if(t<=0)return[e];let n=terminalTextUnits(e),r=0;for(let e of n)r+=e.width;let i=[],a=0,o=0,consume=()=>{let e=n[a];o+=e.text.length,r-=e.width,a+=1};for(;r>t;){let r=findVisibleBreakPoint(n,a,t);i.push(e.slice(o,o+r).trimEnd());let s=o+r;for(;o<s&&a<n.length;)consume();for(;a<n.length;){let e=n[a];if(e.ansi||e.text.trimStart().length>0)break;consume()}}let s=e.slice(o);return(s.length>0||i.length===0)&&i.push(s),i}function findVisibleBreakPoint(e,t,n){let r=sliceUnits(e,t,n+1).lastIndexOf(` `);if(r>0)return r;let i=sliceUnits(e,t,n);if(visibleLength(i)>0)return i.length;let a=0,o=!1;for(let n=t;n<e.length;n+=1){let t=e[n];if(o&&t.width>0)return a;a+=t.text.length,t.width>0&&(o=!0)}return a}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{clipVisible,inputTextWidth,offsetAtVisibleColumn,renderInputText,renderInputWithBlockCursor,sliceVisible,stripAnsi,stripTerminalControls,visibleLength,wrapVisibleLine};