add profile info to sessions

This commit is contained in:
Labhansh Agrawal 2023-06-29 10:34:33 +05:30
parent f0316e90c9
commit 041d403249
10 changed files with 70 additions and 48 deletions

View file

@ -23,7 +23,6 @@ try {
throw createNodePtyError(); throw createNodePtyError();
} }
const envFromConfig = config.getConfig().env || {};
const useConpty = config.getConfig().useConpty; const useConpty = config.getConfig().useConpty;
// Max duration to batch session data before sending it to the renderer process. // Max duration to batch session data before sending it to the renderer process.
@ -89,6 +88,7 @@ interface SessionOptions {
cwd?: string; cwd?: string;
shell?: string; shell?: string;
shellArgs?: string[]; shellArgs?: string[];
profile: string;
} }
export default class Session extends EventEmitter { export default class Session extends EventEmitter {
pty: IPty | null; pty: IPty | null;
@ -96,6 +96,7 @@ export default class Session extends EventEmitter {
shell: string | null; shell: string | null;
ended: boolean; ended: boolean;
initTimestamp: number; initTimestamp: number;
profile!: string;
constructor(options: SessionOptions) { constructor(options: SessionOptions) {
super(); super();
this.pty = null; this.pty = null;
@ -106,7 +107,9 @@ export default class Session extends EventEmitter {
this.init(options); 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 defaultShellArgs = ['--login'];
const shell = _shell || defaultShell; const shell = _shell || defaultShell;
@ -187,7 +190,7 @@ fallback to default shell config: ${JSON.stringify(defaultShellConfig, undefined
`; `;
console.warn(msg); console.warn(msg);
this.batcher?.write(msg.replace(/\n/g, '\r\n')); 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 { } else {
this.ended = true; this.ended = true;
this.emit('exit'); this.emit('exit');

View file

@ -67,21 +67,6 @@ export function newWindow(
window.setBackgroundColor(toElectronBackgroundColor(cfg_.backgroundColor || '#000')); 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 // config changes
const cfgUnsubscribe = app.config.subscribe(() => { const cfgUnsubscribe = app.config.subscribe(() => {
const cfg_ = app.plugins.getDecoratedConfig(getDefaultProfile()); const cfg_ = app.plugins.getDecoratedConfig(getDefaultProfile());
@ -134,10 +119,11 @@ export function newWindow(
if (extraOptions[key] !== undefined) extraOptionsFiltered[key] = extraOptions[key]; if (extraOptions[key] !== undefined) extraOptionsFiltered[key] = extraOptions[key];
}); });
const profile = extraOptionsFiltered.profile || getDefaultProfile();
const activeSession = extraOptionsFiltered.activeUid ? sessions.get(extraOptionsFiltered.activeUid) : undefined;
let cwd = ''; let cwd = '';
if (cfg.preserveCWD === undefined || cfg.preserveCWD) { if (cfg.preserveCWD !== false && activeSession && activeSession.profile === profile) {
const activeUid = extraOptionsFiltered.activeUid; const activePID = activeSession.pty?.pid;
const activePID = activeUid ? sessions.get(activeUid)?.pty?.pid : undefined;
if (activePID !== undefined) { if (activePID !== undefined) {
try { try {
cwd = getWorkingDirectoryFromPID(activePID) || ''; cwd = getWorkingDirectoryFromPID(activePID) || '';
@ -148,16 +134,36 @@ export function newWindow(
cwd = cwd && isAbsolute(cwd) && existsSync(cwd) ? cwd : ''; 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 // 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: cwd || workingDirectory, cwd: cwd || workingDirectory,
splitDirection: undefined, splitDirection: undefined,
shell: cfg.shell, shell: profileCfg.shell,
shellArgs: cfg.shellArgs && Array.from(cfg.shellArgs) shellArgs: profileCfg.shellArgs && Array.from(profileCfg.shellArgs)
}, },
extraOptionsFiltered, extraOptionsFiltered,
{uid} {
profile: extraOptionsFiltered.profile || getDefaultProfile(),
uid
}
); );
const options = decorateSessionOptions(defaultOptions); const options = decorateSessionOptions(defaultOptions);
const DecoratedSession = decorateSessionClass(Session); const DecoratedSession = decorateSessionClass(Session);
@ -177,7 +183,8 @@ export function newWindow(
splitDirection: options.splitDirection, splitDirection: options.splitDirection,
shell: session.shell, shell: session.shell,
pid: session.pty ? session.pty.pid : null, pid: session.pty ? session.pty.pid : null,
activeUid: options.activeUid ?? undefined activeUid: options.activeUid ?? undefined,
profile: options.profile
}); });
session.on('data', (data: string) => { session.on('data', (data: string) => {

8
common.d.ts vendored
View file

@ -10,6 +10,7 @@ export type Session = {
shell: string | null; shell: string | null;
pid: number | null; pid: number | null;
activeUid?: string; activeUid?: string;
profile: string;
}; };
export type sessionExtraOptions = { export type sessionExtraOptions = {
@ -21,6 +22,7 @@ export type sessionExtraOptions = {
cols?: number; cols?: number;
shell?: string; shell?: string;
shellArgs?: string[]; shellArgs?: string[];
profile?: string;
}; };
export type MainEvents = { export type MainEvents = {
@ -72,9 +74,9 @@ export type RendererEvents = {
'term selectAll': never; 'term selectAll': never;
reload: never; reload: never;
'session clear req': never; 'session clear req': never;
'split request horizontal': {activeUid?: string}; 'split request horizontal': {activeUid?: string; profile?: string};
'split request vertical': {activeUid?: string}; 'split request vertical': {activeUid?: string; profile?: string};
'termgroup add req': {activeUid?: string}; 'termgroup add req': {activeUid?: string; profile?: string};
'termgroup close req': never; 'termgroup close req': never;
'session add': Session; 'session add': Session;
'session data': string; 'session data': string;

View file

@ -18,7 +18,7 @@ import {
import type {HyperState, HyperDispatch, HyperActions} from '../hyper'; import type {HyperState, HyperDispatch, HyperActions} from '../hyper';
import type {Session} from '../../common'; 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) => { return (dispatch: HyperDispatch, getState: () => HyperState) => {
const {sessions} = getState(); const {sessions} = getState();
const now = Date.now(); const now = Date.now();
@ -31,20 +31,20 @@ export function addSession({uid, shell, pid, cols = null, rows = null, splitDire
rows, rows,
splitDirection, splitDirection,
activeUid: activeUid ? activeUid : sessions.activeUid, activeUid: activeUid ? activeUid : sessions.activeUid,
now now,
profile
}); });
}; };
} }
export function requestSession() { export function requestSession(profile: string | undefined) {
return (dispatch: HyperDispatch, getState: () => HyperState) => { return (dispatch: HyperDispatch, getState: () => HyperState) => {
dispatch({ dispatch({
type: SESSION_REQUEST, type: SESSION_REQUEST,
effect: () => { effect: () => {
const {ui} = getState(); const {ui} = getState();
// the cols and rows from preview session maybe not accurate. so remove. const {cwd} = ui;
const {/*cols, rows,*/ cwd} = ui; rpc.emit('new', {cwd, profile});
rpc.emit('new', {cwd});
} }
}); });
}; };

View file

@ -13,16 +13,19 @@ import {setActiveSession, ptyExitSession, userExitSession} from './sessions';
import type {ITermState, ITermGroup, HyperState, HyperDispatch, HyperActions} from '../hyper'; import type {ITermState, ITermGroup, HyperState, HyperDispatch, HyperActions} from '../hyper';
function requestSplit(direction: 'VERTICAL' | 'HORIZONTAL') { function requestSplit(direction: 'VERTICAL' | 'HORIZONTAL') {
return (activeUid: string | undefined) => return (_activeUid: string | undefined, _profile: string | undefined) =>
(dispatch: HyperDispatch, getState: () => HyperState): void => { (dispatch: HyperDispatch, getState: () => HyperState): void => {
dispatch({ dispatch({
type: SESSION_REQUEST, type: SESSION_REQUEST,
effect: () => { effect: () => {
const {ui, sessions} = getState(); const {ui, sessions} = getState();
const activeUid = _activeUid ? _activeUid : sessions.activeUid;
const profile = _profile ? _profile : activeUid ? sessions.sessions[activeUid].profile : undefined;
rpc.emit('new', { rpc.emit('new', {
splitDirection: direction, splitDirection: direction,
cwd: ui.cwd, 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) => { return (dispatch: HyperDispatch, getState: () => HyperState) => {
dispatch({ dispatch({
type: TERM_GROUP_REQUEST, type: TERM_GROUP_REQUEST,
effect: () => { effect: () => {
const {ui, sessions} = getState(); const {ui, sessions} = getState();
const {cwd} = ui; const {cwd} = ui;
const activeUid = _activeUid ? _activeUid : sessions.activeUid;
const profile = _profile ? _profile : activeUid ? sessions.sessions[activeUid].profile : undefined;
rpc.emit('new', { rpc.emit('new', {
isNewGroup: true, isNewGroup: true,
cwd, cwd,
activeUid: activeUid ? activeUid : sessions.activeUid activeUid,
profile
}); });
} }
}); });

View file

@ -274,7 +274,7 @@ export function openFile(path: string) {
}); });
}); });
} }
dispatch(requestSession()); dispatch(requestSession(undefined));
}); });
} }
}); });
@ -310,7 +310,7 @@ export function openSSH(parsedUrl: ReturnType<typeof parseUrl>) {
}); });
}); });
dispatch(requestSession()); dispatch(requestSession(undefined));
} }
}); });
}; };

