diff --git a/app/session.ts b/app/session.ts index 827277e2..a8a0f8b1 100644 --- a/app/session.ts +++ b/app/session.ts @@ -23,7 +23,6 @@ try { throw createNodePtyError(); } -const envFromConfig = config.getConfig().env || {}; const useConpty = config.getConfig().useConpty; // Max duration to batch session data before sending it to the renderer process. @@ -89,6 +88,7 @@ interface SessionOptions { cwd?: string; shell?: string; shellArgs?: string[]; + profile: string; } export default class Session extends EventEmitter { pty: IPty | null; @@ -96,6 +96,7 @@ export default class Session extends EventEmitter { shell: string | null; ended: boolean; initTimestamp: number; + profile!: string; constructor(options: SessionOptions) { super(); this.pty = null; @@ -106,7 +107,9 @@ export default class Session extends EventEmitter { this.init(options); } - init({uid, rows, cols, cwd, shell: _shell, shellArgs: _shellArgs}: SessionOptions) { + init({uid, rows, cols, cwd, shell: _shell, shellArgs: _shellArgs, profile}: SessionOptions) { + this.profile = profile; + const envFromConfig = config.getProfileConfig(profile).env || {}; const defaultShellArgs = ['--login']; const shell = _shell || defaultShell; @@ -187,7 +190,7 @@ fallback to default shell config: ${JSON.stringify(defaultShellConfig, undefined `; console.warn(msg); this.batcher?.write(msg.replace(/\n/g, '\r\n')); - this.init({uid, rows, cols, cwd, ...defaultShellConfig}); + this.init({uid, rows, cols, cwd, ...defaultShellConfig, profile}); } else { this.ended = true; this.emit('exit'); diff --git a/app/ui/window.ts b/app/ui/window.ts index 087c41b7..2e6c1434 100644 --- a/app/ui/window.ts +++ b/app/ui/window.ts @@ -67,21 +67,6 @@ export function newWindow( window.setBackgroundColor(toElectronBackgroundColor(cfg_.backgroundColor || '#000')); }; - // set working directory - let argPath = process.argv[1]; - if (argPath && process.platform === 'win32') { - if (/[a-zA-Z]:"/.test(argPath)) { - argPath = argPath.replace('"', sep); - } - argPath = normalize(argPath + sep); - } - let workingDirectory = homeDirectory; - if (argPath && isAbsolute(argPath)) { - workingDirectory = argPath; - } else if (cfg.workingDirectory && isAbsolute(cfg.workingDirectory)) { - workingDirectory = cfg.workingDirectory; - } - // config changes const cfgUnsubscribe = app.config.subscribe(() => { const cfg_ = app.plugins.getDecoratedConfig(getDefaultProfile()); @@ -134,10 +119,11 @@ export function newWindow( if (extraOptions[key] !== undefined) extraOptionsFiltered[key] = extraOptions[key]; }); + const profile = extraOptionsFiltered.profile || getDefaultProfile(); + const activeSession = extraOptionsFiltered.activeUid ? sessions.get(extraOptionsFiltered.activeUid) : undefined; let cwd = ''; - if (cfg.preserveCWD === undefined || cfg.preserveCWD) { - const activeUid = extraOptionsFiltered.activeUid; - const activePID = activeUid ? sessions.get(activeUid)?.pty?.pid : undefined; + if (cfg.preserveCWD !== false && activeSession && activeSession.profile === profile) { + const activePID = activeSession.pty?.pid; if (activePID !== undefined) { try { cwd = getWorkingDirectoryFromPID(activePID) || ''; @@ -148,16 +134,36 @@ export function newWindow( cwd = cwd && isAbsolute(cwd) && existsSync(cwd) ? cwd : ''; } + const profileCfg = app.plugins.getDecoratedConfig(profile); + + // set working directory + let argPath = process.argv[1]; + if (argPath && process.platform === 'win32') { + if (/[a-zA-Z]:"/.test(argPath)) { + argPath = argPath.replace('"', sep); + } + argPath = normalize(argPath + sep); + } + let workingDirectory = homeDirectory; + if (argPath && isAbsolute(argPath)) { + workingDirectory = argPath; + } else if (profileCfg.workingDirectory && isAbsolute(profileCfg.workingDirectory)) { + workingDirectory = profileCfg.workingDirectory; + } + // remove the rows and cols, the wrong value of them will break layout when init create const defaultOptions = Object.assign( { cwd: cwd || workingDirectory, splitDirection: undefined, - shell: cfg.shell, - shellArgs: cfg.shellArgs && Array.from(cfg.shellArgs) + shell: profileCfg.shell, + shellArgs: profileCfg.shellArgs && Array.from(profileCfg.shellArgs) }, extraOptionsFiltered, - {uid} + { + profile: extraOptionsFiltered.profile || getDefaultProfile(), + uid + } ); const options = decorateSessionOptions(defaultOptions); const DecoratedSession = decorateSessionClass(Session); @@ -177,7 +183,8 @@ export function newWindow( splitDirection: options.splitDirection, shell: session.shell, pid: session.pty ? session.pty.pid : null, - activeUid: options.activeUid ?? undefined + activeUid: options.activeUid ?? undefined, + profile: options.profile }); session.on('data', (data: string) => { diff --git a/common.d.ts b/common.d.ts index 153593d1..7ab9fc1c 100644 --- a/common.d.ts +++ b/common.d.ts @@ -10,6 +10,7 @@ export type Session = { shell: string | null; pid: number | null; activeUid?: string; + profile: string; }; export type sessionExtraOptions = { @@ -21,6 +22,7 @@ export type sessionExtraOptions = { cols?: number; shell?: string; shellArgs?: string[]; + profile?: string; }; export type MainEvents = { @@ -72,9 +74,9 @@ export type RendererEvents = { 'term selectAll': never; reload: never; 'session clear req': never; - 'split request horizontal': {activeUid?: string}; - 'split request vertical': {activeUid?: string}; - 'termgroup add req': {activeUid?: string}; + 'split request horizontal': {activeUid?: string; profile?: string}; + 'split request vertical': {activeUid?: string; profile?: string}; + 'termgroup add req': {activeUid?: string; profile?: string}; 'termgroup close req': never; 'session add': Session; 'session data': string; diff --git a/lib/actions/sessions.ts b/lib/actions/sessions.ts index 028a6086..f62e1317 100644 --- a/lib/actions/sessions.ts +++ b/lib/actions/sessions.ts @@ -18,7 +18,7 @@ import { import type {HyperState, HyperDispatch, HyperActions} from '../hyper'; import type {Session} from '../../common'; -export function addSession({uid, shell, pid, cols = null, rows = null, splitDirection, activeUid}: Session) { +export function addSession({uid, shell, pid, cols = null, rows = null, splitDirection, activeUid, profile}: Session) { return (dispatch: HyperDispatch, getState: () => HyperState) => { const {sessions} = getState(); const now = Date.now(); @@ -31,20 +31,20 @@ export function addSession({uid, shell, pid, cols = null, rows = null, splitDire rows, splitDirection, activeUid: activeUid ? activeUid : sessions.activeUid, - now + now, + profile }); }; } -export function requestSession() { +export function requestSession(profile: string | undefined) { return (dispatch: HyperDispatch, getState: () => HyperState) => { dispatch({ type: SESSION_REQUEST, effect: () => { const {ui} = getState(); - // the cols and rows from preview session maybe not accurate. so remove. - const {/*cols, rows,*/ cwd} = ui; - rpc.emit('new', {cwd}); + const {cwd} = ui; + rpc.emit('new', {cwd, profile}); } }); }; diff --git a/lib/actions/term-groups.ts b/lib/actions/term-groups.ts index db3094f4..4210a33c 100644 --- a/lib/actions/term-groups.ts +++ b/lib/actions/term-groups.ts @@ -13,16 +13,19 @@ import {setActiveSession, ptyExitSession, userExitSession} from './sessions'; import type {ITermState, ITermGroup, HyperState, HyperDispatch, HyperActions} from '../hyper'; function requestSplit(direction: 'VERTICAL' | 'HORIZONTAL') { - return (activeUid: string | undefined) => + return (_activeUid: string | undefined, _profile: string | undefined) => (dispatch: HyperDispatch, getState: () => HyperState): void => { dispatch({ type: SESSION_REQUEST, effect: () => { const {ui, sessions} = getState(); + const activeUid = _activeUid ? _activeUid : sessions.activeUid; + const profile = _profile ? _profile : activeUid ? sessions.sessions[activeUid].profile : undefined; rpc.emit('new', { splitDirection: direction, cwd: ui.cwd, - activeUid: activeUid ? activeUid : sessions.activeUid + activeUid, + profile }); } }); @@ -40,17 +43,20 @@ export function resizeTermGroup(uid: string, sizes: number[]): HyperActions { }; } -export function requestTermGroup(activeUid: string | undefined) { +export function requestTermGroup(_activeUid: string | undefined, _profile: string | undefined) { return (dispatch: HyperDispatch, getState: () => HyperState) => { dispatch({ type: TERM_GROUP_REQUEST, effect: () => { const {ui, sessions} = getState(); const {cwd} = ui; + const activeUid = _activeUid ? _activeUid : sessions.activeUid; + const profile = _profile ? _profile : activeUid ? sessions.sessions[activeUid].profile : undefined; rpc.emit('new', { isNewGroup: true, cwd, - activeUid: activeUid ? activeUid : sessions.activeUid + activeUid, + profile }); } }); diff --git a/lib/actions/ui.ts b/lib/actions/ui.ts index 0e6c59c8..bccc5279 100644 --- a/lib/actions/ui.ts +++ b/lib/actions/ui.ts @@ -274,7 +274,7 @@ export function openFile(path: string) { }); }); } - dispatch(requestSession()); + dispatch(requestSession(undefined)); }); } }); @@ -310,7 +310,7 @@ export function openSSH(parsedUrl: ReturnType) { }); }); - dispatch(requestSession()); + dispatch(requestSession(undefined)); } }); }; diff --git a/lib/constants/sessions.ts b/lib/constants/sessions.ts index 27b27e57..db52a757 100644 --- a/lib/constants/sessions.ts +++ b/lib/constants/sessions.ts @@ -22,6 +22,7 @@ export interface SessionAddAction { splitDirection?: 'HORIZONTAL' | 'VERTICAL'; activeUid: string | null; now: number; + profile: string; } export interface SessionResizeAction { type: typeof SESSION_RESIZE; diff --git a/lib/hyper.d.ts b/lib/hyper.d.ts index 428aede6..7769a290 100644 --- a/lib/hyper.d.ts +++ b/lib/hyper.d.ts @@ -121,6 +121,7 @@ export type session = { uid: string; splitDirection?: 'HORIZONTAL' | 'VERTICAL'; activeUid?: string; + profile: string; }; export type sessionState = Immutable<{ diff --git a/lib/index.tsx b/lib/index.tsx index 8035a8e3..5c7d66b5 100644 --- a/lib/index.tsx +++ b/lib/index.tsx @@ -156,16 +156,16 @@ rpc.on('session search close', () => { store_.dispatch(sessionActions.closeSearch()); }); -rpc.on('termgroup add req', ({activeUid}) => { - store_.dispatch(termGroupActions.requestTermGroup(activeUid)); +rpc.on('termgroup add req', ({activeUid, profile}) => { + store_.dispatch(termGroupActions.requestTermGroup(activeUid, profile)); }); -rpc.on('split request horizontal', ({activeUid}) => { - store_.dispatch(termGroupActions.requestHorizontalSplit(activeUid)); +rpc.on('split request horizontal', ({activeUid, profile}) => { + store_.dispatch(termGroupActions.requestHorizontalSplit(activeUid, profile)); }); -rpc.on('split request vertical', ({activeUid}) => { - store_.dispatch(termGroupActions.requestVerticalSplit(activeUid)); +rpc.on('split request vertical', ({activeUid, profile}) => { + store_.dispatch(termGroupActions.requestVerticalSplit(activeUid, profile)); }); rpc.on('reset fontSize req', () => { diff --git a/lib/reducers/sessions.ts b/lib/reducers/sessions.ts index 9d470345..11f2a67e 100644 --- a/lib/reducers/sessions.ts +++ b/lib/reducers/sessions.ts @@ -28,7 +28,8 @@ function Session(obj: Immutable.DeepPartial) { cleared: false, search: false, shell: '', - pid: null + pid: null, + profile: '' }; return Immutable(x).merge(obj); } @@ -51,7 +52,8 @@ const reducer: ISessionReducer = (state = initialState, action) => { rows: action.rows, uid: action.uid, shell: action.shell ? action.shell.split('/').pop() : null, - pid: action.pid + pid: action.pid, + profile: action.profile }) );