From f79006bae1e97532fd180a43c6146c1982ebff83 Mon Sep 17 00:00:00 2001 From: Labhansh Agrawal Date: Sat, 8 Jan 2022 16:17:38 +0530 Subject: [PATCH] Add preserveCWD option --- app/config/config-default.js | 3 +++ app/ui/window.ts | 14 +++++++++++++- lib/actions/term-groups.ts | 4 ++-- lib/config.d.ts | 1 + 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/app/config/config-default.js b/app/config/config-default.js index 53b81b99..0cd88464 100644 --- a/app/config/config-default.js +++ b/app/config/config-default.js @@ -163,6 +163,9 @@ module.exports = { // set to true to enable screen reading apps (like NVDA) to read the contents of the terminal screenReaderMode: false, + // set to true to preserve working directory when creating splits or tabs + preserveCWD: true, + // for advanced config flags please refer to https://hyper.is/#cfg }, diff --git a/app/ui/window.ts b/app/ui/window.ts index b98c4d03..c8b61949 100644 --- a/app/ui/window.ts +++ b/app/ui/window.ts @@ -16,6 +16,7 @@ import {setRendererType, unsetRendererType} from '../utils/renderer-utils'; import {decorateSessionOptions, decorateSessionClass} from '../plugins'; import {enable as remoteEnable} from '@electron/remote/main'; import {configOptions} from '../../lib/config'; +import {getWorkingDirectoryFromPID} from 'native-process-working-directory'; export function newWindow( options_: BrowserWindowConstructorOptions, @@ -129,10 +130,21 @@ export function newWindow( if (extraOptions[key] !== undefined) extraOptionsFiltered[key] = extraOptions[key]; }); + let cwd = ''; + if (cfg.preserveCWD === undefined || cfg.preserveCWD) { + const activePID = extraOptionsFiltered.activeUid && sessions.get(extraOptionsFiltered.activeUid)?.pty?.pid; + try { + cwd = activePID && getWorkingDirectoryFromPID(activePID); + } catch (error) { + console.error(error); + } + cwd = cwd && isAbsolute(cwd) ? cwd : ''; + } + // remove the rows and cols, the wrong value of them will break layout when init create const defaultOptions = Object.assign( { - cwd: workingDirectory, + cwd: cwd || workingDirectory, splitDirection: undefined, shell: cfg.shell, shellArgs: cfg.shellArgs && Array.from(cfg.shellArgs) diff --git a/lib/actions/term-groups.ts b/lib/actions/term-groups.ts index 4696bb6d..e8b1ca67 100644 --- a/lib/actions/term-groups.ts +++ b/lib/actions/term-groups.ts @@ -45,12 +45,12 @@ export function requestTermGroup(activeUid: string) { dispatch({ type: TERM_GROUP_REQUEST, effect: () => { - const {ui} = getState(); + const {ui, sessions} = getState(); const {cwd} = ui; rpc.emit('new', { isNewGroup: true, cwd, - activeUid + activeUid: activeUid ? activeUid : sessions.activeUid }); } }); diff --git a/lib/config.d.ts b/lib/config.d.ts index 2cbc9dcc..7d12c193 100644 --- a/lib/config.d.ts +++ b/lib/config.d.ts @@ -50,6 +50,7 @@ export type configOptions = { cmdIsMeta: boolean; }; padding: string; + preserveCWD: boolean; quickEdit: boolean; screenReaderMode: boolean; scrollback: number;