From af96a62b2ebec64ee6bbf3577a3899fd75dae60e Mon Sep 17 00:00:00 2001 From: Labhansh Agrawal Date: Sat, 8 Jan 2022 15:57:12 +0530 Subject: [PATCH 1/2] Add native-process-working-directory module --- app/menus/menus/window.ts | 2 +- app/package.json | 1 + app/plugins.ts | 2 +- app/tsconfig.json | 3 ++- app/ui/window.ts | 3 ++- app/yarn.lock | 12 ++++++++++++ lib/config.d.ts | 1 + 7 files changed, 20 insertions(+), 4 deletions(-) diff --git a/app/menus/menus/window.ts b/app/menus/menus/window.ts index 56db6fe5266d..4f693bda0041 100644 --- a/app/menus/menus/window.ts +++ b/app/menus/menus/window.ts @@ -5,7 +5,7 @@ export default ( execCommand: (command: string, focusedWindow?: BrowserWindow) => void ): MenuItemConstructorOptions => { // Generating tab:jump array - const tabJump = []; + const tabJump: MenuItemConstructorOptions[] = []; for (let i = 1; i <= 9; i++) { // 9 is a special number because it means 'last' const label = i === 9 ? 'Last' : `${i}`; diff --git a/app/package.json b/app/package.json index bba70204aa10..cd47ffc02bc7 100644 --- a/app/package.json +++ b/app/package.json @@ -23,6 +23,7 @@ "lodash": "4.17.21", "mkdirp": "1.0.4", "ms": "2.1.3", + "native-process-working-directory": "^1.0.2", "node-pty": "0.10.1", "os-locale": "5.0.0", "parse-url": "5.0.7", diff --git a/app/plugins.ts b/app/plugins.ts index f0e418de7e63..8caa26413712 100644 --- a/app/plugins.ts +++ b/app/plugins.ts @@ -234,7 +234,7 @@ function toDependencies(plugins_: {plugins: string[]}) { if (match) { const index = match.index + 1; - const pieces = []; + const pieces: string[] = []; pieces[0] = plugin.substring(0, index); pieces[1] = plugin.substring(index + 1, plugin.length); diff --git a/app/tsconfig.json b/app/tsconfig.json index 467bd9207131..d18cc80a4f89 100644 --- a/app/tsconfig.json +++ b/app/tsconfig.json @@ -2,7 +2,8 @@ "extends": "../tsconfig.base.json", "compilerOptions": { "declarationDir": "../dist/tmp/appdts/", - "outDir": "../target/" + "outDir": "../target/", + "noImplicitAny": false }, "include": [ "./**/*", diff --git a/app/ui/window.ts b/app/ui/window.ts index 0580c65592c8..b98c4d03bb65 100644 --- a/app/ui/window.ts +++ b/app/ui/window.ts @@ -15,10 +15,11 @@ import {execCommand} from '../commands'; 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'; export function newWindow( options_: BrowserWindowConstructorOptions, - cfg: any, + cfg: configOptions, fn?: (win: BrowserWindow) => void ): BrowserWindow { const classOpts = Object.assign({uid: uuidv4()}); diff --git a/app/yarn.lock b/app/yarn.lock index db93259618bb..4e126769eed9 100644 --- a/app/yarn.lock +++ b/app/yarn.lock @@ -441,6 +441,13 @@ nan@^2.14.0: resolved "https://registry.npmjs.org/nan/-/nan-2.14.2.tgz#f5376400695168f4cc694ac9393d0c9585eeea19" integrity sha512-M2ufzIiINKCuDfBSAUr1vWQ+vuVcA9kqx8JJUsbQi6yf1uGRyb7HfpdfUr5qLXf3B/t8dPvcjhKMmlfnP47EzQ== +native-process-working-directory@^1.0.2: + version "1.0.2" + resolved "https://registry.npmjs.org/native-process-working-directory/-/native-process-working-directory-1.0.2.tgz#7843e2fa1490f53cf8d2c7d1913de8b275e8b89a" + integrity sha512-3a67QQV8r3YMUTSOgvtMOCjPDgCpb/8xjv93L8Cqb8bv3hOKsWis4/+8HCu3bgj8ADQV75SCYFSsAGM5G0cXmQ== + dependencies: + node-addon-api "^3.1.0" + native-reg@1.0.0: version "1.0.0" resolved "https://registry.npmjs.org/native-reg/-/native-reg-1.0.0.tgz#77f9acbf59eda02680c00b0b1b9d1e0078b7820d" @@ -448,6 +455,11 @@ native-reg@1.0.0: dependencies: node-gyp-build "4" +node-addon-api@^3.1.0: + version "3.2.1" + resolved "https://registry.npmjs.org/node-addon-api/-/node-addon-api-3.2.1.tgz#81325e0a2117789c0128dab65e7e38f07ceba161" + integrity sha512-mmcei9JghVNDYydghQmeDX8KoAm0FAiYyIcUt/N4nhyAipB17pllZQDOJD2fotxABnt4Mdz+dKTO7eftLg4d0A== + node-gyp-build@4: version "4.3.0" resolved "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.3.0.tgz#9f256b03e5826150be39c764bf51e993946d71a3" diff --git a/lib/config.d.ts b/lib/config.d.ts index ce0e6c133098..2cbc9dccc02a 100644 --- a/lib/config.d.ts +++ b/lib/config.d.ts @@ -65,6 +65,7 @@ export type configOptions = { webGLRenderer: boolean; webLinksActivationKey: 'ctrl' | 'alt' | 'meta' | 'shift'; windowSize: [number, number]; + workingDirectory: string; }; export type rawConfig = { From 8ed191425ba61cd84f869566b446a7257474bfca Mon Sep 17 00:00:00 2001 From: Labhansh Agrawal Date: Sat, 8 Jan 2022 16:17:38 +0530 Subject: [PATCH 2/2] 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 53b81b994feb..0cd88464118f 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 b98c4d03bb65..c8b61949b5fd 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 4696bb6dbeec..e8b1ca6718e2 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 2cbc9dccc02a..7d12c1932ab6 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;