diffstalker 0.2.0 → 0.2.1
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/.github/workflows/release.yml +8 -0
- package/bun.lock +23 -0
- package/dist/App.js +225 -471
- package/dist/FollowMode.js +85 -0
- package/dist/KeyBindings.js +178 -0
- package/dist/MouseHandlers.js +156 -0
- package/dist/core/ExplorerStateManager.js +444 -78
- package/dist/core/GitStateManager.js +169 -93
- package/dist/git/diff.js +4 -0
- package/dist/index.js +54 -53
- package/dist/state/UIState.js +17 -4
- package/dist/ui/PaneRenderers.js +56 -0
- package/dist/ui/modals/FileFinder.js +232 -0
- package/dist/ui/widgets/CompareListView.js +86 -64
- package/dist/ui/widgets/DiffView.js +19 -17
- package/dist/ui/widgets/ExplorerContent.js +15 -28
- package/dist/ui/widgets/ExplorerView.js +140 -31
- package/dist/ui/widgets/Footer.js +6 -2
- package/dist/ui/widgets/Header.js +3 -46
- package/dist/utils/fileCategories.js +37 -0
- package/dist/utils/fileTree.js +148 -0
- package/eslint.metrics.js +16 -0
- package/metrics/.gitkeep +0 -0
- package/metrics/v0.2.1.json +268 -0
- package/package.json +4 -1
- package/dist/utils/ansiToBlessed.js +0 -125
- package/dist/utils/mouseCoordinates.js +0 -165
- package/dist/utils/rowCalculations.js +0 -246
package/dist/index.js
CHANGED
|
@@ -1,59 +1,60 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import
|
|
3
|
-
`)}function
|
|
4
|
-
`)}function
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
`)
|
|
12
|
-
`),
|
|
13
|
-
`),
|
|
14
|
-
`)}
|
|
15
|
-
`)
|
|
16
|
-
`)}function
|
|
17
|
-
`)}function
|
|
18
|
-
`),Z
|
|
19
|
-
`)}function
|
|
20
|
-
`)
|
|
21
|
-
Maximum size: 1 MB`,truncated:!0}});return}let X=await BK.promises.readFile($);if(c8(X)){this.updateState({selectedFile:{path:K,content:"Binary file - cannot display"}});return}let Z=X.toString("utf-8"),Q=!1;if(J.size>h8)Z=`⚠ Large file (${(J.size/1024).toFixed(1)} KB)
|
|
22
|
-
|
|
23
|
-
`+Z;let q=5000,Y=Z.split(`
|
|
24
|
-
`);if(Y.length>q)Z=Y.slice(0,q).join(`
|
|
25
|
-
`)+`
|
|
26
|
-
|
|
27
|
-
... (truncated, ${Y.length-q} more lines)`,Q=!0;this.updateState({selectedFile:{path:K,content:Z,truncated:Q}})}catch($){this.updateState({selectedFile:{path:K,content:$ instanceof Error?`Error: ${$.message}`:"Failed to read file"}})}}async selectIndex(K){if(K<0||K>=this._state.items.length)return;let $=this._state.items[K];if(this.updateState({selectedIndex:K}),$&&!$.isDirectory)await this.loadFile($.path);else this.updateState({selectedFile:null})}navigateUp(K){let $=Math.max(0,this._state.selectedIndex-1);if($===this._state.selectedIndex)return null;if(this.selectIndex($),$<K)return $;return null}navigateDown(K,$){let J=Math.min(this._state.items.length-1,this._state.selectedIndex+1);if(J===this._state.selectedIndex)return null;this.selectIndex(J);let Z=this._state.items.length>$?$-2:$,Q=K+Z;if(J>=Q)return K+1;return null}async enterDirectory(){let K=this._state.items[this._state.selectedIndex];if(!K)return;if(K.isDirectory)if(K.name===".."){let $=L.dirname(this._state.currentPath);await this.loadDirectory($==="."?"":$)}else await this.loadDirectory(K.path)}async goUp(){if(this._state.currentPath){let K=L.dirname(this._state.currentPath);await this.loadDirectory(K==="."?"":K)}}dispose(){this.removeAllListeners()}}import d8 from"neo-blessed";class uK{box;screen;selectedIndex;currentTheme;onSelect;onCancel;constructor(K,$,J,X){if(this.screen=K,this.currentTheme=$,this.onSelect=J,this.onCancel=X,this.selectedIndex=C.indexOf($),this.selectedIndex<0)this.selectedIndex=0;let Z=50,Q=C.length+12;this.box=d8.box({parent:K,top:"center",left:"center",width:Z,height:Q,border:{type:"line"},style:{border:{fg:"cyan"}},tags:!0,keys:!0}),this.setupKeyHandlers(),this.render()}setupKeyHandlers(){this.box.key(["escape","q"],()=>{this.close(),this.onCancel()}),this.box.key(["enter","space"],()=>{let K=C[this.selectedIndex];this.close(),this.onSelect(K)}),this.box.key(["up","k"],()=>{this.selectedIndex=Math.max(0,this.selectedIndex-1),this.render()}),this.box.key(["down","j"],()=>{this.selectedIndex=Math.min(C.length-1,this.selectedIndex+1),this.render()})}render(){let K=[];K.push("{bold}{cyan-fg} Select Theme{/cyan-fg}{/bold}"),K.push("");for(let J=0;J<C.length;J++){let X=C[J],Z=$K[X],Q=J===this.selectedIndex,q=X===this.currentTheme,Y=Q?"{cyan-fg}{bold}> ":" ";if(Y+=Z.displayName,Q)Y+="{/bold}{/cyan-fg}";if(q)Y+=" {gray-fg}(current){/gray-fg}";K.push(Y)}K.push(""),K.push("{gray-fg}Preview:{/gray-fg}");let $=s(C[this.selectedIndex]);K.push(" {green-fg}+ added line{/green-fg}"),K.push(" {red-fg}- deleted line{/red-fg}"),K.push(""),K.push("{gray-fg}j/k: navigate | Enter: select | Esc: cancel{/gray-fg}"),this.box.setContent(K.join(`
|
|
28
|
-
`)),this.screen.render()}close(){this.box.destroy()}focus(){this.box.focus()}}import l8 from"neo-blessed";var w=[{title:"Navigation",entries:[{key:"j/k",description:"Move up/down"},{key:"Tab",description:"Toggle pane focus"}]},{title:"Staging",entries:[{key:"s",description:"Stage file"},{key:"U",description:"Unstage file"},{key:"A",description:"Stage all"},{key:"Z",description:"Unstage all"},{key:"Space",description:"Toggle stage"}]},{title:"Actions",entries:[{key:"c",description:"Commit panel"},{key:"r",description:"Refresh"},{key:"q",description:"Quit"}]},{title:"Resize",entries:[{key:"-",description:"Shrink top pane"},{key:"+",description:"Grow top pane"}]},{title:"Tabs",entries:[{key:"1",description:"Diff view"},{key:"2",description:"Commit panel"},{key:"3",description:"History view"},{key:"4",description:"Compare view"},{key:"5",description:"Explorer view"}]},{title:"Toggles",entries:[{key:"m",description:"Mouse mode"},{key:"w",description:"Wrap mode"},{key:"f",description:"Follow mode"},{key:"t",description:"Theme picker"},{key:"?",description:"This help"}]},{title:"Explorer",entries:[{key:"Enter",description:"Enter directory"},{key:"Backspace",description:"Go up"}]},{title:"Compare",entries:[{key:"b",description:"Base branch picker"},{key:"u",description:"Toggle uncommitted"}]},{title:"Diff",entries:[{key:"d",description:"Discard changes"}]}];class pK{box;screen;onClose;constructor(K,$){this.screen=K,this.onClose=$;let{width:J,height:X}=K,Z=J>=90,Q=Z?Math.min(80,J-4):Math.min(42,J-4),q=Math.min(this.calculateHeight(Z),X-4);this.box=l8.box({parent:K,top:"center",left:"center",width:Q,height:q,border:{type:"line"},style:{border:{fg:"cyan"}},tags:!0,keys:!0,scrollable:!0,alwaysScroll:!0}),this.setupKeyHandlers(),this.render(Z,Q)}calculateHeight(K){if(K){let $=Math.ceil(w.length/2),J=w.slice(0,$),X=w.slice($),Z=J.reduce((q,Y)=>q+Y.entries.length+2,0),Q=X.reduce((q,Y)=>q+Y.entries.length+2,0);return Math.max(Z,Q)+5}else return w.reduce(($,J)=>$+J.entries.length+2,0)+5}setupKeyHandlers(){this.box.key(["escape","enter","?","q"],()=>{this.close(),this.onClose()}),this.box.on("click",()=>{this.close(),this.onClose()})}visibleWidth(K){return K.replace(/\{[^}]+\}/g,"").length}padToVisible(K,$){let J=this.visibleWidth(K),X=Math.max(0,$-J);return K+" ".repeat(X)}render(K,$){let J=[];if(J.push("{bold}{cyan-fg} Keyboard Shortcuts{/cyan-fg}{/bold}"),J.push(""),K){let X=Math.ceil(w.length/2),Z=w.slice(0,X),Q=w.slice(X),q=Math.floor(($-6)/2),Y=this.renderGroups(Z,q),V=this.renderGroups(Q,q),_=Math.max(Y.length,V.length);for(let z=0;z<_;z++){let U=this.padToVisible(Y[z]||"",q),B=V[z]||"";J.push(U+" "+B)}}else for(let X of w){J.push(`{bold}{gray-fg}${X.title}{/gray-fg}{/bold}`);for(let Z of X.entries)J.push(` {cyan-fg}${Z.key.padEnd(10)}{/cyan-fg} ${Z.description}`);J.push("")}J.push(""),J.push("{gray-fg}Press Esc, Enter, or ? to close{/gray-fg}"),this.box.setContent(J.join(`
|
|
29
|
-
`)),this.screen.render()}renderGroups(K,$){let J=[];for(let X of K){J.push(`{bold}{gray-fg}${X.title}{/gray-fg}{/bold}`);for(let Z of X.entries)J.push(` {cyan-fg}${Z.key.padEnd(10)}{/cyan-fg} ${Z.description}`);J.push("")}return J}close(){this.box.destroy()}focus(){this.box.focus()}}import o8 from"neo-blessed";class gK{box;screen;branches;selectedIndex;currentBranch;onSelect;onCancel;constructor(K,$,J,X,Z){if(this.screen=K,this.branches=$,this.currentBranch=J,this.onSelect=X,this.onCancel=Z,this.selectedIndex=J?$.indexOf(J):0,this.selectedIndex<0)this.selectedIndex=0;let Q=50,Y=Math.min($.length,15)+6;this.box=o8.box({parent:K,top:"center",left:"center",width:Q,height:Y,border:{type:"line"},style:{border:{fg:"cyan"}},tags:!0,keys:!0,scrollable:!0,alwaysScroll:!0}),this.setupKeyHandlers(),this.render()}setupKeyHandlers(){this.box.key(["escape","q"],()=>{this.close(),this.onCancel()}),this.box.key(["enter","space"],()=>{let K=this.branches[this.selectedIndex];if(K)this.close(),this.onSelect(K)}),this.box.key(["up","k"],()=>{this.selectedIndex=Math.max(0,this.selectedIndex-1),this.render()}),this.box.key(["down","j"],()=>{this.selectedIndex=Math.min(this.branches.length-1,this.selectedIndex+1),this.render()})}render(){let K=[];if(K.push("{bold}{cyan-fg} Select Base Branch{/cyan-fg}{/bold}"),K.push(""),this.branches.length===0)K.push("{gray-fg}No branches found{/gray-fg}");else for(let $=0;$<this.branches.length;$++){let J=this.branches[$],X=$===this.selectedIndex,Z=J===this.currentBranch,Q=X?"{cyan-fg}{bold}> ":" ";if(Q+=J,X)Q+="{/bold}{/cyan-fg}";if(Z)Q+=" {gray-fg}(current){/gray-fg}";K.push(Q)}K.push(""),K.push("{gray-fg}j/k: navigate | Enter: select | Esc: cancel{/gray-fg}"),this.box.setContent(K.join(`
|
|
30
|
-
`)),this.screen.render()}close(){this.box.destroy()}focus(){this.box.focus()}}import r8 from"neo-blessed";class mK{box;screen;filePath;onConfirm;onCancel;constructor(K,$,J,X){this.screen=K,this.filePath=$,this.onConfirm=J,this.onCancel=X;let Z=Math.min(60,Math.max(40,$.length+20)),Q=7;this.box=r8.box({parent:K,top:"center",left:"center",width:Z,height:Q,border:{type:"line"},style:{border:{fg:"yellow"}},tags:!0,keys:!0}),this.setupKeyHandlers(),this.render()}setupKeyHandlers(){this.box.key(["y","Y"],()=>{this.close(),this.onConfirm()}),this.box.key(["n","N","escape","q"],()=>{this.close(),this.onCancel()})}render(){let K=[];K.push("{bold}{yellow-fg} Discard Changes?{/yellow-fg}{/bold}"),K.push("");let $=this.box.width-6,J=this.filePath.length>$?"..."+this.filePath.slice(-($-3)):this.filePath;K.push(`{white-fg}${J}{/white-fg}`),K.push(""),K.push("{gray-fg}Press {/gray-fg}{green-fg}y{/green-fg}{gray-fg} to confirm, {/gray-fg}{red-fg}n{/red-fg}{gray-fg} or Esc to cancel{/gray-fg}"),this.box.setContent(K.join(`
|
|
31
|
-
`)),this.screen.render()}close(){this.box.destroy()}focus(){this.box.focus()}}import{EventEmitter as s8}from"node:events";function u7(K,$,J){if(!K.trim())return{valid:!1,error:"Commit message cannot be empty"};if($===0&&!J)return{valid:!1,error:"No changes staged for commit"};return{valid:!0,error:null}}function p7(K){return K.trim()}var g7={message:"",amend:!1,isCommitting:!1,error:null,inputFocused:!1};class fK extends s8{_state={...g7};getHeadMessage;onCommit;onSuccess;stagedCount=0;constructor(K){super();this.getHeadMessage=K.getHeadMessage,this.onCommit=K.onCommit,this.onSuccess=K.onSuccess}get state(){return this._state}update(K){this._state={...this._state,...K},this.emit("change",this._state)}setStagedCount(K){this.stagedCount=K}setMessage(K){this.update({message:K,error:null})}setInputFocused(K){this.update({inputFocused:K}),this.emit("focus-change",K)}async toggleAmend(){let K=!this._state.amend;if(this.update({amend:K}),K&&!this._state.message)try{let $=await this.getHeadMessage();if($)this.update({message:$})}catch{}}async submit(){let K=u7(this._state.message,this.stagedCount,this._state.amend);if(!K.valid){this.update({error:K.error});return}this.update({isCommitting:!0,error:null});try{await this.onCommit(p7(this._state.message),this._state.amend),this.update({message:"",amend:!1,isCommitting:!1,inputFocused:!1}),this.onSuccess()}catch($){this.update({isCommitting:!1,error:$ instanceof Error?$.message:"Commit failed"})}}reset(){this._state={...g7},this.emit("change",this._state)}}import{EventEmitter as a8}from"node:events";var n8={currentPane:"files",bottomTab:"diff",selectedIndex:0,fileListScrollOffset:0,diffScrollOffset:0,historyScrollOffset:0,compareScrollOffset:0,explorerScrollOffset:0,explorerFileScrollOffset:0,historySelectedIndex:0,compareSelectedIndex:0,includeUncommitted:!1,explorerSelectedIndex:0,wrapMode:!1,autoTabEnabled:!1,mouseEnabled:!0,showMiddleDots:!1,hideHiddenFiles:!0,hideGitignored:!0,splitRatio:0.4,activeModal:null,pendingDiscard:null,commitInputFocused:!1};class hK extends a8{_state;constructor(K={}){super();this._state={...n8,...K}}get state(){return this._state}update(K){this._state={...this._state,...K},this.emit("change",this._state)}setPane(K){if(this._state.currentPane!==K)this.update({currentPane:K}),this.emit("pane-change",K)}setTab(K){if(this._state.bottomTab!==K){let $={diff:"files",commit:"commit",history:"history",compare:"compare",explorer:"explorer"};this.update({bottomTab:K,currentPane:$[K]}),this.emit("tab-change",K)}}setSelectedIndex(K){if(this._state.selectedIndex!==K)this.update({selectedIndex:K}),this.emit("selection-change",K)}setFileListScrollOffset(K){this.update({fileListScrollOffset:Math.max(0,K)}),this.emit("scroll-change",{type:"fileList",offset:K})}setDiffScrollOffset(K){this.update({diffScrollOffset:Math.max(0,K)}),this.emit("scroll-change",{type:"diff",offset:K})}setHistoryScrollOffset(K){this.update({historyScrollOffset:Math.max(0,K)}),this.emit("scroll-change",{type:"history",offset:K})}setCompareScrollOffset(K){this.update({compareScrollOffset:Math.max(0,K)}),this.emit("scroll-change",{type:"compare",offset:K})}setExplorerScrollOffset(K){this.update({explorerScrollOffset:Math.max(0,K)}),this.emit("scroll-change",{type:"explorer",offset:K})}setExplorerFileScrollOffset(K){this.update({explorerFileScrollOffset:Math.max(0,K)}),this.emit("scroll-change",{type:"explorerFile",offset:K})}setHistorySelectedIndex(K){this.update({historySelectedIndex:Math.max(0,K)})}setCompareSelectedIndex(K){this.update({compareSelectedIndex:Math.max(0,K)})}toggleIncludeUncommitted(){this.update({includeUncommitted:!this._state.includeUncommitted})}setExplorerSelectedIndex(K){this.update({explorerSelectedIndex:Math.max(0,K)})}toggleWrapMode(){this.update({wrapMode:!this._state.wrapMode,diffScrollOffset:0})}toggleAutoTab(){this.update({autoTabEnabled:!this._state.autoTabEnabled})}toggleMouse(){this.update({mouseEnabled:!this._state.mouseEnabled})}toggleMiddleDots(){this.update({showMiddleDots:!this._state.showMiddleDots})}toggleHideHiddenFiles(){this.update({hideHiddenFiles:!this._state.hideHiddenFiles})}toggleHideGitignored(){this.update({hideGitignored:!this._state.hideGitignored})}adjustSplitRatio(K){let $=Math.min(0.85,Math.max(0.15,this._state.splitRatio+K));this.update({splitRatio:$})}setSplitRatio(K){this.update({splitRatio:Math.min(0.85,Math.max(0.15,K))})}openModal(K){this.update({activeModal:K}),this.emit("modal-change",K)}closeModal(){this.update({activeModal:null}),this.emit("modal-change",null)}toggleModal(K){if(this._state.activeModal===K)this.closeModal();else this.openModal(K)}setPendingDiscard(K){this.update({pendingDiscard:K})}setCommitInputFocused(K){this.update({commitInputFocused:K})}togglePane(){let{bottomTab:K,currentPane:$}=this._state;if(K==="diff"||K==="commit")this.setPane($==="files"?"diff":"files");else if(K==="history")this.setPane($==="history"?"diff":"history");else if(K==="compare")this.setPane($==="compare"?"diff":"compare");else if(K==="explorer")this.setPane($==="explorer"?"diff":"explorer")}}import*as W from"node:path";import*as g from"node:fs";import{watch as z8}from"chokidar";import{EventEmitter as J9}from"node:events";import X9 from"ignore";class m7{queue=[];isProcessing=!1;pendingMutations=0;refreshScheduled=!1;enqueue(K){return new Promise(($,J)=>{this.queue.push({execute:K,resolve:$,reject:J}),this.processNext()})}enqueueMutation(K){return this.pendingMutations++,this.enqueue(K).finally(()=>{this.pendingMutations--})}hasPendingMutations(){return this.pendingMutations>0}scheduleRefresh(K){if(this.pendingMutations>0)return;if(this.refreshScheduled)return;this.refreshScheduled=!0,this.enqueue(async()=>{this.refreshScheduled=!1,await K()}).catch(()=>{this.refreshScheduled=!1})}isBusy(){return this.isProcessing||this.queue.length>0}async processNext(){if(this.isProcessing||this.queue.length===0)return;this.isProcessing=!0;let K=this.queue.shift();try{let $=await K.execute();K.resolve($)}catch($){K.reject($ instanceof Error?$:Error(String($)))}finally{this.isProcessing=!1,this.processNext()}}}var cK=new Map;function f7(K){let $=cK.get(K);if(!$)$=new m7,cK.set(K,$);return $}function h7(K){cK.delete(K)}import{simpleGit as y}from"simple-git";import*as l7 from"node:fs";import*as o7 from"node:path";function c7(K){let $=new Map;for(let J of K.trim().split(`
|
|
32
|
-
`)){if(!J)continue;let X=J.split("\t");if(X.length>=3){let Z=X[0]==="-"?0:parseInt(X[0],10),Q=X[1]==="-"?0:parseInt(X[1],10),q=X.slice(2).join("\t");$.set(q,{insertions:Z,deletions:Q})}}return $}async function i8(K,$){try{let J=o7.join(K,$);return(await l7.promises.readFile(J,"utf-8")).split(`
|
|
33
|
-
`).filter((Z)=>Z.length>0).length}catch{return 0}}function d7(K){switch(K){case"M":return"modified";case"A":return"added";case"D":return"deleted";case"?":return"untracked";case"R":return"renamed";case"C":return"copied";default:return"modified"}}async function r7(K){let $=y(K);try{if(!await $.checkIsRepo())return{files:[],branch:{current:"",ahead:0,behind:0},isRepo:!1};let X=await $.status(),Z=[];for(let G of X.staged)Z.push({path:G,status:"added",staged:!0});for(let G of X.modified)if(!Z.find((T)=>T.path===G&&T.staged))Z.push({path:G,status:"modified",staged:!1});for(let G of X.deleted)Z.push({path:G,status:"deleted",staged:!1});for(let G of X.not_added)Z.push({path:G,status:"untracked",staged:!1});for(let G of X.renamed)Z.push({path:G.to,originalPath:G.from,status:"renamed",staged:!0});let Q=[],q=new Set,Y=X.files.filter((G)=>G.working_dir==="?").map((G)=>G.path),V=await _K(K,Y);for(let G of X.files){if(G.index==="!"||G.working_dir==="!"||V.has(G.path))continue;let M=`${G.path}-${G.index!==" "&&G.index!=="?"}`;if(q.has(M))continue;if(q.add(M),G.index&&G.index!==" "&&G.index!=="?")Q.push({path:G.path,status:d7(G.index),staged:!0});if(G.working_dir&&G.working_dir!==" ")Q.push({path:G.path,status:G.working_dir==="?"?"untracked":d7(G.working_dir),staged:!1})}let[_,z]=await Promise.all([$.diff(["--cached","--numstat"]).catch(()=>""),$.diff(["--numstat"]).catch(()=>"")]),U=c7(_),B=c7(z);for(let G of Q){let M=G.staged?U.get(G.path):B.get(G.path);if(M)G.insertions=M.insertions,G.deletions=M.deletions}let k=Q.filter((G)=>G.status==="untracked");if(k.length>0){let G=await Promise.all(k.map((M)=>i8(K,M.path)));for(let M=0;M<k.length;M++)k[M].insertions=G[M],k[M].deletions=0}return{files:Q,branch:{current:X.current||"HEAD",tracking:X.tracking||void 0,ahead:X.ahead,behind:X.behind},isRepo:!0}}catch{return{files:[],branch:{current:"",ahead:0,behind:0},isRepo:!1}}}async function s7(K,$){await y(K).add($)}async function a7(K,$){await y(K).reset(["HEAD","--",$])}async function n7(K){await y(K).add("-A")}async function i7(K){await y(K).reset(["HEAD"])}async function t7(K,$){await y(K).checkout(["--",$])}async function e7(K,$,J=!1){await y(K).commit($,void 0,J?{"--amend":null}:void 0)}async function K8(K){let $=y(K);try{return(await $.log({n:1})).latest?.message||""}catch{return""}}async function $8(K,$=50){let J=y(K);try{return(await J.log({n:$})).all.map((Z)=>({hash:Z.hash,shortHash:Z.hash.slice(0,7),message:Z.message.split(`
|
|
34
|
-
`)[0],author:Z.author_name,date:new Date(Z.date),refs:Z.refs||""}))}catch{return[]}}import{execSync as t8}from"node:child_process";import{simpleGit as n}from"simple-git";function e8(K){let $=K.match(/^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/);if($)return{oldStart:parseInt($[1],10),newStart:parseInt($[2],10)};return null}function GK(K){let $=K.split(`
|
|
35
|
-
`),J=[],X=0,Z=0;for(let Q of $)if(Q.startsWith("diff --git")||Q.startsWith("index ")||Q.startsWith("---")||Q.startsWith("+++")||Q.startsWith("new file")||Q.startsWith("deleted file")||Q.startsWith("Binary files")||Q.startsWith("similarity index")||Q.startsWith("rename from")||Q.startsWith("rename to"))J.push({type:"header",content:Q});else if(Q.startsWith("@@")){let q=e8(Q);if(q)X=q.oldStart,Z=q.newStart;J.push({type:"hunk",content:Q})}else if(Q.startsWith("+"))J.push({type:"addition",content:Q,newLineNum:Z++});else if(Q.startsWith("-"))J.push({type:"deletion",content:Q,oldLineNum:X++});else J.push({type:"context",content:Q,oldLineNum:X++,newLineNum:Z++});return J}async function i(K,$,J=!1){let X=n(K);try{let Z=[];if(J)Z.push("--cached");if($)Z.push("--",$);let Q=await X.diff(Z),q=GK(Q);return{raw:Q,lines:q}}catch{return{raw:"",lines:[]}}}async function dK(K,$){try{let J=t8(`cat "${$}"`,{cwd:K,encoding:"utf-8"}),X=[{type:"header",content:`diff --git a/${$} b/${$}`},{type:"header",content:"new file mode 100644"},{type:"header",content:"--- /dev/null"},{type:"header",content:`+++ b/${$}`}],Z=J.split(`
|
|
36
|
-
`);X.push({type:"hunk",content:`@@ -0,0 +1,${Z.length} @@`});let Q=1;for(let Y of Z)X.push({type:"addition",content:"+"+Y,newLineNum:Q++});return{raw:X.map((Y)=>Y.content).join(`
|
|
37
|
-
`),lines:X}}catch{return{raw:"",lines:[]}}}async function lK(K){return i(K,void 0,!0)}async function oK(K){let $=n(K),J=new Set,X=[];try{let Z=await $.raw(["log","--oneline","--decorate=short","--all","-n","200"]),Q=/\(([^)]+)\)/g;for(let q of Z.split(`
|
|
38
|
-
`)){let Y=Q.exec(q);if(Y){let V=Y[1].split(",").map((_)=>_.trim());for(let _ of V){if(_.startsWith("HEAD")||_.startsWith("tag:")||!_.includes("/"))continue;let z=_.replace(/^.*-> /,"");if(z.includes("/")&&!J.has(z))J.add(z),X.push(z)}}Q.lastIndex=0}if(X.length>0)X.sort((q,Y)=>{let V=q.split("/").slice(1).join("/"),_=Y.split("/").slice(1).join("/"),z=V==="main"||V==="master",U=_==="main"||_==="master";if(z&&!U)return-1;if(!z&&U)return 1;if(z&&U){let B=q.startsWith("origin/"),k=Y.startsWith("origin/");if(B&&!k)return 1;if(!B&&k)return-1}return 0})}catch{}return[...new Set(X)]}async function J8(K){return(await oK(K))[0]??null}async function rK(K,$){let J=n(K),Z=(await J.raw(["merge-base",$,"HEAD"])).trim(),Q=await J.raw(["diff","--numstat",`${Z}...HEAD`]),q=await J.raw(["diff","--name-status",`${Z}...HEAD`]),Y=await J.raw(["diff",`${Z}...HEAD`]),V=Q.trim().split(`
|
|
39
|
-
`).filter((A)=>A),_=new Map;for(let A of V){let j=A.split("\t");if(j.length>=3){let O=j[0]==="-"?0:parseInt(j[0],10),D=j[1]==="-"?0:parseInt(j[1],10),I=j.slice(2).join("\t");_.set(I,{additions:O,deletions:D})}}let z=q.trim().split(`
|
|
40
|
-
`).filter((A)=>A),U=new Map;for(let A of z){let j=A.split("\t");if(j.length>=2){let O=j[0][0],D=j[j.length-1],I;switch(O){case"A":I="added";break;case"D":I="deleted";break;case"R":I="renamed";break;default:I="modified"}U.set(D,I)}}let B=[],k=Y.split(/(?=^diff --git )/m).filter((A)=>A.trim());for(let A of k){let j=A.match(/^diff --git a\/.+ b\/(.+)$/m);if(!j)continue;let O=j[1],D=GK(A),I=_.get(O)||{additions:0,deletions:0},P=U.get(O)||"modified";B.push({path:O,status:P,additions:I.additions,deletions:I.deletions,diff:{raw:A,lines:D}})}let G=0,M=0;for(let A of B)G+=A.additions,M+=A.deletions;let N=(await J.status()).files.length,E=(await J.log({from:Z,to:"HEAD"})).all.map((A)=>({hash:A.hash,shortHash:A.hash.slice(0,7),message:A.message.split(`
|
|
41
|
-
`)[0],author:A.author_name,date:new Date(A.date),refs:A.refs||""}));return{baseBranch:$,stats:{filesChanged:B.length,additions:G,deletions:M},files:B,commits:E,uncommittedCount:N}}async function sK(K,$){let J=n(K);try{let X=await J.raw(["show",$,"--format="]),Z=GK(X);return{raw:X,lines:Z}}catch{return{raw:"",lines:[]}}}async function X8(K,$){let J=n(K),X=await rK(K,$),Z=await J.diff(["--cached","--numstat"]),Q=await J.diff(["--numstat"]),q=await J.diff(["--cached"]),Y=await J.diff([]),V=new Map;for(let A of Z.trim().split(`
|
|
42
|
-
`).filter((j)=>j)){let j=A.split("\t");if(j.length>=3){let O=j[0]==="-"?0:parseInt(j[0],10),D=j[1]==="-"?0:parseInt(j[1],10),I=j.slice(2).join("\t");V.set(I,{additions:O,deletions:D,staged:!0,unstaged:!1})}}for(let A of Q.trim().split(`
|
|
43
|
-
`).filter((j)=>j)){let j=A.split("\t");if(j.length>=3){let O=j[0]==="-"?0:parseInt(j[0],10),D=j[1]==="-"?0:parseInt(j[1],10),I=j.slice(2).join("\t"),P=V.get(I);if(P)P.additions+=O,P.deletions+=D,P.unstaged=!0;else V.set(I,{additions:O,deletions:D,staged:!1,unstaged:!0})}}let _=await J.status(),z=new Map;for(let A of _.files)if(A.index==="A"||A.working_dir==="?")z.set(A.path,"added");else if(A.index==="D"||A.working_dir==="D")z.set(A.path,"deleted");else if(A.index==="R")z.set(A.path,"renamed");else z.set(A.path,"modified");let U=[],k=(q+Y).split(/(?=^diff --git )/m).filter((A)=>A.trim()),G=new Set;for(let A of k){let j=A.match(/^diff --git a\/.+ b\/(.+)$/m);if(!j)continue;let O=j[1];if(G.has(O))continue;G.add(O);let D=GK(A),I=V.get(O)||{additions:0,deletions:0},P=z.get(O)||"modified";U.push({path:O,status:P,additions:I.additions,deletions:I.deletions,diff:{raw:A,lines:D},isUncommitted:!0})}let M=new Set(X.files.map((A)=>A.path)),T=[];for(let A of X.files){let j=U.find((O)=>O.path===A.path);if(j)T.push(A),T.push(j);else T.push(A)}for(let A of U)if(!M.has(A.path))T.push(A);let N=0,H=0,E=new Set;for(let A of T){if(!E.has(A.path))E.add(A.path);N+=A.additions,H+=A.deletions}return{baseBranch:X.baseBranch,stats:{filesChanged:E.size,additions:N,deletions:H},files:T,commits:X.commits,uncommittedCount:X.uncommittedCount}}import*as S from"node:fs";import*as p from"node:path";import*as Z8 from"node:os";var AK=p.join(Z8.homedir(),".cache","diffstalker","base-branches.json");function K9(){let K=p.dirname(AK);if(!S.existsSync(K))S.mkdirSync(K,{recursive:!0})}function Q8(){try{if(S.existsSync(AK))return JSON.parse(S.readFileSync(AK,"utf-8"))}catch{}return{}}function $9(K){K9(),S.writeFileSync(AK,JSON.stringify(K,null,2)+`
|
|
44
|
-
`)}function q8(K){let $=Q8(),J=p.resolve(K);return $[J]}function Y8(K,$){let J=Q8(),X=p.resolve(K);J[X]=$,$9(J)}class V8 extends J9{repoPath;queue;gitWatcher=null;workingDirWatcher=null;ignorer=null;_state={status:null,diff:null,stagedDiff:"",selectedFile:null,isLoading:!1,error:null};_compareState={compareDiff:null,compareBaseBranch:null,compareLoading:!1,compareError:null};_historyState={commits:[],selectedCommit:null,commitDiff:null,isLoading:!1};_compareSelectionState={type:null,index:0,diff:null};constructor(K){super();this.repoPath=K,this.queue=f7(K)}get state(){return this._state}get compareState(){return this._compareState}get historyState(){return this._historyState}get compareSelectionState(){return this._compareSelectionState}updateState(K){this._state={...this._state,...K},this.emit("state-change",this._state)}updateCompareState(K){this._compareState={...this._compareState,...K},this.emit("compare-state-change",this._compareState)}updateHistoryState(K){this._historyState={...this._historyState,...K},this.emit("history-state-change",this._historyState)}updateCompareSelectionState(K){this._compareSelectionState={...this._compareSelectionState,...K},this.emit("compare-selection-change",this._compareSelectionState)}loadGitignore(){let K=X9();K.add(".git");let $=W.join(this.repoPath,".gitignore");if(g.existsSync($))K.add(g.readFileSync($,"utf-8"));let J=W.join(this.repoPath,".git","info","exclude");if(g.existsSync(J))K.add(g.readFileSync(J,"utf-8"));return K}startWatching(){let K=W.join(this.repoPath,".git");if(!g.existsSync(K))return;let $=W.join(K,"index"),J=W.join(K,"HEAD"),X=W.join(K,"refs"),Z=W.join(this.repoPath,".gitignore");this.gitWatcher=z8([$,J,X,Z],{persistent:!0,ignoreInitial:!0,usePolling:!0,interval:100}),this.ignorer=this.loadGitignore(),this.workingDirWatcher=z8(this.repoPath,{persistent:!0,ignoreInitial:!0,ignored:(q)=>{let Y=W.relative(this.repoPath,q);if(!Y)return!1;return this.ignorer?.ignores(Y)??!1},awaitWriteFinish:{stabilityThreshold:100,pollInterval:50}});let Q=()=>this.scheduleRefresh();this.gitWatcher.on("change",(q)=>{if(q===Z)this.ignorer=this.loadGitignore();Q()}),this.gitWatcher.on("add",Q),this.gitWatcher.on("unlink",Q),this.gitWatcher.on("error",(q)=>{let Y=q instanceof Error?q.message:String(q);this.emit("error",`Git watcher error: ${Y}`)}),this.workingDirWatcher.on("change",Q),this.workingDirWatcher.on("add",Q),this.workingDirWatcher.on("unlink",Q),this.workingDirWatcher.on("error",(q)=>{let Y=q instanceof Error?q.message:String(q);this.emit("error",`Working dir watcher error: ${Y}`)})}dispose(){this.gitWatcher?.close(),this.workingDirWatcher?.close(),h7(this.repoPath)}scheduleRefresh(){this.queue.scheduleRefresh(()=>this.doRefresh())}async refresh(){await this.queue.enqueue(()=>this.doRefresh())}async doRefresh(){this.updateState({isLoading:!0,error:null});try{let K=await r7(this.repoPath);if(!K.isRepo){this.updateState({status:K,diff:null,stagedDiff:"",isLoading:!1,error:"Not a git repository"});return}let[$,J]=await Promise.all([lK(this.repoPath),i(this.repoPath,void 0,!1)]),X,Z=this._state.selectedFile;if(Z){let Q=K.files.find((q)=>q.path===Z.path&&q.staged===Z.staged);if(Q)if(Q.status==="untracked")X=await dK(this.repoPath,Q.path);else X=await i(this.repoPath,Q.path,Q.staged);else X=J.raw?J:$,this.updateState({selectedFile:null})}else if(J.raw)X=J;else if($.raw)X=$;else X={raw:"",lines:[]};this.updateState({status:K,diff:X,stagedDiff:$.raw,isLoading:!1})}catch(K){this.updateState({isLoading:!1,error:K instanceof Error?K.message:"Unknown error"})}}async selectFile(K){if(this.updateState({selectedFile:K}),!this._state.status?.isRepo)return;await this.queue.enqueue(async()=>{if(K){let $;if(K.status==="untracked")$=await dK(this.repoPath,K.path);else $=await i(this.repoPath,K.path,K.staged);this.updateState({diff:$})}else{let $=await lK(this.repoPath);this.updateState({diff:$})}})}async stage(K){let $=this._state.status;if($)this.updateState({status:{...$,files:$.files.map((J)=>J.path===K.path&&!J.staged?{...J,staged:!0}:J)}});try{await this.queue.enqueueMutation(()=>s7(this.repoPath,K.path)),this.scheduleRefresh()}catch(J){await this.refresh(),this.updateState({error:`Failed to stage ${K.path}: ${J instanceof Error?J.message:String(J)}`})}}async unstage(K){let $=this._state.status;if($)this.updateState({status:{...$,files:$.files.map((J)=>J.path===K.path&&J.staged?{...J,staged:!1}:J)}});try{await this.queue.enqueueMutation(()=>a7(this.repoPath,K.path)),this.scheduleRefresh()}catch(J){await this.refresh(),this.updateState({error:`Failed to unstage ${K.path}: ${J instanceof Error?J.message:String(J)}`})}}async discard(K){if(K.staged||K.status==="untracked")return;try{await this.queue.enqueueMutation(()=>t7(this.repoPath,K.path)),await this.refresh()}catch($){this.updateState({error:`Failed to discard ${K.path}: ${$ instanceof Error?$.message:String($)}`})}}async stageAll(){try{await this.queue.enqueueMutation(()=>n7(this.repoPath)),await this.refresh()}catch(K){this.updateState({error:`Failed to stage all: ${K instanceof Error?K.message:String(K)}`})}}async unstageAll(){try{await this.queue.enqueueMutation(()=>i7(this.repoPath)),await this.refresh()}catch(K){this.updateState({error:`Failed to unstage all: ${K instanceof Error?K.message:String(K)}`})}}async commit(K,$=!1){try{await this.queue.enqueue(()=>e7(this.repoPath,K,$)),await this.refresh()}catch(J){this.updateState({error:`Failed to commit: ${J instanceof Error?J.message:String(J)}`})}}async getHeadCommitMessage(){return this.queue.enqueue(()=>K8(this.repoPath))}async refreshCompareDiff(K=!1){this.updateCompareState({compareLoading:!0,compareError:null});try{await this.queue.enqueue(async()=>{let $=this._compareState.compareBaseBranch;if(!$)$=q8(this.repoPath)??await J8(this.repoPath),this.updateCompareState({compareBaseBranch:$});if($){let J=K?await X8(this.repoPath,$):await rK(this.repoPath,$);this.updateCompareState({compareDiff:J,compareLoading:!1})}else this.updateCompareState({compareDiff:null,compareLoading:!1,compareError:"No base branch found"})})}catch($){this.updateCompareState({compareLoading:!1,compareError:`Failed to load compare diff: ${$ instanceof Error?$.message:String($)}`})}}async getCandidateBaseBranches(){return oK(this.repoPath)}async setCompareBaseBranch(K,$=!1){this.updateCompareState({compareBaseBranch:K}),Y8(this.repoPath,K),await this.refreshCompareDiff($)}async loadHistory(K=100){this.updateHistoryState({isLoading:!0});try{let $=await this.queue.enqueue(()=>$8(this.repoPath,K));this.updateHistoryState({commits:$,isLoading:!1})}catch($){this.updateHistoryState({isLoading:!1}),this.updateState({error:`Failed to load history: ${$ instanceof Error?$.message:String($)}`})}}async selectHistoryCommit(K){if(this.updateHistoryState({selectedCommit:K,commitDiff:null}),!K)return;try{await this.queue.enqueue(async()=>{let $=await sK(this.repoPath,K.hash);this.updateHistoryState({commitDiff:$})})}catch($){this.updateState({error:`Failed to load commit diff: ${$ instanceof Error?$.message:String($)}`})}}async selectCompareCommit(K){let $=this._compareState.compareDiff;if(!$||K<0||K>=$.commits.length){this.updateCompareSelectionState({type:null,index:0,diff:null});return}let J=$.commits[K];this.updateCompareSelectionState({type:"commit",index:K,diff:null});try{await this.queue.enqueue(async()=>{let X=await sK(this.repoPath,J.hash);this.updateCompareSelectionState({diff:X})})}catch(X){this.updateState({error:`Failed to load commit diff: ${X instanceof Error?X.message:String(X)}`})}}selectCompareFile(K){let $=this._compareState.compareDiff;if(!$||K<0||K>=$.files.length){this.updateCompareSelectionState({type:null,index:0,diff:null});return}let J=$.files[K];this.updateCompareSelectionState({type:"file",index:K,diff:J.diff})}}var kK=new Map;function U8(K){let $=kK.get(K);if(!$)$=new V8(K),kK.set(K,$);return $}function aK(K){let $=kK.get(K);if($)$.dispose(),kK.delete(K)}import*as m from"node:fs";import*as jK from"node:path";import{watch as Z9}from"chokidar";import{EventEmitter as Q9}from"node:events";import*as _8 from"node:path";import*as nK from"node:os";function B8(K){if(K.startsWith("~/"))return _8.join(nK.homedir(),K.slice(2));if(K==="~")return nK.homedir();return K}function iK(K){let $=K.split(`
|
|
45
|
-
`);for(let J=$.length-1;J>=0;J--){let X=$[J].trim();if(X)return X}return""}class tK extends Q9{targetFile;debug;watcher=null;debounceTimer=null;lastReadPath=null;_state={path:null,lastUpdate:null,rawContent:null,sourceFile:null};constructor(K,$=!1){super();this.targetFile=K,this.debug=$,this._state.sourceFile=K}get state(){return this._state}updateState(K){this._state={...this._state,...K},this.emit("path-change",this._state)}processContent(K){if(!K)return null;let $=B8(K);return jK.isAbsolute($)?$:jK.resolve($)}readTargetDebounced(){if(this.debounceTimer)clearTimeout(this.debounceTimer);this.debounceTimer=setTimeout(()=>{this.readTarget()},100)}readTarget(){try{let K=m.readFileSync(this.targetFile,"utf-8"),$=iK(K);if($&&$!==this.lastReadPath){let J=this.processContent($),X=new Date;if(this.debug&&J)process.stderr.write(`[diffstalker ${X.toISOString()}] Path change detected
|
|
2
|
+
import g9 from"neo-blessed";import a from"neo-blessed";var D7=0.05;function f9($,J,K,Z=1){let X=Z+4,Q=$-X,z=Math.floor(Q*K),q=Q-z;return{width:J,height:$,headerHeight:Z,topPaneHeight:z,bottomPaneHeight:q,footerRow:$-1}}function h9($,J,K,Z){let X=J+1,Q=X+K,z=Q+1,q=z+Z,Y=$-1;return{stagingPaneStart:X,fileListEnd:Q,diffPaneStart:z,diffPaneEnd:q,footerRow:Y}}class v7{screen;headerBox;topSeparator;topPane;middleSeparator;bottomPane;bottomSeparator;footerBox;_dimensions;_splitRatio;constructor($,J=0.4){this.screen=$,this._splitRatio=J,this._dimensions=this.calculateDimensions(),this.headerBox=this.createHeaderBox(),this.topSeparator=this.createSeparator(this._dimensions.headerHeight),this.topPane=this.createTopPane(),this.middleSeparator=this.createSeparator(this._dimensions.headerHeight+1+this._dimensions.topPaneHeight),this.bottomPane=this.createBottomPane(),this.bottomSeparator=this.createSeparator(this._dimensions.headerHeight+2+this._dimensions.topPaneHeight+this._dimensions.bottomPaneHeight),this.footerBox=this.createFooterBox(),$.on("resize",()=>this.onResize())}get dimensions(){return this._dimensions}get splitRatio(){return this._splitRatio}setSplitRatio($){this._splitRatio=Math.min(0.85,Math.max(0.15,$)),this.updateLayout()}adjustSplitRatio($){this.setSplitRatio(this._splitRatio+$)}calculateDimensions(){let $=this.screen.height||24,J=this.screen.width||80;return f9($,J,this._splitRatio)}createHeaderBox(){return a.box({parent:this.screen,top:0,left:0,width:"100%",height:this._dimensions.headerHeight,tags:!0})}createSeparator($){let J=this.screen.width||80;return a.box({parent:this.screen,top:$,left:0,width:"100%",height:1,content:"─".repeat(J),style:{fg:"gray"}})}createTopPane(){return a.box({parent:this.screen,top:this._dimensions.headerHeight+1,left:0,width:"100%",height:this._dimensions.topPaneHeight,tags:!0,scrollable:!0,alwaysScroll:!0,wrap:!1,scrollbar:{ch:" ",track:{bg:"gray"},style:{inverse:!0}}})}createBottomPane(){return a.box({parent:this.screen,top:this._dimensions.headerHeight+2+this._dimensions.topPaneHeight,left:0,width:"100%",height:this._dimensions.bottomPaneHeight,tags:!0,scrollable:!0,alwaysScroll:!0,wrap:!1,scrollbar:{ch:" ",track:{bg:"gray"},style:{inverse:!0}}})}createFooterBox(){return a.box({parent:this.screen,top:this._dimensions.footerRow,left:0,width:"100%",height:1,tags:!0})}onResize(){this._dimensions=this.calculateDimensions(),this.updateLayout()}updateLayout(){this._dimensions=this.calculateDimensions();let $=this.screen.width||80;this.headerBox.height=this._dimensions.headerHeight,this.headerBox.width=$,this.topSeparator.top=this._dimensions.headerHeight,this.topSeparator.width=$,this.topSeparator.setContent("─".repeat($)),this.topPane.top=this._dimensions.headerHeight+1,this.topPane.height=this._dimensions.topPaneHeight,this.topPane.width=$,this.middleSeparator.top=this._dimensions.headerHeight+1+this._dimensions.topPaneHeight,this.middleSeparator.width=$,this.middleSeparator.setContent("─".repeat($)),this.bottomPane.top=this._dimensions.headerHeight+2+this._dimensions.topPaneHeight,this.bottomPane.height=this._dimensions.bottomPaneHeight,this.bottomPane.width=$,this.bottomSeparator.top=this._dimensions.headerHeight+2+this._dimensions.topPaneHeight+this._dimensions.bottomPaneHeight,this.bottomSeparator.width=$,this.bottomSeparator.setContent("─".repeat($)),this.footerBox.top=this._dimensions.footerRow,this.footerBox.width=$}getPaneBoundaries(){return h9(this._dimensions.height,this._dimensions.headerHeight,this._dimensions.topPaneHeight,this._dimensions.bottomPaneHeight)}screenYToTopPaneRow($){let J=this._dimensions.headerHeight+1,K=J+this._dimensions.topPaneHeight;if($<J||$>=K)return-1;return $-J}screenYToBottomPaneRow($){let J=this._dimensions.headerHeight+2+this._dimensions.topPaneHeight,K=J+this._dimensions.bottomPaneHeight;if($<J||$>=K)return-1;return $-J}get topPaneTop(){return this._dimensions.headerHeight+1}get bottomPaneTop(){return this._dimensions.headerHeight+2+this._dimensions.topPaneHeight}}function s($){let J=$.filter((X)=>!X.staged&&X.status!=="untracked"),K=$.filter((X)=>!X.staged&&X.status==="untracked"),Z=$.filter((X)=>X.staged);return{modified:J,untracked:K,staged:Z,ordered:[...J,...K,...Z]}}function n($,J){let{modified:K,untracked:Z}=s($),X=K.length,Q=Z.length;if(J<X)return{category:"modified",categoryIndex:J};if(J<X+Q)return{category:"untracked",categoryIndex:J-X};return{category:"staged",categoryIndex:J-X-Q}}function Y8($,J,K){let{modified:Z,untracked:X,staged:Q,ordered:z}=s($);if(z.length===0)return 0;let Y={modified:Z,untracked:X,staged:Q}[J];if(Y.length===0)return z.length-1;let U=Math.min(K,Y.length-1);return{modified:0,untracked:Z.length,staged:Z.length+X.length}[J]+U}function A7($,J){if($.length<=J)return $;let Z=Math.max(J,20);if($.length<=Z)return $;let X=$.split("/");if(X.length===1){let B=Math.floor((Z-1)/2);return $.slice(0,B)+"…"+$.slice(-(Z-B-1))}let Q=X[X.length-1],z=X[0],q="/…/";if(z.length+q.length+Q.length>Z){let B=Z-2;if(Q.length>B){let G=Math.floor((B-1)/2);return"…/"+Q.slice(0,G)+"…"+Q.slice(-(B-G-1))}return"…/"+Q}let U=z,V=1;while(V<X.length-1){let B=X[V],G=U+"/"+B;if(G.length+q.length+Q.length<=Z)U=G,V++;else break}if(V===X.length-1)return $;return U+q+Q}function c9($){switch($){case"modified":return"M";case"added":return"A";case"deleted":return"D";case"untracked":return"?";case"renamed":return"R";case"copied":return"C";default:return" "}}function d9($){switch($){case"modified":return"yellow";case"added":return"green";case"deleted":return"red";case"untracked":return"gray";case"renamed":return"blue";case"copied":return"cyan";default:return"white"}}function l9($,J){if($===void 0&&J===void 0)return"";let K=[];if($!==void 0&&$>0)K.push(`{green-fg}+${$}{/green-fg}`);if(J!==void 0&&J>0)K.push(`{red-fg}-${J}{/red-fg}`);return K.length>0?" "+K.join(" "):""}function K7($){let{modified:J,untracked:K,staged:Z}=s($),X=[],Q=0;if(J.length>0)X.push({type:"header",content:"Modified:",headerColor:"yellow"}),J.forEach((z)=>{X.push({type:"file",file:z,fileIndex:Q++})});if(K.length>0){if(J.length>0)X.push({type:"spacer"});X.push({type:"header",content:"Untracked:",headerColor:"gray"}),K.forEach((z)=>{X.push({type:"file",file:z,fileIndex:Q++})})}if(Z.length>0){if(J.length>0||K.length>0)X.push({type:"spacer"});X.push({type:"header",content:"Staged:",headerColor:"green"}),Z.forEach((z)=>{X.push({type:"file",file:z,fileIndex:Q++})})}return X}function U8($,J,K,Z,X=0,Q){if($.length===0)return"{gray-fg} No changes{/gray-fg}";let z=K7($),q=Z-12,Y=Q?z.slice(X,X+Q):z.slice(X),U=[];for(let V of Y)if(V.type==="header")U.push(`{bold}{${V.headerColor}-fg}${V.content}{/${V.headerColor}-fg}{/bold}`);else if(V.type==="spacer")U.push("");else if(V.type==="file"&&V.file&&V.fileIndex!==void 0){let B=V.file,k=V.fileIndex===J&&K,j=c9(B.status),O=d9(B.status),D=B.staged?"[-]":"[+]",H=B.staged?"red":"green",A=l9(B.insertions,B.deletions),T=A.replace(/\{[^}]+\}/g,"").length,_=q-T,M=A7(B.path,_),v="";if(k)v+="{cyan-fg}{bold}▸ {/bold}{/cyan-fg}";else v+=" ";if(v+=`{${H}-fg}${D}{/${H}-fg} `,v+=`{${O}-fg}${j}{/${O}-fg} `,k)v+=`{cyan-fg}{inverse}${M}{/inverse}{/cyan-fg}`;else v+=M;if(B.originalPath)v+=` {gray-fg}← ${A7(B.originalPath,30)}{/gray-fg}`;v+=A,U.push(v)}return U.join(`
|
|
3
|
+
`)}function B8($){return K7($).length}function b($,J){let{ordered:K}=s($);return K[J]??null}function G8($,J){let Z=K7(J)[$];if(Z?.type==="file"&&Z.fileIndex!==void 0)return Z.fileIndex;return null}function T7($,J){let K=K7(J);for(let Z=0;Z<K.length;Z++)if(K[Z].type==="file"&&K[Z].fileIndex===$)return Z;return 0}function j8($,J,K){$.key(["q","C-c"],()=>{J.exit()}),$.key(["j","down"],()=>{if(K.hasActiveModal())return;J.navigateDown()}),$.key(["k","up"],()=>{if(K.hasActiveModal())return;J.navigateUp()});let Z=[["1","diff"],["2","commit"],["3","history"],["4","compare"],["5","explorer"]];for(let[X,Q]of Z)$.key([X],()=>{if(K.hasActiveModal())return;K.uiState.setTab(Q)});$.key(["tab"],()=>{if(K.hasActiveModal())return;K.uiState.togglePane()}),$.key(["s"],()=>{if(K.hasActiveModal())return;J.stageSelected()}),$.key(["S-u"],()=>{if(K.hasActiveModal())return;J.unstageSelected()}),$.key(["S-a"],()=>{if(K.hasActiveModal())return;J.stageAll()}),$.key(["S-z"],()=>{if(K.hasActiveModal())return;J.unstageAll()}),$.key(["enter","space"],()=>{if(K.hasActiveModal())return;if(K.getBottomTab()==="explorer"&&K.getCurrentPane()==="explorer")J.enterExplorerDirectory();else J.toggleSelected()}),$.key(["backspace"],()=>{if(K.hasActiveModal())return;if(K.getBottomTab()==="explorer"&&K.getCurrentPane()==="explorer")J.goExplorerUp()}),$.key(["g"],()=>{if(K.hasActiveModal())return;if(K.getBottomTab()==="explorer")K.explorerManager?.toggleShowOnlyChanges()}),$.key(["/"],()=>{if(K.hasActiveModal())return;if(K.getBottomTab()==="explorer")J.openFileFinder()}),$.key(["c"],()=>{if(K.hasActiveModal())return;K.uiState.setTab("commit")}),$.key(["i"],()=>{if(K.getBottomTab()==="commit"&&!K.isCommitInputFocused())J.focusCommitInput()}),$.key(["a"],()=>{if(K.getBottomTab()==="commit"&&!K.isCommitInputFocused())K.commitFlowState.toggleAmend(),J.render();else K.uiState.toggleAutoTab()}),$.key(["escape"],()=>{if(K.getBottomTab()==="commit")if(K.isCommitInputFocused())J.unfocusCommitInput();else K.uiState.setTab("diff")}),$.key(["r"],()=>J.refresh()),$.key(["w"],()=>K.uiState.toggleWrapMode()),$.key(["m"],()=>J.toggleMouseMode()),$.key(["S-t"],()=>K.uiState.toggleAutoTab()),$.key(["-","_","["],()=>{K.uiState.adjustSplitRatio(-D7),K.layout.setSplitRatio(K.uiState.state.splitRatio),J.render()}),$.key(["=","+","]"],()=>{K.uiState.adjustSplitRatio(D7),K.layout.setSplitRatio(K.uiState.state.splitRatio),J.render()}),$.key(["t"],()=>K.uiState.openModal("theme")),$.key(["?"],()=>K.uiState.toggleModal("hotkeys")),$.key(["f"],()=>J.toggleFollow()),$.key(["b"],()=>{if(K.getBottomTab()==="compare")K.uiState.openModal("baseBranch")}),$.key(["u"],()=>{if(K.getBottomTab()==="compare"){K.uiState.toggleIncludeUncommitted();let X=K.uiState.state.includeUncommitted;K.gitManager?.refreshCompareDiff(X)}}),$.key(["d"],()=>{if(K.getBottomTab()==="diff"){let X=K.getStatusFiles(),Q=b(X,K.getSelectedIndex());if(Q&&!Q.staged&&Q.status!=="untracked")J.showDiscardConfirm(Q)}})}function Z7($){let K=new Date().getTime()-$.getTime(),Z=Math.floor(K/3600000),X=Math.floor(K/86400000);if(Z<1)return`${Math.floor(K/60000)}m ago`;else if(Z<48)return`${Z}h ago`;else if(X<=14)return`${X}d ago`;else return $.toLocaleDateString("en-US",{month:"short",day:"numeric"})}function k8($){return $.toLocaleString("en-US",{year:"numeric",month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"})}function o9($,J){if($.length<=J)return $;if(J<=3)return $.slice(0,J);return $.slice(0,J-3)+"..."}function X7($,J,K,Z=20){let X=J||"",Q=Math.max(0,K-Z-1),z=X;if(z.length>Q&&Q>3)z=z.slice(0,Q-3)+"...";else if(z.length>Q)z="";let q=z?z.length+1:0,Y=Math.max(Z,K-q);return{displayMessage:o9($,Y),displayRefs:z}}function _8($,J,K,Z,X=0,Q){if($.length===0)return"{gray-fg}No commits yet{/gray-fg}";let z=Q?$.slice(X,X+Q):$.slice(X),q=[];for(let Y=0;Y<z.length;Y++){let U=z[Y],G=X+Y===J&&K,k=Z7(U.date),j=11+k.length+2+2,O=Math.max(10,Z-j),{displayMessage:D,displayRefs:H}=X7(U.message,U.refs,O),A="";if(G)A+="{cyan-fg}{bold}▸ {/bold}{/cyan-fg}";else A+=" ";if(A+=`{yellow-fg}${U.shortHash}{/yellow-fg} `,G)A+=`{cyan-fg}{inverse}${N7(D)}{/inverse}{/cyan-fg}`;else A+=N7(D);if(A+=` {gray-fg}(${k}){/gray-fg}`,H)A+=` {green-fg}${N7(H)}{/green-fg}`;q.push(A)}return q.join(`
|
|
4
|
+
`)}function N7($){return $.replace(/\{/g,"{{").replace(/\}/g,"}}")}function O8($,J){return $[J]??null}function M8($){let J={name:"",fullPath:"",isDirectory:!0,children:[],depth:0};for(let K=0;K<$.length;K++){let X=$[K].path.split("/"),Q=J;for(let z=0;z<X.length;z++){let q=X[z],Y=z===X.length-1,U=X.slice(0,z+1).join("/"),V=Q.children.find((B)=>B.name===q&&B.isDirectory===!Y);if(!V)V={name:q,fullPath:U,isDirectory:!Y,children:[],depth:Q.depth+1,fileIndex:Y?K:void 0},Q.children.push(V);Q=V}}return H8(J),v8(J),J}function H8($){for(let J of $.children)H8(J);for(let J=0;J<$.children.length;J++){let K=$.children[J];while(K.isDirectory&&K.children.length===1&&K.children[0].isDirectory){let Z=K.children[0];K.name=`${K.name}/${Z.name}`,K.fullPath=Z.fullPath,K.children=Z.children,D8(K,K.depth)}}}function D8($,J){$.depth=J;for(let K of $.children)D8(K,J+1)}function v8($){$.children.sort((J,K)=>{if(J.isDirectory&&!K.isDirectory)return-1;if(!J.isDirectory&&K.isDirectory)return 1;return J.name.localeCompare(K.name)});for(let J of $.children)v8(J)}function A8($){let J=[];function K(Z,X){for(let Q=0;Q<Z.children.length;Q++){let z=Z.children[Q],q=Q===Z.children.length-1;if(J.push({type:z.isDirectory?"directory":"file",name:z.name,fullPath:z.fullPath,depth:z.depth-1,fileIndex:z.fileIndex,isLast:q,parentIsLast:[...X]}),z.isDirectory)K(z,[...X,q])}}return K($,[]),J}function W7($){let J="";for(let K=0;K<$.depth;K++)if($.parentIsLast[K])J+=" ";else J+="│ ";if($.depth>=0)if($.isLast)J+="└ ";else J+="├ ";return J}var L="\x1B[0m",r9="\x1B[1m",d="\x1B[90m",L7="\x1B[36m",N8="\x1B[33m",E7="\x1B[32m",T8="\x1B[31m",W8="\x1B[34m",F7="\x1B[35m",F8="\x1B[7m";function i($,J,K=!0,Z=!0){let X=[];if($.length>0){if(X.push({type:"section-header",sectionType:"commits"}),K)$.forEach((Q,z)=>{X.push({type:"commit",commitIndex:z,commit:Q})})}if(J.length>0){if($.length>0)X.push({type:"spacer"});if(X.push({type:"section-header",sectionType:"files"}),Z){let Q=M8(J),z=A8(Q);for(let q of z)if(q.type==="directory")X.push({type:"directory",treeRow:q});else{let Y=J[q.fileIndex];X.push({type:"file",fileIndex:q.fileIndex,file:Y,treeRow:q})}}}return X}function a9($,J,K,Z){let X=J&&K,Q=Z7($.date),z=13+Q.length+2,q=Math.max(10,Z-z),{displayMessage:Y,displayRefs:U}=X7($.message,$.refs,q),V=` ${N8}${$.shortHash}${L} `;if(X)V+=`${L7}${F8}${Y}${L}`;else V+=Y;if(V+=` ${d}(${Q})${L}`,U)V+=` ${E7}${U}${L}`;return`{escape}${V}{/escape}`}function s9($,J){let K=W7($),Z="▸ ",X=J-K.length-2-2,Q=$.name;if(Q.length>X)Q=Q.slice(0,X-1)+"…";return`{escape}${`${d}${K}${L}${W8}${"▸ "}${Q}${L}`}{/escape}`}function n9($,J,K,Z,X){let Q=K&&Z,z=$.isUncommitted??!1,q=W7(J),Y={added:E7,modified:N8,deleted:T8,renamed:W8},U={added:"+",modified:"●",deleted:"−",renamed:"→"},V=z?F7:Y[$.status],B=U[$.status],G=`(+${$.additions} -${$.deletions})`,k=z?" [uncommitted]":"",j=q.length+2+G.length+k.length+2,O=Math.max(5,X-j),D=J.name;if(D.length>O)D=D.slice(0,O-1)+"…";let H=`${d}${q}${L}`;if(H+=`${V}${B}${L} `,Q)H+=`${L7}${F8}${D}${L}`;else if(z)H+=`${F7}${D}${L}`;else H+=D;if(H+=` ${d}(${E7}+${$.additions}${L} ${T8}-${$.deletions}${d})${L}`,z)H+=` ${F7}[uncommitted]${L}`;return`{escape}${H}{/escape}`}function E8($,J,K,Z,X,Q=0,z){if($.length===0&&J.length===0)return"{gray-fg}No changes compared to base branch{/gray-fg}";let q=i($,J),Y=z?q.slice(Q,Q+z):q.slice(Q),U=[];for(let V of Y)if(V.type==="section-header"){let B=V.sectionType==="commits",G=B?$.length:J.length,k=B?"Commits":"Files";U.push(`{escape}${L7}${r9}▼ ${k}${L} ${d}(${G})${L}{/escape}`)}else if(V.type==="spacer")U.push("");else if(V.type==="commit"&&V.commit&&V.commitIndex!==void 0){let B=K?.type==="commit"&&K.index===V.commitIndex;U.push(a9(V.commit,B,Z,X))}else if(V.type==="directory"&&V.treeRow)U.push(s9(V.treeRow,X));else if(V.type==="file"&&V.file&&V.fileIndex!==void 0&&V.treeRow){let B=K?.type==="file"&&K.index===V.fileIndex;U.push(n9(V.file,V.treeRow,B,Z,X))}return U.join(`
|
|
5
|
+
`)}function L8($,J,K=!0,Z=!0){return i($,J,K,Z).length}function I7($,J,K,Z=!0,X=!0){let z=i(J,K,Z,X)[$];if(!z)return null;if(z.type==="commit"&&z.commitIndex!==void 0)return{type:"commit",index:z.commitIndex};if(z.type==="file"&&z.fileIndex!==void 0)return{type:"file",index:z.fileIndex};return null}function Q7($,J,K,Z=!0,X=!0){let Q=i(J,K,Z,X);for(let z=0;z<Q.length;z++){let q=Q[z];if($.type==="commit"&&q.type==="commit"&&q.commitIndex===$.index)return z;if($.type==="file"&&q.type==="file"&&q.fileIndex===$.index)return z}return 0}function R7($,J,K,Z){let X=i(J,K),Q=0;if($)Q=Q7($,J,K);let z=Z==="down"?1:-1,q=Q+z;while(q>=0&&q<X.length){let Y=I7(q,J,K);if(Y)return Y;q+=z}return $}function i9($){let J="";for(let K=0;K<$.depth;K++)if($.parentIsLast[K])J+=" ";else J+="│ ";if($.depth>0||$.parentIsLast.length===0)if($.isLast)J+="└ ";else J+="├ ";return J}function t9($){if(!$)return"";switch($){case"modified":return"M";case"added":return"A";case"deleted":return"D";case"untracked":return"?";case"renamed":return"R";case"copied":return"C";default:return""}}function e9($){if(!$)return"\x1B[0m";switch($){case"modified":return"\x1B[33m";case"added":return"\x1B[32m";case"deleted":return"\x1B[31m";case"untracked":return"\x1B[90m";case"renamed":return"\x1B[34m";case"copied":return"\x1B[35m";default:return"\x1B[0m"}}function I8($,J,K,Z,X=0,Q,z=!1,q=null){if(q)return`{red-fg}Error: ${$$(q)}{/red-fg}`;if(z)return"{gray-fg}Loading...{/gray-fg}";if($.length===0)return"{gray-fg}(empty directory){/gray-fg}";let Y=Q?$.slice(X,X+Q):$.slice(X),U=[];for(let V=0;V<Y.length;V++){let B=Y[V],j=X+V===J&&K,O=B.node,D=i9(B),H="";if(O.isDirectory)H=O.expanded?"▾ ":"▸ ";let A=t9(O.gitStatus),T=e9(O.gitStatus),_=A?`${T}${A}\x1B[0m `:"",M=O.isDirectory&&O.hasChangedChildren?`${"\x1B[33m"}●${"\x1B[0m"} `:"",v=D.length+H.length+(A?2:0)+(O.hasChangedChildren&&O.isDirectory?2:0),W=Math.max(5,Z-v-2),N=O.isDirectory?`${O.name}/`:O.name;if(N.length>W)N=N.slice(0,W-1)+"…";let F=`\x1B[90m${D}\x1B[0m`;if(O.isDirectory)if(F+=`\x1B[34m${H}\x1B[0m`,F+=M,j)F+=`\x1B[36m\x1B[1m\x1B[7m${N}\x1B[0m`;else F+=`\x1B[34m${N}\x1B[0m`;else if(F+=_,j)F+=`\x1B[36m\x1B[1m\x1B[7m${N}\x1B[0m`;else if(O.gitStatus)F+=`${T}${N}\x1B[0m`;else F+=N;U.push(`{escape}${F}{/escape}`)}return U.join(`
|
|
6
|
+
`)}function $$($){return $.replace(/\{/g,"{{").replace(/\}/g,"}}")}function R8($){return $.length}var J$={name:"dark",displayName:"Dark",colors:{addBg:"#022800",delBg:"#3D0100",addHighlight:"#044700",delHighlight:"#5C0200",text:"white",addLineNum:"#368F35",delLineNum:"#A14040",contextLineNum:"gray",addSymbol:"greenBright",delSymbol:"redBright"}},K$={name:"light",displayName:"Light",colors:{addBg:"#69db7c",delBg:"#ffa8b4",addHighlight:"#2f9d44",delHighlight:"#d1454b",text:"black",addLineNum:"#2f9d44",delLineNum:"#d1454b",contextLineNum:"#6c757d",addSymbol:"green",delSymbol:"red"}},Z$={name:"dark-colorblind",displayName:"Dark (colorblind)",colors:{addBg:"#004466",delBg:"#660000",addHighlight:"#0077b3",delHighlight:"#b30000",text:"white",addLineNum:"#0077b3",delLineNum:"#b30000",contextLineNum:"gray",addSymbol:"cyanBright",delSymbol:"redBright"}},X$={name:"light-colorblind",displayName:"Light (colorblind)",colors:{addBg:"#99ccff",delBg:"#ffcccc",addHighlight:"#3366cc",delHighlight:"#993333",text:"black",addLineNum:"#3366cc",delLineNum:"#993333",contextLineNum:"#6c757d",addSymbol:"blue",delSymbol:"red"}},Q$={name:"dark-ansi",displayName:"Dark (ANSI)",colors:{addBg:"green",delBg:"red",addHighlight:"greenBright",delHighlight:"redBright",text:"white",addLineNum:"greenBright",delLineNum:"redBright",contextLineNum:"gray",addSymbol:"greenBright",delSymbol:"redBright"}},z$={name:"light-ansi",displayName:"Light (ANSI)",colors:{addBg:"green",delBg:"red",addHighlight:"greenBright",delHighlight:"redBright",text:"black",addLineNum:"green",delLineNum:"red",contextLineNum:"gray",addSymbol:"green",delSymbol:"red"}},z7={dark:J$,light:K$,"dark-colorblind":Z$,"light-colorblind":X$,"dark-ansi":Q$,"light-ansi":z$},x=["dark","light","dark-colorblind","light-colorblind","dark-ansi","light-ansi"];function t($){return z7[$]??z7.dark}function q$($){return!($.startsWith("index ")||$.startsWith("--- ")||$.startsWith("+++ ")||$.startsWith("similarity index"))}function y8($){if($.type!=="header")return!0;return q$($.content)}function q7($,J,K=!0){if(J<=0)return[{text:$,isContinuation:!1}];if($.length<=J)return[{text:$,isContinuation:!1}];let Z=[],X=$,Q=!0;while(X.length>0){if(X.length<=J){Z.push({text:X,isContinuation:!Q});break}Z.push({text:X.slice(0,J),isContinuation:!Q}),X=X.slice(J),Q=!1}if(K)V$($,J,Z);return Z}function V$($,J,K){let Z=K.map((X)=>X.text).join("");if(Z!==$)throw Error(`[LineBreaking] Content was lost during breaking!
|
|
7
|
+
Original (${$.length} chars): "${$.slice(0,50)}${$.length>50?"...":""}"
|
|
8
|
+
Joined (${Z.length} chars): "${Z.slice(0,50)}${Z.length>50?"...":""}"`);for(let X=0;X<K.length;X++){let Q=K[X];if(Q.text.length>J&&J>=1)throw Error(`[LineBreaking] Segment ${X} exceeds maxWidth!
|
|
9
|
+
Segment length: ${Q.text.length}, maxWidth: ${J}
|
|
10
|
+
Segment: "${Q.text.slice(0,50)}${Q.text.length>50?"...":""}"`)}if(K.length>0&&K[0].isContinuation)throw Error("[LineBreaking] First segment incorrectly marked as continuation!");for(let X=1;X<K.length;X++)if(!K[X].isContinuation)throw Error(`[LineBreaking] Segment ${X} should be marked as continuation but isn't!`)}function y7($,J){if(J<=0)return 1;if($.length<=J)return 1;return Math.ceil($.length/J)}import l from"fast-diff";function C8($,J){if(!$||!J)return!1;let K=l($,J),Z=0,X=0;for(let[z,q]of K)if(X+=q.length,z===l.EQUAL)Z+=q.length;if(X===0)return!1;return Z/X>=0.5}function P8($,J){let K=l($,J),Z=[],X=[];for(let[Q,z]of K)if(Q===l.EQUAL)Z.push({text:z,type:"same"}),X.push({text:z,type:"same"});else if(Q===l.DELETE)Z.push({text:z,type:"changed"});else if(Q===l.INSERT)X.push({text:z,type:"changed"});return{oldSegments:Z,newSegments:X}}import{createEmphasize as Y$}from"emphasize";import{common as U$}from"lowlight";var P7=Y$(U$),B$={ts:"typescript",tsx:"typescript",js:"javascript",jsx:"javascript",mjs:"javascript",cjs:"javascript",html:"xml",htm:"xml",xml:"xml",svg:"xml",css:"css",scss:"scss",sass:"scss",less:"less",json:"json",yaml:"yaml",yml:"yaml",toml:"ini",sh:"bash",bash:"bash",zsh:"bash",fish:"bash",ps1:"powershell",bat:"dos",cmd:"dos",c:"c",h:"c",cpp:"cpp",hpp:"cpp",cc:"cpp",cxx:"cpp",rs:"rust",go:"go",zig:"zig",java:"java",kt:"kotlin",kts:"kotlin",scala:"scala",groovy:"groovy",gradle:"groovy",py:"python",rb:"ruby",pl:"perl",lua:"lua",php:"php",r:"r",hs:"haskell",ml:"ocaml",fs:"fsharp",fsx:"fsharp",ex:"elixir",exs:"elixir",erl:"erlang",clj:"clojure",cljs:"clojure",cs:"csharp",vb:"vbnet",md:"markdown",markdown:"markdown",rst:"plaintext",txt:"plaintext",Makefile:"makefile",Dockerfile:"dockerfile",cmake:"cmake",ini:"ini",conf:"ini",cfg:"ini",sql:"sql",vim:"vim",diff:"diff",patch:"diff"},S8={Makefile:"makefile",makefile:"makefile",GNUmakefile:"makefile",Dockerfile:"dockerfile",dockerfile:"dockerfile",Jenkinsfile:"groovy",Vagrantfile:"ruby",Gemfile:"ruby",Rakefile:"ruby",".gitignore":"plaintext",".gitattributes":"plaintext",".editorconfig":"ini",".prettierrc":"json",".eslintrc":"json","tsconfig.json":"json","package.json":"json","package-lock.json":"json","bun.lockb":"plaintext","yarn.lock":"yaml","pnpm-lock.yaml":"yaml","Cargo.toml":"ini","Cargo.lock":"ini","go.mod":"go","go.sum":"plaintext"},C7=null;function b8(){if(!C7)C7=new Set(P7.listLanguages());return C7}function V7($){if(!$)return null;let J=$.split("/").pop()??"";if(S8[J]){let X=S8[J];return b8().has(X)?X:null}let K=J.includes(".")?J.split(".").pop()?.toLowerCase():null;if(!K)return null;let Z=B$[K];if(!Z)return null;return b8().has(Z)?Z:null}function x8($,J){if(!$||!J)return $;try{return P7.highlight(J,$).value}catch{return $}}function S7($,J){if(!J||$.length===0)return $;try{let K=$.join(`
|
|
11
|
+
`);return P7.highlight(J,K).value.replace(/\x1b\[0m/g,"\x1B[39m").split(`
|
|
12
|
+
`)}catch{return $}}function w($){let J;if($.type==="addition"||$.type==="deletion")J=$.content.slice(1);else if($.type==="context")J=$.content.startsWith(" ")?$.content.slice(1):$.content;else J=$.content;return J.replace(/[\x00-\x08\x0a-\x1f\x7f]/g,"").replace(/\t/g," ")}function w8($){switch($.type){case"header":return{type:"diff-header",content:$.content};case"hunk":return{type:"diff-hunk",content:$.content};case"addition":return{type:"diff-add",lineNum:$.newLineNum,content:w($)};case"deletion":return{type:"diff-del",lineNum:$.oldLineNum,content:w($)};case"context":return{type:"diff-context",lineNum:$.oldLineNum??$.newLineNum,content:w($)}}}function G$($){let J=$.match(/^diff --git a\/.+ b\/(.+)$/);return J?J[1]:null}function b7($){if(!$)return[];let J=$.lines.filter(y8),K=[],Z=[],X=null,Q=0;while(Q<J.length){let z=J[Q];if(z.type==="header"){let G=G$(z.content);if(G){if(X)Z.push(X),K.push({type:"spacer"});X={language:V7(G),startRowIndex:K.length,oldContent:[],oldRowIndices:[],newContent:[],newRowIndices:[]}}K.push(w8(z)),Q++;continue}if(z.type==="hunk"){K.push(w8(z)),Q++;continue}if(z.type==="context"){let G=w(z),k=K.length;if(K.push({type:"diff-context",lineNum:z.oldLineNum??z.newLineNum,content:G}),X&&X.language)X.oldContent.push(G),X.oldRowIndices.push(k),X.newContent.push(G),X.newRowIndices.push(k);Q++;continue}let q=[];while(Q<J.length&&J[Q].type==="deletion")q.push(J[Q]),Q++;let Y=[];while(Q<J.length&&J[Q].type==="addition")Y.push(J[Q]),Q++;let U=new Map,V=new Map,B=Math.min(q.length,Y.length);for(let G=0;G<B;G++){let k=w(q[G]),j=w(Y[G]);if(C8(k,j)){let{oldSegments:O,newSegments:D}=P8(k,j);U.set(G,O),V.set(G,D)}}for(let G=0;G<q.length;G++){let k=q[G],j=w(k),O=U.get(G),D=K.length;if(K.push({type:"diff-del",lineNum:k.oldLineNum,content:j,...O&&{wordDiffSegments:O}}),X&&X.language&&!O)X.oldContent.push(j),X.oldRowIndices.push(D)}for(let G=0;G<Y.length;G++){let k=Y[G],j=w(k),O=V.get(G),D=K.length;if(K.push({type:"diff-add",lineNum:k.newLineNum,content:j,...O&&{wordDiffSegments:O}}),X&&X.language&&!O)X.newContent.push(j),X.newRowIndices.push(D)}}if(X)Z.push(X);for(let z of Z){if(!z.language)continue;if(z.oldContent.length>0){let q=S7(z.oldContent,z.language);for(let Y=0;Y<z.oldRowIndices.length;Y++){let U=z.oldRowIndices[Y],V=K[U],B=q[Y];if(B&&B!==V.content&&(V.type==="diff-del"||V.type==="diff-context"))V.highlighted=B}}if(z.newContent.length>0){let q=S7(z.newContent,z.language);for(let Y=0;Y<z.newRowIndices.length;Y++){let U=z.newRowIndices[Y],V=K[U],B=q[Y];if(B&&B!==V.content&&(V.type==="diff-add"||V.type==="diff-context"))V.highlighted=B}}}return K}function u8($,J){let K=[];if($){K.push({type:"commit-header",content:`commit ${$.hash}`}),K.push({type:"commit-header",content:`Author: ${$.author}`}),K.push({type:"commit-header",content:`Date: ${k8($.date)}`}),K.push({type:"spacer"});for(let Z of $.message.split(`
|
|
13
|
+
`))K.push({type:"commit-message",content:` ${Z}`});K.push({type:"spacer"})}return K.push(...b7(J)),K}function x7($){let J=0;for(let K of $)if("lineNum"in K&&K.lineNum!==void 0)J=Math.max(J,K.lineNum);return Math.max(3,String(J).length)}function w7($,J,K){if(!K)return $;let X=Math.max(10,J),Q=[];for(let z of $)if(z.type==="diff-add"||z.type==="diff-del"||z.type==="diff-context"){let q=z.content;if(!q||q.length<=X){Q.push(z);continue}let Y=q7(q,X);for(let U=0;U<Y.length;U++){let V=Y[U];Q.push({...z,content:V.text,lineNum:V.isContinuation?void 0:z.lineNum,isContinuation:V.isContinuation})}}else Q.push(z);return Q}var p8=/\x1b\[[0-9;]*m/g;function Y7($,J,K="…"){if(J<=0)return K;if(!$.includes("\x1B")&&$.length<=J)return $;if(!$.includes("\x1B")){if($.length<=J)return $;return $.slice(0,J-K.length)+K}let Z=[],X=0;p8.lastIndex=0;let Q;while((Q=p8.exec($))!==null){if(Q.index>X)Z.push({type:"text",content:$.slice(X,Q.index)});Z.push({type:"ansi",content:Q[0]}),X=Q.index+Q[0].length}if(X<$.length)Z.push({type:"text",content:$.slice(X)});let z="",q=0,Y=J-K.length,U=!1,V=!1;for(let B of Z)if(B.type==="ansi")z+=B.content,U=!0;else{let G=Y-q;if(G<=0){V=!0;break}if(B.content.length<=G)z+=B.content,q+=B.content.length;else{z+=B.content.slice(0,G),q+=G,V=!0;break}}if(V){if(U)z+="\x1B[0m";z+=K}return z}var E="\x1B[0m",j$="\x1B[1m",g8="\x1B[90m",u7="\x1B[36m",k$="\x1B[33m";function I($,J){if(J<=0||$.length<=J)return $;if(J<=1)return"…";return $.slice(0,J-1)+"…"}function p7($,J){if($===void 0)return" ".repeat(J);return String($).padStart(J," ")}function m8($){return $.replace(/\{/g,"{{").replace(/\}/g,"}}")}function U7($){let J=parseInt($.slice(1,3),16),K=parseInt($.slice(3,5),16),Z=parseInt($.slice(5,7),16);return`\x1B[48;2;${J};${K};${Z}m`}function f8($){let J=parseInt($.slice(1,3),16),K=parseInt($.slice(3,5),16),Z=parseInt($.slice(5,7),16);return`\x1B[38;2;${J};${K};${Z}m`}function h8($,J,K,Z,X,Q){let{colors:z}=X;switch($.type){case"diff-header":{let q=$.content;if(q.startsWith("diff --git")){let Y=q.match(/diff --git a\/.+ b\/(.+)$/);if(Y){let U=Z-6,V=I(Y[1],U);return`{escape}${j$}${u7}── ${V} ──${E}{/escape}`}}return`{escape}${g8}${I(q,Z)}${E}{/escape}`}case"diff-hunk":{let q=$.content.match(/^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(.*)$/);if(q){let Y=parseInt(q[1],10),U=q[2]?parseInt(q[2],10):1,V=parseInt(q[3],10),B=q[4]?parseInt(q[4],10):1,G=q[5].trim(),k=Y+U-1,j=V+B-1,O=U===1?`${Y}`:`${Y}-${k}`,D=B===1?`${V}`:`${V}-${j}`,H=`Lines ${O} → ${D}`,A=Z-H.length-1,T=G&&A>3?" "+I(G,A):"";return`{escape}${u7}${H}${g8}${T}${E}{/escape}`}return`{escape}${u7}${I($.content,Z)}${E}{/escape}`}case"diff-add":{let q=$.isContinuation,Y=q?"»":"+",V=`${p7($.lineNum,J)} ${Y} `;if(X.name.includes("ansi")){let H=Q?$.content||"":I($.content||"",K),T=`${V}${H}`.padEnd(Z," ");return`{green-bg}{white-fg}${m8(T)}{/white-fg}{/green-bg}`}let B=U7(z.addBg),G=U7(z.addHighlight),k=f8("#ffffff");if($.wordDiffSegments&&!q){let H=$.content||"";if(!Q&&H.length>K){let M=I(H,K),W=`${V}${M}`.padEnd(Z," ");return`{escape}${B}${k}${W}${E}{/escape}`}let A="";for(let M of $.wordDiffSegments)if(M.type==="changed")A+=`${G}${M.text}${B}`;else A+=M.text;let T=V.length+H.length,_=" ".repeat(Math.max(0,Z-T));return`{escape}${B}${k}${V}${A}${_}${E}{/escape}`}if($.highlighted&&!q){let H=$.content||"";if(!Q&&H.length>K){let _=I(H,K),v=`${V}${_}`.padEnd(Z," ");return`{escape}${B}${k}${v}${E}{/escape}`}let A=V.length+H.length,T=" ".repeat(Math.max(0,Z-A));return`{escape}${B}${k}${V}${$.highlighted}${T}${E}{/escape}`}let j=Q?$.content||"":I($.content||"",K),D=`${V}${j}`.padEnd(Z," ");return`{escape}${B}${k}${D}${E}{/escape}`}case"diff-del":{let q=$.isContinuation,Y=q?"»":"-",V=`${p7($.lineNum,J)} ${Y} `;if(X.name.includes("ansi")){let H=Q?$.content||"":I($.content||"",K),T=`${V}${H}`.padEnd(Z," ");return`{red-bg}{white-fg}${m8(T)}{/white-fg}{/red-bg}`}let B=U7(z.delBg),G=U7(z.delHighlight),k=f8("#ffffff");if($.wordDiffSegments&&!q){let H=$.content||"";if(!Q&&H.length>K){let M=I(H,K),W=`${V}${M}`.padEnd(Z," ");return`{escape}${B}${k}${W}${E}{/escape}`}let A="";for(let M of $.wordDiffSegments)if(M.type==="changed")A+=`${G}${M.text}${B}`;else A+=M.text;let T=V.length+H.length,_=" ".repeat(Math.max(0,Z-T));return`{escape}${B}${k}${V}${A}${_}${E}{/escape}`}if($.highlighted&&!q){let H=$.content||"";if(!Q&&H.length>K){let _=I(H,K),v=`${V}${_}`.padEnd(Z," ");return`{escape}${B}${k}${v}${E}{/escape}`}let A=V.length+H.length,T=" ".repeat(Math.max(0,Z-A));return`{escape}${B}${k}${V}${$.highlighted}${T}${E}{/escape}`}let j=Q?$.content||"":I($.content||"",K),D=`${V}${j}`.padEnd(Z," ");return`{escape}${B}${k}${D}${E}{/escape}`}case"diff-context":{let q=$.isContinuation,Y=q?"»":" ",V=`${p7($.lineNum,J)} ${Y} `,B=$.content||"",G=`\x1B[90m${V}\x1B[0m`;if($.highlighted&&!q){let j=Q?$.highlighted:Y7($.highlighted,K);return`{escape}${G}${j}${E}{/escape}`}let k=Q?B:I(B,K);return`{escape}${G}${k}${E}{/escape}`}case"commit-header":return`{escape}${k$}${I($.content,Z)}${E}{/escape}`;case"commit-message":return`{escape}${I($.content,Z)}${E}{/escape}`;case"spacer":return""}}function g7($,J,K=0,Z,X="dark",Q=!1){let z=b7($);if(z.length===0)return{content:"{gray-fg}No diff to display{/gray-fg}",totalRows:0};let q=t(X),Y=x7(z),U=J-Y-5,V=J-2,B=w7(z,U,Q),G=B.length;return{content:(Z?B.slice(K,K+Z):B.slice(K)).map((O)=>h8(O,Y,U,V,q,Q)).join(`
|
|
14
|
+
`),totalRows:G}}function c8($,J,K,Z=0,X,Q="dark",z=!1){let q=u8($,J);if(q.length===0)return{content:"{gray-fg}No commit selected{/gray-fg}",totalRows:0};let Y=t(Q),U=x7(q),V=K-U-5,B=K-2,G=w7(q,V,z),k=G.length;return{content:(X?G.slice(Z,Z+X):G.slice(Z)).map((D)=>h8(D,U,V,B,Y,z)).join(`
|
|
15
|
+
`),totalRows:k}}function d8($,J,K){let Z=[],X="{bold}Commit Message{/bold}";if($.amend)X+=" {yellow-fg}(amending){/yellow-fg}";Z.push(X),Z.push("");let Q=$.inputFocused?"│":"│",z=$.inputFocused?"cyan":"gray",q=Math.max(20,K-6);Z.push(`{${z}-fg}┌${"─".repeat(q+2)}┐{/${z}-fg}`);let Y=$.message||($.inputFocused?"":"Press i or Enter to edit..."),U=$.message?"":"{gray-fg}",V=$.message?"":"{/gray-fg}",B=Y.length>q?Y.slice(0,q-1)+"…":Y.padEnd(q);Z.push(`{${z}-fg}${Q}{/${z}-fg} ${U}${B}${V} {${z}-fg}${Q}{/${z}-fg}`),Z.push(`{${z}-fg}└${"─".repeat(q+2)}┘{/${z}-fg}`),Z.push("");let G=$.amend?"[x]":"[ ]",k=$.amend?"green":"gray";if(Z.push(`{${k}-fg}${G}{/${k}-fg} Amend {gray-fg}(a){/gray-fg}`),$.error)Z.push(""),Z.push(`{red-fg}${$.error}{/red-fg}`);if($.isCommitting)Z.push(""),Z.push("{yellow-fg}Committing...{/yellow-fg}");Z.push("");let j=$.inputFocused?"Enter: commit | Esc: unfocus":"i/Enter: edit | Esc: cancel | a: toggle amend";return Z.push(`{gray-fg}Staged: ${J} file(s) | ${j}{/gray-fg}`),Z.join(`
|
|
16
|
+
`)}function m7($,J,K){if(!$)return[];let Z=$.split(`
|
|
17
|
+
`),X=[],Q=J?V7(J):null;for(let z=0;z<Z.length;z++){let q=Z[z],Y=Q?x8(q,Q):void 0;X.push({type:"code",lineNum:z+1,content:q,highlighted:Y})}if(K)X.push({type:"truncation",content:"(file truncated)"});return X}function l8($,J,K){if(!K)return $;let X=Math.max(10,J),Q=[];for(let z of $)if(z.type==="code"){let q=z.content;if(!q||q.length<=X){Q.push(z);continue}let Y=q7(q,X);for(let U=0;U<Y.length;U++){let V=Y[U];Q.push({type:"code",lineNum:V.isContinuation?0:z.lineNum,content:V.text,highlighted:void 0,isContinuation:V.isContinuation})}}else Q.push(z);return Q}function o8($,J,K){if(!K)return $.length;let X=Math.max(10,J),Q=0;for(let z of $)if(z.type==="code"){let q=z.content;if(!q||q.length<=X)Q+=1;else Q+=y7(q,X)}else Q+=1;return Q}function f7($){let J=0;for(let K of $)if(K.type==="code"&&K.lineNum>J)J=K.lineNum;return Math.max(3,String(J).length)}var r8="\x1B[0m",_$="\x1B[90m",O$="\x1B[36m",M$="\x1B[33m";function a8($,J,K,Z=0,X,Q=!1,z=!1){if(!$)return"{gray-fg}Select a file to view its contents{/gray-fg}";if(!J)return"{gray-fg}Loading...{/gray-fg}";let q=m7(J,$,Q);if(q.length===0)return"{gray-fg}(empty file){/gray-fg}";let Y=f7(q),U=K-Y-2,V=l8(q,U,z),B=X?V.slice(Z,Z+X):V.slice(Z),G=[];for(let k of B){if(k.type==="truncation"){G.push(`{escape}${M$}${k.content}${r8}{/escape}`);continue}let j=k.isContinuation??!1,O;if(j)O=">>".padStart(Y," ");else O=String(k.lineNum).padStart(Y," ");let D=k.content,H=!z&&D.length>U,A=k.highlighted&&!j,T;if(A&&k.highlighted)T=H?Y7(k.highlighted,U):k.highlighted;else{let v=D;if(H)v=v.slice(0,Math.max(0,U-1))+"...";T=v}let M=`{escape}${j?O$:_$}${O}${r8} ${T||" "}{/escape}`;G.push(M)}return G.join(`
|
|
18
|
+
`)}function s8($,J,K,Z,X){if(!$)return 0;let Q=m7($,J,K),z=f7(Q),q=Z-z-2;return o8(Q,q,X)}function n8($,J,K,Z,X,Q,z,q){if($.bottomTab==="history")return _8(K,$.historySelectedIndex,$.currentPane==="history",z,$.historyScrollOffset,q);if($.bottomTab==="compare"){let Y=Z?.commits??[],U=Z?.files??[];return E8(Y,U,X,$.currentPane==="compare",z,$.compareScrollOffset,q)}if($.bottomTab==="explorer"){let Y=Q?.displayRows??[];return I8(Y,$.explorerSelectedIndex,$.currentPane==="explorer",z,$.explorerScrollOffset,q,Q?.isLoading??!1,Q?.error??null)}return U8(J,$.selectedIndex,$.currentPane==="files",z,$.fileListScrollOffset,q)}function i8($,J,K,Z,X,Q,z,q,Y,U){if($.bottomTab==="commit")return{content:d8(Q,z,Y),totalRows:0};if($.bottomTab==="history"){let G=K?.selectedCommit??null,k=K?.commitDiff??null,{content:j,totalRows:O}=c8(G,k,Y,$.diffScrollOffset,U,q,$.wrapMode);return{content:j,totalRows:O}}if($.bottomTab==="compare"){let G=Z?.diff??null;if(G){let{content:k,totalRows:j}=g7(G,Y,$.diffScrollOffset,U,q,$.wrapMode);return{content:k,totalRows:j}}return{content:"{gray-fg}Select a commit or file to view diff{/gray-fg}",totalRows:0}}if($.bottomTab==="explorer")return{content:a8(X?.path??null,X?.content??null,Y,$.explorerFileScrollOffset,U,X?.truncated??!1,$.wrapMode),totalRows:0};let{content:V,totalRows:B}=g7(J,Y,$.diffScrollOffset,U,q,$.wrapMode);return{content:V,totalRows:B}}var B7=3;function $9($,J,K){$.topPane.on("wheeldown",()=>{t8(B7,$,K)}),$.topPane.on("wheelup",()=>{t8(-B7,$,K)}),$.bottomPane.on("wheeldown",()=>{e8(B7,$,K)}),$.bottomPane.on("wheelup",()=>{e8(-B7,$,K)}),$.topPane.on("click",(Z)=>{let X=$.screenYToTopPaneRow(Z.y);if(X>=0)H$(X,Z.x,J,K)}),$.footerBox.on("click",(Z)=>{D$(Z.x,J,K)})}function H$($,J,K,Z){let X=Z.uiState.state;if(X.bottomTab==="history"){let Q=X.historyScrollOffset+$;Z.uiState.setHistorySelectedIndex(Q),K.selectHistoryCommitByIndex(Q)}else if(X.bottomTab==="compare"){let Q=Z.getCompareCommits(),z=Z.getCompareFiles(),q=I7(X.compareScrollOffset+$,Q,z);if(q)K.selectCompareItem(q)}else if(X.bottomTab==="explorer"){let Q=X.explorerScrollOffset+$;Z.explorerManager?.selectIndex(Q),Z.uiState.setExplorerSelectedIndex(Q)}else{let Q=Z.getStatusFiles(),z=G8($+X.fileListScrollOffset,Q);if(z!==null&&z>=0)if(J!==void 0&&J>=2&&J<=4)K.toggleFileByIndex(z);else Z.uiState.setSelectedIndex(z),K.selectFileByIndex(z)}}function D$($,J,K){let Z=K.getScreenWidth(),X=[{tab:"explorer",width:11},{tab:"compare",width:10},{tab:"history",width:10},{tab:"commit",width:9},{tab:"diff",width:7}],Q=Z;for(let{tab:z,width:q}of X){let Y=Q-q-1;if($>=Y&&$<Q){K.uiState.setTab(z);return}Q=Y}if($>=2&&$<=9)J.toggleMouseMode();else if($>=11&&$<=16)K.uiState.toggleAutoTab();else if($>=18&&$<=23)K.uiState.toggleWrapMode();else if($>=25&&$<=32)J.toggleFollow();else if($>=34&&$<=43&&K.uiState.state.bottomTab==="explorer")K.explorerManager?.toggleShowOnlyChanges();else if($===0)K.uiState.openModal("hotkeys")}function t8($,J,K){let Z=K.uiState.state,X=J.dimensions.topPaneHeight;if(Z.bottomTab==="history"){let Q=K.getHistoryCommitCount(),z=Math.max(0,Q-X),q=Math.min(z,Math.max(0,Z.historyScrollOffset+$));K.uiState.setHistoryScrollOffset(q)}else if(Z.bottomTab==="compare"){let Q=L8(K.getCompareCommits(),K.getCompareFiles()),z=Math.max(0,Q-X),q=Math.min(z,Math.max(0,Z.compareScrollOffset+$));K.uiState.setCompareScrollOffset(q)}else if(Z.bottomTab==="explorer"){let Q=R8(K.explorerManager?.state.displayRows??[]),z=Math.max(0,Q-X),q=Math.min(z,Math.max(0,Z.explorerScrollOffset+$));K.uiState.setExplorerScrollOffset(q)}else{let Q=K.getStatusFiles(),z=B8(Q),q=Math.max(0,z-X),Y=Math.min(q,Math.max(0,Z.fileListScrollOffset+$));K.uiState.setFileListScrollOffset(Y)}}function e8($,J,K){let Z=K.uiState.state,X=J.dimensions.bottomPaneHeight,Q=K.getScreenWidth();if(Z.bottomTab==="explorer"){let z=K.explorerManager?.state.selectedFile,q=s8(z?.content??null,z?.path??null,z?.truncated??!1,Q,Z.wrapMode),Y=Math.max(0,q-X),U=Math.min(Y,Math.max(0,Z.explorerFileScrollOffset+$));K.uiState.setExplorerFileScrollOffset(U)}else{let z=Math.max(0,K.getBottomPaneTotalRows()-X),q=Math.min(z,Math.max(0,Z.diffScrollOffset+$));K.uiState.setDiffScrollOffset(q)}}import*as m from"node:fs";import*as j7 from"node:path";import{watch as N$}from"chokidar";import{EventEmitter as W$}from"node:events";import*as y from"node:fs";import*as r from"node:path";import*as G7 from"node:os";var v$={targetFile:r.join(G7.homedir(),".cache","diffstalker","target"),watcherEnabled:!1,debug:!1,theme:"dark"},o=r.join(G7.homedir(),".config","diffstalker","config.json"),A$=["dark","light","dark-colorblind","light-colorblind","dark-ansi","light-ansi"];function T$($){return typeof $==="string"&&A$.includes($)}function J9(){let $={...v$};if(process.env.DIFFSTALKER_PAGER)$.pager=process.env.DIFFSTALKER_PAGER;if(y.existsSync(o))try{let J=JSON.parse(y.readFileSync(o,"utf-8"));if(J.pager)$.pager=J.pager;if(J.targetFile)$.targetFile=J.targetFile;if(T$(J.theme))$.theme=J.theme;if(typeof J.splitRatio==="number"&&J.splitRatio>=0.15&&J.splitRatio<=0.85)$.splitRatio=J.splitRatio}catch{}return $}function h7($){let J=r.dirname(o);if(!y.existsSync(J))y.mkdirSync(J,{recursive:!0});let K={};if(y.existsSync(o))try{K=JSON.parse(y.readFileSync(o,"utf-8"))}catch{}Object.assign(K,$),y.writeFileSync(o,JSON.stringify(K,null,2)+`
|
|
19
|
+
`)}function K9($){let J=r.dirname($);if(!y.existsSync(J))y.mkdirSync(J,{recursive:!0})}function Z9($){let J=G7.homedir();if($.startsWith(J))return"~"+$.slice(J.length);return $}import*as X9 from"node:path";import*as c7 from"node:os";function Q9($){if($.startsWith("~/"))return X9.join(c7.homedir(),$.slice(2));if($==="~")return c7.homedir();return $}function d7($){let J=$.split(`
|
|
20
|
+
`);for(let K=J.length-1;K>=0;K--){let Z=J[K].trim();if(Z)return Z}return""}class l7 extends W${targetFile;debug;watcher=null;debounceTimer=null;lastReadPath=null;_state={path:null,lastUpdate:null,rawContent:null,sourceFile:null};constructor($,J=!1){super();this.targetFile=$,this.debug=J,this._state.sourceFile=$}get state(){return this._state}updateState($){this._state={...this._state,...$},this.emit("path-change",this._state)}processContent($){if(!$)return null;let J=Q9($);return j7.isAbsolute(J)?J:j7.resolve(J)}readTargetDebounced(){if(this.debounceTimer)clearTimeout(this.debounceTimer);this.debounceTimer=setTimeout(()=>{this.readTarget()},100)}readTarget(){try{let $=m.readFileSync(this.targetFile,"utf-8"),J=d7($);if(J&&J!==this.lastReadPath){let K=this.processContent(J),Z=new Date;if(this.debug&&K)process.stderr.write(`[diffstalker ${Z.toISOString()}] Path change detected
|
|
46
21
|
`),process.stderr.write(` Source file: ${this.targetFile}
|
|
47
|
-
`),process.stderr.write(` Raw content: "${
|
|
22
|
+
`),process.stderr.write(` Raw content: "${J}"
|
|
48
23
|
`),process.stderr.write(` Previous: "${this.lastReadPath??"(none)"}"
|
|
49
|
-
`),process.stderr.write(` Resolved: "${
|
|
50
|
-
`);this.lastReadPath=
|
|
24
|
+
`),process.stderr.write(` Resolved: "${K}"
|
|
25
|
+
`);this.lastReadPath=K,this.updateState({path:K,lastUpdate:Z,rawContent:J})}}catch{}}start(){if(K9(this.targetFile),!m.existsSync(this.targetFile))m.writeFileSync(this.targetFile,"");try{let $=m.readFileSync(this.targetFile,"utf-8"),J=d7($);if(J){let K=this.processContent(J),Z=new Date;if(this.debug&&K)process.stderr.write(`[diffstalker ${Z.toISOString()}] Initial path read
|
|
51
26
|
`),process.stderr.write(` Source file: ${this.targetFile}
|
|
52
|
-
`),process.stderr.write(` Raw content: "${
|
|
53
|
-
`),process.stderr.write(` Resolved: "${
|
|
54
|
-
`);this.lastReadPath=J,this._state={path:J,lastUpdate:X,rawContent:$,sourceFile:this.targetFile}}}catch{}this.watcher=Z9(this.targetFile,{persistent:!0,ignoreInitial:!0}),this.watcher.on("change",()=>this.readTargetDebounced()),this.watcher.on("add",()=>this.readTargetDebounced())}stop(){if(this.debounceTimer)clearTimeout(this.debounceTimer),this.debounceTimer=null;if(this.watcher)this.watcher.close(),this.watcher=null}}class eK{screen;layout;uiState;gitManager=null;fileWatcher=null;explorerManager=null;config;commandServer;repoPath;watcherState={enabled:!1};currentTheme;commitFlowState;commitTextarea=null;activeModal=null;bottomPaneTotalRows=0;constructor(K){this.config=K.config,this.commandServer=K.commandServer??null,this.repoPath=K.initialPath??process.cwd(),this.currentTheme=K.config.theme,this.uiState=new hK({splitRatio:K.config.splitRatio??0.4}),this.screen=G8.screen({smartCSR:!0,fullUnicode:!0,title:"diffstalker",mouse:!0,terminal:"xterm-256color"});let $=this.screen;if($.tput)$.tput.colors=256;if($.program?.tput)$.program.tput.colors=256;if(this.layout=new OK(this.screen,this.uiState.state.splitRatio),this.screen.on("resize",()=>{setImmediate(()=>this.render())}),this.commitFlowState=new fK({getHeadMessage:()=>this.gitManager?.getHeadCommitMessage()??Promise.resolve(""),onCommit:async(J,X)=>{await this.gitManager?.commit(J,X)},onSuccess:()=>{this.uiState.setTab("diff"),this.render()}}),this.commitTextarea=G8.textarea({parent:this.layout.bottomPane,top:3,left:1,width:"100%-4",height:1,inputOnFocus:!0,hidden:!0,style:{fg:"white",bg:"default"}}),this.commitTextarea.on("submit",()=>{this.commitFlowState.submit()}),this.commitTextarea.on("keypress",()=>{setImmediate(()=>{let J=this.commitTextarea?.getValue()??"";this.commitFlowState.setMessage(J)})}),this.setupKeyboardHandlers(),this.setupMouseHandlers(),this.setupStateListeners(),this.config.watcherEnabled)this.setupFileWatcher();if(this.commandServer)this.setupCommandHandler();this.initGitManager(),this.render()}setupKeyboardHandlers(){this.screen.key(["q","C-c"],()=>{this.exit()}),this.screen.key(["j","down"],()=>{if(this.activeModal)return;this.navigateDown()}),this.screen.key(["k","up"],()=>{if(this.activeModal)return;this.navigateUp()}),this.screen.key(["1"],()=>{if(this.activeModal)return;this.uiState.setTab("diff")}),this.screen.key(["2"],()=>{if(this.activeModal)return;this.uiState.setTab("commit")}),this.screen.key(["3"],()=>{if(this.activeModal)return;this.uiState.setTab("history")}),this.screen.key(["4"],()=>{if(this.activeModal)return;this.uiState.setTab("compare")}),this.screen.key(["5"],()=>{if(this.activeModal)return;this.uiState.setTab("explorer")}),this.screen.key(["tab"],()=>{if(this.activeModal)return;this.uiState.togglePane()}),this.screen.key(["s"],()=>{if(this.activeModal)return;this.stageSelected()}),this.screen.key(["S-u"],()=>{if(this.activeModal)return;this.unstageSelected()}),this.screen.key(["S-a"],()=>{if(this.activeModal)return;this.stageAll()}),this.screen.key(["S-z"],()=>{if(this.activeModal)return;this.unstageAll()}),this.screen.key(["enter","space"],()=>{if(this.activeModal)return;let K=this.uiState.state;if(K.bottomTab==="explorer"&&K.currentPane==="explorer")this.enterExplorerDirectory();else this.toggleSelected()}),this.screen.key(["backspace"],()=>{if(this.activeModal)return;let K=this.uiState.state;if(K.bottomTab==="explorer"&&K.currentPane==="explorer")this.goExplorerUp()}),this.screen.key(["c"],()=>{if(this.activeModal)return;this.uiState.setTab("commit")}),this.screen.key(["i"],()=>{if(this.uiState.state.bottomTab==="commit"&&!this.commitFlowState.state.inputFocused)this.focusCommitInput()}),this.screen.key(["a"],()=>{if(this.uiState.state.bottomTab==="commit"&&!this.commitFlowState.state.inputFocused)this.commitFlowState.toggleAmend(),this.render()}),this.screen.key(["escape"],()=>{if(this.uiState.state.bottomTab==="commit")if(this.commitFlowState.state.inputFocused)this.unfocusCommitInput();else this.uiState.setTab("diff")}),this.screen.key(["r"],()=>this.refresh()),this.screen.key(["w"],()=>this.uiState.toggleWrapMode()),this.screen.key(["m"],()=>this.toggleMouseMode()),this.screen.key(["S-t"],()=>this.uiState.toggleAutoTab()),this.screen.key(["-","_"],()=>{this.uiState.adjustSplitRatio(-MK),this.layout.setSplitRatio(this.uiState.state.splitRatio),this.render()}),this.screen.key(["=","+"],()=>{this.uiState.adjustSplitRatio(MK),this.layout.setSplitRatio(this.uiState.state.splitRatio),this.render()}),this.screen.key(["t"],()=>this.uiState.openModal("theme")),this.screen.key(["?"],()=>this.uiState.toggleModal("hotkeys")),this.screen.key(["f"],()=>this.toggleFollow()),this.screen.key(["b"],()=>{if(this.uiState.state.bottomTab==="compare")this.uiState.openModal("baseBranch")}),this.screen.key(["u"],()=>{if(this.uiState.state.bottomTab==="compare"){this.uiState.toggleIncludeUncommitted();let K=this.uiState.state.includeUncommitted;this.gitManager?.refreshCompareDiff(K)}}),this.screen.key(["d"],()=>{if(this.uiState.state.bottomTab==="diff"){let K=this.gitManager?.state.status?.files??[],$=this.uiState.state.selectedIndex,J=K[$];if(J&&!J.staged&&J.status!=="untracked")this.showDiscardConfirm(J)}})}setupMouseHandlers(){this.layout.topPane.on("wheeldown",()=>{this.handleTopPaneScroll(3)}),this.layout.topPane.on("wheelup",()=>{this.handleTopPaneScroll(-3)}),this.layout.bottomPane.on("wheeldown",()=>{this.handleBottomPaneScroll(3)}),this.layout.bottomPane.on("wheelup",()=>{this.handleBottomPaneScroll(-3)}),this.layout.topPane.on("click",($)=>{let J=this.layout.screenYToTopPaneRow($.y);if(J>=0)this.handleTopPaneClick(J)}),this.layout.footerBox.on("click",($)=>{this.handleFooterClick($.x)})}handleTopPaneClick(K){let $=this.uiState.state;if($.bottomTab==="history"){let J=$.historyScrollOffset+K;this.uiState.setHistorySelectedIndex(J),this.selectHistoryCommitByIndex(J)}else if($.bottomTab==="compare"){let J=this.gitManager?.compareState,X=J?.compareDiff?.commits??[],Z=J?.compareDiff?.files??[],Q=PK($.compareScrollOffset+K,X,Z);if(Q)this.selectCompareItem(Q)}else if($.bottomTab==="explorer"){let J=$.explorerScrollOffset+K;this.explorerManager?.selectIndex(J),this.uiState.setExplorerSelectedIndex(J)}else{let J=this.gitManager?.state.status?.files??[],X=V7(K+$.fileListScrollOffset,J);if(X!==null&&X>=0)this.uiState.setSelectedIndex(X),this.selectFileByIndex(X)}}handleFooterClick(K){let $=this.screen.width||80,J=[{tab:"explorer",label:"[5]Explorer",width:11},{tab:"compare",label:"[4]Compare",width:10},{tab:"history",label:"[3]History",width:10},{tab:"commit",label:"[2]Commit",width:9},{tab:"diff",label:"[1]Diff",width:7}],X=$;for(let{tab:Z,width:Q}of J){let q=X-Q-1;if(K>=q&&K<X){this.uiState.setTab(Z);return}X=q}if(K>=2&&K<=9)this.toggleMouseMode();else if(K>=11&&K<=16)this.uiState.toggleAutoTab();else if(K>=18&&K<=23)this.uiState.toggleWrapMode();else if(K>=25&&K<=30&&this.uiState.state.bottomTab==="explorer")this.uiState.toggleMiddleDots();else if(K===0)this.uiState.openModal("hotkeys")}handleTopPaneScroll(K){let $=this.uiState.state,J=this.layout.dimensions.topPaneHeight;if($.bottomTab==="history"){let X=this.gitManager?.historyState.commits.length??0,Z=Math.max(0,X-J),Q=Math.min(Z,Math.max(0,$.historyScrollOffset+K));this.uiState.setHistoryScrollOffset(Q)}else if($.bottomTab==="compare"){let X=this.gitManager?.compareState,Z=L7(X?.compareDiff?.commits??[],X?.compareDiff?.files??[]),Q=Math.max(0,Z-J),q=Math.min(Q,Math.max(0,$.compareScrollOffset+K));this.uiState.setCompareScrollOffset(q)}else if($.bottomTab==="explorer"){let X=y7(this.explorerManager?.state.items??[]),Z=Math.max(0,X-J),Q=Math.min(Z,Math.max(0,$.explorerScrollOffset+K));this.uiState.setExplorerScrollOffset(Q)}else{let X=this.gitManager?.state.status?.files??[],Z=Y7(X),Q=Math.max(0,Z-J),q=Math.min(Q,Math.max(0,$.fileListScrollOffset+K));this.uiState.setFileListScrollOffset(q)}}handleBottomPaneScroll(K){let $=this.uiState.state,J=this.layout.dimensions.bottomPaneHeight,X=this.screen.width||80;if($.bottomTab==="explorer"){let Z=this.explorerManager?.state.selectedFile,Q=w7(Z?.content??null,Z?.path??null,Z?.truncated??!1,X,$.wrapMode),q=Math.max(0,Q-J),Y=Math.min(q,Math.max(0,$.explorerFileScrollOffset+K));this.uiState.setExplorerFileScrollOffset(Y)}else{let Z=Math.max(0,this.bottomPaneTotalRows-J),Q=Math.min(Z,Math.max(0,$.diffScrollOffset+K));this.uiState.setDiffScrollOffset(Q)}}setupStateListeners(){this.uiState.on("change",()=>{this.render()}),this.uiState.on("tab-change",($)=>{if($==="history")this.gitManager?.loadHistory();else if($==="compare")this.gitManager?.refreshCompareDiff(this.uiState.state.includeUncommitted);else if($==="explorer"){if(!this.explorerManager?.state.items.length)this.explorerManager?.loadDirectory("")}}),this.uiState.on("modal-change",($)=>{if(this.activeModal)this.activeModal=null;if($==="theme")this.activeModal=new uK(this.screen,this.currentTheme,(J)=>{this.currentTheme=J,NK({theme:J}),this.activeModal=null,this.uiState.closeModal(),this.render()},()=>{this.activeModal=null,this.uiState.closeModal()}),this.activeModal.focus();else if($==="hotkeys")this.activeModal=new pK(this.screen,()=>{this.activeModal=null,this.uiState.closeModal()}),this.activeModal.focus();else if($==="baseBranch")this.gitManager?.getCandidateBaseBranches().then((J)=>{let X=this.gitManager?.compareState.compareBaseBranch??null;this.activeModal=new gK(this.screen,J,X,(Z)=>{this.activeModal=null,this.uiState.closeModal();let Q=this.uiState.state.includeUncommitted;this.gitManager?.setCompareBaseBranch(Z,Q)},()=>{this.activeModal=null,this.uiState.closeModal()}),this.activeModal.focus()})});let K=null;this.uiState.on("change",($)=>{if(K)clearTimeout(K);K=setTimeout(()=>{if($.splitRatio!==this.config.splitRatio)NK({splitRatio:$.splitRatio})},500)})}setupFileWatcher(){this.fileWatcher=new tK(this.config.targetFile),this.fileWatcher.on("path-change",($)=>{if($.path&&$.path!==this.repoPath)this.repoPath=$.path,this.watcherState={enabled:!0,sourceFile:$.sourceFile??this.config.targetFile,rawContent:$.rawContent??void 0,lastUpdate:$.lastUpdate??void 0},this.initGitManager(),this.render();if($.rawContent)this.navigateToFile($.rawContent),this.render()}),this.watcherState={enabled:!0,sourceFile:this.config.targetFile},this.fileWatcher.start();let K=this.fileWatcher.state;if(K.rawContent)this.watcherState.rawContent=K.rawContent,this.navigateToFile(K.rawContent)}initGitManager(){if(this.gitManager)this.gitManager.removeAllListeners(),aK(this.repoPath);this.gitManager=U8(this.repoPath),this.gitManager.on("state-change",()=>{this.render()}),this.gitManager.on("history-state-change",(K)=>{if(K.commits.length>0&&!K.selectedCommit){let $=this.uiState.state;if($.bottomTab==="history")this.selectHistoryCommitByIndex($.historySelectedIndex)}this.render()}),this.gitManager.on("compare-state-change",()=>{this.render()}),this.gitManager.on("compare-selection-change",()=>{this.render()}),this.gitManager.startWatching(),this.gitManager.refresh(),this.initExplorerManager()}initExplorerManager(){if(this.explorerManager)this.explorerManager.dispose();let K={hideHidden:!0,hideGitignored:!0};this.explorerManager=new wK(this.repoPath,K),this.explorerManager.on("state-change",()=>{this.render()}),this.explorerManager.loadDirectory("")}setupCommandHandler(){if(!this.commandServer)return;let K={navigateUp:()=>this.navigateUp(),navigateDown:()=>this.navigateDown(),switchTab:($)=>this.uiState.setTab($),togglePane:()=>this.uiState.togglePane(),stage:async()=>this.stageSelected(),unstage:async()=>this.unstageSelected(),stageAll:async()=>this.stageAll(),unstageAll:async()=>this.unstageAll(),commit:async($)=>this.commit($),refresh:async()=>this.refresh(),getState:()=>this.getAppState(),quit:()=>this.exit()};this.commandServer.setHandler(K),this.commandServer.notifyReady()}getAppState(){let K=this.uiState.state,$=this.gitManager?.state,J=this.gitManager?.historyState,X=$?.status?.files??[],Z=J?.commits??[];return{currentTab:K.bottomTab,currentPane:K.currentPane,selectedIndex:K.selectedIndex,totalFiles:X.length,stagedCount:X.filter((Q)=>Q.staged).length,files:X.map((Q)=>({path:Q.path,status:Q.status,staged:Q.staged})),historySelectedIndex:K.historySelectedIndex,historyCommitCount:Z.length,compareSelectedIndex:K.compareSelectedIndex,compareTotalItems:0,includeUncommitted:K.includeUncommitted,explorerPath:this.repoPath,explorerSelectedIndex:K.explorerSelectedIndex,explorerItemCount:0,wrapMode:K.wrapMode,mouseEnabled:K.mouseEnabled,autoTabEnabled:K.autoTabEnabled}}navigateUp(){let K=this.uiState.state;if(K.bottomTab==="history"){if(K.currentPane==="history")this.navigateHistoryUp();else if(K.currentPane==="diff")this.uiState.setDiffScrollOffset(Math.max(0,K.diffScrollOffset-3));return}if(K.bottomTab==="compare"){if(K.currentPane==="compare")this.navigateCompareUp();else if(K.currentPane==="diff")this.uiState.setDiffScrollOffset(Math.max(0,K.diffScrollOffset-3));return}if(K.bottomTab==="explorer"){if(K.currentPane==="explorer")this.navigateExplorerUp();else if(K.currentPane==="diff")this.uiState.setExplorerFileScrollOffset(Math.max(0,K.explorerFileScrollOffset-3));return}if(K.currentPane==="files"){let $=this.gitManager?.state.status?.files??[],J=Math.max(0,K.selectedIndex-1);this.uiState.setSelectedIndex(J),this.selectFileByIndex(J);let X=TK(J,$);if(X<K.fileListScrollOffset)this.uiState.setFileListScrollOffset(X)}else if(K.currentPane==="diff")this.uiState.setDiffScrollOffset(Math.max(0,K.diffScrollOffset-3))}navigateDown(){let K=this.uiState.state,$=this.gitManager?.state.status?.files??[];if(K.bottomTab==="history"){if(K.currentPane==="history")this.navigateHistoryDown();else if(K.currentPane==="diff")this.uiState.setDiffScrollOffset(K.diffScrollOffset+3);return}if(K.bottomTab==="compare"){if(K.currentPane==="compare")this.navigateCompareDown();else if(K.currentPane==="diff")this.uiState.setDiffScrollOffset(K.diffScrollOffset+3);return}if(K.bottomTab==="explorer"){if(K.currentPane==="explorer")this.navigateExplorerDown();else if(K.currentPane==="diff")this.uiState.setExplorerFileScrollOffset(K.explorerFileScrollOffset+3);return}if(K.currentPane==="files"){let J=Math.min($.length-1,K.selectedIndex+1);this.uiState.setSelectedIndex(J),this.selectFileByIndex(J);let X=TK(J,$),Z=K.fileListScrollOffset+this.layout.dimensions.topPaneHeight-1;if(X>=Z)this.uiState.setFileListScrollOffset(K.fileListScrollOffset+(X-Z+1))}else if(K.currentPane==="diff")this.uiState.setDiffScrollOffset(K.diffScrollOffset+3)}navigateHistoryUp(){let K=this.uiState.state,$=Math.max(0,K.historySelectedIndex-1);if($!==K.historySelectedIndex){if(this.uiState.setHistorySelectedIndex($),$<K.historyScrollOffset)this.uiState.setHistoryScrollOffset($);this.selectHistoryCommitByIndex($)}}navigateHistoryDown(){let K=this.uiState.state,$=this.gitManager?.historyState.commits??[],J=Math.min($.length-1,K.historySelectedIndex+1);if(J!==K.historySelectedIndex){this.uiState.setHistorySelectedIndex(J);let X=K.historyScrollOffset+this.layout.dimensions.topPaneHeight-1;if(J>=X)this.uiState.setHistoryScrollOffset(K.historyScrollOffset+1);this.selectHistoryCommitByIndex(J)}}selectHistoryCommitByIndex(K){let $=this.gitManager?.historyState.commits??[],J=F7($,K);if(J)this.uiState.setDiffScrollOffset(0),this.gitManager?.selectHistoryCommit(J)}compareSelection=null;navigateCompareUp(){let K=this.gitManager?.compareState,$=K?.compareDiff?.commits??[],J=K?.compareDiff?.files??[];if($.length===0&&J.length===0)return;let X=CK(this.compareSelection,$,J,"up");if(X&&(X.type!==this.compareSelection?.type||X.index!==this.compareSelection?.index)){this.selectCompareItem(X);let Z=this.uiState.state,Q=UK(X,$,J);if(Q<Z.compareScrollOffset)this.uiState.setCompareScrollOffset(Q)}}navigateCompareDown(){let K=this.gitManager?.compareState,$=K?.compareDiff?.commits??[],J=K?.compareDiff?.files??[];if($.length===0&&J.length===0)return;if(!this.compareSelection){if($.length>0)this.selectCompareItem({type:"commit",index:0});else if(J.length>0)this.selectCompareItem({type:"file",index:0});return}let X=CK(this.compareSelection,$,J,"down");if(X&&(X.type!==this.compareSelection?.type||X.index!==this.compareSelection?.index)){this.selectCompareItem(X);let Z=this.uiState.state,Q=UK(X,$,J),q=Z.compareScrollOffset+this.layout.dimensions.topPaneHeight-1;if(Q>=q)this.uiState.setCompareScrollOffset(Z.compareScrollOffset+(Q-q+1))}}selectCompareItem(K){if(this.compareSelection=K,this.uiState.setDiffScrollOffset(0),K.type==="commit")this.gitManager?.selectCompareCommit(K.index);else this.gitManager?.selectCompareFile(K.index)}navigateExplorerUp(){let K=this.uiState.state;if((this.explorerManager?.state.items??[]).length===0)return;let J=this.explorerManager?.navigateUp(K.explorerScrollOffset);if(J!==null&&J!==void 0)this.uiState.setExplorerScrollOffset(J);this.uiState.setExplorerSelectedIndex(this.explorerManager?.state.selectedIndex??0)}navigateExplorerDown(){let K=this.uiState.state;if((this.explorerManager?.state.items??[]).length===0)return;let J=this.layout.dimensions.topPaneHeight,X=this.explorerManager?.navigateDown(K.explorerScrollOffset,J);if(X!==null&&X!==void 0)this.uiState.setExplorerScrollOffset(X);this.uiState.setExplorerSelectedIndex(this.explorerManager?.state.selectedIndex??0)}async enterExplorerDirectory(){await this.explorerManager?.enterDirectory(),this.uiState.setExplorerScrollOffset(0),this.uiState.setExplorerFileScrollOffset(0),this.uiState.setExplorerSelectedIndex(0)}async goExplorerUp(){await this.explorerManager?.goUp(),this.uiState.setExplorerScrollOffset(0),this.uiState.setExplorerFileScrollOffset(0),this.uiState.setExplorerSelectedIndex(0)}selectFileByIndex(K){let $=this.gitManager?.state.status?.files??[],J=z7($,K);if(J)this.uiState.setDiffScrollOffset(0),this.gitManager?.selectFile(J)}navigateToFile(K){if(!K||!this.repoPath)return;let $=this.repoPath.endsWith("/")?this.repoPath:this.repoPath+"/";if(!K.startsWith($))return;let J=K.slice($.length);if(!J)return;let Z=(this.gitManager?.state.status?.files??[]).findIndex((Q)=>Q.path===J);if(Z>=0)this.uiState.setSelectedIndex(Z),this.selectFileByIndex(Z)}async stageSelected(){let $=(this.gitManager?.state.status?.files??[])[this.uiState.state.selectedIndex];if($&&!$.staged)await this.gitManager?.stage($)}async unstageSelected(){let $=(this.gitManager?.state.status?.files??[])[this.uiState.state.selectedIndex];if($?.staged)await this.gitManager?.unstage($)}async toggleSelected(){let $=(this.gitManager?.state.status?.files??[])[this.uiState.state.selectedIndex];if($)if($.staged)await this.gitManager?.unstage($);else await this.gitManager?.stage($)}async stageAll(){await this.gitManager?.stageAll()}async unstageAll(){await this.gitManager?.unstageAll()}showDiscardConfirm(K){this.activeModal=new mK(this.screen,K.path,async()=>{this.activeModal=null,await this.gitManager?.discard(K)},()=>{this.activeModal=null}),this.activeModal.focus()}async commit(K){await this.gitManager?.commit(K)}async refresh(){await this.gitManager?.refresh()}toggleMouseMode(){let K=!this.uiState.state.mouseEnabled;this.uiState.toggleMouse();let $=this.screen.program;if(K)$.enableMouse();else $.disableMouse()}toggleFollow(){if(this.fileWatcher)this.fileWatcher.stop(),this.fileWatcher=null,this.watcherState={enabled:!1};else this.setupFileWatcher();this.render()}focusCommitInput(){if(this.commitTextarea)this.commitTextarea.show(),this.commitTextarea.focus(),this.commitTextarea.setValue(this.commitFlowState.state.message),this.commitFlowState.setInputFocused(!0),this.render()}unfocusCommitInput(){if(this.commitTextarea){let K=this.commitTextarea.getValue()??"";this.commitFlowState.setMessage(K),this.commitTextarea.hide(),this.commitFlowState.setInputFocused(!1),this.screen.focusPush(this.layout.bottomPane),this.render()}}render(){this.updateHeader(),this.updateTopPane(),this.updateBottomPane(),this.updateFooter(),this.screen.render()}updateHeader(){let K=this.gitManager?.state,$=this.screen.width||80,J=X7(this.repoPath,K?.status?.branch??null,K?.isLoading??!1,K?.error??null,this.watcherState,$);this.layout.headerBox.setContent(J)}updateTopPane(){let K=this.gitManager?.state,$=this.gitManager?.historyState,J=this.gitManager?.compareState,X=K?.status?.files??[],Z=this.uiState.state,Q=this.screen.width||80,q;if(Z.bottomTab==="history"){let Y=$?.commits??[];q=R7(Y,Z.historySelectedIndex,Z.currentPane==="history",Q,Z.historyScrollOffset,this.layout.dimensions.topPaneHeight)}else if(Z.bottomTab==="compare"){let Y=J?.compareDiff,V=Y?.commits??[],_=Y?.files??[];q=v7(V,_,this.compareSelection,Z.currentPane==="compare",Q,Z.compareScrollOffset,this.layout.dimensions.topPaneHeight)}else if(Z.bottomTab==="explorer"){let Y=this.explorerManager?.state,V=Y?.items??[];q=W7(V,Z.explorerSelectedIndex,Z.currentPane==="explorer",Q,Z.explorerScrollOffset,this.layout.dimensions.topPaneHeight,Y?.isLoading??!1,Y?.error??null)}else q=q7(X,Z.selectedIndex,Z.currentPane==="files",Q,Z.fileListScrollOffset,this.layout.dimensions.topPaneHeight);this.layout.topPane.setContent(q)}updateBottomPane(){let K=this.gitManager?.state,$=this.gitManager?.historyState,J=K?.diff??null,X=this.uiState.state,Z=this.screen.width||80,q=(K?.status?.files??[]).filter((Y)=>Y.staged).length;if(this.commitFlowState.setStagedCount(q),X.bottomTab==="commit"){let Y=D7(this.commitFlowState.state,q,Z);if(this.layout.bottomPane.setContent(Y),this.commitTextarea)if(this.commitFlowState.state.inputFocused)this.commitTextarea.show();else this.commitTextarea.hide()}else if(X.bottomTab==="history"){if(this.commitTextarea)this.commitTextarea.hide();let Y=$?.selectedCommit??null,V=$?.commitDiff??null,{content:_,totalRows:z}=I7(Y,V,Z,X.diffScrollOffset,this.layout.dimensions.bottomPaneHeight,this.currentTheme,X.wrapMode);this.bottomPaneTotalRows=z,this.layout.bottomPane.setContent(_)}else if(X.bottomTab==="compare"){if(this.commitTextarea)this.commitTextarea.hide();let V=this.gitManager?.compareSelectionState?.diff??null;if(V){let{content:_,totalRows:z}=yK(V,Z,X.diffScrollOffset,this.layout.dimensions.bottomPaneHeight,this.currentTheme,X.wrapMode);this.bottomPaneTotalRows=z,this.layout.bottomPane.setContent(_)}else this.bottomPaneTotalRows=0,this.layout.bottomPane.setContent("{gray-fg}Select a commit or file to view diff{/gray-fg}")}else if(X.bottomTab==="explorer"){if(this.commitTextarea)this.commitTextarea.hide();let V=this.explorerManager?.state?.selectedFile??null,_=x7(V?.path??null,V?.content??null,Z,X.explorerFileScrollOffset,this.layout.dimensions.bottomPaneHeight,V?.truncated??!1,X.wrapMode,X.showMiddleDots);this.layout.bottomPane.setContent(_)}else{if(this.commitTextarea)this.commitTextarea.hide();let{content:Y,totalRows:V}=yK(J,Z,X.diffScrollOffset,this.layout.dimensions.bottomPaneHeight,this.currentTheme,X.wrapMode);this.bottomPaneTotalRows=V,this.layout.bottomPane.setContent(Y)}}updateFooter(){let K=this.uiState.state,$=this.screen.width||80,J=Q7(K.bottomTab,K.mouseEnabled,K.autoTabEnabled,K.wrapMode,K.showMiddleDots,$);this.layout.footerBox.setContent(J)}exit(){if(this.gitManager)aK(this.repoPath);if(this.explorerManager)this.explorerManager.dispose();if(this.fileWatcher)this.fileWatcher.stop();if(this.commandServer)this.commandServer.stop();this.screen.destroy()}start(){return new Promise((K)=>{this.screen.on("destroy",()=>{K()})})}}import*as A8 from"net";import*as u from"fs";import{EventEmitter as q9}from"events";class K7 extends q9{server=null;socketPath;handler=null;ready=!1;constructor(K){super();this.socketPath=K}setHandler(K){this.handler=K}notifyReady(){this.ready=!0,this.emit("ready")}isReady(){return this.ready&&this.handler!==null}async start(){if(u.existsSync(this.socketPath))u.unlinkSync(this.socketPath);return new Promise((K,$)=>{this.server=A8.createServer((J)=>{this.handleConnection(J)}),this.server.on("error",(J)=>{$(J)}),this.server.listen(this.socketPath,()=>{u.chmodSync(this.socketPath,384),K()})})}stop(){if(this.server)this.server.close(),this.server=null;if(u.existsSync(this.socketPath))u.unlinkSync(this.socketPath)}handleConnection(K){let $="";K.on("data",async(J)=>{$+=J.toString();let X=$.split(`
|
|
55
|
-
`)
|
|
56
|
-
|
|
27
|
+
`),process.stderr.write(` Raw content: "${J}"
|
|
28
|
+
`),process.stderr.write(` Resolved: "${K}"
|
|
29
|
+
`);this.lastReadPath=K,this._state={path:K,lastUpdate:Z,rawContent:J,sourceFile:this.targetFile}}}catch{}this.watcher=N$(this.targetFile,{persistent:!0,ignoreInitial:!0}),this.watcher.on("change",()=>this.readTargetDebounced()),this.watcher.on("add",()=>this.readTargetDebounced())}stop(){if(this.debounceTimer)clearTimeout(this.debounceTimer),this.debounceTimer=null;if(this.watcher)this.watcher.close(),this.watcher=null}}class k7{targetFile;getCurrentRepoPath;callbacks;watcher=null;_watcherState={enabled:!1};constructor($,J,K){this.targetFile=$,this.getCurrentRepoPath=J,this.callbacks=K}get watcherState(){return this._watcherState}get isEnabled(){return this.watcher!==null}start(){this.watcher=new l7(this.targetFile),this.watcher.on("path-change",(J)=>{if(J.path&&J.path!==this.getCurrentRepoPath())this._watcherState={enabled:!0,sourceFile:J.sourceFile??this.targetFile,rawContent:J.rawContent??void 0,lastUpdate:J.lastUpdate??void 0},this.callbacks.onRepoChange(J.path,this._watcherState);if(J.rawContent)this.callbacks.onFileNavigate(J.rawContent)}),this._watcherState={enabled:!0,sourceFile:this.targetFile},this.watcher.start();let $=this.watcher.state;if($.path&&$.path!==this.getCurrentRepoPath())this._watcherState={enabled:!0,sourceFile:$.sourceFile??this.targetFile,rawContent:$.rawContent??void 0,lastUpdate:$.lastUpdate??void 0},this.callbacks.onRepoChange($.path,this._watcherState);else if($.rawContent)this._watcherState.rawContent=$.rawContent,this.callbacks.onFileNavigate($.rawContent)}toggle(){if(this.watcher)this.stop();else this.start()}stop(){if(this.watcher)this.watcher.stop(),this.watcher=null,this._watcherState={enabled:!1}}}function F$($){let J=`{bold}{green-fg}${$.current}{/green-fg}{/bold}`;if($.tracking)J+=` {gray-fg}→{/gray-fg} {blue-fg}${$.tracking}{/blue-fg}`;if($.ahead>0)J+=` {green-fg}↑${$.ahead}{/green-fg}`;if($.behind>0)J+=` {red-fg}↓${$.behind}{/red-fg}`;return J}function z9($,J,K,Z,X){if(!$)return"{gray-fg}Waiting for target path...{/gray-fg}";let Q=Z9($),z=Z==="Not a git repository",q=`{bold}{cyan-fg}${Q}{/cyan-fg}{/bold}`;if(K)q+=" {yellow-fg}⟳{/yellow-fg}";if(z)q+=" {yellow-fg}(not a git repository){/yellow-fg}";else if(Z)q+=` {red-fg}(${Z}){/red-fg}`;let Y=J?F$(J):"";if(Y){let U=Q.length;if(K)U+=2;if(z)U+=24;else if(Z)U+=Z.length+3;let V=J?J.current.length+(J.tracking?3+J.tracking.length:0)+(J.ahead>0?3+String(J.ahead).length:0)+(J.behind>0?3+String(J.behind).length:0):0,B=Math.max(1,X-U-V-2);return q+" ".repeat(B)+Y}return q}function q9($){return $.replace(/\{[^}]+\}/g,"").length}function V9($,J,K,Z,X,Q,z){let q="{gray-fg}?{/gray-fg} ";if(q+=J?"{yellow-fg}[scroll]{/yellow-fg}":"{yellow-fg}m:[select]{/yellow-fg}",q+=" ",q+=K?"{blue-fg}[auto]{/blue-fg}":"{gray-fg}[auto]{/gray-fg}",q+=" ",q+=Z?"{blue-fg}[wrap]{/blue-fg}":"{gray-fg}[wrap]{/gray-fg}",q+=" ",q+=X?"{blue-fg}[follow]{/blue-fg}":"{gray-fg}[follow]{/gray-fg}",$==="explorer")q+=" ",q+=Q?"{blue-fg}[changes]{/blue-fg}":"{gray-fg}[changes]{/gray-fg}";let U=[{key:"1",label:"Diff",tab:"diff"},{key:"2",label:"Commit",tab:"commit"},{key:"3",label:"History",tab:"history"},{key:"4",label:"Compare",tab:"compare"},{key:"5",label:"Explorer",tab:"explorer"}].map(({key:k,label:j,tab:O})=>{if($===O)return`{bold}{cyan-fg}[${k}]${j}{/cyan-fg}{/bold}`;return`[${k}]${j}`}).join(" "),V=q9(q),B=q9(U),G=Math.max(1,z-V-B);return q+" ".repeat(G)+U}import*as f from"node:fs";import*as R from"node:path";import{EventEmitter as L$}from"node:events";import{simpleGit as E$}from"simple-git";async function e($,J){if(J.length===0)return new Set;let K=E$($),Z=new Set,X=100;for(let Q=0;Q<J.length;Q+=X){let z=J.slice(Q,Q+X);try{let Y=(await K.raw(["check-ignore",...z])).trim().split(`
|
|
30
|
+
`).filter((U)=>U.length>0);for(let U of Y)Z.add(U)}catch{}}return Z}var I$=1048576,R$=102400;function y$($){let J=Math.min($.length,8192);for(let K=0;K<J;K++)if($[K]===0)return!0;return!1}class o7 extends L${repoPath;options;expandedPaths=new Set;gitStatusMap={files:new Map,directories:new Set};_state={currentPath:"",tree:null,displayRows:[],selectedIndex:0,selectedFile:null,isLoading:!1,error:null};constructor($,J){super();this.repoPath=$,this.options={hideHidden:J.hideHidden??!0,hideGitignored:J.hideGitignored??!0,showOnlyChanges:J.showOnlyChanges??!1},this.expandedPaths.add("")}get state(){return this._state}updateState($){this._state={...this._state,...$},this.emit("state-change",this._state)}async setOptions($){this.options={...this.options,...$},await this.loadTree()}setGitStatus($){if(this.gitStatusMap=$,this._state.tree)this.applyGitStatusToTree(this._state.tree),this.refreshDisplayRows()}async toggleShowOnlyChanges(){this.options.showOnlyChanges=!this.options.showOnlyChanges,this.refreshDisplayRows()}get showOnlyChanges(){return this.options.showOnlyChanges}async loadTree(){this.updateState({isLoading:!0,error:null});try{let $=await this.buildTreeNode("",0);if($){$.expanded=!0,this.applyGitStatusToTree($);let J=this.flattenTree($);this.updateState({tree:$,displayRows:J,selectedIndex:0,selectedFile:null,isLoading:!1})}else this.updateState({tree:null,displayRows:[],isLoading:!1,error:"Failed to load directory"})}catch($){this.updateState({error:$ instanceof Error?$.message:"Failed to read directory",tree:null,displayRows:[],isLoading:!1})}}async buildTreeNode($,J){try{let K=R.join(this.repoPath,$);if(!(await f.promises.stat(K)).isDirectory())return{name:R.basename($)||this.getRepoName(),path:$,isDirectory:!1,expanded:!1,children:[],childrenLoaded:!0};let X=this.expandedPaths.has($),Q={name:R.basename($)||this.getRepoName(),path:$,isDirectory:!0,expanded:X,children:[],childrenLoaded:!1};if($===""||X)await this.loadChildrenForNode(Q);return Q}catch(K){return null}}async loadChildrenForNode($){if($.childrenLoaded)return;try{let J=R.join(this.repoPath,$.path),K=await f.promises.readdir(J,{withFileTypes:!0}),Z=K.map((z)=>$.path?R.join($.path,z.name):z.name),X=this.options.hideGitignored?await e(this.repoPath,Z):new Set,Q=[];for(let z of K){if(this.options.hideHidden&&z.name.startsWith("."))continue;let q=$.path?R.join($.path,z.name):z.name;if(this.options.hideGitignored&&X.has(q))continue;let Y=z.isDirectory(),U=this.expandedPaths.has(q),V={name:z.name,path:q,isDirectory:Y,expanded:U,children:[],childrenLoaded:!Y};if(Y&&U)await this.loadChildrenForNode(V);Q.push(V)}Q.sort((z,q)=>{if(z.isDirectory&&!q.isDirectory)return-1;if(!z.isDirectory&&q.isDirectory)return 1;return z.name.localeCompare(q.name)}),this.collapseNode($,Q),$.childrenLoaded=!0}catch(J){$.childrenLoaded=!0,$.children=[]}}collapseNode($,J){for(let K of J)if(K.isDirectory&&K.childrenLoaded)while(K.children.length===1&&K.children[0].isDirectory&&K.children[0].childrenLoaded){let Z=K.children[0];K.name=`${K.name}/${Z.name}`,K.path=Z.path,K.children=Z.children,K.expanded=this.expandedPaths.has(K.path)}$.children=J}applyGitStatusToTree($){if(!$.isDirectory){let J=this.gitStatusMap.files.get($.path);if(J)$.gitStatus=J.status;else $.gitStatus=void 0}else{$.hasChangedChildren=this.gitStatusMap.directories.has($.path);for(let J of $.children)this.applyGitStatusToTree(J)}}flattenTree($){let J=[],K=(Z,X,Q)=>{if(X===0){for(let z=0;z<Z.children.length;z++){let q=Z.children[z],Y=z===Z.children.length-1;if(this.options.showOnlyChanges){if(q.isDirectory&&!q.hasChangedChildren)continue;if(!q.isDirectory&&!q.gitStatus)continue}if(J.push({node:q,depth:0,isLast:Y,parentIsLast:[]}),q.isDirectory&&q.expanded)K(q,1,[Y])}return}for(let z=0;z<Z.children.length;z++){let q=Z.children[z],Y=z===Z.children.length-1;if(this.options.showOnlyChanges){if(q.isDirectory&&!q.hasChangedChildren)continue;if(!q.isDirectory&&!q.gitStatus)continue}if(J.push({node:q,depth:X,isLast:Y,parentIsLast:[...Q]}),q.isDirectory&&q.expanded)K(q,X+1,[...Q,Y])}};return K($,0,[]),J}refreshDisplayRows(){if(!this._state.tree)return;let $=this._state.displayRows[this._state.selectedIndex]?.node.path??null,J=this.flattenTree(this._state.tree),K=0;if($!==null){let Z=J.findIndex((X)=>X.node.path===$);if(Z>=0)K=Z}K=Math.min(K,Math.max(0,J.length-1)),this.updateState({displayRows:J,selectedIndex:K})}getRepoName(){return R.basename(this.repoPath)||"repo"}async loadDirectory($){this._state.currentPath=$,await this.loadTree()}async loadFile($){try{let J=R.join(this.repoPath,$),K=await f.promises.stat(J);if(K.size>I$){this.updateState({selectedFile:{path:$,content:`File too large to display (${(K.size/1024/1024).toFixed(2)} MB).
|
|
31
|
+
Maximum size: 1 MB`,truncated:!0}});return}let Z=await f.promises.readFile(J);if(y$(Z)){this.updateState({selectedFile:{path:$,content:"Binary file - cannot display"}});return}let X=Z.toString("utf-8"),Q=!1;if(K.size>R$)X=`Warning: Large file (${(K.size/1024).toFixed(1)} KB)
|
|
32
|
+
|
|
33
|
+
`+X;let z=5000,q=X.split(`
|
|
34
|
+
`);if(q.length>z)X=q.slice(0,z).join(`
|
|
35
|
+
`)+`
|
|
36
|
+
|
|
37
|
+
... (truncated, ${q.length-z} more lines)`,Q=!0;this.updateState({selectedFile:{path:$,content:X,truncated:Q}})}catch(J){this.updateState({selectedFile:{path:$,content:J instanceof Error?`Error: ${J.message}`:"Failed to read file"}})}}async selectIndex($){let J=this._state.displayRows;if($<0||$>=J.length)return;let K=J[$];if(this.updateState({selectedIndex:$}),K&&!K.node.isDirectory)await this.loadFile(K.node.path);else this.updateState({selectedFile:null})}navigateUp($){let J=Math.max(0,this._state.selectedIndex-1);if(J===this._state.selectedIndex)return null;if(this.selectIndex(J),J<$)return J;return null}navigateDown($,J){let K=Math.min(this._state.displayRows.length-1,this._state.selectedIndex+1);if(K===this._state.selectedIndex)return null;this.selectIndex(K);let X=this._state.displayRows.length>J?J-2:J,Q=$+X;if(K>=Q)return $+1;return null}async toggleExpand(){let $=this._state.displayRows,J=this._state.selectedIndex;if(J<0||J>=$.length)return;let K=$[J];if(!K.node.isDirectory)return;let Z=K.node;if(Z.expanded)this.expandedPaths.delete(Z.path),Z.expanded=!1;else if(this.expandedPaths.add(Z.path),Z.expanded=!0,!Z.childrenLoaded)await this.loadChildrenForNode(Z),this.applyGitStatusToTree(Z);this.refreshDisplayRows()}async enterDirectory(){let $=this._state.displayRows,J=this._state.selectedIndex;if(J<0||J>=$.length)return;if($[J].node.isDirectory)await this.toggleExpand()}async goUp(){let $=this._state.displayRows,J=this._state.selectedIndex;if(J<0||J>=$.length)return;let Z=$[J].node.path,X=R.dirname(Z);if(X==="."||X==="")return;let Q=Z.split("/");for(let z=Q.length-1;z>0;z--){let q=Q.slice(0,z).join("/");if(this.expandedPaths.has(q)){this.expandedPaths.delete(q);let Y=this.findNodeByPath(q);if(Y)Y.expanded=!1;this.refreshDisplayRows();let V=this._state.displayRows.findIndex((B)=>B.node.path===q);if(V>=0)this.updateState({selectedIndex:V,selectedFile:null});return}}}findNodeByPath($){if(!this._state.tree)return null;let J=(K)=>{if(K.path===$)return K;for(let Z of K.children){let X=J(Z);if(X)return X}return null};return J(this._state.tree)}async getAllFilePaths(){let $=[],J=async(K)=>{try{let Z=R.join(this.repoPath,K),X=await f.promises.readdir(Z,{withFileTypes:!0}),Q=X.map((q)=>K?R.join(K,q.name):q.name),z=this.options.hideGitignored?await e(this.repoPath,Q):new Set;for(let q of X){if(this.options.hideHidden&&q.name.startsWith("."))continue;let Y=K?R.join(K,q.name):q.name;if(this.options.hideGitignored&&z.has(Y))continue;if(q.isDirectory())await J(Y);else $.push(Y)}}catch(Z){}};return await J(""),$}async navigateToPath($){if(!this._state.tree)return!1;let J=$.split("/"),K="";for(let X=0;X<J.length-1;X++)K=K?`${K}/${J[X]}`:J[X],this.expandedPaths.add(K);await this.loadTree();let Z=this._state.displayRows.findIndex((X)=>X.node.path===$);if(Z>=0)return await this.selectIndex(Z),!0;return!1}dispose(){this.removeAllListeners()}}import C$ from"neo-blessed";class r7{box;screen;selectedIndex;currentTheme;onSelect;onCancel;constructor($,J,K,Z){if(this.screen=$,this.currentTheme=J,this.onSelect=K,this.onCancel=Z,this.selectedIndex=x.indexOf(J),this.selectedIndex<0)this.selectedIndex=0;let X=50,Q=x.length+12;this.box=C$.box({parent:$,top:"center",left:"center",width:X,height:Q,border:{type:"line"},style:{border:{fg:"cyan"}},tags:!0,keys:!0}),this.setupKeyHandlers(),this.render()}setupKeyHandlers(){this.box.key(["escape","q"],()=>{this.close(),this.onCancel()}),this.box.key(["enter","space"],()=>{let $=x[this.selectedIndex];this.close(),this.onSelect($)}),this.box.key(["up","k"],()=>{this.selectedIndex=Math.max(0,this.selectedIndex-1),this.render()}),this.box.key(["down","j"],()=>{this.selectedIndex=Math.min(x.length-1,this.selectedIndex+1),this.render()})}render(){let $=[];$.push("{bold}{cyan-fg} Select Theme{/cyan-fg}{/bold}"),$.push("");for(let K=0;K<x.length;K++){let Z=x[K],X=z7[Z],Q=K===this.selectedIndex,z=Z===this.currentTheme,q=Q?"{cyan-fg}{bold}> ":" ";if(q+=X.displayName,Q)q+="{/bold}{/cyan-fg}";if(z)q+=" {gray-fg}(current){/gray-fg}";$.push(q)}$.push(""),$.push("{gray-fg}Preview:{/gray-fg}");let J=t(x[this.selectedIndex]);$.push(" {green-fg}+ added line{/green-fg}"),$.push(" {red-fg}- deleted line{/red-fg}"),$.push(""),$.push("{gray-fg}j/k: navigate | Enter: select | Esc: cancel{/gray-fg}"),this.box.setContent($.join(`
|
|
38
|
+
`)),this.screen.render()}close(){this.box.destroy()}focus(){this.box.focus()}}import P$ from"neo-blessed";var u=[{title:"Navigation",entries:[{key:"j/k",description:"Move up/down"},{key:"Tab",description:"Toggle pane focus"}]},{title:"Staging",entries:[{key:"s",description:"Stage file"},{key:"U",description:"Unstage file"},{key:"A",description:"Stage all"},{key:"Z",description:"Unstage all"},{key:"Space",description:"Toggle stage"}]},{title:"Actions",entries:[{key:"c",description:"Commit panel"},{key:"r",description:"Refresh"},{key:"q",description:"Quit"}]},{title:"Resize",entries:[{key:"-",description:"Shrink top pane"},{key:"+",description:"Grow top pane"}]},{title:"Tabs",entries:[{key:"1",description:"Diff view"},{key:"2",description:"Commit panel"},{key:"3",description:"History view"},{key:"4",description:"Compare view"},{key:"5",description:"Explorer view"}]},{title:"Toggles",entries:[{key:"m",description:"Mouse mode"},{key:"w",description:"Wrap mode"},{key:"f",description:"Follow mode"},{key:"t",description:"Theme picker"},{key:"?",description:"This help"}]},{title:"Explorer",entries:[{key:"Enter",description:"Enter directory"},{key:"Backspace",description:"Go up"}]},{title:"Compare",entries:[{key:"b",description:"Base branch picker"},{key:"u",description:"Toggle uncommitted"}]},{title:"Diff",entries:[{key:"d",description:"Discard changes"}]}];class a7{box;screen;onClose;constructor($,J){this.screen=$,this.onClose=J;let{width:K,height:Z}=$,X=K>=90,Q=X?Math.min(80,K-4):Math.min(42,K-4),z=Math.min(this.calculateHeight(X),Z-4);this.box=P$.box({parent:$,top:"center",left:"center",width:Q,height:z,border:{type:"line"},style:{border:{fg:"cyan"}},tags:!0,keys:!0,scrollable:!0,alwaysScroll:!0}),this.setupKeyHandlers(),this.render(X,Q)}calculateHeight($){if($){let J=Math.ceil(u.length/2),K=u.slice(0,J),Z=u.slice(J),X=K.reduce((z,q)=>z+q.entries.length+2,0),Q=Z.reduce((z,q)=>z+q.entries.length+2,0);return Math.max(X,Q)+5}else return u.reduce((J,K)=>J+K.entries.length+2,0)+5}setupKeyHandlers(){this.box.key(["escape","enter","?","q"],()=>{this.close(),this.onClose()}),this.box.on("click",()=>{this.close(),this.onClose()})}visibleWidth($){return $.replace(/\{[^}]+\}/g,"").length}padToVisible($,J){let K=this.visibleWidth($),Z=Math.max(0,J-K);return $+" ".repeat(Z)}render($,J){let K=[];if(K.push("{bold}{cyan-fg} Keyboard Shortcuts{/cyan-fg}{/bold}"),K.push(""),$){let Z=Math.ceil(u.length/2),X=u.slice(0,Z),Q=u.slice(Z),z=Math.floor((J-6)/2),q=this.renderGroups(X,z),Y=this.renderGroups(Q,z),U=Math.max(q.length,Y.length);for(let V=0;V<U;V++){let B=this.padToVisible(q[V]||"",z),G=Y[V]||"";K.push(B+" "+G)}}else for(let Z of u){K.push(`{bold}{gray-fg}${Z.title}{/gray-fg}{/bold}`);for(let X of Z.entries)K.push(` {cyan-fg}${X.key.padEnd(10)}{/cyan-fg} ${X.description}`);K.push("")}K.push(""),K.push("{gray-fg}Press Esc, Enter, or ? to close{/gray-fg}"),this.box.setContent(K.join(`
|
|
39
|
+
`)),this.screen.render()}renderGroups($,J){let K=[];for(let Z of $){K.push(`{bold}{gray-fg}${Z.title}{/gray-fg}{/bold}`);for(let X of Z.entries)K.push(` {cyan-fg}${X.key.padEnd(10)}{/cyan-fg} ${X.description}`);K.push("")}return K}close(){this.box.destroy()}focus(){this.box.focus()}}import S$ from"neo-blessed";class s7{box;screen;branches;selectedIndex;currentBranch;onSelect;onCancel;constructor($,J,K,Z,X){if(this.screen=$,this.branches=J,this.currentBranch=K,this.onSelect=Z,this.onCancel=X,this.selectedIndex=K?J.indexOf(K):0,this.selectedIndex<0)this.selectedIndex=0;let Q=50,q=Math.min(J.length,15)+6;this.box=S$.box({parent:$,top:"center",left:"center",width:Q,height:q,border:{type:"line"},style:{border:{fg:"cyan"}},tags:!0,keys:!0,scrollable:!0,alwaysScroll:!0}),this.setupKeyHandlers(),this.render()}setupKeyHandlers(){this.box.key(["escape","q"],()=>{this.close(),this.onCancel()}),this.box.key(["enter","space"],()=>{let $=this.branches[this.selectedIndex];if($)this.close(),this.onSelect($)}),this.box.key(["up","k"],()=>{this.selectedIndex=Math.max(0,this.selectedIndex-1),this.render()}),this.box.key(["down","j"],()=>{this.selectedIndex=Math.min(this.branches.length-1,this.selectedIndex+1),this.render()})}render(){let $=[];if($.push("{bold}{cyan-fg} Select Base Branch{/cyan-fg}{/bold}"),$.push(""),this.branches.length===0)$.push("{gray-fg}No branches found{/gray-fg}");else for(let J=0;J<this.branches.length;J++){let K=this.branches[J],Z=J===this.selectedIndex,X=K===this.currentBranch,Q=Z?"{cyan-fg}{bold}> ":" ";if(Q+=K,Z)Q+="{/bold}{/cyan-fg}";if(X)Q+=" {gray-fg}(current){/gray-fg}";$.push(Q)}$.push(""),$.push("{gray-fg}j/k: navigate | Enter: select | Esc: cancel{/gray-fg}"),this.box.setContent($.join(`
|
|
40
|
+
`)),this.screen.render()}close(){this.box.destroy()}focus(){this.box.focus()}}import b$ from"neo-blessed";class n7{box;screen;filePath;onConfirm;onCancel;constructor($,J,K,Z){this.screen=$,this.filePath=J,this.onConfirm=K,this.onCancel=Z;let X=Math.min(60,Math.max(40,J.length+20)),Q=7;this.box=b$.box({parent:$,top:"center",left:"center",width:X,height:Q,border:{type:"line"},style:{border:{fg:"yellow"}},tags:!0,keys:!0}),this.setupKeyHandlers(),this.render()}setupKeyHandlers(){this.box.key(["y","Y"],()=>{this.close(),this.onConfirm()}),this.box.key(["n","N","escape","q"],()=>{this.close(),this.onCancel()})}render(){let $=[];$.push("{bold}{yellow-fg} Discard Changes?{/yellow-fg}{/bold}"),$.push("");let J=this.box.width-6,K=this.filePath.length>J?"..."+this.filePath.slice(-(J-3)):this.filePath;$.push(`{white-fg}${K}{/white-fg}`),$.push(""),$.push("{gray-fg}Press {/gray-fg}{green-fg}y{/green-fg}{gray-fg} to confirm, {/gray-fg}{red-fg}n{/red-fg}{gray-fg} or Esc to cancel{/gray-fg}"),this.box.setContent($.join(`
|
|
41
|
+
`)),this.screen.render()}close(){this.box.destroy()}focus(){this.box.focus()}}import Y9 from"neo-blessed";var _7=15;function x$($,J){let K=$.toLowerCase(),Z=J.toLowerCase(),X=0,Q=0,z=-1;for(let q=0;q<Z.length&&X<K.length;q++)if(Z[q]===K[X]){if(z===q-1)Q+=10;if(q===0||Z[q-1]==="/"||Z[q-1]===".")Q+=5;Q+=1,z=q,X++}if(X<K.length)return-1;return Q+=Math.max(0,50-J.length),Q}function w$($,J){if(!$)return J;let K=$.toLowerCase(),Z=J.toLowerCase(),X="",Q=0;for(let z=0;z<J.length;z++)if(Q<K.length&&Z[z]===K[Q])X+=`{yellow-fg}${J[z]}{/yellow-fg}`,Q++;else X+=J[z];return X}class i7{box;textbox;screen;allPaths;results=[];selectedIndex=0;query="";onSelect;onCancel;constructor($,J,K,Z){this.screen=$,this.allPaths=J,this.onSelect=K,this.onCancel=Z;let X=Math.min(80,$.width-10),Q=_7+6;this.box=Y9.box({parent:$,top:"center",left:"center",width:X,height:Q,border:{type:"line"},style:{border:{fg:"cyan"}},tags:!0,keys:!1}),this.textbox=Y9.textarea({parent:this.box,top:1,left:1,width:X-4,height:1,inputOnFocus:!0,style:{fg:"white",bg:"default"}}),this.setupKeyHandlers(),this.updateResults(),this.render()}setupKeyHandlers(){this.textbox.key(["escape"],()=>{this.close(),this.onCancel()}),this.textbox.key(["enter"],()=>{if(this.results.length>0){let $=this.results[this.selectedIndex];this.close(),this.onSelect($.path)}}),this.textbox.key(["C-j","down"],()=>{this.selectedIndex=Math.min(this.results.length-1,this.selectedIndex+1),this.render()}),this.textbox.key(["C-k","up"],()=>{this.selectedIndex=Math.max(0,this.selectedIndex-1),this.render()}),this.textbox.key(["tab"],()=>{this.selectedIndex=(this.selectedIndex+1)%Math.max(1,this.results.length),this.render()}),this.textbox.key(["S-tab"],()=>{this.selectedIndex=(this.selectedIndex-1+this.results.length)%Math.max(1,this.results.length),this.render()}),this.textbox.on("keypress",()=>{setImmediate(()=>{let $=this.textbox.getValue()||"";if($!==this.query)this.query=$,this.selectedIndex=0,this.updateResults(),this.render()})})}updateResults(){if(!this.query){this.results=this.allPaths.slice(0,_7).map((J)=>({path:J,score:0}));return}let $=[];for(let J of this.allPaths){let K=x$(this.query,J);if(K>=0)$.push({path:J,score:K})}$.sort((J,K)=>K.score-J.score),this.results=$.slice(0,_7)}render(){let $=[],J=this.box.width-4;if($.push("{bold}{cyan-fg}Find File{/cyan-fg}{/bold}"),$.push(""),$.push(""),this.results.length===0&&this.query)$.push("{gray-fg}No matches{/gray-fg}");else for(let K=0;K<this.results.length;K++){let Z=this.results[K],X=K===this.selectedIndex,Q=Z.path,z=J-4;if(Q.length>z)Q="…"+Q.slice(-(z-1));let q=w$(this.query,Q);if(X)$.push(`{cyan-fg}{bold}> ${q}{/bold}{/cyan-fg}`);else $.push(` ${q}`)}while($.length<_7+3)$.push("");$.push("{gray-fg}Enter: select | Esc: cancel | Ctrl+j/k or ↑↓: navigate{/gray-fg}"),this.box.setContent($.join(`
|
|
42
|
+
`)),this.screen.render()}close(){this.textbox.destroy(),this.box.destroy()}focus(){this.textbox.focus()}}import{EventEmitter as u$}from"node:events";function U9($,J,K){if(!$.trim())return{valid:!1,error:"Commit message cannot be empty"};if(J===0&&!K)return{valid:!1,error:"No changes staged for commit"};return{valid:!0,error:null}}function B9($){return $.trim()}var G9={message:"",amend:!1,isCommitting:!1,error:null,inputFocused:!1};class t7 extends u${_state={...G9};getHeadMessage;onCommit;onSuccess;stagedCount=0;constructor($){super();this.getHeadMessage=$.getHeadMessage,this.onCommit=$.onCommit,this.onSuccess=$.onSuccess}get state(){return this._state}update($){this._state={...this._state,...$},this.emit("change",this._state)}setStagedCount($){this.stagedCount=$}setMessage($){this.update({message:$,error:null})}setInputFocused($){this.update({inputFocused:$}),this.emit("focus-change",$)}async toggleAmend(){let $=!this._state.amend;if(this.update({amend:$}),$&&!this._state.message)try{let J=await this.getHeadMessage();if(J)this.update({message:J})}catch{}}async submit(){let $=U9(this._state.message,this.stagedCount,this._state.amend);if(!$.valid){this.update({error:$.error});return}this.update({isCommitting:!0,error:null});try{await this.onCommit(B9(this._state.message),this._state.amend),this.update({message:"",amend:!1,isCommitting:!1,inputFocused:!1}),this.onSuccess()}catch(J){this.update({isCommitting:!1,error:J instanceof Error?J.message:"Commit failed"})}}reset(){this._state={...G9},this.emit("change",this._state)}}import{EventEmitter as p$}from"node:events";var g$={currentPane:"files",bottomTab:"diff",selectedIndex:0,fileListScrollOffset:0,diffScrollOffset:0,historyScrollOffset:0,compareScrollOffset:0,explorerScrollOffset:0,explorerFileScrollOffset:0,historySelectedIndex:0,compareSelectedIndex:0,includeUncommitted:!1,explorerSelectedIndex:0,wrapMode:!1,autoTabEnabled:!1,mouseEnabled:!0,hideHiddenFiles:!0,hideGitignored:!0,splitRatio:0.4,activeModal:null,pendingDiscard:null,commitInputFocused:!1};class e7 extends p${_state;constructor($={}){super();this._state={...g$,...$}}get state(){return this._state}update($){this._state={...this._state,...$},this.emit("change",this._state)}setPane($){if(this._state.currentPane!==$)this.update({currentPane:$}),this.emit("pane-change",$)}setTab($){if(this._state.bottomTab!==$){let J={diff:"files",commit:"commit",history:"history",compare:"compare",explorer:"explorer"};this.update({bottomTab:$,currentPane:J[$]}),this.emit("tab-change",$)}}setSelectedIndex($){if(this._state.selectedIndex!==$)this.update({selectedIndex:$}),this.emit("selection-change",$)}setFileListScrollOffset($){this.update({fileListScrollOffset:Math.max(0,$)}),this.emit("scroll-change",{type:"fileList",offset:$})}setDiffScrollOffset($){this.update({diffScrollOffset:Math.max(0,$)}),this.emit("scroll-change",{type:"diff",offset:$})}setHistoryScrollOffset($){this.update({historyScrollOffset:Math.max(0,$)}),this.emit("scroll-change",{type:"history",offset:$})}setCompareScrollOffset($){this.update({compareScrollOffset:Math.max(0,$)}),this.emit("scroll-change",{type:"compare",offset:$})}setExplorerScrollOffset($){this.update({explorerScrollOffset:Math.max(0,$)}),this.emit("scroll-change",{type:"explorer",offset:$})}setExplorerFileScrollOffset($){this.update({explorerFileScrollOffset:Math.max(0,$)}),this.emit("scroll-change",{type:"explorerFile",offset:$})}setHistorySelectedIndex($){this.update({historySelectedIndex:Math.max(0,$)})}setCompareSelectedIndex($){this.update({compareSelectedIndex:Math.max(0,$)})}toggleIncludeUncommitted(){this.update({includeUncommitted:!this._state.includeUncommitted})}setExplorerSelectedIndex($){this.update({explorerSelectedIndex:Math.max(0,$)})}toggleWrapMode(){this.update({wrapMode:!this._state.wrapMode,diffScrollOffset:0})}toggleAutoTab(){this.update({autoTabEnabled:!this._state.autoTabEnabled})}toggleMouse(){this.update({mouseEnabled:!this._state.mouseEnabled})}toggleHideHiddenFiles(){this.update({hideHiddenFiles:!this._state.hideHiddenFiles})}toggleHideGitignored(){this.update({hideGitignored:!this._state.hideGitignored})}adjustSplitRatio($){let J=Math.min(0.85,Math.max(0.15,this._state.splitRatio+$));this.update({splitRatio:J})}setSplitRatio($){this.update({splitRatio:Math.min(0.85,Math.max(0.15,$))})}openModal($){this.update({activeModal:$}),this.emit("modal-change",$)}closeModal(){this.update({activeModal:null}),this.emit("modal-change",null)}toggleModal($){if(this._state.activeModal===$)this.closeModal();else this.openModal($)}setPendingDiscard($){this.update({pendingDiscard:$})}setCommitInputFocused($){this.update({commitInputFocused:$})}togglePane(){let{bottomTab:$,currentPane:J}=this._state;if($==="diff"||$==="commit")this.setPane(J==="files"?"diff":"files");else if($==="history")this.setPane(J==="history"?"diff":"history");else if($==="compare")this.setPane(J==="compare"?"diff":"compare");else if($==="explorer")this.setPane(J==="explorer"?"diff":"explorer")}resetForNewRepo(){this._state={...this._state,selectedIndex:0,fileListScrollOffset:0,diffScrollOffset:0,historySelectedIndex:0,historyScrollOffset:0,compareSelectedIndex:0,compareScrollOffset:0,explorerSelectedIndex:0,explorerScrollOffset:0,explorerFileScrollOffset:0},this.emit("change",this._state)}}import*as C from"node:path";import*as p from"node:fs";import{execFileSync as l$}from"node:child_process";import{watch as x9}from"chokidar";import{EventEmitter as o$}from"node:events";import w9 from"ignore";class j9{queue=[];isProcessing=!1;pendingMutations=0;refreshScheduled=!1;enqueue($){return new Promise((J,K)=>{this.queue.push({execute:$,resolve:J,reject:K}),this.processNext()})}enqueueMutation($){return this.pendingMutations++,this.enqueue($).finally(()=>{this.pendingMutations--})}hasPendingMutations(){return this.pendingMutations>0}scheduleRefresh($){if(this.pendingMutations>0)return;if(this.refreshScheduled)return;this.refreshScheduled=!0,this.enqueue(async()=>{this.refreshScheduled=!1,await $()}).catch(()=>{this.refreshScheduled=!1})}isBusy(){return this.isProcessing||this.queue.length>0}async processNext(){if(this.isProcessing||this.queue.length===0)return;this.isProcessing=!0;let $=this.queue.shift();try{let J=await $.execute();$.resolve(J)}catch(J){$.reject(J instanceof Error?J:Error(String(J)))}finally{this.isProcessing=!1,this.processNext()}}}var $8=new Map;function k9($){let J=$8.get($);if(!J)J=new j9,$8.set($,J);return J}function _9($){$8.delete($)}import{simpleGit as P}from"simple-git";import*as H9 from"node:fs";import*as D9 from"node:path";function O9($){let J=new Map;for(let K of $.trim().split(`
|
|
43
|
+
`)){if(!K)continue;let Z=K.split("\t");if(Z.length>=3){let X=Z[0]==="-"?0:parseInt(Z[0],10),Q=Z[1]==="-"?0:parseInt(Z[1],10),z=Z.slice(2).join("\t");J.set(z,{insertions:X,deletions:Q})}}return J}async function m$($,J){try{let K=D9.join($,J);return(await H9.promises.readFile(K,"utf-8")).split(`
|
|
44
|
+
`).filter((X)=>X.length>0).length}catch{return 0}}function M9($){switch($){case"M":return"modified";case"A":return"added";case"D":return"deleted";case"?":return"untracked";case"R":return"renamed";case"C":return"copied";default:return"modified"}}async function J8($){let J=P($);try{if(!await J.checkIsRepo())return{files:[],branch:{current:"",ahead:0,behind:0},isRepo:!1};let Z=await J.status(),X=[];for(let j of Z.staged)X.push({path:j,status:"added",staged:!0});for(let j of Z.modified)if(!X.find((D)=>D.path===j&&D.staged))X.push({path:j,status:"modified",staged:!1});for(let j of Z.deleted)X.push({path:j,status:"deleted",staged:!1});for(let j of Z.not_added)X.push({path:j,status:"untracked",staged:!1});for(let j of Z.renamed)X.push({path:j.to,originalPath:j.from,status:"renamed",staged:!0});let Q=[],z=new Set,q=Z.files.filter((j)=>j.working_dir==="?").map((j)=>j.path),Y=await e($,q);for(let j of Z.files){if(j.index==="!"||j.working_dir==="!"||Y.has(j.path))continue;let O=`${j.path}-${j.index!==" "&&j.index!=="?"}`;if(z.has(O))continue;if(z.add(O),j.index&&j.index!==" "&&j.index!=="?")Q.push({path:j.path,status:M9(j.index),staged:!0});if(j.working_dir&&j.working_dir!==" ")Q.push({path:j.path,status:j.working_dir==="?"?"untracked":M9(j.working_dir),staged:!1})}let[U,V]=await Promise.all([J.diff(["--cached","--numstat"]).catch(()=>""),J.diff(["--numstat"]).catch(()=>"")]),B=O9(U),G=O9(V);for(let j of Q){let O=j.staged?B.get(j.path):G.get(j.path);if(O)j.insertions=O.insertions,j.deletions=O.deletions}let k=Q.filter((j)=>j.status==="untracked");if(k.length>0){let j=await Promise.all(k.map((O)=>m$($,O.path)));for(let O=0;O<k.length;O++)k[O].insertions=j[O],k[O].deletions=0}return{files:Q,branch:{current:Z.current||"HEAD",tracking:Z.tracking||void 0,ahead:Z.ahead,behind:Z.behind},isRepo:!0}}catch{return{files:[],branch:{current:"",ahead:0,behind:0},isRepo:!1}}}async function v9($,J){await P($).add(J)}async function A9($,J){await P($).reset(["HEAD","--",J])}async function T9($){await P($).add("-A")}async function N9($){await P($).reset(["HEAD"])}async function W9($,J){await P($).checkout(["--",J])}async function F9($,J,K=!1){await P($).commit(J,void 0,K?{"--amend":null}:void 0)}async function E9($){let J=P($);try{return(await J.log({n:1})).latest?.message||""}catch{return""}}async function L9($,J=50){let K=P($);try{return(await K.log({n:J})).all.map((X)=>({hash:X.hash,shortHash:X.hash.slice(0,7),message:X.message.split(`
|
|
45
|
+
`)[0],author:X.author_name,date:new Date(X.date),refs:X.refs||""}))}catch{return[]}}import{execSync as f$}from"node:child_process";import{simpleGit as $7}from"simple-git";function h$($){let J=$.match(/^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/);if(J)return{oldStart:parseInt(J[1],10),newStart:parseInt(J[2],10)};return null}function O7($){let J=$.split(`
|
|
46
|
+
`),K=[],Z=0,X=0;for(let Q of J)if(Q.startsWith("diff --git")||Q.startsWith("index ")||Q.startsWith("---")||Q.startsWith("+++")||Q.startsWith("new file")||Q.startsWith("deleted file")||Q.startsWith("Binary files")||Q.startsWith("similarity index")||Q.startsWith("rename from")||Q.startsWith("rename to"))K.push({type:"header",content:Q});else if(Q.startsWith("@@")){let z=h$(Q);if(z)Z=z.oldStart,X=z.newStart;K.push({type:"hunk",content:Q})}else if(Q.startsWith("+"))K.push({type:"addition",content:Q,newLineNum:X++});else if(Q.startsWith("-"))K.push({type:"deletion",content:Q,oldLineNum:Z++});else K.push({type:"context",content:Q,oldLineNum:Z++,newLineNum:X++});return K}async function J7($,J,K=!1){let Z=$7($);try{let X=[];if(K)X.push("--cached");if(J)X.push("--",J);let Q=await Z.diff(X),z=O7(Q);return{raw:Q,lines:z}}catch{return{raw:"",lines:[]}}}async function K8($,J){try{let K=f$(`cat "${J}"`,{cwd:$,encoding:"utf-8"}),Z=[{type:"header",content:`diff --git a/${J} b/${J}`},{type:"header",content:"new file mode 100644"},{type:"header",content:"--- /dev/null"},{type:"header",content:`+++ b/${J}`}],X=K.split(`
|
|
47
|
+
`);Z.push({type:"hunk",content:`@@ -0,0 +1,${X.length} @@`});let Q=1;for(let q of X)Z.push({type:"addition",content:"+"+q,newLineNum:Q++});return{raw:Z.map((q)=>q.content).join(`
|
|
48
|
+
`),lines:Z}}catch{return{raw:"",lines:[]}}}async function I9($){return J7($,void 0,!0)}async function Z8($){let J=$7($),K=new Set,Z=[];try{let X=await J.raw(["log","--oneline","--decorate=short","--all","-n","200"]),Q=/\(([^)]+)\)/g;for(let z of X.split(`
|
|
49
|
+
`)){let q=Q.exec(z);if(q){let Y=q[1].split(",").map((U)=>U.trim());for(let U of Y){if(U.startsWith("HEAD")||U.startsWith("tag:")||!U.includes("/"))continue;let V=U.replace(/^.*-> /,"");if(V.includes("/")&&!K.has(V))K.add(V),Z.push(V)}}Q.lastIndex=0}if(Z.length>0)Z.sort((z,q)=>{let Y=z.split("/").slice(1).join("/"),U=q.split("/").slice(1).join("/"),V=Y==="main"||Y==="master",B=U==="main"||U==="master";if(V&&!B)return-1;if(!V&&B)return 1;if(V&&B){let G=z.startsWith("origin/"),k=q.startsWith("origin/");if(G&&!k)return 1;if(!G&&k)return-1}return 0})}catch{}return[...new Set(Z)]}async function R9($){return(await Z8($))[0]??null}async function X8($,J){let K=$7($),X=(await K.raw(["merge-base",J,"HEAD"])).trim(),Q=await K.raw(["diff","--numstat",`${X}...HEAD`]),z=await K.raw(["diff","--name-status",`${X}...HEAD`]),q=await K.raw(["diff",`${X}...HEAD`]),Y=Q.trim().split(`
|
|
50
|
+
`).filter((_)=>_),U=new Map;for(let _ of Y){let M=_.split("\t");if(M.length>=3){let v=M[0]==="-"?0:parseInt(M[0],10),W=M[1]==="-"?0:parseInt(M[1],10),N=M.slice(2).join("\t");U.set(N,{additions:v,deletions:W})}}let V=z.trim().split(`
|
|
51
|
+
`).filter((_)=>_),B=new Map;for(let _ of V){let M=_.split("\t");if(M.length>=2){let v=M[0][0],W=M[M.length-1],N;switch(v){case"A":N="added";break;case"D":N="deleted";break;case"R":N="renamed";break;default:N="modified"}B.set(W,N)}}let G=[],k=q.split(/(?=^diff --git )/m).filter((_)=>_.trim());for(let _ of k){let M=_.match(/^diff --git a\/.+ b\/(.+)$/m);if(!M)continue;let v=M[1],W=O7(_),N=U.get(v)||{additions:0,deletions:0},F=B.get(v)||"modified";G.push({path:v,status:F,additions:N.additions,deletions:N.deletions,diff:{raw:_,lines:W}})}let j=0,O=0;for(let _ of G)j+=_.additions,O+=_.deletions;let H=(await K.status()).files.length,T=(await K.log({from:X,to:"HEAD"})).all.map((_)=>({hash:_.hash,shortHash:_.hash.slice(0,7),message:_.message.split(`
|
|
52
|
+
`)[0],author:_.author_name,date:new Date(_.date),refs:_.refs||""}));return G.sort((_,M)=>_.path.localeCompare(M.path)),{baseBranch:J,stats:{filesChanged:G.length,additions:j,deletions:O},files:G,commits:T,uncommittedCount:H}}async function Q8($,J){let K=$7($);try{let Z=await K.raw(["show",J,"--format="]),X=O7(Z);return{raw:Z,lines:X}}catch{return{raw:"",lines:[]}}}async function y9($,J){let K=$7($),Z=await X8($,J),X=await K.diff(["--cached","--numstat"]),Q=await K.diff(["--numstat"]),z=await K.diff(["--cached"]),q=await K.diff([]),Y=new Map;for(let _ of X.trim().split(`
|
|
53
|
+
`).filter((M)=>M)){let M=_.split("\t");if(M.length>=3){let v=M[0]==="-"?0:parseInt(M[0],10),W=M[1]==="-"?0:parseInt(M[1],10),N=M.slice(2).join("\t");Y.set(N,{additions:v,deletions:W,staged:!0,unstaged:!1})}}for(let _ of Q.trim().split(`
|
|
54
|
+
`).filter((M)=>M)){let M=_.split("\t");if(M.length>=3){let v=M[0]==="-"?0:parseInt(M[0],10),W=M[1]==="-"?0:parseInt(M[1],10),N=M.slice(2).join("\t"),F=Y.get(N);if(F)F.additions+=v,F.deletions+=W,F.unstaged=!0;else Y.set(N,{additions:v,deletions:W,staged:!1,unstaged:!0})}}let U=await K.status(),V=new Map;for(let _ of U.files)if(_.index==="A"||_.working_dir==="?")V.set(_.path,"added");else if(_.index==="D"||_.working_dir==="D")V.set(_.path,"deleted");else if(_.index==="R")V.set(_.path,"renamed");else V.set(_.path,"modified");let B=[],k=(z+q).split(/(?=^diff --git )/m).filter((_)=>_.trim()),j=new Set;for(let _ of k){let M=_.match(/^diff --git a\/.+ b\/(.+)$/m);if(!M)continue;let v=M[1];if(j.has(v))continue;j.add(v);let W=O7(_),N=Y.get(v)||{additions:0,deletions:0},F=V.get(v)||"modified";B.push({path:v,status:F,additions:N.additions,deletions:N.deletions,diff:{raw:_,lines:W},isUncommitted:!0})}let O=new Set(Z.files.map((_)=>_.path)),D=[];for(let _ of Z.files){let M=B.find((v)=>v.path===_.path);if(M)D.push(_),D.push(M);else D.push(_)}for(let _ of B)if(!O.has(_.path))D.push(_);let H=0,A=0,T=new Set;for(let _ of D){if(!T.has(_.path))T.add(_.path);H+=_.additions,A+=_.deletions}return D.sort((_,M)=>_.path.localeCompare(M.path)),{baseBranch:Z.baseBranch,stats:{filesChanged:T.size,additions:H,deletions:A},files:D,commits:Z.commits,uncommittedCount:Z.uncommittedCount}}import*as S from"node:fs";import*as h from"node:path";import*as C9 from"node:os";var M7=h.join(C9.homedir(),".cache","diffstalker","base-branches.json");function c$(){let $=h.dirname(M7);if(!S.existsSync($))S.mkdirSync($,{recursive:!0})}function P9(){try{if(S.existsSync(M7))return JSON.parse(S.readFileSync(M7,"utf-8"))}catch{}return{}}function d$($){c$(),S.writeFileSync(M7,JSON.stringify($,null,2)+`
|
|
55
|
+
`)}function S9($){let J=P9(),K=h.resolve($);return J[K]}function b9($,J){let K=P9(),Z=h.resolve($);K[Z]=J,d$(K)}class u9 extends o${repoPath;queue;gitWatcher=null;workingDirWatcher=null;ignorers=new Map;diffDebounceTimer=null;_state={status:null,diff:null,selectedFile:null,isLoading:!1,error:null};_compareState={compareDiff:null,compareBaseBranch:null,compareLoading:!1,compareError:null};_historyState={commits:[],selectedCommit:null,commitDiff:null,isLoading:!1};_compareSelectionState={type:null,index:0,diff:null};constructor($){super();this.repoPath=$,this.queue=k9($)}get state(){return this._state}get compareState(){return this._compareState}get historyState(){return this._historyState}get compareSelectionState(){return this._compareSelectionState}updateState($){this._state={...this._state,...$},this.emit("state-change",this._state)}updateCompareState($){this._compareState={...this._compareState,...$},this.emit("compare-state-change",this._compareState)}updateHistoryState($){this._historyState={...this._historyState,...$},this.emit("history-state-change",this._historyState)}updateCompareSelectionState($){this._compareSelectionState={...this._compareSelectionState,...$},this.emit("compare-selection-change",this._compareSelectionState)}loadGitignores(){let $=new Map,J=w9();J.add(".git");let K=C.join(this.repoPath,".gitignore");if(p.existsSync(K))J.add(p.readFileSync(K,"utf-8"));let Z=C.join(this.repoPath,".git","info","exclude");if(p.existsSync(Z))J.add(p.readFileSync(Z,"utf-8"));$.set("",J);try{let X=l$("git",["ls-files","-z","--cached","--others","**/.gitignore"],{cwd:this.repoPath,encoding:"utf-8"});for(let Q of X.split("\x00")){if(!Q||Q===".gitignore")continue;if(!Q.endsWith(".gitignore"))continue;let z=C.dirname(Q),q=C.join(this.repoPath,Q);try{let Y=p.readFileSync(q,"utf-8"),U=w9();U.add(Y),$.set(z,U)}catch{}}}catch{}return $}startWatching(){let $=C.join(this.repoPath,".git");if(!p.existsSync($))return;let J=C.join($,"index"),K=C.join($,"HEAD"),Z=C.join($,"refs"),X=C.join(this.repoPath,".gitignore");this.gitWatcher=x9([J,K,Z,X],{persistent:!0,ignoreInitial:!0,usePolling:!0,interval:100}),this.ignorers=this.loadGitignores(),this.workingDirWatcher=x9(this.repoPath,{persistent:!0,ignoreInitial:!0,ignored:(z)=>{let q=C.relative(this.repoPath,z);if(!q)return!1;let Y=q.split("/");for(let U=0;U<Y.length;U++){let V=U===0?"":Y.slice(0,U).join("/"),B=this.ignorers.get(V);if(B){let G=U===0?q:Y.slice(U).join("/");if(B.ignores(G))return!0}}return!1},awaitWriteFinish:{stabilityThreshold:100,pollInterval:50}});let Q=()=>this.scheduleRefresh();this.gitWatcher.on("change",(z)=>{if(z===X)this.ignorers=this.loadGitignores();Q()}),this.gitWatcher.on("add",Q),this.gitWatcher.on("unlink",Q),this.gitWatcher.on("error",(z)=>{let q=z instanceof Error?z.message:String(z);this.emit("error",`Git watcher error: ${q}`)}),this.workingDirWatcher.on("change",Q),this.workingDirWatcher.on("add",Q),this.workingDirWatcher.on("unlink",Q),this.workingDirWatcher.on("error",(z)=>{let q=z instanceof Error?z.message:String(z);this.emit("error",`Working dir watcher error: ${q}`)})}dispose(){if(this.diffDebounceTimer)clearTimeout(this.diffDebounceTimer);this.gitWatcher?.close(),this.workingDirWatcher?.close(),_9(this.repoPath)}scheduleRefresh(){this.queue.scheduleRefresh(async()=>{if(await this.doRefresh(),this._historyState.commits.length>0)await this.doLoadHistory();if(this._compareState.compareBaseBranch)await this.doRefreshCompareDiff(!1)})}scheduleStatusRefresh(){this.queue.scheduleRefresh(async()=>{let $=await J8(this.repoPath);if(!$.isRepo){this.updateState({status:$,diff:null,isLoading:!1,error:"Not a git repository"});return}this.updateState({status:$,isLoading:!1})})}async refresh(){await this.queue.enqueue(()=>this.doRefresh())}async doRefresh(){this.updateState({isLoading:!0,error:null});try{let $=await J8(this.repoPath);if(!$.isRepo){this.updateState({status:$,diff:null,isLoading:!1,error:"Not a git repository"});return}this.updateState({status:$});let J=await J7(this.repoPath,void 0,!1),K,Z=this._state.selectedFile;if(Z){let X=$.files.find((Q)=>Q.path===Z.path&&Q.staged===Z.staged);if(X)if(X.status==="untracked")K=await K8(this.repoPath,X.path);else K=await J7(this.repoPath,X.path,X.staged);else K=J,this.updateState({selectedFile:null})}else K=J;this.updateState({diff:K,isLoading:!1})}catch($){this.updateState({isLoading:!1,error:$ instanceof Error?$.message:"Unknown error"})}}selectFile($){if(this.updateState({selectedFile:$}),!this._state.status?.isRepo)return;if(this.diffDebounceTimer)clearTimeout(this.diffDebounceTimer),this.diffDebounceTimer=setTimeout(()=>{this.diffDebounceTimer=null,this.fetchDiffForSelection()},20);else this.fetchDiffForSelection(),this.diffDebounceTimer=setTimeout(()=>{this.diffDebounceTimer=null},20)}fetchDiffForSelection(){let $=this._state.selectedFile;this.queue.enqueue(async()=>{if($!==this._state.selectedFile)return;if($){let J;if($.status==="untracked")J=await K8(this.repoPath,$.path);else J=await J7(this.repoPath,$.path,$.staged);if($===this._state.selectedFile)this.updateState({diff:J})}else{let J=await I9(this.repoPath);if(this._state.selectedFile===null)this.updateState({diff:J})}}).catch((J)=>{this.updateState({error:`Failed to load diff: ${J instanceof Error?J.message:String(J)}`})})}async stage($){try{await this.queue.enqueueMutation(()=>v9(this.repoPath,$.path)),this.scheduleStatusRefresh()}catch(J){await this.refresh(),this.updateState({error:`Failed to stage ${$.path}: ${J instanceof Error?J.message:String(J)}`})}}async unstage($){try{await this.queue.enqueueMutation(()=>A9(this.repoPath,$.path)),this.scheduleStatusRefresh()}catch(J){await this.refresh(),this.updateState({error:`Failed to unstage ${$.path}: ${J instanceof Error?J.message:String(J)}`})}}async discard($){if($.staged||$.status==="untracked")return;try{await this.queue.enqueueMutation(()=>W9(this.repoPath,$.path)),await this.refresh()}catch(J){this.updateState({error:`Failed to discard ${$.path}: ${J instanceof Error?J.message:String(J)}`})}}async stageAll(){try{await this.queue.enqueueMutation(()=>T9(this.repoPath)),await this.refresh()}catch($){this.updateState({error:`Failed to stage all: ${$ instanceof Error?$.message:String($)}`})}}async unstageAll(){try{await this.queue.enqueueMutation(()=>N9(this.repoPath)),await this.refresh()}catch($){this.updateState({error:`Failed to unstage all: ${$ instanceof Error?$.message:String($)}`})}}async commit($,J=!1){try{await this.queue.enqueue(()=>F9(this.repoPath,$,J)),await this.refresh()}catch(K){this.updateState({error:`Failed to commit: ${K instanceof Error?K.message:String(K)}`})}}async getHeadCommitMessage(){return this.queue.enqueue(()=>E9(this.repoPath))}async refreshCompareDiff($=!1){this.updateCompareState({compareLoading:!0,compareError:null});try{await this.queue.enqueue(()=>this.doRefreshCompareDiff($))}catch(J){this.updateCompareState({compareLoading:!1,compareError:`Failed to load compare diff: ${J instanceof Error?J.message:String(J)}`})}}async doRefreshCompareDiff($){let J=this._compareState.compareBaseBranch;if(!J)J=S9(this.repoPath)??await R9(this.repoPath),this.updateCompareState({compareBaseBranch:J});if(J){let K=$?await y9(this.repoPath,J):await X8(this.repoPath,J);this.updateCompareState({compareDiff:K,compareLoading:!1})}else this.updateCompareState({compareDiff:null,compareLoading:!1,compareError:"No base branch found"})}async getCandidateBaseBranches(){return Z8(this.repoPath)}async setCompareBaseBranch($,J=!1){this.updateCompareState({compareBaseBranch:$}),b9(this.repoPath,$),await this.refreshCompareDiff(J)}async loadHistory($=100){this.updateHistoryState({isLoading:!0});try{await this.queue.enqueue(()=>this.doLoadHistory($))}catch(J){this.updateHistoryState({isLoading:!1}),this.updateState({error:`Failed to load history: ${J instanceof Error?J.message:String(J)}`})}}async doLoadHistory($=100){let J=await L9(this.repoPath,$);this.updateHistoryState({commits:J,isLoading:!1})}async selectHistoryCommit($){if(this.updateHistoryState({selectedCommit:$,commitDiff:null}),!$)return;try{await this.queue.enqueue(async()=>{let J=await Q8(this.repoPath,$.hash);this.updateHistoryState({commitDiff:J})})}catch(J){this.updateState({error:`Failed to load commit diff: ${J instanceof Error?J.message:String(J)}`})}}async selectCompareCommit($){let J=this._compareState.compareDiff;if(!J||$<0||$>=J.commits.length){this.updateCompareSelectionState({type:null,index:0,diff:null});return}let K=J.commits[$];this.updateCompareSelectionState({type:"commit",index:$,diff:null});try{await this.queue.enqueue(async()=>{let Z=await Q8(this.repoPath,K.hash);this.updateCompareSelectionState({diff:Z})})}catch(Z){this.updateState({error:`Failed to load commit diff: ${Z instanceof Error?Z.message:String(Z)}`})}}selectCompareFile($){let J=this._compareState.compareDiff;if(!J||$<0||$>=J.files.length){this.updateCompareSelectionState({type:null,index:0,diff:null});return}let K=J.files[$];this.updateCompareSelectionState({type:"file",index:$,diff:K.diff})}}var H7=new Map;function p9($){let J=H7.get($);if(!J)J=new u9($),H7.set($,J);return J}function z8($){let J=H7.get($);if(J)J.dispose(),H7.delete($)}class q8{screen;layout;uiState;gitManager=null;followMode=null;explorerManager=null;config;commandServer;repoPath;currentTheme;commitFlowState;commitTextarea=null;activeModal=null;bottomPaneTotalRows=0;pendingSelectionAnchor=null;constructor($){this.config=$.config,this.commandServer=$.commandServer??null,this.repoPath=$.initialPath??process.cwd(),this.currentTheme=$.config.theme,this.uiState=new e7({splitRatio:$.config.splitRatio??0.4}),this.screen=g9.screen({smartCSR:!0,fullUnicode:!0,title:"diffstalker",mouse:!0,terminal:"xterm-256color"});let J=this.screen;if(J.tput)J.tput.colors=256;if(J.program?.tput)J.program.tput.colors=256;if(this.layout=new v7(this.screen,this.uiState.state.splitRatio),this.screen.on("resize",()=>{setImmediate(()=>this.render())}),this.commitFlowState=new t7({getHeadMessage:()=>this.gitManager?.getHeadCommitMessage()??Promise.resolve(""),onCommit:async(K,Z)=>{await this.gitManager?.commit(K,Z)},onSuccess:()=>{this.uiState.setTab("diff"),this.render()}}),this.commitTextarea=g9.textarea({parent:this.layout.bottomPane,top:3,left:1,width:"100%-4",height:1,inputOnFocus:!0,hidden:!0,style:{fg:"white",bg:"default"}}),this.commitTextarea.on("submit",()=>{this.commitFlowState.submit()}),this.commitTextarea.on("keypress",()=>{setImmediate(()=>{let K=this.commitTextarea?.getValue()??"";this.commitFlowState.setMessage(K)})}),this.setupKeyboardHandlers(),this.setupMouseEventHandlers(),this.setupStateListeners(),this.config.watcherEnabled)this.followMode=new k7(this.config.targetFile,()=>this.repoPath,{onRepoChange:(K,Z)=>this.handleFollowRepoChange(K,Z),onFileNavigate:(K)=>this.handleFollowFileNavigate(K)}),this.followMode.start();if(this.commandServer)this.setupCommandHandler();this.initGitManager(),this.render()}setupKeyboardHandlers(){j8(this.screen,{exit:()=>this.exit(),navigateDown:()=>this.navigateDown(),navigateUp:()=>this.navigateUp(),stageSelected:()=>this.stageSelected(),unstageSelected:()=>this.unstageSelected(),stageAll:()=>this.stageAll(),unstageAll:()=>this.unstageAll(),toggleSelected:()=>this.toggleSelected(),enterExplorerDirectory:()=>this.enterExplorerDirectory(),goExplorerUp:()=>this.goExplorerUp(),openFileFinder:()=>this.openFileFinder(),focusCommitInput:()=>this.focusCommitInput(),unfocusCommitInput:()=>this.unfocusCommitInput(),refresh:()=>this.refresh(),toggleMouseMode:()=>this.toggleMouseMode(),toggleFollow:()=>this.toggleFollow(),showDiscardConfirm:($)=>this.showDiscardConfirm($),render:()=>this.render()},{hasActiveModal:()=>this.activeModal!==null,getBottomTab:()=>this.uiState.state.bottomTab,getCurrentPane:()=>this.uiState.state.currentPane,isCommitInputFocused:()=>this.commitFlowState.state.inputFocused,getStatusFiles:()=>this.gitManager?.state.status?.files??[],getSelectedIndex:()=>this.uiState.state.selectedIndex,uiState:this.uiState,explorerManager:this.explorerManager,commitFlowState:this.commitFlowState,gitManager:this.gitManager,layout:this.layout})}setupMouseEventHandlers(){$9(this.layout,{selectHistoryCommitByIndex:($)=>this.selectHistoryCommitByIndex($),selectCompareItem:($)=>this.selectCompareItem($),selectFileByIndex:($)=>this.selectFileByIndex($),toggleFileByIndex:($)=>this.toggleFileByIndex($),toggleMouseMode:()=>this.toggleMouseMode(),toggleFollow:()=>this.toggleFollow(),render:()=>this.render()},{uiState:this.uiState,explorerManager:this.explorerManager,getStatusFiles:()=>this.gitManager?.state.status?.files??[],getHistoryCommitCount:()=>this.gitManager?.historyState.commits.length??0,getCompareCommits:()=>this.gitManager?.compareState?.compareDiff?.commits??[],getCompareFiles:()=>this.gitManager?.compareState?.compareDiff?.files??[],getBottomPaneTotalRows:()=>this.bottomPaneTotalRows,getScreenWidth:()=>this.screen.width||80})}async toggleFileByIndex($){let J=this.gitManager?.state.status?.files??[],K=b(J,$);if(K)if(this.pendingSelectionAnchor=n(J,this.uiState.state.selectedIndex),K.staged)await this.gitManager?.unstage(K);else await this.gitManager?.stage(K)}setupStateListeners(){this.uiState.on("change",()=>{this.render()}),this.uiState.on("tab-change",(J)=>{if(J==="history")this.gitManager?.loadHistory();else if(J==="compare")this.gitManager?.refreshCompareDiff(this.uiState.state.includeUncommitted);else if(J==="explorer"){if(!this.explorerManager?.state.displayRows.length)this.explorerManager?.loadDirectory("")}}),this.uiState.on("modal-change",(J)=>{if(this.activeModal)this.activeModal=null;if(J==="theme")this.activeModal=new r7(this.screen,this.currentTheme,(K)=>{this.currentTheme=K,h7({theme:K}),this.activeModal=null,this.uiState.closeModal(),this.render()},()=>{this.activeModal=null,this.uiState.closeModal()}),this.activeModal.focus();else if(J==="hotkeys")this.activeModal=new a7(this.screen,()=>{this.activeModal=null,this.uiState.closeModal()}),this.activeModal.focus();else if(J==="baseBranch")this.gitManager?.getCandidateBaseBranches().then((K)=>{let Z=this.gitManager?.compareState.compareBaseBranch??null;this.activeModal=new s7(this.screen,K,Z,(X)=>{this.activeModal=null,this.uiState.closeModal();let Q=this.uiState.state.includeUncommitted;this.gitManager?.setCompareBaseBranch(X,Q)},()=>{this.activeModal=null,this.uiState.closeModal()}),this.activeModal.focus()})});let $=null;this.uiState.on("change",(J)=>{if($)clearTimeout($);$=setTimeout(()=>{if(J.splitRatio!==this.config.splitRatio)h7({splitRatio:J.splitRatio})},500)})}handleFollowRepoChange($,J){let K=this.repoPath;this.repoPath=$,this.initGitManager(K),this.resetRepoSpecificState(),this.loadCurrentTabData(),this.render()}handleFollowFileNavigate($){this.navigateToFile($),this.render()}initGitManager($){if(this.gitManager)this.gitManager.removeAllListeners(),z8($??this.repoPath);this.gitManager=p9(this.repoPath),this.gitManager.on("state-change",()=>{let J=this.gitManager?.state.status?.files??[];if(this.pendingSelectionAnchor){let K=this.pendingSelectionAnchor;this.pendingSelectionAnchor=null;let Z=Y8(J,K.category,K.categoryIndex);this.uiState.setSelectedIndex(Z),this.selectFileByIndex(Z)}else if(J.length>0){let K=J.length-1;if(this.uiState.state.selectedIndex>K)this.uiState.setSelectedIndex(K)}this.updateExplorerGitStatus(),this.render()}),this.gitManager.on("history-state-change",(J)=>{if(J.commits.length>0&&!J.selectedCommit){let K=this.uiState.state;if(K.bottomTab==="history")this.selectHistoryCommitByIndex(K.historySelectedIndex)}this.render()}),this.gitManager.on("compare-state-change",()=>{this.render()}),this.gitManager.on("compare-selection-change",()=>{this.render()}),this.gitManager.startWatching(),this.gitManager.refresh(),this.initExplorerManager()}initExplorerManager(){if(this.explorerManager)this.explorerManager.dispose();let $={hideHidden:!0,hideGitignored:!0,showOnlyChanges:!1};this.explorerManager=new o7(this.repoPath,$),this.explorerManager.on("state-change",()=>{this.render()}),this.explorerManager.loadDirectory(""),this.updateExplorerGitStatus()}updateExplorerGitStatus(){if(!this.explorerManager||!this.gitManager)return;let $=this.gitManager.state.status?.files??[],J={files:new Map,directories:new Set};for(let K of $){J.files.set(K.path,{status:K.status,staged:K.staged});let Z=K.path.split("/"),X="";for(let Q=0;Q<Z.length-1;Q++)X=X?`${X}/${Z[Q]}`:Z[Q],J.directories.add(X);J.directories.add("")}this.explorerManager.setGitStatus(J)}resetRepoSpecificState(){this.compareSelection=null,this.uiState.resetForNewRepo()}loadCurrentTabData(){let $=this.uiState.state.bottomTab;if($==="history")this.gitManager?.loadHistory();else if($==="compare")this.gitManager?.refreshCompareDiff(this.uiState.state.includeUncommitted)}setupCommandHandler(){if(!this.commandServer)return;let $={navigateUp:()=>this.navigateUp(),navigateDown:()=>this.navigateDown(),switchTab:(J)=>this.uiState.setTab(J),togglePane:()=>this.uiState.togglePane(),stage:async()=>this.stageSelected(),unstage:async()=>this.unstageSelected(),stageAll:async()=>this.stageAll(),unstageAll:async()=>this.unstageAll(),commit:async(J)=>this.commit(J),refresh:async()=>this.refresh(),getState:()=>this.getAppState(),quit:()=>this.exit()};this.commandServer.setHandler($),this.commandServer.notifyReady()}getAppState(){let $=this.uiState.state,J=this.gitManager?.state,K=this.gitManager?.historyState,Z=J?.status?.files??[],X=K?.commits??[];return{currentTab:$.bottomTab,currentPane:$.currentPane,selectedIndex:$.selectedIndex,totalFiles:Z.length,stagedCount:Z.filter((Q)=>Q.staged).length,files:Z.map((Q)=>({path:Q.path,status:Q.status,staged:Q.staged})),historySelectedIndex:$.historySelectedIndex,historyCommitCount:X.length,compareSelectedIndex:$.compareSelectedIndex,compareTotalItems:0,includeUncommitted:$.includeUncommitted,explorerPath:this.repoPath,explorerSelectedIndex:$.explorerSelectedIndex,explorerItemCount:0,wrapMode:$.wrapMode,mouseEnabled:$.mouseEnabled,autoTabEnabled:$.autoTabEnabled}}navigateUp(){let $=this.uiState.state;if($.bottomTab==="history"){if($.currentPane==="history")this.navigateHistoryUp();else if($.currentPane==="diff")this.uiState.setDiffScrollOffset(Math.max(0,$.diffScrollOffset-3));return}if($.bottomTab==="compare"){if($.currentPane==="compare")this.navigateCompareUp();else if($.currentPane==="diff")this.uiState.setDiffScrollOffset(Math.max(0,$.diffScrollOffset-3));return}if($.bottomTab==="explorer"){if($.currentPane==="explorer")this.navigateExplorerUp();else if($.currentPane==="diff")this.uiState.setExplorerFileScrollOffset(Math.max(0,$.explorerFileScrollOffset-3));return}if($.currentPane==="files"){let J=this.gitManager?.state.status?.files??[],K=Math.max(0,$.selectedIndex-1);this.uiState.setSelectedIndex(K),this.selectFileByIndex(K);let Z=T7(K,J);if(Z<$.fileListScrollOffset)this.uiState.setFileListScrollOffset(Z)}else if($.currentPane==="diff")this.uiState.setDiffScrollOffset(Math.max(0,$.diffScrollOffset-3))}navigateDown(){let $=this.uiState.state,J=this.gitManager?.state.status?.files??[];if($.bottomTab==="history"){if($.currentPane==="history")this.navigateHistoryDown();else if($.currentPane==="diff")this.uiState.setDiffScrollOffset($.diffScrollOffset+3);return}if($.bottomTab==="compare"){if($.currentPane==="compare")this.navigateCompareDown();else if($.currentPane==="diff")this.uiState.setDiffScrollOffset($.diffScrollOffset+3);return}if($.bottomTab==="explorer"){if($.currentPane==="explorer")this.navigateExplorerDown();else if($.currentPane==="diff")this.uiState.setExplorerFileScrollOffset($.explorerFileScrollOffset+3);return}if($.currentPane==="files"){let K=Math.min(J.length-1,$.selectedIndex+1);this.uiState.setSelectedIndex(K),this.selectFileByIndex(K);let Z=T7(K,J),X=$.fileListScrollOffset+this.layout.dimensions.topPaneHeight-1;if(Z>=X)this.uiState.setFileListScrollOffset($.fileListScrollOffset+(Z-X+1))}else if($.currentPane==="diff")this.uiState.setDiffScrollOffset($.diffScrollOffset+3)}navigateHistoryUp(){let $=this.uiState.state,J=Math.max(0,$.historySelectedIndex-1);if(J!==$.historySelectedIndex){if(this.uiState.setHistorySelectedIndex(J),J<$.historyScrollOffset)this.uiState.setHistoryScrollOffset(J);this.selectHistoryCommitByIndex(J)}}navigateHistoryDown(){let $=this.uiState.state,J=this.gitManager?.historyState.commits??[],K=Math.min(J.length-1,$.historySelectedIndex+1);if(K!==$.historySelectedIndex){this.uiState.setHistorySelectedIndex(K);let Z=$.historyScrollOffset+this.layout.dimensions.topPaneHeight-1;if(K>=Z)this.uiState.setHistoryScrollOffset($.historyScrollOffset+1);this.selectHistoryCommitByIndex(K)}}selectHistoryCommitByIndex($){let J=this.gitManager?.historyState.commits??[],K=O8(J,$);if(K)this.uiState.setDiffScrollOffset(0),this.gitManager?.selectHistoryCommit(K)}compareSelection=null;navigateCompareUp(){let $=this.gitManager?.compareState,J=$?.compareDiff?.commits??[],K=$?.compareDiff?.files??[];if(J.length===0&&K.length===0)return;let Z=R7(this.compareSelection,J,K,"up");if(Z&&(Z.type!==this.compareSelection?.type||Z.index!==this.compareSelection?.index)){this.selectCompareItem(Z);let X=this.uiState.state,Q=Q7(Z,J,K);if(Q<X.compareScrollOffset)this.uiState.setCompareScrollOffset(Q)}}navigateCompareDown(){let $=this.gitManager?.compareState,J=$?.compareDiff?.commits??[],K=$?.compareDiff?.files??[];if(J.length===0&&K.length===0)return;if(!this.compareSelection){if(J.length>0)this.selectCompareItem({type:"commit",index:0});else if(K.length>0)this.selectCompareItem({type:"file",index:0});return}let Z=R7(this.compareSelection,J,K,"down");if(Z&&(Z.type!==this.compareSelection?.type||Z.index!==this.compareSelection?.index)){this.selectCompareItem(Z);let X=this.uiState.state,Q=Q7(Z,J,K),z=X.compareScrollOffset+this.layout.dimensions.topPaneHeight-1;if(Q>=z)this.uiState.setCompareScrollOffset(X.compareScrollOffset+(Q-z+1))}}selectCompareItem($){if(this.compareSelection=$,this.uiState.setDiffScrollOffset(0),$.type==="commit")this.gitManager?.selectCompareCommit($.index);else this.gitManager?.selectCompareFile($.index)}navigateExplorerUp(){let $=this.uiState.state;if((this.explorerManager?.state.displayRows??[]).length===0)return;let K=this.explorerManager?.navigateUp($.explorerScrollOffset);if(K!==null&&K!==void 0)this.uiState.setExplorerScrollOffset(K);this.uiState.setExplorerSelectedIndex(this.explorerManager?.state.selectedIndex??0)}navigateExplorerDown(){let $=this.uiState.state;if((this.explorerManager?.state.displayRows??[]).length===0)return;let K=this.layout.dimensions.topPaneHeight,Z=this.explorerManager?.navigateDown($.explorerScrollOffset,K);if(Z!==null&&Z!==void 0)this.uiState.setExplorerScrollOffset(Z);this.uiState.setExplorerSelectedIndex(this.explorerManager?.state.selectedIndex??0)}async enterExplorerDirectory(){await this.explorerManager?.enterDirectory(),this.uiState.setExplorerFileScrollOffset(0),this.uiState.setExplorerSelectedIndex(this.explorerManager?.state.selectedIndex??0)}async goExplorerUp(){await this.explorerManager?.goUp(),this.uiState.setExplorerFileScrollOffset(0),this.uiState.setExplorerSelectedIndex(this.explorerManager?.state.selectedIndex??0)}selectFileByIndex($){let J=this.gitManager?.state.status?.files??[],K=b(J,$);if(K)this.uiState.setDiffScrollOffset(0),this.gitManager?.selectFile(K)}navigateToFile($){if(!$||!this.repoPath)return;let J=this.repoPath.endsWith("/")?this.repoPath:this.repoPath+"/";if(!$.startsWith(J))return;let K=$.slice(J.length);if(!K)return;let X=(this.gitManager?.state.status?.files??[]).findIndex((Q)=>Q.path===K);if(X>=0)this.uiState.setSelectedIndex(X),this.selectFileByIndex(X)}async stageSelected(){let $=this.gitManager?.state.status?.files??[],J=this.uiState.state.selectedIndex,K=b($,J);if(K&&!K.staged)this.pendingSelectionAnchor=n($,J),await this.gitManager?.stage(K)}async unstageSelected(){let $=this.gitManager?.state.status?.files??[],J=this.uiState.state.selectedIndex,K=b($,J);if(K?.staged)this.pendingSelectionAnchor=n($,J),await this.gitManager?.unstage(K)}async toggleSelected(){let $=this.gitManager?.state.status?.files??[],J=this.uiState.state.selectedIndex,K=b($,J);if(K)if(this.pendingSelectionAnchor=n($,J),K.staged)await this.gitManager?.unstage(K);else await this.gitManager?.stage(K)}async stageAll(){await this.gitManager?.stageAll()}async unstageAll(){await this.gitManager?.unstageAll()}showDiscardConfirm($){this.activeModal=new n7(this.screen,$.path,async()=>{this.activeModal=null,await this.gitManager?.discard($)},()=>{this.activeModal=null}),this.activeModal.focus()}async openFileFinder(){let $=await this.explorerManager?.getAllFilePaths()??[];if($.length===0)return;this.activeModal=new i7(this.screen,$,async(J)=>{if(this.activeModal=null,await this.explorerManager?.navigateToPath(J))this.uiState.setExplorerScrollOffset(0),this.uiState.setExplorerFileScrollOffset(0);this.render()},()=>{this.activeModal=null,this.render()}),this.activeModal.focus()}async commit($){await this.gitManager?.commit($)}async refresh(){await this.gitManager?.refresh()}toggleMouseMode(){let $=!this.uiState.state.mouseEnabled;this.uiState.toggleMouse();let J=this.screen.program;if($)J.enableMouse();else J.disableMouse()}toggleFollow(){if(!this.followMode)this.followMode=new k7(this.config.targetFile,()=>this.repoPath,{onRepoChange:($,J)=>this.handleFollowRepoChange($,J),onFileNavigate:($)=>this.handleFollowFileNavigate($)});this.followMode.toggle(),this.render()}focusCommitInput(){if(this.commitTextarea)this.commitTextarea.show(),this.commitTextarea.focus(),this.commitTextarea.setValue(this.commitFlowState.state.message),this.commitFlowState.setInputFocused(!0),this.render()}unfocusCommitInput(){if(this.commitTextarea){let $=this.commitTextarea.getValue()??"";this.commitFlowState.setMessage($),this.commitTextarea.hide(),this.commitFlowState.setInputFocused(!1),this.screen.focusPush(this.layout.bottomPane),this.render()}}render(){this.updateHeader(),this.updateTopPane(),this.updateBottomPane(),this.updateFooter(),this.screen.render()}updateHeader(){let $=this.gitManager?.state,J=this.screen.width||80,K=z9(this.repoPath,$?.status?.branch??null,$?.isLoading??!1,$?.error??null,J);this.layout.headerBox.setContent(K)}updateTopPane(){let $=this.uiState.state,J=this.screen.width||80,K=n8($,this.gitManager?.state.status?.files??[],this.gitManager?.historyState?.commits??[],this.gitManager?.compareState?.compareDiff??null,this.compareSelection,this.explorerManager?.state,J,this.layout.dimensions.topPaneHeight);this.layout.topPane.setContent(K)}updateBottomPane(){let $=this.uiState.state,J=this.screen.width||80,Z=(this.gitManager?.state.status?.files??[]).filter((z)=>z.staged).length;this.commitFlowState.setStagedCount(Z);let{content:X,totalRows:Q}=i8($,this.gitManager?.state.diff??null,this.gitManager?.historyState,this.gitManager?.compareSelectionState,this.explorerManager?.state?.selectedFile??null,this.commitFlowState.state,Z,this.currentTheme,J,this.layout.dimensions.bottomPaneHeight);if(this.bottomPaneTotalRows=Q,this.layout.bottomPane.setContent(X),this.commitTextarea)if($.bottomTab==="commit"&&this.commitFlowState.state.inputFocused)this.commitTextarea.show();else this.commitTextarea.hide()}updateFooter(){let $=this.uiState.state,J=this.screen.width||80,K=V9($.bottomTab,$.mouseEnabled,$.autoTabEnabled,$.wrapMode,this.followMode?.isEnabled??!1,this.explorerManager?.showOnlyChanges??!1,J);this.layout.footerBox.setContent(K)}exit(){if(this.gitManager)z8(this.repoPath);if(this.explorerManager)this.explorerManager.dispose();if(this.followMode)this.followMode.stop();if(this.commandServer)this.commandServer.stop();this.screen.destroy()}start(){return new Promise(($)=>{this.screen.on("destroy",()=>{$()})})}}import*as m9 from"net";import*as g from"fs";import{EventEmitter as r$}from"events";class V8 extends r${server=null;socketPath;handler=null;ready=!1;constructor($){super();this.socketPath=$}setHandler($){this.handler=$}notifyReady(){this.ready=!0,this.emit("ready")}isReady(){return this.ready&&this.handler!==null}async start(){if(g.existsSync(this.socketPath))g.unlinkSync(this.socketPath);return new Promise(($,J)=>{this.server=m9.createServer((K)=>{this.handleConnection(K)}),this.server.on("error",(K)=>{J(K)}),this.server.listen(this.socketPath,()=>{g.chmodSync(this.socketPath,384),$()})})}stop(){if(this.server)this.server.close(),this.server=null;if(g.existsSync(this.socketPath))g.unlinkSync(this.socketPath)}handleConnection($){let J="";$.on("data",async(K)=>{J+=K.toString();let Z=J.split(`
|
|
56
|
+
`);J=Z.pop()||"";for(let X of Z)if(X.trim()){let Q=await this.processCommand(X);$.write(JSON.stringify(Q)+`
|
|
57
|
+
`)}}),$.on("error",(K)=>{this.emit("error",K)})}async processCommand($){try{let J=JSON.parse($);if(J.action==="ping")return{success:!0,ready:this.isReady()};if(!this.handler)return{success:!1,error:"No handler registered"};switch(J.action){case"navigateUp":return this.handler.navigateUp(),{success:!0};case"navigateDown":return this.handler.navigateDown(),{success:!0};case"switchTab":return this.handler.switchTab(J.tab),{success:!0};case"togglePane":return this.handler.togglePane(),{success:!0};case"stage":return await this.handler.stage(),{success:!0};case"unstage":return await this.handler.unstage(),{success:!0};case"stageAll":return await this.handler.stageAll(),{success:!0};case"unstageAll":return await this.handler.unstageAll(),{success:!0};case"commit":return await this.handler.commit(J.message),{success:!0};case"refresh":return await this.handler.refresh(),{success:!0};case"getState":return{success:!0,state:this.handler.getState()};case"quit":return this.handler.quit(),{success:!0};default:return{success:!1,error:`Unknown action: ${J.action}`}}}catch(J){return{success:!1,error:J instanceof Error?J.message:String(J)}}}}function c(){process.stdout.write("\x1B[?1006l"),process.stdout.write("\x1B[?1002l"),process.stdout.write("\x1B[?1000l"),process.stdout.write("\x1B[?1003l"),process.stdout.write("\x1B[?25h")}c();process.on("exit",c);process.on("SIGINT",()=>{c(),process.exit(0)});process.on("SIGTERM",()=>{c(),process.exit(0)});process.on("uncaughtException",($)=>{c(),console.error("Uncaught exception:",$),process.exit(1)});process.on("unhandledRejection",($)=>{c(),console.error("Unhandled rejection:",$),process.exit(1)});function a$($){let J={};for(let K=0;K<$.length;K++){let Z=$[K];if(Z==="--follow"||Z==="-f"){if(J.follow=!0,$[K+1]&&!$[K+1].startsWith("-"))J.followFile=$[++K]}else if(Z==="--once")J.once=!0;else if(Z==="--debug"||Z==="-d")J.debug=!0;else if(Z==="--socket"||Z==="-s")if($[K+1]&&!$[K+1].startsWith("-"))J.socket=$[++K];else console.error("Error: --socket requires a path argument"),process.exit(1);else if(Z==="--help"||Z==="-h")console.log(`
|
|
57
58
|
diffstalker - Terminal git diff/status viewer
|
|
58
59
|
|
|
59
60
|
Usage: diffstalker [options] [path]
|
|
@@ -94,4 +95,4 @@ Keyboard:
|
|
|
94
95
|
Mouse:
|
|
95
96
|
Click Select file / focus pane
|
|
96
97
|
Scroll Navigate files / scroll diff
|
|
97
|
-
`),process.exit(0);else if(!
|
|
98
|
+
`),process.exit(0);else if(!Z.startsWith("-"))J.initialPath=Z}return J}async function s$(){let $=a$(process.argv.slice(2)),J=J9();if($.follow){if(J.watcherEnabled=!0,$.followFile)J.targetFile=$.followFile}if($.debug)J.debug=!0;let K=null;if($.socket){K=new V8($.socket);try{await K.start()}catch(X){console.error("Failed to start command server:",X),process.exit(1)}}if(await new q8({config:J,initialPath:$.initialPath,commandServer:K}).start(),K)K.stop();process.exit(0)}s$().catch(($)=>{console.error("Fatal error:",$),c(),process.exit(1)});
|