Skip to content

Commit f489a5b

Browse files
Add CWD preservation
1 parent 1457bf1 commit f489a5b

File tree

12 files changed

+51
-31
lines changed

12 files changed

+51
-31
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/menus/menus/window.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default (
55
execCommand: (command: string, focusedWindow?: BrowserWindow) => void
66
): MenuItemConstructorOptions => {
77
// Generating tab:jump array
8-
const tabJump = [];
8+
const tabJump: MenuItemConstructorOptions[] = [];
99
for (let i = 1; i <= 9; i++) {
1010
// 9 is a special number because it means 'last'
1111
const label = i === 9 ? 'Last' : `${i}`;

app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"lodash": "4.17.21",
2424
"mkdirp": "1.0.4",
2525
"ms": "2.1.3",
26+
"native-process-working-directory": "^1.0.2",
2627
"node-pty": "0.10.1",
2728
"os-locale": "5.0.0",
2829
"parse-url": "5.0.7",

app/plugins.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ function toDependencies(plugins_: {plugins: string[]}) {
234234

235235
if (match) {
236236
const index = match.index + 1;
237-
const pieces = [];
237+
const pieces: string[] = [];
238238

239239
pieces[0] = plugin.substring(0, index);
240240
pieces[1] = plugin.substring(index + 1, plugin.length);

app/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"extends": "../tsconfig.base.json",
33
"compilerOptions": {
44
"declarationDir": "../dist/tmp/appdts/",
5-
"outDir": "../target/"
5+
"outDir": "../target/",
6+
"noImplicitAny": false
67
},
78
"include": [
89
"./**/*",

app/ui/window.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ import {execCommand} from '../commands';
1515
import {setRendererType, unsetRendererType} from '../utils/renderer-utils';
1616
import {decorateSessionOptions, decorateSessionClass} from '../plugins';
1717
import {enable as remoteEnable} from '@electron/remote/main';
18+
import {configOptions} from '../../lib/config';
19+
import {getWorkingDirectoryFromPID} from 'native-process-working-directory';
1820

1921
export function newWindow(
2022
options_: BrowserWindowConstructorOptions,
21-
cfg: any,
23+
cfg: configOptions,
2224
fn?: (win: BrowserWindow) => void
2325
): BrowserWindow {
2426
const classOpts = Object.assign({uid: uuidv4()});
@@ -128,10 +130,21 @@ export function newWindow(
128130
if (extraOptions[key] !== undefined) extraOptionsFiltered[key] = extraOptions[key];
129131
});
130132

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+
131144
// remove the rows and cols, the wrong value of them will break layout when init create
132145
const defaultOptions = Object.assign(
133146
{
134-
cwd: workingDirectory,
147+
cwd: cwd || workingDirectory,
135148
splitDirection: undefined,
136149
shell: cfg.shell,
137150
shellArgs: cfg.shellArgs && Array.from(cfg.shellArgs)

app/yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,13 +441,25 @@ nan@^2.14.0:
441441
resolved "https://registry.npmjs.org/nan/-/nan-2.14.2.tgz#f5376400695168f4cc694ac9393d0c9585eeea19"
442442
integrity sha512-M2ufzIiINKCuDfBSAUr1vWQ+vuVcA9kqx8JJUsbQi6yf1uGRyb7HfpdfUr5qLXf3B/t8dPvcjhKMmlfnP47EzQ==
443443

444+
native-process-working-directory@^1.0.2:
445+
version "1.0.2"
446+
resolved "https://registry.npmjs.org/native-process-working-directory/-/native-process-working-directory-1.0.2.tgz#7843e2fa1490f53cf8d2c7d1913de8b275e8b89a"
447+
integrity sha512-3a67QQV8r3YMUTSOgvtMOCjPDgCpb/8xjv93L8Cqb8bv3hOKsWis4/+8HCu3bgj8ADQV75SCYFSsAGM5G0cXmQ==
448+
dependencies:
449+
node-addon-api "^3.1.0"
450+
444451
445452
version "1.0.0"
446453
resolved "https://registry.npmjs.org/native-reg/-/native-reg-1.0.0.tgz#77f9acbf59eda02680c00b0b1b9d1e0078b7820d"
447454
integrity sha512-MxukmqY7jOeiS9+b4TAlfG9cvaQ03oLET35nUGYGHDRcLx0NFk7eeoWqX4wAXaFiMW50ZiFalOA6W8q3fprcsw==
448455
dependencies:
449456
node-gyp-build "4"
450457

458+
node-addon-api@^3.1.0:
459+
version "3.2.1"
460+
resolved "https://registry.npmjs.org/node-addon-api/-/node-addon-api-3.2.1.tgz#81325e0a2117789c0128dab65e7e38f07ceba161"
461+
integrity sha512-mmcei9JghVNDYydghQmeDX8KoAm0FAiYyIcUt/N4nhyAipB17pllZQDOJD2fotxABnt4Mdz+dKTO7eftLg4d0A==
462+
451463
node-gyp-build@4:
452464
version "4.3.0"
453465
resolved "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.3.0.tgz#9f256b03e5826150be39c764bf51e993946d71a3"

lib/actions/sessions.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,12 @@ export function addSession({uid, shell, pid, cols, rows, splitDirection, activeU
3636
};
3737
}
3838

39-
export function requestSession() {
40-
return (dispatch: HyperDispatch, getState: () => HyperState) => {
41-
dispatch({
42-
type: SESSION_REQUEST,
43-
effect: () => {
44-
const {ui} = getState();
45-
// the cols and rows from preview session maybe not accurate. so remove.
46-
const {/*cols, rows,*/ cwd} = ui;
47-
rpc.emit('new', {cwd});
48-
}
49-
});
39+
export function requestSession(cwd?: string): HyperActions {
40+
return {
41+
type: SESSION_REQUEST,
42+
effect: () => {
43+
rpc.emit('new', {cwd});
44+
}
5045
};
5146
}
5247

lib/actions/term-groups.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ import {getRootGroups} from '../selectors';
1212
import {setActiveSession, ptyExitSession, userExitSession} from './sessions';
1313
import {ITermState, ITermGroup, HyperState, HyperDispatch, HyperActions} from '../hyper';
1414

15-
function requestSplit(direction: 'VERTICAL' | 'HORIZONTAL') {
15+
function requestSplit(direction: 'VERTICAL' | 'HORIZONTAL', cwd?: string) {
1616
return (activeUid: string) =>
1717
(dispatch: HyperDispatch, getState: () => HyperState): void => {
1818
dispatch({
1919
type: SESSION_REQUEST,
2020
effect: () => {
21-
const {ui, sessions} = getState();
21+
const {sessions} = getState();
2222
rpc.emit('new', {
2323
splitDirection: direction,
24-
cwd: ui.cwd,
24+
...(cwd && {cwd}),
2525
activeUid: activeUid ? activeUid : sessions.activeUid
2626
});
2727
}
@@ -40,17 +40,16 @@ export function resizeTermGroup(uid: string, sizes: number[]): HyperActions {
4040
};
4141
}
4242

43-
export function requestTermGroup(activeUid: string) {
43+
export function requestTermGroup(activeUid: string, cwd?: string) {
4444
return (dispatch: HyperDispatch, getState: () => HyperState) => {
4545
dispatch({
4646
type: TERM_GROUP_REQUEST,
4747
effect: () => {
48-
const {ui} = getState();
49-
const {cwd} = ui;
48+
const {sessions} = getState();
5049
rpc.emit('new', {
5150
isNewGroup: true,
52-
cwd,
53-
activeUid
51+
...(cwd && {cwd}),
52+
activeUid: activeUid ? activeUid : sessions.activeUid
5453
});
5554
}
5655
});

lib/config.d.ts

Lines changed: 2 additions & 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;
@@ -65,6 +66,7 @@ export type configOptions = {
6566
webGLRenderer: boolean;
6667
webLinksActivationKey: 'ctrl' | 'alt' | 'meta' | 'shift';
6768
windowSize: [number, number];
69+
workingDirectory: string;
6870
};
6971

7072
export type rawConfig = {

0 commit comments

Comments
 (0)