@@ -9,6 +9,8 @@ const execFileAsync = promisify(execFile);
99const SESSION_FMT = "#{session_name}\t#{session_attached}\t#{session_windows}" ;
1010const WINDOW_FMT = "#{window_index}\t#{window_name}\t#{window_active}\t#{window_panes}" ;
1111const PANE_FMT = "#{pane_index}\t#{pane_id}\t#{pane_current_command}\t#{pane_active}\t#{pane_width}x#{pane_height}" ;
12+ const SESSION_DEFAULT_DIRECTORY_OPTION = "@tmux_mobile_session_default_directory" ;
13+ const WINDOW_DEFAULT_DIRECTORY_OPTION = "@tmux_mobile_window_default_directory" ;
1214
1315interface TmuxCliExecutorOptions {
1416 socketName ?: string ;
@@ -77,6 +79,23 @@ export class TmuxCliExecutor implements TmuxGateway {
7779 }
7880 }
7981
82+ private async showOption ( args : string [ ] ) : Promise < string | null > {
83+ try {
84+ const value = await this . runTmux ( [ "show-options" , "-qv" , ...args ] ) ;
85+ return value === "" ? null : value ;
86+ } catch {
87+ return null ;
88+ }
89+ }
90+
91+ private async getSessionDefaultDirectory ( session : string ) : Promise < string | null > {
92+ return this . showOption ( [ "-t" , session , SESSION_DEFAULT_DIRECTORY_OPTION ] ) ;
93+ }
94+
95+ private async getWindowDefaultDirectory ( windowId : string ) : Promise < string | null > {
96+ return this . showOption ( [ "-w" , "-t" , windowId , WINDOW_DEFAULT_DIRECTORY_OPTION ] ) ;
97+ }
98+
8099 public async listSessions ( ) {
81100 const output = await this . runTmuxMaybeNoServer ( [ "list-sessions" , "-F" , SESSION_FMT ] ) ;
82101 if ( ! output ) {
@@ -120,7 +139,12 @@ export class TmuxCliExecutor implements TmuxGateway {
120139 }
121140
122141 public async newWindow ( session : string ) : Promise < void > {
123- await this . runTmux ( [ "new-window" , "-t" , session ] ) ;
142+ const defaultDirectory = await this . getSessionDefaultDirectory ( session ) ;
143+ const args = [ "new-window" , "-t" , session ] ;
144+ if ( defaultDirectory ) {
145+ args . push ( "-c" , defaultDirectory ) ;
146+ }
147+ await this . runTmux ( args ) ;
124148 }
125149
126150 public async killWindow ( session : string , windowIndex : number ) : Promise < void > {
@@ -132,7 +156,20 @@ export class TmuxCliExecutor implements TmuxGateway {
132156 }
133157
134158 public async splitWindow ( paneId : string , orientation : "h" | "v" ) : Promise < void > {
135- await this . runTmux ( [ "split-window" , `-${ orientation } ` , "-t" , paneId ] ) ;
159+ const [ windowId , sessionName ] = await Promise . all ( [
160+ this . runTmux ( [ "display-message" , "-p" , "-t" , paneId , "#{window_id}" ] ) ,
161+ this . runTmux ( [ "display-message" , "-p" , "-t" , paneId , "#{session_name}" ] )
162+ ] ) ;
163+ const windowDirectory = await this . getWindowDefaultDirectory ( windowId ) ;
164+ const sessionDirectory = await this . getSessionDefaultDirectory ( sessionName ) ;
165+ const defaultDirectory = windowDirectory ?? sessionDirectory ;
166+
167+ const args = [ "split-window" , `-${ orientation } ` ] ;
168+ if ( defaultDirectory ) {
169+ args . push ( "-c" , defaultDirectory ) ;
170+ }
171+ args . push ( "-t" , paneId ) ;
172+ await this . runTmux ( args ) ;
136173 }
137174
138175 public async killPane ( paneId : string ) : Promise < void > {
@@ -170,4 +207,56 @@ export class TmuxCliExecutor implements TmuxGateway {
170207 public async swapWindow ( session : string , srcIndex : number , dstIndex : number ) : Promise < void > {
171208 await this . runTmux ( [ "swap-window" , "-s" , `${ session } :${ srcIndex } ` , "-t" , `${ session } :${ dstIndex } ` ] ) ;
172209 }
210+
211+ public async setSessionDefaultDirectory (
212+ session : string ,
213+ directory : string | null
214+ ) : Promise < void > {
215+ if ( ! directory ) {
216+ await this . runTmux ( [
217+ "set-option" ,
218+ "-u" ,
219+ "-t" ,
220+ session ,
221+ SESSION_DEFAULT_DIRECTORY_OPTION
222+ ] ) ;
223+ return ;
224+ }
225+
226+ await this . runTmux ( [
227+ "set-option" ,
228+ "-t" ,
229+ session ,
230+ SESSION_DEFAULT_DIRECTORY_OPTION ,
231+ directory
232+ ] ) ;
233+ }
234+
235+ public async setWindowDefaultDirectory (
236+ session : string ,
237+ windowIndex : number ,
238+ directory : string | null
239+ ) : Promise < void > {
240+ const target = `${ session } :${ windowIndex } ` ;
241+ if ( ! directory ) {
242+ await this . runTmux ( [
243+ "set-option" ,
244+ "-u" ,
245+ "-w" ,
246+ "-t" ,
247+ target ,
248+ WINDOW_DEFAULT_DIRECTORY_OPTION
249+ ] ) ;
250+ return ;
251+ }
252+
253+ await this . runTmux ( [
254+ "set-option" ,
255+ "-w" ,
256+ "-t" ,
257+ target ,
258+ WINDOW_DEFAULT_DIRECTORY_OPTION ,
259+ directory
260+ ] ) ;
261+ }
173262}
0 commit comments