Skip to content

Commit f79006b

Browse files
Add preserveCWD option
1 parent cc550b7 commit f79006b

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

app/config/config-default.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ module.exports = {
163163
// set to true to enable screen reading apps (like NVDA) to read the contents of the terminal
164164
screenReaderMode: false,
165165

166+
// set to true to preserve working directory when creating splits or tabs
167+
preserveCWD: true,
168+
166169
// for advanced config flags please refer to https://hyper.is/#cfg
167170
},
168171

app/ui/window.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {setRendererType, unsetRendererType} from '../utils/renderer-utils';
1616
import {decorateSessionOptions, decorateSessionClass} from '../plugins';
1717
import {enable as remoteEnable} from '@electron/remote/main';
1818
import {configOptions} from '../../lib/config';
19+
import {getWorkingDirectoryFromPID} from 'native-process-working-directory';
1920

2021
export function newWindow(
2122
options_: BrowserWindowConstructorOptions,
@@ -129,10 +130,21 @@ export function newWindow(
129130
if (extraOptions[key] !== undefined) extraOptionsFiltered[key] = extraOptions[key];
130131
});
131132

133+
let cwd = '';
134+
if (cfg.preserveCWD === undefined || cfg.preserveCWD) {
135+
const activePID = extraOptionsFiltered.activeUid && sessions.get(extraOptionsFiltered.activeUid)?.pty?.pid;
136+
try {
137+
cwd = activePID && getWorkingDirectoryFromPID(activePID);
138+
} catch (error) {
139+
console.error(error);
140+
}
141+
cwd = cwd && isAbsolute(cwd) ? cwd : '';
142+
}
143+
132144
// remove the rows and cols, the wrong value of them will break layout when init create
133145
const defaultOptions = Object.assign(
134146
{
135-
cwd: workingDirectory,
147+
cwd: cwd || workingDirectory,
136148
splitDirection: undefined,
137149
shell: cfg.shell,
138150
shellArgs: cfg.shellArgs && Array.from(cfg.shellArgs)

lib/actions/term-groups.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ export function requestTermGroup(activeUid: string) {
4545
dispatch({
4646
type: TERM_GROUP_REQUEST,
4747
effect: () => {
48-
const {ui} = getState();
48+
const {ui, sessions} = getState();
4949
const {cwd} = ui;
5050
rpc.emit('new', {
5151
isNewGroup: true,
5252
cwd,
53-
activeUid
53+
activeUid: activeUid ? activeUid : sessions.activeUid
5454
});
5555
}
5656
});

lib/config.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export type configOptions = {
5050
cmdIsMeta: boolean;
5151
};
5252
padding: string;
53+
preserveCWD: boolean;
5354
quickEdit: boolean;
5455
screenReaderMode: boolean;
5556
scrollback: number;

0 commit comments

Comments
 (0)