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();
}
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');

View file

@ -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) => {

8
common.d.ts vendored
View file

@ -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;

View file

@ -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});
}
});
};

View file

@ -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
});
}
});

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';
activeUid: string | null;
now: number;
profile: string;
}
export interface SessionResizeAction {
type: typeof SESSION_RESIZE;

1
lib/hyper.d.ts vendored
View file

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

View file

@ -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', () => {

View file

@ -28,7 +28,8 @@ function Session(obj: Immutable.DeepPartial<session>) {
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
})
);