View file

@ -22,6 +22,7 @@ export interface SessionAddAction {
splitDirection?: 'HORIZONTAL' | 'VERTICAL'; splitDirection?: 'HORIZONTAL' | 'VERTICAL';
activeUid: string | null; activeUid: string | null;
now: number; now: number;
profile: string;
} }
export interface SessionResizeAction { export interface SessionResizeAction {
type: typeof SESSION_RESIZE; type: typeof SESSION_RESIZE;

1
lib/hyper.d.ts vendored
View file

@ -121,6 +121,7 @@ export type session = {
uid: string; uid: string;
splitDirection?: 'HORIZONTAL' | 'VERTICAL'; splitDirection?: 'HORIZONTAL' | 'VERTICAL';
activeUid?: string; activeUid?: string;
profile: string;
}; };
export type sessionState = Immutable<{ export type sessionState = Immutable<{

View file

@ -156,16 +156,16 @@ rpc.on('session search close', () => {
store_.dispatch(sessionActions.closeSearch()); store_.dispatch(sessionActions.closeSearch());
}); });
rpc.on('termgroup add req', ({activeUid}) => { rpc.on('termgroup add req', ({activeUid, profile}) => {
store_.dispatch(termGroupActions.requestTermGroup(activeUid)); store_.dispatch(termGroupActions.requestTermGroup(activeUid, profile));
}); });
rpc.on('split request horizontal', ({activeUid}) => { rpc.on('split request horizontal', ({activeUid, profile}) => {
store_.dispatch(termGroupActions.requestHorizontalSplit(activeUid)); store_.dispatch(termGroupActions.requestHorizontalSplit(activeUid, profile));
}); });
rpc.on('split request vertical', ({activeUid}) => { rpc.on('split request vertical', ({activeUid, profile}) => {
store_.dispatch(termGroupActions.requestVerticalSplit(activeUid)); store_.dispatch(termGroupActions.requestVerticalSplit(activeUid, profile));
}); });
rpc.on('reset fontSize req', () => { rpc.on('reset fontSize req', () => {

View file

@ -28,7 +28,8 @@ function Session(obj: Immutable.DeepPartial<session>) {
cleared: false, cleared: false,
search: false, search: false,
shell: '', shell: '',
pid: null pid: null,
profile: ''
}; };
return Immutable(x).merge(obj); return Immutable(x).merge(obj);
} }
@ -51,7 +52,8 @@ const reducer: ISessionReducer = (state = initialState, action) => {
rows: action.rows, rows: action.rows,
uid: action.uid, uid: action.uid,
shell: action.shell ? action.shell.split('/').pop() : null, shell: action.shell ? action.shell.split('/').pop() : null,
pid: action.pid pid: action.pid,
profile: action.profile
}) })
); );