Add preserveCWD option

This commit is contained in:
Labhansh Agrawal 2022-01-08 16:17:38 +05:30
parent cc550b7c73
commit f79006bae1
4 changed files with 19 additions and 3 deletions

View file

@ -163,6 +163,9 @@ module.exports = {
// set to true to enable screen reading apps (like NVDA) to read the contents of the terminal // set to true to enable screen reading apps (like NVDA) to read the contents of the terminal
screenReaderMode: false, 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 // for advanced config flags please refer to https://hyper.is/#cfg
}, },

View file

@ -16,6 +16,7 @@ import {setRendererType, unsetRendererType} from '../utils/renderer-utils';
import {decorateSessionOptions, decorateSessionClass} from '../plugins'; import {decorateSessionOptions, decorateSessionClass} from '../plugins';
import {enable as remoteEnable} from '@electron/remote/main'; import {enable as remoteEnable} from '@electron/remote/main';
import {configOptions} from '../../lib/config'; import {configOptions} from '../../lib/config';
import {getWorkingDirectoryFromPID} from 'native-process-working-directory';
export function newWindow( export function newWindow(
options_: BrowserWindowConstructorOptions, options_: BrowserWindowConstructorOptions,
@ -129,10 +130,21 @@ export function newWindow(
if (extraOptions[key] !== undefined) extraOptionsFiltered[key] = extraOptions[key]; 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 // remove the rows and cols, the wrong value of them will break layout when init create
const defaultOptions = Object.assign( const defaultOptions = Object.assign(
{ {
cwd: workingDirectory, cwd: cwd || workingDirectory,
splitDirection: undefined, splitDirection: undefined,
shell: cfg.shell, shell: cfg.shell,
shellArgs: cfg.shellArgs && Array.from(cfg.shellArgs) shellArgs: cfg.shellArgs && Array.from(cfg.shellArgs)

View file

@ -45,12 +45,12 @@ export function requestTermGroup(activeUid: string) {
dispatch({ dispatch({
type: TERM_GROUP_REQUEST, type: TERM_GROUP_REQUEST,
effect: () => { effect: () => {
const {ui} = getState(); const {ui, sessions} = getState();
const {cwd} = ui; const {cwd} = ui;
rpc.emit('new', { rpc.emit('new', {
isNewGroup: true, isNewGroup: true,
cwd, cwd,
activeUid activeUid: activeUid ? activeUid : sessions.activeUid
}); });
} }
}); });

1
lib/config.d.ts vendored
View file

@ -50,6 +50,7 @@ export type configOptions = {
cmdIsMeta: boolean; cmdIsMeta: boolean;
}; };
padding: string; padding: string;
preserveCWD: boolean;
quickEdit: boolean; quickEdit: boolean;
screenReaderMode: boolean; screenReaderMode: boolean;
scrollback: number; scrollback: number;