eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 2.12 kB
JavaScript
import{PROMPT_COMMANDS}from"./prompt-commands.js";import{sliceVisible,visibleLength}from"#cli/ui/terminal-text.js";import{renderCursorRow}from"#setup/cli/option-row.js";const MAX_VISIBLE_SUGGESTIONS=PROMPT_COMMANDS.length;function typeaheadFor(e,t,n){let r=matchingCommands(e,t),i=n===void 0?void 0:n.matches[n.selectedIndex],a=i===void 0?-1:r.indexOf(i);return{query:t,matches:r,selectedIndex:a>=0?a:0,dismissed:n!==void 0&&n.dismissed&&n.query===t}}function matchingCommands(e,t){if(!t.startsWith(`/`)||/\s/.test(t))return[];let n=t.slice(1);return e.filter(e=>[e.name,...e.aliases].some(e=>e.startsWith(n)))}function isTypeaheadOpen(e){return e.matches.length>0&&!e.dismissed}function moveTypeaheadSelection(e,t){let n=e.matches.length;if(n===0)return e;let r=(e.selectedIndex+t+n)%n;return{...e,selectedIndex:r}}function dismissTypeahead(e){return e.dismissed?e:{...e,dismissed:!0}}function selectedTypeaheadCommand(e){return e.matches[e.selectedIndex]}function typeaheadCompletion(e){return`/${e.name}${e.takesArgument?` `:``}`}function inlineCommandHint(e){if(e.dismissed||e.matches.length!==1)return;let t=e.matches[0],n=e.query.startsWith(`/`)?e.query.slice(1):e.query;if([t.name,...t.aliases].includes(n))return t.argumentHint??``}function renderCommandSuggestions(e,t,n){let r=t.colors,i=e.matches.length,a=Math.min(i,MAX_VISIBLE_SUGGESTIONS),o=Math.max(0,Math.min(e.selectedIndex-Math.floor(a/2),i-a)),s=Math.min(o+a,i),c=e.matches.slice(o,s),invocation=e=>{let t=e.aliases.map(e=>` (/${e})`).join(``);return`/${e.name}${t}`},l=Math.max(...c.map(e=>invocation(e).length))+2;return c.map((n,i)=>{let a=o+i===e.selectedIndex,s=`/${n.name}`,c=a?`${t.glyph.selectedPointer} ${s}`:` ${s}`,u=renderCursorRow(c,a,r),d=invocation(n).slice(`/${n.name}`.length),f=` `.repeat(l-invocation(n).length);return a&&(d.startsWith(` `)?d=d.slice(1):f=f.slice(1)),`${u}${r.dim(d)}${f}${r.dim(n.description)}`}).map(e=>visibleLength(e)>n?sliceVisible(e,n):e)}export{dismissTypeahead,inlineCommandHint,isTypeaheadOpen,moveTypeaheadSelection,renderCommandSuggestions,selectedTypeaheadCommand,typeaheadCompletion,typeaheadFor};