accompany 0.27.2 → 0.28.0

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/cli.js CHANGED
@@ -36,7 +36,7 @@ ${x.join(`
36
36
  \x1B[2m${gi}\x1B[0m\r
37
37
  `);this.screen.onTitleChange((t)=>{let n=xi(t);if(n===this.title)return;this.title=n,this.onTitleChange()});for(let[t,n]of[["h",!0],["l",!1]])this.screen.parser.registerCsiHandler({prefix:"?",final:t},(l)=>{if(l.includes(25))this.cursorHidden=!n;for(let p of[1006,1016])if(l.includes(p))this.mouseEncoding=n?p:void 0;return!1});this.screen.parser.registerEscHandler({final:"c"},()=>{if(this.cursorHidden=!1,this.mouseEncoding=void 0,this.title!=="")this.title="",this.onTitleChange();return!1})}attach(e){let r=[],s=!1,c=(f)=>s?e(f):r.push(f);if(this.listeners.add(c),this.screen.write("",()=>{e({type:"resize",size:this.size}),e({type:"trimmed",lines:this.trimmed.count}),e({type:"marks",...this.marks.state()});let f=this.serializer.serialize()+(this.cursorHidden?"\x1B[?25l":"")+(this.mouseEncoding?`\x1B[?${this.mouseEncoding}h`:"");if(f)e({type:"output",data:Nr.encode(f)});e({type:"restored"}),r.forEach(e),s=!0}),!this.proc&&!this.startTimer)this.startTimer=setTimeout(()=>this.start(),mi);return{resize:(f)=>{this.requestedSizes.set(c,f),this.fitToSmallest(),this.start()},detach:()=>{this.listeners.delete(c),this.requestedSizes.delete(c),this.fitToSmallest()}}}start(){if(clearTimeout(this.startTimer),this.proc||this.killed)return;this.proc=this.spawn((e)=>Bun.spawn(this.command,{cwd:e,terminal:{...this.size,data:(r,s)=>{this.screen.write(s),this.activity.output(),this.scheduleDetect(),this.broadcast({type:"output",data:s}),this.onOutput()}}})),this.proc.exited.then(()=>this.onExit())}spawn(e){if(this.startDir)try{return e(this.startDir)}catch{}return e()}async cwd(){let e=this.proc?.pid;if(e===void 0)return this.startDir;let r=await qe(e,this.tty);return(r&&await Et(r.foreground.pgid))??await Et(e)}async savedCwd({refresh:e=!1}={}){if(this.proc===void 0)return this.lastCwd;if(!e){let s=this.lastInputAt>=this.cwdCheckedAt-bi,c=this.foreground?.agent===!0&&this.foreground.pgid===this.cwdPgid;if(!s||c)return this.lastCwd}this.cwdCheckedAt=performance.now(),this.cwdPgid=this.foreground?.pgid;let r=await Promise.race([this.cwd(),Bun.sleep(yi).then(()=>{return})]);if(r)this.lastCwd=r;return this.lastCwd}write(e){this.lastInputAt=performance.now(),this.activity.input(e),this.scheduleDetect(),this.proc?.terminal?.write(e)}reset(){let e=Nr.encode("\x1Bc");this.screen.write(e),this.broadcast({type:"output",data:e})}snapshot(){return new Promise((e)=>this.screen.write("",()=>e({...this.size,scrollback:this.serializer.serialize({excludeAltBuffer:!0,excludeModes:!0})})))}get status(){if(!this.foreground?.agent)return;return this.activity.status==="idle"&&this.unseen?"done":this.activity.status}markSeen(){let e=this.status==="done";return this.unseen=!1,e}kill(){if(this.hangingUp)return this.hangingUp;this.killed=!0,clearTimeout(this.startTimer),clearTimeout(this.detectTimer),this.activity.dispose();let e=this.proc;if(!e||e.exitCode!==null||e.signalCode!==null)return this.hangingUp=Promise.resolve();let r=Bun.sleep(wi);return this.hangingUp=(async()=>{let s=await Promise.race([qe(e.pid,this.tty),Bun.sleep(Si)]);if(e.exitCode!==null||e.signalCode!==null)return;let c=[...new Set([e.pid,s?.foreground.pgid])].filter((t)=>t!==void 0&&t>1);for(let t of c)try{process.kill(-t,"SIGHUP")}catch{}let f=c.filter((t)=>t!==e.pid),a=!1,y=(async()=>{await e.exited;while(!a&&f.some(Ci))await Bun.sleep(20)})();await Promise.race([y,r]),a=!0})(),this.hangingUp}scheduleDetect(){if(this.detecting){this.detectAgain=!0;return}if(this.detectTimer||this.killed)return;this.detectTimer=setTimeout(()=>{this.detectTimer=void 0,this.detect()},vi)}async detect(){let e=this.proc?.pid;if(e===void 0)return;this.detecting=!0,this.detectAgain=!1;let r;try{r=await qe(e,this.tty)}finally{if(this.detecting=!1,this.detectAgain)this.scheduleDetect()}if(this.killed||!r)return;this.tty=r.tty;let s=this.foreground,c=r.foreground;if(this.foreground=c,!c.agent){if(this.unseen=!1,s?.agent)this.onStatus();return}if(!s?.agent||s.pgid!==c.pgid){if(!this.startedAsAgent||c.pgid!==e)this.activity.reset(),this.unseen=!1;this.onStatus()}}fitToSmallest(){let e=[...this.requestedSizes.values()];if(e.length===0)return;let r=(c)=>{let f=e.map((t)=>t.fits[c]),a=e.flatMap((t)=>t.min?[t.min[c]]:[]),y=a.length>0?Math.min(...a):0;return Math.min(Math.max(...f),Math.max(Math.min(...f),y))},s=ye({cols:r("cols"),rows:r("rows")});if(s.cols===this.size.cols&&s.rows===this.size.rows)return;this.size=s,this.screen.resize(s.cols,s.rows),this.proc?.terminal?.resize(s.cols,s.rows),this.broadcast({type:"resize",size:s})}broadcast(e){this.listeners.forEach((r)=>r(e))}}var Fr=256;function xi(e){let r=[...Tt(e)];return r.length>Fr?`${r.slice(0,Fr-1).join("")}\u2026`:r.join("")}function Tt(e){return e.replace(/[\u2028\u2029\u0085]/g," ").replace(/[\p{Cc}\u061c\u200e\u200f\u202a-\u202e\u2066-\u2069]/gu,"")}var $r=32,Ei=$r*32,Ai=new Intl.Segmenter(void 0,{granularity:"grapheme"}),ki=/[\p{Default_Ignorable_Code_Point}\s\u2800]/gu;function ge(e){let r=Tt(e.slice(0,Ei).replace(/[\t\n\v\f\r]/g," ")).replace(/\p{Cs}/gu,"").replace(/\s+/g," ").trim(),s=[...Ai.segment(r)].slice(0,$r).map((c)=>c.segment).join("").trim();return s.replace(ki,"")?s:void 0}function xe(e){return e.normalize("NFKC").replace(/[\p{Default_Ignorable_Code_Point}\p{M}]/gu,"")}var Ur={width:480,height:1080},Ee={width:1920,height:1080};function Ae(e){if(typeof e!=="object"||e===null)return;let{width:r,height:s}=e,c=(f,a)=>Number.isSafeInteger(f)&&f>=0&&f<=a;return c(r,Ee.width)&&c(s,Ee.height)?{width:r,height:s}:void 0}function jr(e){let r=/^(\d+)x(\d+)$/.exec(e);return r?Ae({width:Number(r[1]),height:Number(r[2])}):void 0}var Gr=100;function zr(e){return e.replace(/[^\P{Cc}\n\t]|\p{Cf}/gu,"")}function Rt(e,r){return[...e].slice(0,r).join("")}function Qe(e){if(typeof e.content!=="string"||typeof e.name!=="string")return;let r=Rt(zr(e.content.replace(/\r\n?|[\u2028\u2029\u0085]/g,`
38
38
  `)),1e4).replace(/\n+$/,"");if(r.trim()==="")return;return{name:Rt(zr(e.name.replace(/[\r\n\t\u2028\u2029\u0085]/g," ")).trim(),100)||Rt(r.trim().split(`
39
- `)[0],100),content:r,run:e.run===!0}}import{lstatSync as Li,mkdirSync as Ti,mkdtempSync as Ri,rmSync as Bi,writeFileSync as Di}from"fs";import{tmpdir as Mi}from"os";import{join as Bt}from"path";var Vr=104857600;class Dt{root=qr();nextId=1;async save(e){let r=await Promise.all(e.map(async(f)=>new Uint8Array(await f.arrayBuffer()))),s=Bt(this.currentRoot(),String(this.nextId++));Ti(s,{mode:448});let c=new Set;return e.map((f,a)=>{let y=Xr(f.name);for(let n=2;c.has(y);n++)y=`${n}-${Xr(f.name)}`;c.add(y);let t=Bt(s,y);return Di(t,r[a],{flag:"wx",mode:384}),t})}remove(){Bi(this.root,{recursive:!0,force:!0})}currentRoot(){if(!Pi(this.root))this.root=qr();return this.root}}function qr(){return Ri(Bt(Mi(),"accompany-"))}function Pi(e){try{let r=Li(e);return r.isDirectory()&&r.uid===process.getuid?.()&&(r.mode&63)===0}catch{return!1}}function Xr(e){let r=e.split(/[\\/]/).pop()??"";return Array.from(r.replace(/[\x00-\x1f\x7f]/g,"")).slice(-60).join("").replace(/^[.\s-]+/,"")||"file"}var Oi=new Set(["localhost","127.0.0.1","[::1]"]),Qr=Ar(async(e,r)=>{let s=e.req.header("Host")??"",c=e.req.header("Origin"),f=URL.parse(`http://${s}`)?.hostname??"";if(!Oi.has(f)||c!==`http://${s}`)return e.text("Forbidden",403);await r()});function Ii(e){let{cols:r,rows:s,minCols:c,minRows:f}=e,a=(t,n)=>Number.isSafeInteger(t)&&t>=n;if(!a(r,1)||!a(s,1))return;let y=ye({cols:r,rows:s});if(c===void 0&&f===void 0)return{fits:y};if(!a(c,0)||!a(f,0))return;return{fits:y,min:ye({cols:c,rows:f})}}var Hi=Xe+Ce.rows;function Ni(e){let{start:r,end:s}=e;if(r===void 0&&s===void 0)return null;let c=(t,n)=>Number.isSafeInteger(t)&&t>=0&&t<=n,f=(t)=>{let{x:n,y:l}=t??{};return c(n,Ce.cols)&&c(l,Number.MAX_SAFE_INTEGER)?{x:n,y:l}:void 0},a=f(r),y=f(s);if(!a||!y||a.y>y.y||a.y===y.y&&a.x>y.x)return;if(y.y-a.y>=Hi)return;return{start:a,end:y}}var Fi=1024;function Mt(e,r){let s=typeof r==="string"?r.length:r.byteLength;e.send(r,{compress:s>=Fi})}function pe(e){if(typeof e.tab==="string")return[e.tab];let{tabs:r}=e;return Array.isArray(r)&&r.every((s)=>typeof s==="string")?r:void 0}var Wi=new Set(["left","right","bottom"]);function Kr(e){try{return JSON.parse(String(e))}catch{return}}function Yr(e,r){let s=new je;return s.use("/ws/*",Qr),s.use("/api/*",Qr),s.get("/ws/workspace",Ne(()=>{let c;return{onOpen:(f,a)=>{c=e.join((y)=>Mt(a,JSON.stringify(y)))},onMessage:(f)=>{let a=Kr(f.data),y=typeof a?.terminal==="string"?a.terminal:void 0;if(a?.type==="split"&&(a.direction==="row"||a.direction==="column"))c?.split(String(a.terminal),a.direction);else if(a?.type==="move-tab"&&pe(a)&&Number.isSafeInteger(a.index)&&(a.group===void 0||a.group===null||typeof a.group==="string"))e.moveTab(pe(a),a.index,a.group);else if(a?.type==="new-group"&&pe(a))e.newGroup(pe(a));else if(a?.type==="add-to-group"&&pe(a)&&typeof a.group==="string")e.addToGroup(pe(a),a.group);else if(a?.type==="remove-from-group"&&pe(a))e.removeFromGroup(pe(a));else if(a?.type==="move-group"&&typeof a.group==="string"&&Number.isSafeInteger(a.index))e.moveGroup(a.group,a.index);else if(a?.type==="rename-group"&&typeof a.group==="string"&&typeof a.name==="string")e.renameGroup(a.group,ge(a.name));else if(a?.type==="recolor-group"&&typeof a.group==="string"&&typeof a.color==="string")e.recolorGroup(a.group,a.color);else if(a?.type==="ungroup"&&typeof a.group==="string")e.ungroup(a.group);else if(a?.type==="close-group"&&typeof a.group==="string")e.closeGroup(a.group);else if(a?.type==="rename-tab"&&typeof a.tab==="string"&&typeof a.name==="string")e.renameTab(a.tab,ge(a.name));else if(a?.type==="close")e.close(String(a.terminal));else if(a?.type==="resize-split"&&typeof a.ratio==="number")c?.resizeSplit(String(a.split),a.ratio);else if(a?.type==="swap-panes"&&typeof a.tab==="string"&&typeof a.with==="string")c?.swapPanes(a.tab,a.with);else if(a?.type==="flip-split"&&typeof a.split==="string")c?.flipSplit(a.split);else if(a?.type==="rotate-split"&&typeof a.split==="string")c?.rotateSplit(a.split);else if(a?.type==="new-tab")c?.newTab(y);else if(a?.type==="select-tab")c?.selectTab(String(a.tab));else if(a?.type==="add-to-view"&&(a.edge===void 0||Wi.has(a.edge)))c?.addToView(String(a.tab),y,a.edge);else if(a?.type==="separate"&&typeof a.tab==="string")c?.separate(a.tab);else if(a?.type==="remove-from-view")c?.removeFromView(String(a.tab));else if(a?.type==="focus")c?.focus(String(a.terminal));else if(a?.type==="select"){let t=Ni(a);if(t!==void 0)c?.select(String(a.terminal),t??void 0)}else if(a?.type==="visibility")c?.visible(a.visible===!0);else if(a?.type==="name"&&typeof a.name==="string")c?.rename(ge(a.name));else if(a?.type==="follow")c?.follow(Number.isSafeInteger(a.member)?a.member:void 0);else if(a?.type==="scroll"&&Number.isSafeInteger(a.offset)&&a.offset>=0){let t=String(a.terminal);if(e.terminal(t))c?.scroll(t,a.offset)}else if(a?.type==="add-command"||a?.type==="update-command"){let t=Qe(a);if(!t)return;if(a.type==="add-command")e.addCommand(t);else e.updateCommand(String(a.id),t)}else if(a?.type==="remove-command")e.removeCommand(String(a.id));else if(a?.type==="set-min-window"){let t=Ae(a);if(t)e.setMinWindow(t)}},onClose:()=>c?.leave()}})),s.get("/ws/terminals/:id",async(c,f)=>e.terminal(c.req.param("id"))?f():c.text("Not Found",404),Ne((c)=>{let f=e.terminal(c.req.param("id")),a;return{onOpen:(y,t)=>{a=f.attach((n)=>n.type==="output"?Mt(t,n.data):Mt(t,JSON.stringify(n.type==="resize"?{type:"resize",...n.size}:n)))},onMessage:(y)=>{let t=Kr(y.data);if(t?.type==="input"&&typeof t.data==="string")f.write(t.data);else if(t?.type==="reset")f.reset();else if(t?.type==="resize"){let n=Ii(t);if(n)a?.resize(n)}},onClose:()=>a?.detach()}})),s.post("/api/uploads",Er({maxSize:Vr,onError:(c)=>c.text("Payload Too Large",413)}),async(c)=>{let a=(await c.req.formData().catch(()=>{return}))?.getAll("files").filter((y)=>y instanceof File)??[];if(a.length===0)return c.text("Bad Request",400);return c.json({paths:await r.save(a)})}),s}import{parseArgs as rs}from"util";var Jr="0.27.2";import{basename as Ui}from"path";var Ke=["zsh","bash","fish"];function Zr(e){return Ke.includes(e)}function es(e=process.env.SHELL??""){return Ui(e)}var ji={zsh:`# Accompany \u306E\u30B7\u30A7\u30EB\u306E\u7D71\u5408\uFF08OSC 133\uFF09\u3002~/.zshrc \u306B eval "$(accompany activate zsh)" \u3068\u66F8\u3044\u3066\u8AAD\u307F\u8FBC\u3080
39
+ `)[0],100),content:r,run:e.run===!0}}import{lstatSync as Li,mkdirSync as Ti,mkdtempSync as Ri,rmSync as Bi,writeFileSync as Di}from"fs";import{tmpdir as Mi}from"os";import{join as Bt}from"path";var Vr=104857600;class Dt{root=qr();nextId=1;async save(e){let r=await Promise.all(e.map(async(f)=>new Uint8Array(await f.arrayBuffer()))),s=Bt(this.currentRoot(),String(this.nextId++));Ti(s,{mode:448});let c=new Set;return e.map((f,a)=>{let y=Xr(f.name);for(let n=2;c.has(y);n++)y=`${n}-${Xr(f.name)}`;c.add(y);let t=Bt(s,y);return Di(t,r[a],{flag:"wx",mode:384}),t})}remove(){Bi(this.root,{recursive:!0,force:!0})}currentRoot(){if(!Pi(this.root))this.root=qr();return this.root}}function qr(){return Ri(Bt(Mi(),"accompany-"))}function Pi(e){try{let r=Li(e);return r.isDirectory()&&r.uid===process.getuid?.()&&(r.mode&63)===0}catch{return!1}}function Xr(e){let r=e.split(/[\\/]/).pop()??"";return Array.from(r.replace(/[\x00-\x1f\x7f]/g,"")).slice(-60).join("").replace(/^[.\s-]+/,"")||"file"}var Oi=new Set(["localhost","127.0.0.1","[::1]"]),Qr=Ar(async(e,r)=>{let s=e.req.header("Host")??"",c=e.req.header("Origin"),f=URL.parse(`http://${s}`)?.hostname??"";if(!Oi.has(f)||c!==`http://${s}`)return e.text("Forbidden",403);await r()});function Ii(e){let{cols:r,rows:s,minCols:c,minRows:f}=e,a=(t,n)=>Number.isSafeInteger(t)&&t>=n;if(!a(r,1)||!a(s,1))return;let y=ye({cols:r,rows:s});if(c===void 0&&f===void 0)return{fits:y};if(!a(c,0)||!a(f,0))return;return{fits:y,min:ye({cols:c,rows:f})}}var Hi=Xe+Ce.rows;function Ni(e){let{start:r,end:s}=e;if(r===void 0&&s===void 0)return null;let c=(t,n)=>Number.isSafeInteger(t)&&t>=0&&t<=n,f=(t)=>{let{x:n,y:l}=t??{};return c(n,Ce.cols)&&c(l,Number.MAX_SAFE_INTEGER)?{x:n,y:l}:void 0},a=f(r),y=f(s);if(!a||!y||a.y>y.y||a.y===y.y&&a.x>y.x)return;if(y.y-a.y>=Hi)return;return{start:a,end:y}}var Fi=1024;function Mt(e,r){let s=typeof r==="string"?r.length:r.byteLength;e.send(r,{compress:s>=Fi})}function pe(e){if(typeof e.tab==="string")return[e.tab];let{tabs:r}=e;return Array.isArray(r)&&r.every((s)=>typeof s==="string")?r:void 0}var Wi=new Set(["left","right","bottom"]);function Kr(e){try{return JSON.parse(String(e))}catch{return}}function Yr(e,r){let s=new je;return s.use("/ws/*",Qr),s.use("/api/*",Qr),s.get("/ws/workspace",Ne(()=>{let c;return{onOpen:(f,a)=>{c=e.join((y)=>Mt(a,JSON.stringify(y)))},onMessage:(f)=>{let a=Kr(f.data),y=typeof a?.terminal==="string"?a.terminal:void 0;if(a?.type==="split"&&(a.direction==="row"||a.direction==="column"))c?.split(String(a.terminal),a.direction);else if(a?.type==="move-tab"&&pe(a)&&Number.isSafeInteger(a.index)&&(a.group===void 0||a.group===null||typeof a.group==="string"))e.moveTab(pe(a),a.index,a.group);else if(a?.type==="new-group"&&pe(a))e.newGroup(pe(a));else if(a?.type==="add-to-group"&&pe(a)&&typeof a.group==="string")e.addToGroup(pe(a),a.group);else if(a?.type==="remove-from-group"&&pe(a))e.removeFromGroup(pe(a));else if(a?.type==="move-group"&&typeof a.group==="string"&&Number.isSafeInteger(a.index))e.moveGroup(a.group,a.index);else if(a?.type==="rename-group"&&typeof a.group==="string"&&typeof a.name==="string")e.renameGroup(a.group,ge(a.name));else if(a?.type==="recolor-group"&&typeof a.group==="string"&&typeof a.color==="string")e.recolorGroup(a.group,a.color);else if(a?.type==="ungroup"&&typeof a.group==="string")e.ungroup(a.group);else if(a?.type==="close-group"&&typeof a.group==="string")e.closeGroup(a.group);else if(a?.type==="rename-tab"&&typeof a.tab==="string"&&typeof a.name==="string")e.renameTab(a.tab,ge(a.name));else if(a?.type==="close")e.close(String(a.terminal));else if(a?.type==="resize-split"&&typeof a.ratio==="number")c?.resizeSplit(String(a.split),a.ratio);else if(a?.type==="swap-panes"&&typeof a.tab==="string"&&typeof a.with==="string")c?.swapPanes(a.tab,a.with);else if(a?.type==="flip-split"&&typeof a.split==="string")c?.flipSplit(a.split);else if(a?.type==="rotate-split"&&typeof a.split==="string")c?.rotateSplit(a.split);else if(a?.type==="new-tab")c?.newTab(y);else if(a?.type==="select-tab")c?.selectTab(String(a.tab));else if(a?.type==="add-to-view"&&(a.edge===void 0||Wi.has(a.edge)))c?.addToView(String(a.tab),y,a.edge);else if(a?.type==="separate"&&typeof a.tab==="string")c?.separate(a.tab);else if(a?.type==="remove-from-view")c?.removeFromView(String(a.tab));else if(a?.type==="focus")c?.focus(String(a.terminal));else if(a?.type==="select"){let t=Ni(a);if(t!==void 0)c?.select(String(a.terminal),t??void 0)}else if(a?.type==="visibility")c?.visible(a.visible===!0);else if(a?.type==="name"&&typeof a.name==="string")c?.rename(ge(a.name));else if(a?.type==="follow")c?.follow(Number.isSafeInteger(a.member)?a.member:void 0);else if(a?.type==="scroll"&&Number.isSafeInteger(a.offset)&&a.offset>=0){let t=String(a.terminal);if(e.terminal(t))c?.scroll(t,a.offset)}else if(a?.type==="add-command"||a?.type==="update-command"){let t=Qe(a);if(!t)return;if(a.type==="add-command")e.addCommand(t);else e.updateCommand(String(a.id),t)}else if(a?.type==="remove-command")e.removeCommand(String(a.id));else if(a?.type==="set-min-window"){let t=Ae(a);if(t)e.setMinWindow(t)}},onClose:()=>c?.leave()}})),s.get("/ws/terminals/:id",async(c,f)=>e.terminal(c.req.param("id"))?f():c.text("Not Found",404),Ne((c)=>{let f=e.terminal(c.req.param("id")),a;return{onOpen:(y,t)=>{a=f.attach((n)=>n.type==="output"?Mt(t,n.data):Mt(t,JSON.stringify(n.type==="resize"?{type:"resize",...n.size}:n)))},onMessage:(y)=>{let t=Kr(y.data);if(t?.type==="input"&&typeof t.data==="string")f.write(t.data);else if(t?.type==="reset")f.reset();else if(t?.type==="resize"){let n=Ii(t);if(n)a?.resize(n)}},onClose:()=>a?.detach()}})),s.post("/api/uploads",Er({maxSize:Vr,onError:(c)=>c.text("Payload Too Large",413)}),async(c)=>{let a=(await c.req.formData().catch(()=>{return}))?.getAll("files").filter((y)=>y instanceof File)??[];if(a.length===0)return c.text("Bad Request",400);return c.json({paths:await r.save(a)})}),s}import{parseArgs as rs}from"util";var Jr="0.28.0";import{basename as Ui}from"path";var Ke=["zsh","bash","fish"];function Zr(e){return Ke.includes(e)}function es(e=process.env.SHELL??""){return Ui(e)}var ji={zsh:`# Accompany \u306E\u30B7\u30A7\u30EB\u306E\u7D71\u5408\uFF08OSC 133\uFF09\u3002~/.zshrc \u306B eval "$(accompany activate zsh)" \u3068\u66F8\u3044\u3066\u8AAD\u307F\u8FBC\u3080
40
40
  autoload -Uz add-zsh-hook
41
41
  _accompany_hook() {
42
42
  local code=$?
@@ -138,7 +138,7 @@ ${Pt}`),process.exit(1);return r}var ke=65535;function zi(e){let r;try{r=rs({arg
138
138
 
139
139
  ${Ye}`),process.exit(1)}if(r.values.help)console.log(Ye),process.exit(0);if(r.positionals.length>1)console.error(`Accompany: activate \u306B\u6E21\u305B\u308B\u30B7\u30A7\u30EB\u306F 1 \u3064\u3067\u3059: ${r.positionals.join(" ")}
140
140
 
141
- ${Ye}`),process.exit(1);let s=r.positionals[0],c=s??es();if(!Zr(c)){let f=`\u5BFE\u5FDC\u3057\u3066\u3044\u308B\u30B7\u30A7\u30EB: ${Ke.join("\u30FB")}`;console.error(s===void 0?`Accompany: $SHELL\uFF08${process.env.SHELL??"\u672A\u8A2D\u5B9A"}\uFF09\u306E\u30B7\u30A7\u30EB\u306B\u306F\u5BFE\u5FDC\u3057\u3066\u3044\u307E\u305B\u3093\u3002accompany activate zsh \u306E\u3088\u3046\u306B\u30B7\u30A7\u30EB\u3092\u6307\u5B9A\u3057\u3066\u304F\u3060\u3055\u3044\uFF08${f}\uFF09`:`Accompany: \u77E5\u3089\u306A\u3044\u30B7\u30A7\u30EB\u3067\u3059: ${c}\uFF08${f}\uFF09`),process.exit(1)}process.stdout.write(ts(c)),process.exit(0)}function is(e){let r=e instanceof Error?e.message:String(e),s=e?.code;if(s==="ERR_PARSE_ARGS_UNKNOWN_OPTION"){let c=r.match(/Unknown option '([^']+)'/)?.[1];if(c)return`\u77E5\u3089\u306A\u3044\u30AA\u30D7\u30B7\u30E7\u30F3\u3067\u3059: ${c}`}if(s==="ERR_PARSE_ARGS_INVALID_OPTION_VALUE"){let c=r.match(/Option '(-[^\s']+)/)?.[1];if(c&&/argument missing|argument is ambiguous/.test(r))return`${c} \u306B\u306F\u5024\u304C\u8981\u308A\u307E\u3059`;if(c&&/does not take an argument/.test(r))return`${c} \u306F\u5024\u3092\u53D6\u308A\u307E\u305B\u3093`}return r}var ns=Qt("{\"index\":\"public/index.html\",\"files\":[{\"input\":\"public/index.html\",\"path\":\"./index-y4w7v5jm.js\",\"loader\":\"js\",\"isEntry\":true,\"headers\":{\"etag\":\"3gS8ZzslEpQ\",\"content-type\":\"text/javascript;charset=utf-8\"}},{\"input\":\"public/index.html\",\"path\":\"public/index.html\",\"loader\":\"html\",\"isEntry\":true,\"headers\":{\"etag\":\"CrKNIMuV5oI\",\"content-type\":\"text/html;charset=utf-8\"}},{\"input\":\"public/index.html\",\"path\":\"./index-e5af4q9z.css\",\"loader\":\"css\",\"isEntry\":true,\"headers\":{\"etag\":\"LgVqL2T3Cb8\",\"content-type\":\"text/css;charset=utf-8\"}},{\"input\":\"public/favicon.png\",\"path\":\"./favicon-0wwf4h3c.png\",\"loader\":\"file\",\"isEntry\":false,\"headers\":{\"etag\":\"YHwcDyQxo4w\",\"content-type\":\"image/png\"}},{\"input\":\"public/apple-touch-icon.png\",\"path\":\"./apple-touch-icon-ehhpc4a9.png\",\"loader\":\"file\",\"isEntry\":false,\"headers\":{\"etag\":\"LjHRtszkCok\",\"content-type\":\"image/png\"}}]}");import{createHash as qi}from"crypto";import{chmodSync as Xi,mkdirSync as Vi,readFileSync as Qi,renameSync as Ki,rmSync as os,writeFileSync as Yi}from"fs";import{homedir as Ji}from"os";import{dirname as Zi,isAbsolute as cs,join as Ot}from"path";var Le=["blue","red","yellow","green","pink","purple","cyan","grey"];function Je(e){return typeof e==="string"&&Le.includes(e)}var as=448,en=384;function ls(e=process.env){if(e.ACCOMPANY_STATE_DIR)return e.ACCOMPANY_STATE_DIR;let r=e.XDG_STATE_HOME&&cs(e.XDG_STATE_HOME)?e.XDG_STATE_HOME:Ot(Ji(),".local","state");return Ot(r,"accompany")}class It{dir;file;constructor(e,r){this.dir=e;let s=qi("sha256").update(r).digest("hex").slice(0,16);this.file=Ot(e,"sessions",`${s}.json`)}load(){let e;try{e=Qi(this.file,"utf8")}catch(r){if(r.code!=="ENOENT")console.warn(`Accompany: \u4FDD\u5B58\u3057\u305F\u753B\u9762\u3092\u8AAD\u3081\u307E\u305B\u3093\u3067\u3057\u305F: ${r}`);return}try{let r=tn(JSON.parse(e));if(!r)console.warn(`Accompany: \u4FDD\u5B58\u3057\u305F\u753B\u9762\u306E\u5F62\u5F0F\u304C\u6B63\u3057\u304F\u306A\u3044\u305F\u3081\u3001\u65B0\u3057\u304F\u59CB\u3081\u307E\u3059: ${this.file}`);return r}catch(r){console.warn(`Accompany: \u4FDD\u5B58\u3057\u305F\u753B\u9762\u3092\u8AAD\u3081\u306A\u3044\u305F\u3081\u3001\u65B0\u3057\u304F\u59CB\u3081\u307E\u3059: ${r}`)}}save(e){let r=Zi(this.file);for(let c of[this.dir,r])Vi(c,{recursive:!0,mode:as}),Xi(c,as);let s=`${this.file}.${process.pid}.tmp`;os(s,{force:!0});try{Yi(s,JSON.stringify(e),{mode:en,flag:"wx"}),Ki(s,this.file)}catch(c){throw os(s,{force:!0}),c}}}function tn(e){if(!ne(e)||!Array.isArray(e.tabs)||e.tabs.length===0)return;let r=e.version===2?sn(e.tabs):e.version===1?nn(e.tabs):void 0;if(!r)return;let s=ne(e.terminals)?e.terminals:{},c={};for(let l of r){let p=s[l];c[l]={...ne(p)&&an(p)?ye({cols:p.cols,rows:p.rows}):{cols:80,rows:24},scrollback:ne(p)&&typeof p.scrollback==="string"?p.scrollback:"",initial:ne(p)&&p.initial===!0,...ne(p)&&on(p.cwd)&&{cwd:p.cwd}}}let f=[];for(let l of Array.isArray(e.commands)?e.commands:[]){let p=ne(l)&&Ze(l.id)&&Qe(l);if(p&&!f.some((g)=>g.id===l.id))f.push({id:l.id,...p})}let a=ne(e.names)?e.names:{},y={};for(let l of r){let p=typeof a[l]==="string"?ge(a[l]):void 0;if(p!==void 0)y[l]=p}let t=rn(e.groups,r),n=Ae(e.minWindow);return{version:2,cwd:String(e.cwd??""),tabs:r,names:y,groups:t,terminals:c,commands:f,...n&&{minWindow:n}}}function rn(e,r){let s=[],c=new Set;for(let f of Array.isArray(e)?e:[]){if(!ne(f)||!Ze(f.id)||!Je(f.color)||s.some((t)=>t.id===f.id))continue;let a=(Array.isArray(f.tabs)?f.tabs:[]).filter((t)=>typeof t==="string"&&r.includes(t)&&!c.has(t));if(a.length===0)continue;for(let t of a)c.add(t);let y=typeof f.name==="string"?ge(f.name):void 0;s.push({id:f.id,...y!==void 0&&{name:y},color:f.color,tabs:a})}return s}function sn(e){let r=[];for(let s of e){if(!Ze(s)||r.includes(s))return;r.push(s)}return r}function nn(e){let r=new Set,s=[];for(let c of e)if(!ne(c)||!hs(c.layout,r,s,0))return;return s}function hs(e,r,s,c){if(!ne(e)||c>100)return!1;if(e.type==="terminal"){if(!Ze(e.id)||r.has(e.id))return!1;return r.add(e.id),s.push(e.id),!0}if(e.type!=="split"||typeof e.id!=="string"||!/^split-[1-9]\d{0,8}$/.test(e.id))return!1;if(r.has(e.id))return!1;if(r.add(e.id),!Array.isArray(e.children)||e.children.length!==2)return!1;return e.children.every((f)=>hs(f,r,s,c+1))}function ne(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}function Ze(e){return typeof e==="string"&&/^[1-9]\d{0,8}$/.test(e)}function on(e){return typeof e==="string"&&cs(e)&&!e.includes("\x00")}function an(e){let r=(s)=>Number.isSafeInteger(s)&&s>=1;return r(e.cols)&&r(e.rows)}import{isAbsolute as cn,resolve as ln}from"path";var hn=0.9;function ps({allowVscodeWebview:e=!1}={}){return`frame-ancestors 'self'${e?" vscode-webview: vscode-file:":""}`}async function _s(e,r){let{files:s}=e;if(s?.some((f)=>!cn(f.path)))return us(fn(s),r);let c=Bun.serve({hostname:"127.0.0.1",port:0,development:!1,reusePort:!1,routes:{"/":e},fetch:()=>new Response("Not Found",{status:404})});try{return await us(un(c.url),r)}finally{c.stop(!0)}}async function us(e,r){let s=fs(await e("/"),r),c=new TextDecoder().decode(s.body),f=new Set([...c.matchAll(/(?:src|href)="(\/[^"]+)"/g)].map((y)=>y[1])),a={"/":ds(s)};for(let y of f){let t=fs(await e(y),r);if(!t.headers.has("cache-control"))t.headers.set("cache-control","public, max-age=31536000, immutable");a[y]=ds(t)}return a}function un(e){return async(r)=>{let s=await fetch(new URL(r,e));if(!s.ok)throw Error(`\u753B\u9762\u306E\u30D5\u30A1\u30A4\u30EB\u3092\u7528\u610F\u3067\u304D\u307E\u305B\u3093\u3067\u3057\u305F: ${r} (${s.status})`);return{body:new Uint8Array(await s.arrayBuffer()),headers:new Headers(s.headers)}}}function fn(e){return async(r)=>{let s=r==="/"?e.find((f)=>f.loader==="html"):e.find((f)=>f.loader!=="html"&&`/${f.path.replace(/^\.\//,"")}`===r);if(!s)throw Error(`\u753B\u9762\u306E\u30D5\u30A1\u30A4\u30EB\u3092\u7528\u610F\u3067\u304D\u307E\u305B\u3093\u3067\u3057\u305F: ${r}\uFF08\u56FA\u3081\u305F JS \u306E\u4E00\u89A7\u306B\u306A\u3044\uFF09`);return{body:await Bun.file(ln(import.meta.dir,s.path)).bytes(),headers:new Headers(s.headers)}}}function fs({body:e,headers:r},s){let c=new Headers(r);c.delete("content-length"),c.delete("date"),c.set("vary","Accept-Encoding"),c.set("content-security-policy",s);let f=Bun.gzipSync(e,{level:9});return{body:e,headers:c,gzip:f.byteLength<e.byteLength*hn?f:void 0}}function ds({body:e,gzip:r,headers:s}){let c=s.get("etag");return(f)=>{if(c&&f.headers.get("if-none-match")===c)return new Response(null,{status:304,headers:s});let a=/\bgzip\b/.test(f.headers.get("accept-encoding")??"");if(!r||!a)return new Response(e,{headers:s});let y=new Headers(s);return y.set("content-encoding","gzip"),new Response(r,{headers:y})}}var gs=3,ms=["#4fc3f7","#ffb74d","#81c784","#f06292","#ba68c8","#fff176"];class $t{shellCommand;terminals=new Map;order=[];tabNames=new Map;groups=new Map;tabGroups=new Map;nextGroupId=1;members=new Map;views=new Map;chosenNames=new Map;visibleMembers=new Set;nextMemberId=1;nextId=1;nextSplitId=1;titlesQueued=!1;nextCommandId=1;stopping=!1;initialTerminal;commands=[];minWindow;savedMinWindow;onChange=()=>{};constructor(e,r,s,c){this.shellCommand=r;if(this.savedMinWindow=s?.minWindow,this.minWindow=c??s?.minWindow??Ur,!s){this.initialTerminal=this.createTerminal(e??r);return}for(let a of s.tabs){let y=s.terminals[a];if(y.initial)this.initialTerminal=a;let t=y.initial&&e!==void 0;this.addTerminal(a,t?e:r,y,t?void 0:y.cwd)}for(let[a,y]of Object.entries(s.names??{}))this.tabNames.set(a,y);for(let a of s.groups??[]){this.groups.set(a.id,{color:a.color,...a.name!==void 0&&{name:a.name}});for(let y of a.tabs)this.tabGroups.set(y,a.id)}this.nextGroupId=Math.max(0,...[...this.groups.keys()].map(Number))+1,this.normalizeGroups(),this.commands=s.commands;let f=(a)=>Math.max(0,...a.map(Number))+1;if(this.nextId=f(this.order),this.nextCommandId=f(this.commands.map((a)=>a.id)),e&&!this.initialTerminal)this.initialTerminal=this.createTerminal(e)}async snapshot(e,{refreshCwd:r=!1}={}){let s={};return await Promise.all([...this.terminals].map(async([c,f])=>{let[a,y]=await Promise.all([f.snapshot(),f.savedCwd({refresh:r})]);s[c]={...a,initial:c===this.initialTerminal,...y&&{cwd:y}}})),{version:2,cwd:e,tabs:[...this.order],names:Object.fromEntries(this.tabNames),groups:[...this.groups].map(([c,{name:f,color:a}])=>({id:c,...f!==void 0&&{name:f},color:a,tabs:this.order.filter((y)=>this.tabGroups.get(y)===c)})),terminals:s,commands:this.commands,...this.savedMinWindow&&{minWindow:this.savedMinWindow}}}terminal(e){return this.terminals.get(e)}join(e){let r=this.nextMemberId++,s={id:r,name:Ht(r),color:ms[(r-1)%ms.length],selections:{}};this.members.set(e,s);let c=this.initialTerminal&&this.terminals.has(this.initialTerminal)?this.initialTerminal:this.order[0];return this.views.set(s,{layout:oe(c),splits:[]}),e({type:"welcome",you:r}),e(this.tabsEvent()),e(Ft(this.views.get(s))),e(this.titlesEvent()),e({type:"status",statuses:this.statuses()}),e(this.commandsEvent()),e(this.minWindowEvent()),this.notifyPresence(),{newTab:async(f)=>{let a=f??s.terminal,y=a===void 0?void 0:await this.terminals.get(a)?.cwd();if(!this.views.has(s))return;let t=this.createTerminal(this.shellCommand,y);this.notifyTabs(),this.changeView(s,(n)=>({layout:oe(t),splits:vs(n)}))},split:async(f,a)=>{if(!this.canAdd(s))return;let y=await this.terminals.get(f)?.cwd();if(!this.canAdd(s))return;let t=this.createTerminal(this.shellCommand,y);this.notifyTabs();let n=this.changeView(s,(l)=>({...l,layout:this.placeBeside(l.layout,oe(t),f,a)}));if(n)this.gather(n.layout,t)},selectTab:(f)=>{if(!this.terminals.has(f))return;this.changeView(s,(a)=>{if(ee(a.layout).includes(f))return;let y=a.splits.find((n)=>ee(n).includes(f)),t=vs(a).filter((n)=>n!==y);return{layout:y??oe(f),splits:t}})},addToView:(f,a,y)=>{if(!this.terminals.has(f)||!this.canAdd(s))return;let t=this.changeView(s,(n)=>{if(ee(n.layout).includes(f))return;return{layout:y?pn(n.layout,oe(f),y,`split-${this.nextSplitId++}`):this.placeBeside(n.layout,oe(f),a,"row"),splits:ys(n.splits,f)}});if(t)this.gather(t.layout,f)},removeFromView:(f)=>{this.changeView(s,(a)=>{let y=ee(a.layout);return y.length>1&&y.includes(f)?{...a,layout:Re(a.layout,f)}:void 0})},separate:(f)=>{this.changeView(s,(a)=>{let y=ee(a.layout);if(y.includes(f)){if(y.length<2)return;let n=s.terminal!==void 0&&y.includes(s.terminal)?s.terminal:f;return{...a,layout:oe(n)}}let t=a.splits.find((n)=>ee(n).includes(f));return t?{...a,splits:a.splits.filter((n)=>n!==t)}:void 0})},resizeSplit:(f,a)=>{this.changeView(s,(y)=>et(y,(t)=>me(t,f)?_n(t,f,Math.min(0.9,Math.max(0.1,a))):void 0))},swapPanes:(f,a)=>{let y=this.changeView(s,(t)=>et(t,(n)=>{let l=ee(n);return f!==a&&l.includes(f)&&l.includes(a)?ws(n,f,a):void 0}));if(y)this.gatherSplitOf(y,(t)=>ee(t).includes(f))},flipSplit:(f)=>{let a=this.changeView(s,(y)=>et(y,(t)=>me(t,f)?tt(t,f,(n)=>({...n,ratio:1-n.ratio,children:[n.children[1],n.children[0]]})):void 0));if(a)this.gatherSplitOf(a,(y)=>me(y,f)!==void 0)},rotateSplit:(f)=>{let a=this.changeView(s,(y)=>et(y,(t)=>me(t,f)?Te(tt(t,f,(n)=>({...n,direction:n.direction==="row"?"column":"row"}))):void 0));if(a)this.gatherSplitOf(a,(y)=>me(y,f)!==void 0)},focus:(f)=>{s.terminal=f,this.notifyPresence(),this.updateSeen()},visible:(f)=>{if(f)this.visibleMembers.add(s);else this.visibleMembers.delete(s);this.updateSeen()},select:(f,a)=>{if(a&&this.terminals.has(f))s.selections[f]=a;else if(!a&&Object.hasOwn(s.selections,f))delete s.selections[f];else return;this.notifyPresence()},follow:(f)=>{let a=[...this.members.values()].find((t)=>t.id===f&&t!==s);if(s.following===a?.id)return;let y=s.following;if(s.following=a?.id,this.forgetScroll(y),this.notifyPresence(),a)this.setView(s,this.views.get(a))},scroll:(f,a)=>{if(s.scroll?.terminal===f&&s.scroll.offset===a)return;s.scroll={terminal:f,offset:a},this.notifyPresence()},rename:(f)=>{if(this.chosenNames.get(s)===f)return;if(f===void 0)this.chosenNames.delete(s);else this.chosenNames.set(s,f);this.notifyPresence()},leave:()=>{this.members.delete(e),this.views.delete(s),this.chosenNames.delete(s),this.visibleMembers.delete(s),this.members.forEach((f)=>{if(f.following===s.id)f.following=void 0}),this.forgetScroll(s.following),this.notifyPresence()}}}close(e){if(this.stopping)return;let r=this.order.indexOf(e);if(r<0||this.order.length===1)return;this.order.splice(r,1),this.removeTerminal(e);let s=this.order[Math.min(r,this.order.length-1)];this.notifyTabs();for(let[c,f]of this.members){let a=this.views.get(f);if(!a)continue;let y=ee(a.layout).includes(e),t=a.splits.some((g)=>ee(g).includes(e));if(!y&&!t)continue;let n=ys(a.splits,e),l=a.layout;if(y){let g=Re(a.layout,e),d=g?void 0:n.find((_)=>ee(_).includes(s));l=g??d??oe(s),n=n.filter((_)=>_!==d)}let p={layout:l,splits:n};this.views.set(f,p),c(Ft(p))}this.updateSeen()}moveTab(e,r,s){if(!this.validTabs(e))return;let c=this.order.filter((n)=>e.includes(n)),f=this.order.filter((n)=>!e.includes(n));if(!Number.isSafeInteger(r)||r<0||r>f.length)return;if(typeof s==="string"&&!this.groups.has(s))return;let a=r;if(typeof s==="string"){let n=f.flatMap((l,p)=>this.tabGroups.get(l)===s?[p]:[]);if(n.length>0)a=Math.min(Math.max(r,n[0]),n.at(-1)+1)}let y=[...f.slice(0,a),...c,...f.slice(a)];if(!(s!==void 0&&c.some((n)=>(this.tabGroups.get(n)??null)!==s))&&y.every((n,l)=>n===this.order[l]))return;this.order=y;for(let n of c)if(s===null)this.tabGroups.delete(n);else if(s!==void 0)this.tabGroups.set(n,s);this.notifyTabs()}newGroup(e){if(!this.validTabs(e))return;let r=new Set([...this.groups.values()].map((f)=>f.color)),s=Le.find((f)=>!r.has(f))??Le[this.groups.size%Le.length],c=String(this.nextGroupId++);this.groups.set(c,{color:s});for(let f of e)this.tabGroups.set(f,c);this.notifyTabs()}addToGroup(e,r){if(!this.validTabs(e)||!this.groups.has(r)||e.every((f)=>this.tabGroups.get(f)===r))return;let s=this.order.filter((f)=>e.includes(f));this.order=this.order.filter((f)=>!e.includes(f));let c=this.order.findLastIndex((f)=>this.tabGroups.get(f)===r);this.order.splice(c+1,0,...s);for(let f of s)this.tabGroups.set(f,r);this.notifyTabs()}removeFromGroup(e){if(!this.validTabs(e))return;let r=!1;for(let s of this.order.filter((c)=>e.includes(c)).reverse()){let c=this.tabGroups.get(s);if(c===void 0)continue;r=!0,this.tabGroups.delete(s);let f=this.order.indexOf(s);this.order.splice(f,1);let a=this.order.findLastIndex((y)=>this.tabGroups.get(y)===c);this.order.splice(a<0?f:a+1,0,s)}if(r)this.notifyTabs()}moveGroup(e,r){if(!this.groups.has(e))return;let s=this.order.filter((a)=>this.tabGroups.get(a)===e),c=this.order.filter((a)=>this.tabGroups.get(a)!==e);if(!Number.isSafeInteger(r)||r<0||r>c.length)return;let f=[...c.slice(0,r),...s,...c.slice(r)];if(f.every((a,y)=>a===this.order[y]))return;this.order=f,this.notifyTabs()}renameGroup(e,r){let s=this.groups.get(e);if(!s||s.name===r)return;if(r===void 0)delete s.name;else s.name=r;this.notifyTabs()}recolorGroup(e,r){let s=this.groups.get(e);if(!s||!Je(r)||s.color===r)return;s.color=r,this.notifyTabs()}ungroup(e){if(!this.groups.delete(e))return;for(let[r,s]of this.tabGroups)if(s===e)this.tabGroups.delete(r);this.notifyTabs()}closeGroup(e){let r=this.order.filter((s)=>this.tabGroups.get(s)===e);if(r.length===0||r.length===this.order.length)return;for(let s of r)this.close(s)}renameTab(e,r){if(!this.terminals.has(e)||this.tabNames.get(e)===r)return;if(r===void 0)this.tabNames.delete(e);else this.tabNames.set(e,r);this.notifyTabs()}addCommand(e){if(this.commands.length>=Gr)return;this.commands.push({id:String(this.nextCommandId++),...e}),this.notifyCommands()}updateCommand(e,r){let s=this.commands.findIndex((c)=>c.id===e);if(s<0)return;this.commands[s]={id:e,...r},this.notifyCommands()}removeCommand(e){let r=this.commands.length;if(this.commands=this.commands.filter((s)=>s.id!==e),this.commands.length!==r)this.notifyCommands()}setMinWindow(e){if(this.savedMinWindow=e,e.width===this.minWindow.width&&e.height===this.minWindow.height)return this.onChange();this.minWindow=e;let r=this.minWindowEvent();this.members.forEach((s,c)=>c(r)),this.onChange()}async killAll(){this.stopping=!0,await Promise.all([...this.terminals.values()].map((e)=>e.kill()))}createTerminal(e,r){let s=String(this.nextId++);return this.addTerminal(s,e,void 0,r),s}addTerminal(e,r,s,c){this.order.push(e),this.terminals.set(e,new Lt(r,()=>this.close(e),()=>this.notifyTitles(),()=>this.notifyStatus(),{restored:s,onOutput:()=>this.onChange(),cwd:c}))}removeTerminal(e){this.terminals.get(e)?.kill(),this.terminals.delete(e),this.tabNames.delete(e),this.tabGroups.delete(e);let r=!1;for(let s of this.members.values()){if(!Object.hasOwn(s.selections,e))continue;delete s.selections[e],r=!0}if(r)this.notifyPresence()}canAdd(e){let r=this.views.get(e);return r!==void 0&&ee(r.layout).length<gs}validTabs(e){return e.length>0&&e.length<=gs&&new Set(e).size===e.length&&e.every((r)=>this.terminals.has(r))}placeBeside(e,r,s,c){let f=(a)=>({type:"split",id:`split-${this.nextSplitId++}`,direction:c,ratio:0.5,children:[a,r]});if(s===void 0||!ee(e).includes(s))return Te(f(e));return Te(bs(e,s,f(oe(s))))}changeView(e,r){let s=this.views.get(e),c=s&&r(s);if(!c)return;if(e.following!==void 0){let f=e.following;e.following=void 0,this.forgetScroll(f),this.notifyPresence()}return this.setView(e,c),c}gather(e,r){let s=ee(e);if(s.length<2)return;let c=this.order.find((n)=>s.includes(n)&&n!==r);if(c===void 0)return;let f=this.tabGroups.get(c),a=this.order.indexOf(c),y=[...this.order.slice(0,a).filter((n)=>!s.includes(n)),...s,...this.order.slice(a).filter((n)=>!s.includes(n))];if(s.every((n)=>this.tabGroups.get(n)===f)&&y.every((n,l)=>n===this.order[l]))return;this.order=y;for(let n of s)if(f===void 0)this.tabGroups.delete(n);else this.tabGroups.set(n,f);this.notifyTabs()}gatherSplitOf(e,r){let s=[e.layout,...e.splits].find(r);if(s)this.gather(s)}setView(e,r){let s=new Set,c=(f)=>{s.add(f),this.views.set(f,r);for(let[a,y]of this.members)if(y===f)a(Ft(r));for(let a of this.members.values())if(a.following===f.id&&!s.has(a))c(a)};c(e),this.updateSeen()}tabsEvent(){let e=(f)=>xe(f).replace(/\s/g,""),r=new Map(this.order.map((f)=>[f,e(this.tabNames.get(f)??Nt(f))])),s=this.order.map((f)=>{let a=this.tabNames.get(f);if(a===void 0)return{id:f,name:Nt(f)};let y=r.get(f),t=y.match(/^\u30BF\u30FC\u30DF\u30CA\u30EB(\d+)$/),n=t!==null&&t[1]!==f||/\(\u30BF\u30FC\u30DF\u30CA\u30EB\d+\)$/.test(y)||this.order.some((l)=>l!==f&&r.get(l)===y);return{id:f,name:n?`${a}\uFF08${Nt(f)}\uFF09`:a,custom:a}});for(let f of s){let a=this.tabGroups.get(f.id);if(a!==void 0)f.group=a}let c=[];for(let f of new Set(this.order.flatMap((a)=>this.tabGroups.get(a)??[]))){let{name:a,color:y}=this.groups.get(f);c.push({id:f,name:a??dn(f),...a!==void 0&&{custom:a},color:y})}return{type:"tabs",tabs:s,groups:c}}normalizeGroups(){for(let[c,f]of this.tabGroups)if(!this.groups.has(f)||!this.terminals.has(c))this.tabGroups.delete(c);let e=new Set(this.tabGroups.values());for(let c of this.groups.keys())if(!e.has(c))this.groups.delete(c);let r=new Set,s=[];for(let c of this.order){if(r.has(c))continue;let f=this.tabGroups.get(c),a=f===void 0?[c]:this.order.filter((y)=>this.tabGroups.get(y)===f);for(let y of a)r.add(y),s.push(y)}this.order=s}notifyTabs(){this.normalizeGroups();let e=this.tabsEvent();this.members.forEach((r,s)=>s(e)),this.onChange()}titlesEvent(){let e={};return this.terminals.forEach((r,s)=>{if(r.title)e[s]=r.title}),{type:"titles",titles:e}}notifyTitles(){if(this.titlesQueued)return;this.titlesQueued=!0,queueMicrotask(()=>{this.titlesQueued=!1;let e=this.titlesEvent();this.members.forEach((r,s)=>s(e))})}statuses(){let e={};return this.terminals.forEach((r,s)=>{if(r.status)e[s]=r.status}),e}notifyStatus(){this.clearSeen();let e=this.statuses();this.members.forEach((r,s)=>s({type:"status",statuses:e}))}updateSeen(){if(this.clearSeen())this.notifyStatus()}clearSeen(){let e=!1;for(let r of this.visibleMembers){let s=r.terminal,c=this.views.get(r)?.layout;if(s&&c&&ee(c).includes(s)&&this.terminals.get(s)?.markSeen())e=!0}return e}commandsEvent(){return{type:"commands",commands:this.commands}}minWindowEvent(){return{type:"min-window",...this.minWindow}}notifyCommands(){let e=this.commandsEvent();this.members.forEach((r,s)=>s(e)),this.onChange()}forgetScroll(e){let r=[...this.members.values()],s=r.find((c)=>c.id===e);if(s&&!r.some((c)=>c.following===e))s.scroll=void 0}updateNames(){let e=[...this.members.values()],r=(c)=>this.chosenNames.get(c)??Ht(c.id),s=new Map;for(let c of e)s.set(xe(r(c)),(s.get(xe(r(c)))??0)+1);for(let c of e){let f=r(c);c.name=s.get(xe(f))>1?`${f}\uFF08${Ht(c.id)}\uFF09`:f}}notifyPresence(){this.updateNames();let e=[...this.members.values()];this.members.forEach((r,s)=>s({type:"presence",members:e}))}}function Ht(e){return`\u53C2\u52A0\u8005 ${e}`}function Nt(e){return`\u30BF\u30FC\u30DF\u30CA\u30EB ${e}`}function dn(e){return`\u30B0\u30EB\u30FC\u30D7 ${e}`}function Ft(e){return{type:"view",layout:e.layout,splits:e.splits}}function vs(e){return e.layout.type==="split"?[...e.splits,e.layout]:e.splits}function ys(e,r){return e.flatMap((s)=>{if(!ee(s).includes(r))return[s];let c=Re(s,r);return c&&c.type==="split"?[c]:[]})}function et(e,r){let s=r(e.layout);if(s)return{...e,layout:s};for(let[c,f]of e.splits.entries()){let a=r(f);if(a)return{...e,splits:e.splits.with(c,a)}}return}function pn(e,r,s,c){return Te({type:"split",id:c,direction:s==="bottom"?"column":"row",ratio:0.5,children:s==="left"?[r,e]:[e,r]})}function oe(e){return{type:"terminal",id:e}}function ee(e){return e.type==="terminal"?[e.id]:e.children.flatMap(ee)}function me(e,r){if(e.type==="terminal")return;return e.id===r?e:me(e.children[0],r)??me(e.children[1],r)}function Ut(e,r){return{...e,children:[r(e.children[0]),r(e.children[1])]}}function bs(e,r,s){if(e.type==="terminal")return e.id===r?s:e;return Ut(e,(c)=>bs(c,r,s))}function Re(e,r){if(e.type==="terminal")return e.id===r?void 0:e;let s=Re(e.children[0],r),c=Re(e.children[1],r);if(!s||!c)return s??c;return{...e,children:[s,c]}}function _n(e,r,s){return tt(e,r,(c)=>({...c,ratio:s}))}function tt(e,r,s){if(e.type==="terminal")return e;if(e.id===r)return s(e);return Ut(e,(c)=>tt(c,r,s))}function ws(e,r,s){if(e.type==="terminal")return e.id===r?oe(s):e.id===s?oe(r):e;return Ut(e,(c)=>ws(c,r,s))}function Te(e){if(e.type==="terminal")return e;let r=e.children.map(Te),[s,c]=r.map((f)=>Wt(f,e.direction));return{...e,ratio:s/(s+c),children:r}}function Wt(e,r){if(e.type==="terminal"||e.direction!==r)return 1;return Wt(e.children[0],r)+Wt(e.children[1],r)}var{values:it,positionals:Ss}=ss(),st=it["min-window"],Cs=st===void 0?void 0:jr(st);if(st!==void 0&&!Cs)console.error(`Accompany: --min-window \u306F\u300C\u5E45x\u9AD8\u3055\u300D\uFF08\u4F8B: 480x1080\u3002\u5E45 0\u301C${Ee.width}\u3001\u9AD8\u3055 0\u301C${Ee.height} \u306E\u6574\u6570\uFF09\u3067\u6307\u5B9A\u3057\u3066\u304F\u3060\u3055\u3044: ${st}`),process.exit(1);var gn=[process.env.SHELL??"/bin/sh"],xs=process.cwd(),Gt=new It(ls(),xs),Es=it.fresh?void 0:Gt.load(),Be=new $t(Ss.length>0?Ss:void 0,gn,Es,Cs),qt=new Dt,mn=Yr(Be,qt),As=10,ve=Number(it.port),vn=await _s(ns,ps({allowVscodeWebview:it["allow-vscode-webview"]})),be;for(let e=ve;e<ve+As&&e<=ke;e++)try{be=Bun.serve({hostname:"127.0.0.1",port:e,development:!1,reusePort:!1,routes:vn,fetch:mn.fetch,websocket:{...ut,perMessageDeflate:!0}});break}catch(r){if(r.code!=="EADDRINUSE")throw r}if(!be){let e=Math.min(ve+As-1,ke);console.error(`Accompany: \u30DD\u30FC\u30C8 ${ve}\u301C${e} \u306F\u3059\u3079\u3066\u4F7F\u308F\u308C\u3066\u3044\u307E\u3059\u3002--port \u3067\u5225\u306E\u30DD\u30FC\u30C8\u3092\u6307\u5B9A\u3057\u3066\u304F\u3060\u3055\u3044`),await Be.killAll(),qt.remove(),process.exit(1)}if(be.port!==ve&&ve!==0)console.log(`Accompany: \u30DD\u30FC\u30C8 ${ve} \u306F\u4F7F\u308F\u308C\u3066\u3044\u308B\u306E\u3067\u3001${be.port} \u3067\u5F85\u3061\u53D7\u3051\u307E\u3059`);var jt=!1;async function ks({stopping:e=!1}={}){try{Gt.save(await Be.snapshot(xs,{refreshCwd:e})),jt=!1}catch(r){if(!jt)console.warn(`Accompany: \u753B\u9762\u3092\u4FDD\u5B58\u3067\u304D\u307E\u305B\u3093\u3067\u3057\u305F: ${r}`);jt=!0}}var yn=1000,rt,zt=!1;Be.onChange=()=>{if(rt||zt)return;rt=setTimeout(()=>{rt=void 0,ks()},yn)};var bn=3000;for(let e of["SIGINT","SIGTERM","SIGHUP"])process.on(e,async()=>{if(zt)return;zt=!0,clearTimeout(rt),await Promise.race([ks({stopping:!0}),Bun.sleep(bn)]),await Be.killAll(),qt.remove(),process.exit(0)});console.log(`Accompany: http://${be.hostname}:${be.port}`);if(Es)console.log(`\u524D\u56DE\u306E\u753B\u9762\u3092\u623B\u3057\u307E\u3057\u305F\uFF08\u65B0\u3057\u304F\u59CB\u3081\u308B\u3068\u304D\u306F --fresh \u3092\u4ED8\u3051\u3066\u8D77\u52D5\u3059\u308B\uFF09: ${Gt.file}`);
141
+ ${Ye}`),process.exit(1);let s=r.positionals[0],c=s??es();if(!Zr(c)){let f=`\u5BFE\u5FDC\u3057\u3066\u3044\u308B\u30B7\u30A7\u30EB: ${Ke.join("\u30FB")}`;console.error(s===void 0?`Accompany: $SHELL\uFF08${process.env.SHELL??"\u672A\u8A2D\u5B9A"}\uFF09\u306E\u30B7\u30A7\u30EB\u306B\u306F\u5BFE\u5FDC\u3057\u3066\u3044\u307E\u305B\u3093\u3002accompany activate zsh \u306E\u3088\u3046\u306B\u30B7\u30A7\u30EB\u3092\u6307\u5B9A\u3057\u3066\u304F\u3060\u3055\u3044\uFF08${f}\uFF09`:`Accompany: \u77E5\u3089\u306A\u3044\u30B7\u30A7\u30EB\u3067\u3059: ${c}\uFF08${f}\uFF09`),process.exit(1)}process.stdout.write(ts(c)),process.exit(0)}function is(e){let r=e instanceof Error?e.message:String(e),s=e?.code;if(s==="ERR_PARSE_ARGS_UNKNOWN_OPTION"){let c=r.match(/Unknown option '([^']+)'/)?.[1];if(c)return`\u77E5\u3089\u306A\u3044\u30AA\u30D7\u30B7\u30E7\u30F3\u3067\u3059: ${c}`}if(s==="ERR_PARSE_ARGS_INVALID_OPTION_VALUE"){let c=r.match(/Option '(-[^\s']+)/)?.[1];if(c&&/argument missing|argument is ambiguous/.test(r))return`${c} \u306B\u306F\u5024\u304C\u8981\u308A\u307E\u3059`;if(c&&/does not take an argument/.test(r))return`${c} \u306F\u5024\u3092\u53D6\u308A\u307E\u305B\u3093`}return r}var ns=Qt("{\"index\":\"public/index.html\",\"files\":[{\"input\":\"public/index.html\",\"path\":\"./index-tg9wkazb.js\",\"loader\":\"js\",\"isEntry\":true,\"headers\":{\"etag\":\"WjBpfLNKX6s\",\"content-type\":\"text/javascript;charset=utf-8\"}},{\"input\":\"public/index.html\",\"path\":\"public/index.html\",\"loader\":\"html\",\"isEntry\":true,\"headers\":{\"etag\":\"7zs875MY4DE\",\"content-type\":\"text/html;charset=utf-8\"}},{\"input\":\"public/index.html\",\"path\":\"./index-e5af4q9z.css\",\"loader\":\"css\",\"isEntry\":true,\"headers\":{\"etag\":\"LgVqL2T3Cb8\",\"content-type\":\"text/css;charset=utf-8\"}},{\"input\":\"public/favicon.png\",\"path\":\"./favicon-0wwf4h3c.png\",\"loader\":\"file\",\"isEntry\":false,\"headers\":{\"etag\":\"YHwcDyQxo4w\",\"content-type\":\"image/png\"}},{\"input\":\"public/apple-touch-icon.png\",\"path\":\"./apple-touch-icon-ehhpc4a9.png\",\"loader\":\"file\",\"isEntry\":false,\"headers\":{\"etag\":\"LjHRtszkCok\",\"content-type\":\"image/png\"}}]}");import{createHash as qi}from"crypto";import{chmodSync as Xi,mkdirSync as Vi,readFileSync as Qi,renameSync as Ki,rmSync as os,writeFileSync as Yi}from"fs";import{homedir as Ji}from"os";import{dirname as Zi,isAbsolute as cs,join as Ot}from"path";var Le=["blue","red","yellow","green","pink","purple","cyan","grey"];function Je(e){return typeof e==="string"&&Le.includes(e)}var as=448,en=384;function ls(e=process.env){if(e.ACCOMPANY_STATE_DIR)return e.ACCOMPANY_STATE_DIR;let r=e.XDG_STATE_HOME&&cs(e.XDG_STATE_HOME)?e.XDG_STATE_HOME:Ot(Ji(),".local","state");return Ot(r,"accompany")}class It{dir;file;constructor(e,r){this.dir=e;let s=qi("sha256").update(r).digest("hex").slice(0,16);this.file=Ot(e,"sessions",`${s}.json`)}load(){let e;try{e=Qi(this.file,"utf8")}catch(r){if(r.code!=="ENOENT")console.warn(`Accompany: \u4FDD\u5B58\u3057\u305F\u753B\u9762\u3092\u8AAD\u3081\u307E\u305B\u3093\u3067\u3057\u305F: ${r}`);return}try{let r=tn(JSON.parse(e));if(!r)console.warn(`Accompany: \u4FDD\u5B58\u3057\u305F\u753B\u9762\u306E\u5F62\u5F0F\u304C\u6B63\u3057\u304F\u306A\u3044\u305F\u3081\u3001\u65B0\u3057\u304F\u59CB\u3081\u307E\u3059: ${this.file}`);return r}catch(r){console.warn(`Accompany: \u4FDD\u5B58\u3057\u305F\u753B\u9762\u3092\u8AAD\u3081\u306A\u3044\u305F\u3081\u3001\u65B0\u3057\u304F\u59CB\u3081\u307E\u3059: ${r}`)}}save(e){let r=Zi(this.file);for(let c of[this.dir,r])Vi(c,{recursive:!0,mode:as}),Xi(c,as);let s=`${this.file}.${process.pid}.tmp`;os(s,{force:!0});try{Yi(s,JSON.stringify(e),{mode:en,flag:"wx"}),Ki(s,this.file)}catch(c){throw os(s,{force:!0}),c}}}function tn(e){if(!ne(e)||!Array.isArray(e.tabs)||e.tabs.length===0)return;let r=e.version===2?sn(e.tabs):e.version===1?nn(e.tabs):void 0;if(!r)return;let s=ne(e.terminals)?e.terminals:{},c={};for(let l of r){let p=s[l];c[l]={...ne(p)&&an(p)?ye({cols:p.cols,rows:p.rows}):{cols:80,rows:24},scrollback:ne(p)&&typeof p.scrollback==="string"?p.scrollback:"",initial:ne(p)&&p.initial===!0,...ne(p)&&on(p.cwd)&&{cwd:p.cwd}}}let f=[];for(let l of Array.isArray(e.commands)?e.commands:[]){let p=ne(l)&&Ze(l.id)&&Qe(l);if(p&&!f.some((g)=>g.id===l.id))f.push({id:l.id,...p})}let a=ne(e.names)?e.names:{},y={};for(let l of r){let p=typeof a[l]==="string"?ge(a[l]):void 0;if(p!==void 0)y[l]=p}let t=rn(e.groups,r),n=Ae(e.minWindow);return{version:2,cwd:String(e.cwd??""),tabs:r,names:y,groups:t,terminals:c,commands:f,...n&&{minWindow:n}}}function rn(e,r){let s=[],c=new Set;for(let f of Array.isArray(e)?e:[]){if(!ne(f)||!Ze(f.id)||!Je(f.color)||s.some((t)=>t.id===f.id))continue;let a=(Array.isArray(f.tabs)?f.tabs:[]).filter((t)=>typeof t==="string"&&r.includes(t)&&!c.has(t));if(a.length===0)continue;for(let t of a)c.add(t);let y=typeof f.name==="string"?ge(f.name):void 0;s.push({id:f.id,...y!==void 0&&{name:y},color:f.color,tabs:a})}return s}function sn(e){let r=[];for(let s of e){if(!Ze(s)||r.includes(s))return;r.push(s)}return r}function nn(e){let r=new Set,s=[];for(let c of e)if(!ne(c)||!hs(c.layout,r,s,0))return;return s}function hs(e,r,s,c){if(!ne(e)||c>100)return!1;if(e.type==="terminal"){if(!Ze(e.id)||r.has(e.id))return!1;return r.add(e.id),s.push(e.id),!0}if(e.type!=="split"||typeof e.id!=="string"||!/^split-[1-9]\d{0,8}$/.test(e.id))return!1;if(r.has(e.id))return!1;if(r.add(e.id),!Array.isArray(e.children)||e.children.length!==2)return!1;return e.children.every((f)=>hs(f,r,s,c+1))}function ne(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}function Ze(e){return typeof e==="string"&&/^[1-9]\d{0,8}$/.test(e)}function on(e){return typeof e==="string"&&cs(e)&&!e.includes("\x00")}function an(e){let r=(s)=>Number.isSafeInteger(s)&&s>=1;return r(e.cols)&&r(e.rows)}import{isAbsolute as cn,resolve as ln}from"path";var hn=0.9;function ps({allowVscodeWebview:e=!1}={}){return`frame-ancestors 'self'${e?" vscode-webview: vscode-file:":""}`}async function _s(e,r){let{files:s}=e;if(s?.some((f)=>!cn(f.path)))return us(fn(s),r);let c=Bun.serve({hostname:"127.0.0.1",port:0,development:!1,reusePort:!1,routes:{"/":e},fetch:()=>new Response("Not Found",{status:404})});try{return await us(un(c.url),r)}finally{c.stop(!0)}}async function us(e,r){let s=fs(await e("/"),r),c=new TextDecoder().decode(s.body),f=new Set([...c.matchAll(/(?:src|href)="(\/[^"]+)"/g)].map((y)=>y[1])),a={"/":ds(s)};for(let y of f){let t=fs(await e(y),r);if(!t.headers.has("cache-control"))t.headers.set("cache-control","public, max-age=31536000, immutable");a[y]=ds(t)}return a}function un(e){return async(r)=>{let s=await fetch(new URL(r,e));if(!s.ok)throw Error(`\u753B\u9762\u306E\u30D5\u30A1\u30A4\u30EB\u3092\u7528\u610F\u3067\u304D\u307E\u305B\u3093\u3067\u3057\u305F: ${r} (${s.status})`);return{body:new Uint8Array(await s.arrayBuffer()),headers:new Headers(s.headers)}}}function fn(e){return async(r)=>{let s=r==="/"?e.find((f)=>f.loader==="html"):e.find((f)=>f.loader!=="html"&&`/${f.path.replace(/^\.\//,"")}`===r);if(!s)throw Error(`\u753B\u9762\u306E\u30D5\u30A1\u30A4\u30EB\u3092\u7528\u610F\u3067\u304D\u307E\u305B\u3093\u3067\u3057\u305F: ${r}\uFF08\u56FA\u3081\u305F JS \u306E\u4E00\u89A7\u306B\u306A\u3044\uFF09`);return{body:await Bun.file(ln(import.meta.dir,s.path)).bytes(),headers:new Headers(s.headers)}}}function fs({body:e,headers:r},s){let c=new Headers(r);c.delete("content-length"),c.delete("date"),c.set("vary","Accept-Encoding"),c.set("content-security-policy",s);let f=Bun.gzipSync(e,{level:9});return{body:e,headers:c,gzip:f.byteLength<e.byteLength*hn?f:void 0}}function ds({body:e,gzip:r,headers:s}){let c=s.get("etag");return(f)=>{if(c&&f.headers.get("if-none-match")===c)return new Response(null,{status:304,headers:s});let a=/\bgzip\b/.test(f.headers.get("accept-encoding")??"");if(!r||!a)return new Response(e,{headers:s});let y=new Headers(s);return y.set("content-encoding","gzip"),new Response(r,{headers:y})}}var gs=3,ms=["#4fc3f7","#ffb74d","#81c784","#f06292","#ba68c8","#fff176"];class $t{shellCommand;terminals=new Map;order=[];tabNames=new Map;groups=new Map;tabGroups=new Map;nextGroupId=1;members=new Map;views=new Map;chosenNames=new Map;visibleMembers=new Set;nextMemberId=1;nextId=1;nextSplitId=1;titlesQueued=!1;nextCommandId=1;stopping=!1;initialTerminal;commands=[];minWindow;savedMinWindow;onChange=()=>{};constructor(e,r,s,c){this.shellCommand=r;if(this.savedMinWindow=s?.minWindow,this.minWindow=c??s?.minWindow??Ur,!s){this.initialTerminal=this.createTerminal(e??r);return}for(let a of s.tabs){let y=s.terminals[a];if(y.initial)this.initialTerminal=a;let t=y.initial&&e!==void 0;this.addTerminal(a,t?e:r,y,t?void 0:y.cwd)}for(let[a,y]of Object.entries(s.names??{}))this.tabNames.set(a,y);for(let a of s.groups??[]){this.groups.set(a.id,{color:a.color,...a.name!==void 0&&{name:a.name}});for(let y of a.tabs)this.tabGroups.set(y,a.id)}this.nextGroupId=Math.max(0,...[...this.groups.keys()].map(Number))+1,this.normalizeGroups(),this.commands=s.commands;let f=(a)=>Math.max(0,...a.map(Number))+1;if(this.nextId=f(this.order),this.nextCommandId=f(this.commands.map((a)=>a.id)),e&&!this.initialTerminal)this.initialTerminal=this.createTerminal(e)}async snapshot(e,{refreshCwd:r=!1}={}){let s={};return await Promise.all([...this.terminals].map(async([c,f])=>{let[a,y]=await Promise.all([f.snapshot(),f.savedCwd({refresh:r})]);s[c]={...a,initial:c===this.initialTerminal,...y&&{cwd:y}}})),{version:2,cwd:e,tabs:[...this.order],names:Object.fromEntries(this.tabNames),groups:[...this.groups].map(([c,{name:f,color:a}])=>({id:c,...f!==void 0&&{name:f},color:a,tabs:this.order.filter((y)=>this.tabGroups.get(y)===c)})),terminals:s,commands:this.commands,...this.savedMinWindow&&{minWindow:this.savedMinWindow}}}terminal(e){return this.terminals.get(e)}join(e){let r=this.nextMemberId++,s={id:r,name:Ht(r),color:ms[(r-1)%ms.length],selections:{}};this.members.set(e,s);let c=this.initialTerminal&&this.terminals.has(this.initialTerminal)?this.initialTerminal:this.order[0];return this.views.set(s,{layout:oe(c),splits:[]}),e({type:"welcome",you:r}),e(this.tabsEvent()),e(Ft(this.views.get(s))),e(this.titlesEvent()),e({type:"status",statuses:this.statuses()}),e(this.commandsEvent()),e(this.minWindowEvent()),this.notifyPresence(),{newTab:async(f)=>{let a=f??s.terminal,y=a===void 0?void 0:await this.terminals.get(a)?.cwd();if(!this.views.has(s))return;let t=this.createTerminal(this.shellCommand,y);this.notifyTabs(),this.changeView(s,(n)=>({layout:oe(t),splits:vs(n)}))},split:async(f,a)=>{if(!this.canAdd(s))return;let y=await this.terminals.get(f)?.cwd();if(!this.canAdd(s))return;let t=this.createTerminal(this.shellCommand,y);this.notifyTabs();let n=this.changeView(s,(l)=>({...l,layout:this.placeBeside(l.layout,oe(t),f,a)}));if(n)this.gather(n.layout,t)},selectTab:(f)=>{if(!this.terminals.has(f))return;this.changeView(s,(a)=>{if(ee(a.layout).includes(f))return;let y=a.splits.find((n)=>ee(n).includes(f)),t=vs(a).filter((n)=>n!==y);return{layout:y??oe(f),splits:t}})},addToView:(f,a,y)=>{if(!this.terminals.has(f)||!this.canAdd(s))return;let t=this.changeView(s,(n)=>{if(ee(n.layout).includes(f))return;return{layout:y?pn(n.layout,oe(f),y,`split-${this.nextSplitId++}`):this.placeBeside(n.layout,oe(f),a,"row"),splits:ys(n.splits,f)}});if(t)this.gather(t.layout,f)},removeFromView:(f)=>{this.changeView(s,(a)=>{let y=ee(a.layout);return y.length>1&&y.includes(f)?{...a,layout:Re(a.layout,f)}:void 0})},separate:(f)=>{this.changeView(s,(a)=>{let y=ee(a.layout);if(y.includes(f)){if(y.length<2)return;let n=s.terminal!==void 0&&y.includes(s.terminal)?s.terminal:f;return{...a,layout:oe(n)}}let t=a.splits.find((n)=>ee(n).includes(f));return t?{...a,splits:a.splits.filter((n)=>n!==t)}:void 0})},resizeSplit:(f,a)=>{this.changeView(s,(y)=>et(y,(t)=>me(t,f)?_n(t,f,Math.min(0.9,Math.max(0.1,a))):void 0))},swapPanes:(f,a)=>{let y=this.changeView(s,(t)=>et(t,(n)=>{let l=ee(n);return f!==a&&l.includes(f)&&l.includes(a)?ws(n,f,a):void 0}));if(y)this.gatherSplitOf(y,(t)=>ee(t).includes(f))},flipSplit:(f)=>{let a=this.changeView(s,(y)=>et(y,(t)=>me(t,f)?tt(t,f,(n)=>({...n,ratio:1-n.ratio,children:[n.children[1],n.children[0]]})):void 0));if(a)this.gatherSplitOf(a,(y)=>me(y,f)!==void 0)},rotateSplit:(f)=>{let a=this.changeView(s,(y)=>et(y,(t)=>me(t,f)?Te(tt(t,f,(n)=>({...n,direction:n.direction==="row"?"column":"row"}))):void 0));if(a)this.gatherSplitOf(a,(y)=>me(y,f)!==void 0)},focus:(f)=>{s.terminal=f,this.notifyPresence(),this.updateSeen()},visible:(f)=>{if(f)this.visibleMembers.add(s);else this.visibleMembers.delete(s);this.updateSeen()},select:(f,a)=>{if(a&&this.terminals.has(f))s.selections[f]=a;else if(!a&&Object.hasOwn(s.selections,f))delete s.selections[f];else return;this.notifyPresence()},follow:(f)=>{let a=[...this.members.values()].find((t)=>t.id===f&&t!==s);if(s.following===a?.id)return;let y=s.following;if(s.following=a?.id,this.forgetScroll(y),this.notifyPresence(),a)this.setView(s,this.views.get(a))},scroll:(f,a)=>{if(s.scroll?.terminal===f&&s.scroll.offset===a)return;s.scroll={terminal:f,offset:a},this.notifyPresence()},rename:(f)=>{if(this.chosenNames.get(s)===f)return;if(f===void 0)this.chosenNames.delete(s);else this.chosenNames.set(s,f);this.notifyPresence()},leave:()=>{this.members.delete(e),this.views.delete(s),this.chosenNames.delete(s),this.visibleMembers.delete(s),this.members.forEach((f)=>{if(f.following===s.id)f.following=void 0}),this.forgetScroll(s.following),this.notifyPresence()}}}close(e){if(this.stopping)return;let r=this.order.indexOf(e);if(r<0||this.order.length===1)return;this.order.splice(r,1),this.removeTerminal(e);let s=this.order[Math.min(r,this.order.length-1)];this.notifyTabs();for(let[c,f]of this.members){let a=this.views.get(f);if(!a)continue;let y=ee(a.layout).includes(e),t=a.splits.some((g)=>ee(g).includes(e));if(!y&&!t)continue;let n=ys(a.splits,e),l=a.layout;if(y){let g=Re(a.layout,e),d=g?void 0:n.find((_)=>ee(_).includes(s));l=g??d??oe(s),n=n.filter((_)=>_!==d)}let p={layout:l,splits:n};this.views.set(f,p),c(Ft(p))}this.updateSeen()}moveTab(e,r,s){if(!this.validTabs(e))return;let c=this.order.filter((n)=>e.includes(n)),f=this.order.filter((n)=>!e.includes(n));if(!Number.isSafeInteger(r)||r<0||r>f.length)return;if(typeof s==="string"&&!this.groups.has(s))return;let a=r;if(typeof s==="string"){let n=f.flatMap((l,p)=>this.tabGroups.get(l)===s?[p]:[]);if(n.length>0)a=Math.min(Math.max(r,n[0]),n.at(-1)+1)}let y=[...f.slice(0,a),...c,...f.slice(a)];if(!(s!==void 0&&c.some((n)=>(this.tabGroups.get(n)??null)!==s))&&y.every((n,l)=>n===this.order[l]))return;this.order=y;for(let n of c)if(s===null)this.tabGroups.delete(n);else if(s!==void 0)this.tabGroups.set(n,s);this.notifyTabs()}newGroup(e){if(!this.validTabs(e))return;let r=new Set([...this.groups.values()].map((f)=>f.color)),s=Le.find((f)=>!r.has(f))??Le[this.groups.size%Le.length],c=String(this.nextGroupId++);this.groups.set(c,{color:s});for(let f of e)this.tabGroups.set(f,c);this.notifyTabs()}addToGroup(e,r){if(!this.validTabs(e)||!this.groups.has(r)||e.every((f)=>this.tabGroups.get(f)===r))return;let s=this.order.filter((f)=>e.includes(f));this.order=this.order.filter((f)=>!e.includes(f));let c=this.order.findLastIndex((f)=>this.tabGroups.get(f)===r);this.order.splice(c+1,0,...s);for(let f of s)this.tabGroups.set(f,r);this.notifyTabs()}removeFromGroup(e){if(!this.validTabs(e))return;let r=!1;for(let s of this.order.filter((c)=>e.includes(c)).reverse()){let c=this.tabGroups.get(s);if(c===void 0)continue;r=!0,this.tabGroups.delete(s);let f=this.order.indexOf(s);this.order.splice(f,1);let a=this.order.findLastIndex((y)=>this.tabGroups.get(y)===c);this.order.splice(a<0?f:a+1,0,s)}if(r)this.notifyTabs()}moveGroup(e,r){if(!this.groups.has(e))return;let s=this.order.filter((a)=>this.tabGroups.get(a)===e),c=this.order.filter((a)=>this.tabGroups.get(a)!==e);if(!Number.isSafeInteger(r)||r<0||r>c.length)return;let f=[...c.slice(0,r),...s,...c.slice(r)];if(f.every((a,y)=>a===this.order[y]))return;this.order=f,this.notifyTabs()}renameGroup(e,r){let s=this.groups.get(e);if(!s||s.name===r)return;if(r===void 0)delete s.name;else s.name=r;this.notifyTabs()}recolorGroup(e,r){let s=this.groups.get(e);if(!s||!Je(r)||s.color===r)return;s.color=r,this.notifyTabs()}ungroup(e){if(!this.groups.delete(e))return;for(let[r,s]of this.tabGroups)if(s===e)this.tabGroups.delete(r);this.notifyTabs()}closeGroup(e){let r=this.order.filter((s)=>this.tabGroups.get(s)===e);if(r.length===0||r.length===this.order.length)return;for(let s of r)this.close(s)}renameTab(e,r){if(!this.terminals.has(e)||this.tabNames.get(e)===r)return;if(r===void 0)this.tabNames.delete(e);else this.tabNames.set(e,r);this.notifyTabs()}addCommand(e){if(this.commands.length>=Gr)return;this.commands.push({id:String(this.nextCommandId++),...e}),this.notifyCommands()}updateCommand(e,r){let s=this.commands.findIndex((c)=>c.id===e);if(s<0)return;this.commands[s]={id:e,...r},this.notifyCommands()}removeCommand(e){let r=this.commands.length;if(this.commands=this.commands.filter((s)=>s.id!==e),this.commands.length!==r)this.notifyCommands()}setMinWindow(e){if(this.savedMinWindow=e,e.width===this.minWindow.width&&e.height===this.minWindow.height)return this.onChange();this.minWindow=e;let r=this.minWindowEvent();this.members.forEach((s,c)=>c(r)),this.onChange()}async killAll(){this.stopping=!0,await Promise.all([...this.terminals.values()].map((e)=>e.kill()))}createTerminal(e,r){let s=String(this.nextId++);return this.addTerminal(s,e,void 0,r),s}addTerminal(e,r,s,c){this.order.push(e),this.terminals.set(e,new Lt(r,()=>this.close(e),()=>this.notifyTitles(),()=>this.notifyStatus(),{restored:s,onOutput:()=>this.onChange(),cwd:c}))}removeTerminal(e){this.terminals.get(e)?.kill(),this.terminals.delete(e),this.tabNames.delete(e),this.tabGroups.delete(e);let r=!1;for(let s of this.members.values()){if(!Object.hasOwn(s.selections,e))continue;delete s.selections[e],r=!0}if(r)this.notifyPresence()}canAdd(e){let r=this.views.get(e);return r!==void 0&&ee(r.layout).length<gs}validTabs(e){return e.length>0&&e.length<=gs&&new Set(e).size===e.length&&e.every((r)=>this.terminals.has(r))}placeBeside(e,r,s,c){let f=(a)=>({type:"split",id:`split-${this.nextSplitId++}`,direction:c,ratio:0.5,children:[a,r]});if(s===void 0||!ee(e).includes(s))return Te(f(e));return Te(bs(e,s,f(oe(s))))}changeView(e,r){let s=this.views.get(e),c=s&&r(s);if(!c)return;if(e.following!==void 0){let f=e.following;e.following=void 0,this.forgetScroll(f),this.notifyPresence()}return this.setView(e,c),c}gather(e,r){let s=ee(e);if(s.length<2)return;let c=this.order.find((n)=>s.includes(n)&&n!==r);if(c===void 0)return;let f=this.tabGroups.get(c),a=this.order.indexOf(c),y=[...this.order.slice(0,a).filter((n)=>!s.includes(n)),...s,...this.order.slice(a).filter((n)=>!s.includes(n))];if(s.every((n)=>this.tabGroups.get(n)===f)&&y.every((n,l)=>n===this.order[l]))return;this.order=y;for(let n of s)if(f===void 0)this.tabGroups.delete(n);else this.tabGroups.set(n,f);this.notifyTabs()}gatherSplitOf(e,r){let s=[e.layout,...e.splits].find(r);if(s)this.gather(s)}setView(e,r){let s=new Set,c=(f)=>{s.add(f),this.views.set(f,r);for(let[a,y]of this.members)if(y===f)a(Ft(r));for(let a of this.members.values())if(a.following===f.id&&!s.has(a))c(a)};c(e),this.updateSeen()}tabsEvent(){let e=(f)=>xe(f).replace(/\s/g,""),r=new Map(this.order.map((f)=>[f,e(this.tabNames.get(f)??Nt(f))])),s=this.order.map((f)=>{let a=this.tabNames.get(f);if(a===void 0)return{id:f,name:Nt(f)};let y=r.get(f),t=y.match(/^\u30BF\u30FC\u30DF\u30CA\u30EB(\d+)$/),n=t!==null&&t[1]!==f||/\(\u30BF\u30FC\u30DF\u30CA\u30EB\d+\)$/.test(y)||this.order.some((l)=>l!==f&&r.get(l)===y);return{id:f,name:n?`${a}\uFF08${Nt(f)}\uFF09`:a,custom:a}});for(let f of s){let a=this.tabGroups.get(f.id);if(a!==void 0)f.group=a}let c=[];for(let f of new Set(this.order.flatMap((a)=>this.tabGroups.get(a)??[]))){let{name:a,color:y}=this.groups.get(f);c.push({id:f,name:a??dn(f),...a!==void 0&&{custom:a},color:y})}return{type:"tabs",tabs:s,groups:c}}normalizeGroups(){for(let[c,f]of this.tabGroups)if(!this.groups.has(f)||!this.terminals.has(c))this.tabGroups.delete(c);let e=new Set(this.tabGroups.values());for(let c of this.groups.keys())if(!e.has(c))this.groups.delete(c);let r=new Set,s=[];for(let c of this.order){if(r.has(c))continue;let f=this.tabGroups.get(c),a=f===void 0?[c]:this.order.filter((y)=>this.tabGroups.get(y)===f);for(let y of a)r.add(y),s.push(y)}this.order=s}notifyTabs(){this.normalizeGroups();let e=this.tabsEvent();this.members.forEach((r,s)=>s(e)),this.onChange()}titlesEvent(){let e={};return this.terminals.forEach((r,s)=>{if(r.title)e[s]=r.title}),{type:"titles",titles:e}}notifyTitles(){if(this.titlesQueued)return;this.titlesQueued=!0,queueMicrotask(()=>{this.titlesQueued=!1;let e=this.titlesEvent();this.members.forEach((r,s)=>s(e))})}statuses(){let e={};return this.terminals.forEach((r,s)=>{if(r.status)e[s]=r.status}),e}notifyStatus(){this.clearSeen();let e=this.statuses();this.members.forEach((r,s)=>s({type:"status",statuses:e}))}updateSeen(){if(this.clearSeen())this.notifyStatus()}clearSeen(){let e=!1;for(let r of this.visibleMembers){let s=r.terminal,c=this.views.get(r)?.layout;if(s&&c&&ee(c).includes(s)&&this.terminals.get(s)?.markSeen())e=!0}return e}commandsEvent(){return{type:"commands",commands:this.commands}}minWindowEvent(){return{type:"min-window",...this.minWindow}}notifyCommands(){let e=this.commandsEvent();this.members.forEach((r,s)=>s(e)),this.onChange()}forgetScroll(e){let r=[...this.members.values()],s=r.find((c)=>c.id===e);if(s&&!r.some((c)=>c.following===e))s.scroll=void 0}updateNames(){let e=[...this.members.values()],r=(c)=>this.chosenNames.get(c)??Ht(c.id),s=new Map;for(let c of e)s.set(xe(r(c)),(s.get(xe(r(c)))??0)+1);for(let c of e){let f=r(c);c.name=s.get(xe(f))>1?`${f}\uFF08${Ht(c.id)}\uFF09`:f}}notifyPresence(){this.updateNames();let e=[...this.members.values()];this.members.forEach((r,s)=>s({type:"presence",members:e}))}}function Ht(e){return`\u53C2\u52A0\u8005 ${e}`}function Nt(e){return`\u30BF\u30FC\u30DF\u30CA\u30EB ${e}`}function dn(e){return`\u30B0\u30EB\u30FC\u30D7 ${e}`}function Ft(e){return{type:"view",layout:e.layout,splits:e.splits}}function vs(e){return e.layout.type==="split"?[...e.splits,e.layout]:e.splits}function ys(e,r){return e.flatMap((s)=>{if(!ee(s).includes(r))return[s];let c=Re(s,r);return c&&c.type==="split"?[c]:[]})}function et(e,r){let s=r(e.layout);if(s)return{...e,layout:s};for(let[c,f]of e.splits.entries()){let a=r(f);if(a)return{...e,splits:e.splits.with(c,a)}}return}function pn(e,r,s,c){return Te({type:"split",id:c,direction:s==="bottom"?"column":"row",ratio:0.5,children:s==="left"?[r,e]:[e,r]})}function oe(e){return{type:"terminal",id:e}}function ee(e){return e.type==="terminal"?[e.id]:e.children.flatMap(ee)}function me(e,r){if(e.type==="terminal")return;return e.id===r?e:me(e.children[0],r)??me(e.children[1],r)}function Ut(e,r){return{...e,children:[r(e.children[0]),r(e.children[1])]}}function bs(e,r,s){if(e.type==="terminal")return e.id===r?s:e;return Ut(e,(c)=>bs(c,r,s))}function Re(e,r){if(e.type==="terminal")return e.id===r?void 0:e;let s=Re(e.children[0],r),c=Re(e.children[1],r);if(!s||!c)return s??c;return{...e,children:[s,c]}}function _n(e,r,s){return tt(e,r,(c)=>({...c,ratio:s}))}function tt(e,r,s){if(e.type==="terminal")return e;if(e.id===r)return s(e);return Ut(e,(c)=>tt(c,r,s))}function ws(e,r,s){if(e.type==="terminal")return e.id===r?oe(s):e.id===s?oe(r):e;return Ut(e,(c)=>ws(c,r,s))}function Te(e){if(e.type==="terminal")return e;let r=e.children.map(Te),[s,c]=r.map((f)=>Wt(f,e.direction));return{...e,ratio:s/(s+c),children:r}}function Wt(e,r){if(e.type==="terminal"||e.direction!==r)return 1;return Wt(e.children[0],r)+Wt(e.children[1],r)}var{values:it,positionals:Ss}=ss(),st=it["min-window"],Cs=st===void 0?void 0:jr(st);if(st!==void 0&&!Cs)console.error(`Accompany: --min-window \u306F\u300C\u5E45x\u9AD8\u3055\u300D\uFF08\u4F8B: 480x1080\u3002\u5E45 0\u301C${Ee.width}\u3001\u9AD8\u3055 0\u301C${Ee.height} \u306E\u6574\u6570\uFF09\u3067\u6307\u5B9A\u3057\u3066\u304F\u3060\u3055\u3044: ${st}`),process.exit(1);var gn=[process.env.SHELL??"/bin/sh"],xs=process.cwd(),Gt=new It(ls(),xs),Es=it.fresh?void 0:Gt.load(),Be=new $t(Ss.length>0?Ss:void 0,gn,Es,Cs),qt=new Dt,mn=Yr(Be,qt),As=10,ve=Number(it.port),vn=await _s(ns,ps({allowVscodeWebview:it["allow-vscode-webview"]})),be;for(let e=ve;e<ve+As&&e<=ke;e++)try{be=Bun.serve({hostname:"127.0.0.1",port:e,development:!1,reusePort:!1,routes:vn,fetch:mn.fetch,websocket:{...ut,perMessageDeflate:!0}});break}catch(r){if(r.code!=="EADDRINUSE")throw r}if(!be){let e=Math.min(ve+As-1,ke);console.error(`Accompany: \u30DD\u30FC\u30C8 ${ve}\u301C${e} \u306F\u3059\u3079\u3066\u4F7F\u308F\u308C\u3066\u3044\u307E\u3059\u3002--port \u3067\u5225\u306E\u30DD\u30FC\u30C8\u3092\u6307\u5B9A\u3057\u3066\u304F\u3060\u3055\u3044`),await Be.killAll(),qt.remove(),process.exit(1)}if(be.port!==ve&&ve!==0)console.log(`Accompany: \u30DD\u30FC\u30C8 ${ve} \u306F\u4F7F\u308F\u308C\u3066\u3044\u308B\u306E\u3067\u3001${be.port} \u3067\u5F85\u3061\u53D7\u3051\u307E\u3059`);var jt=!1;async function ks({stopping:e=!1}={}){try{Gt.save(await Be.snapshot(xs,{refreshCwd:e})),jt=!1}catch(r){if(!jt)console.warn(`Accompany: \u753B\u9762\u3092\u4FDD\u5B58\u3067\u304D\u307E\u305B\u3093\u3067\u3057\u305F: ${r}`);jt=!0}}var yn=1000,rt,zt=!1;Be.onChange=()=>{if(rt||zt)return;rt=setTimeout(()=>{rt=void 0,ks()},yn)};var bn=3000;for(let e of["SIGINT","SIGTERM","SIGHUP"])process.on(e,async()=>{if(zt)return;zt=!0,clearTimeout(rt),await Promise.race([ks({stopping:!0}),Bun.sleep(bn)]),await Be.killAll(),qt.remove(),process.exit(0)});console.log(`Accompany: http://${be.hostname}:${be.port}`);if(Es)console.log(`\u524D\u56DE\u306E\u753B\u9762\u3092\u623B\u3057\u307E\u3057\u305F\uFF08\u65B0\u3057\u304F\u59CB\u3081\u308B\u3068\u304D\u306F --fresh \u3092\u4ED8\u3051\u3066\u8D77\u52D5\u3059\u308B\uFF09: ${Gt.file}`);
142
142
 
143
- //# debugId=5AFB14D3780BE17E64756E2164756E21
143
+ //# debugId=7FE49E7EBE45D70A64756E2164756E21
144
144
  //# sourceMappingURL=/cli.js.map