2017-05-13 06:19:03 -08:00
|
|
|
import {php_escapeshellcmd as escapeShellCmd} from 'php-escape-shell';
|
2019-10-02 16:08:40 -08:00
|
|
|
import {isExecutable} from '../utils/file';
|
2019-12-03 03:03:52 -09:00
|
|
|
import {getRootGroups} from '../selectors';
|
2016-10-25 04:53:15 -08:00
|
|
|
import findBySession from '../utils/term-groups';
|
2016-08-01 14:52:21 -08:00
|
|
|
import notify from '../utils/notify';
|
2016-07-13 12:44:24 -08:00
|
|
|
import rpc from '../rpc';
|
2020-01-02 09:27:27 -09:00
|
|
|
import {requestSession, sendSessionData, setActiveSession} from './sessions';
|
2016-07-13 12:44:24 -08:00
|
|
|
import {
|
|
|
|
|
UI_FONT_SIZE_SET,
|
|
|
|
|
UI_FONT_SIZE_INCR,
|
|
|
|
|
UI_FONT_SIZE_DECR,
|
|
|
|
|
UI_FONT_SIZE_RESET,
|
2016-07-19 10:30:57 -08:00
|
|
|
UI_FONT_SMOOTHING_SET,
|
2016-07-13 12:44:24 -08:00
|
|
|
UI_MOVE_LEFT,
|
|
|
|
|
UI_MOVE_RIGHT,
|
|
|
|
|
UI_MOVE_TO,
|
2016-10-03 18:00:50 -08:00
|
|
|
UI_MOVE_NEXT_PANE,
|
|
|
|
|
UI_MOVE_PREV_PANE,
|
2017-01-10 20:45:49 -09:00
|
|
|
UI_WINDOW_GEOMETRY_CHANGED,
|
2016-08-01 14:52:21 -08:00
|
|
|
UI_WINDOW_MOVE,
|
2017-11-02 18:51:18 -08:00
|
|
|
UI_OPEN_FILE,
|
2019-05-08 17:43:50 -08:00
|
|
|
UI_ENTER_FULLSCREEN,
|
|
|
|
|
UI_LEAVE_FULLSCREEN,
|
2018-02-12 11:20:45 -09:00
|
|
|
UI_OPEN_SSH_URL,
|
2017-11-03 12:24:41 -08:00
|
|
|
UI_CONTEXTMENU_OPEN,
|
2017-11-02 18:51:18 -08:00
|
|
|
UI_COMMAND_EXEC
|
2016-07-13 12:44:24 -08:00
|
|
|
} from '../constants/ui';
|
|
|
|
|
|
2016-10-03 18:00:50 -08:00
|
|
|
import {setActiveGroup} from './term-groups';
|
2018-02-12 11:20:45 -09:00
|
|
|
import parseUrl from 'parse-url';
|
2021-03-05 07:03:35 -09:00
|
|
|
import {HyperState, HyperDispatch, HyperActions, ITermGroups} from '../hyper';
|
2021-03-28 11:54:27 -08:00
|
|
|
import {stat, Stats} from 'fs';
|
2016-08-01 14:52:21 -08:00
|
|
|
|
2019-10-19 08:59:56 -08:00
|
|
|
export function openContextMenu(uid: string, selection: any) {
|
2020-01-02 09:27:27 -09:00
|
|
|
return (dispatch: HyperDispatch, getState: () => HyperState) => {
|
2017-11-03 12:24:41 -08:00
|
|
|
dispatch({
|
|
|
|
|
type: UI_CONTEXTMENU_OPEN,
|
|
|
|
|
uid,
|
|
|
|
|
effect() {
|
|
|
|
|
const state = getState();
|
|
|
|
|
const show = !state.ui.quickEdit;
|
|
|
|
|
if (show) {
|
|
|
|
|
rpc.emit('open context menu', selection);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
export function increaseFontSize() {
|
2020-01-02 09:27:27 -09:00
|
|
|
return (dispatch: HyperDispatch, getState: () => HyperState) => {
|
2016-07-13 12:44:24 -08:00
|
|
|
dispatch({
|
|
|
|
|
type: UI_FONT_SIZE_INCR,
|
2016-09-21 06:27:11 -08:00
|
|
|
effect() {
|
2016-07-13 12:44:24 -08:00
|
|
|
const state = getState();
|
|
|
|
|
const old = state.ui.fontSizeOverride || state.ui.fontSize;
|
2016-07-13 21:37:46 -08:00
|
|
|
const value = old + 1;
|
2016-07-13 12:44:24 -08:00
|
|
|
dispatch({
|
|
|
|
|
type: UI_FONT_SIZE_SET,
|
2016-07-13 21:37:46 -08:00
|
|
|
value
|
2016-07-13 12:44:24 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
export function decreaseFontSize() {
|
2020-01-02 09:27:27 -09:00
|
|
|
return (dispatch: HyperDispatch, getState: () => HyperState) => {
|
2016-07-13 12:44:24 -08:00
|
|
|
dispatch({
|
|
|
|
|
type: UI_FONT_SIZE_DECR,
|
2016-09-21 06:27:11 -08:00
|
|
|
effect() {
|
2016-07-13 12:44:24 -08:00
|
|
|
const state = getState();
|
|
|
|
|
const old = state.ui.fontSizeOverride || state.ui.fontSize;
|
2019-03-05 13:42:54 -09:00
|
|
|
// when the font-size is really small, below 5px, xterm starts showing up issues.
|
|
|
|
|
const value = old > 5 ? old - 1 : old;
|
2016-07-13 12:44:24 -08:00
|
|
|
dispatch({
|
|
|
|
|
type: UI_FONT_SIZE_SET,
|
2016-07-13 21:37:46 -08:00
|
|
|
value
|
2016-07-13 12:44:24 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-02 09:27:27 -09:00
|
|
|
export function resetFontSize(): HyperActions {
|
2016-07-13 12:44:24 -08:00
|
|
|
return {
|
|
|
|
|
type: UI_FONT_SIZE_RESET
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
export function setFontSmoothing() {
|
2020-01-02 09:27:27 -09:00
|
|
|
return (dispatch: HyperDispatch) => {
|
2016-07-26 09:46:23 -08:00
|
|
|
setTimeout(() => {
|
|
|
|
|
const devicePixelRatio = window.devicePixelRatio;
|
2017-09-10 05:35:10 -08:00
|
|
|
const fontSmoothing = devicePixelRatio < 2 ? 'subpixel-antialiased' : 'antialiased';
|
2016-07-19 10:30:57 -08:00
|
|
|
|
2016-07-26 09:46:23 -08:00
|
|
|
dispatch({
|
|
|
|
|
type: UI_FONT_SMOOTHING_SET,
|
|
|
|
|
fontSmoothing
|
|
|
|
|
});
|
|
|
|
|
}, 100);
|
2016-07-19 10:30:57 -08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-02 09:27:27 -09:00
|
|
|
export function windowGeometryUpdated(): HyperActions {
|
2017-01-10 20:45:49 -09:00
|
|
|
return {
|
|
|
|
|
type: UI_WINDOW_GEOMETRY_CHANGED
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-03 18:00:50 -08:00
|
|
|
// Find all sessions that are below the given
|
|
|
|
|
// termGroup uid in the hierarchy:
|
2021-03-05 07:03:35 -09:00
|
|
|
const findChildSessions = (termGroups: ITermGroups, uid: string): string[] => {
|
2016-10-03 18:00:50 -08:00
|
|
|
const group = termGroups[uid];
|
|
|
|
|
if (group.sessionUid) {
|
|
|
|
|
return [uid];
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-19 04:51:34 -08:00
|
|
|
return group.children
|
|
|
|
|
.asMutable()
|
|
|
|
|
.reduce((total: string[], childUid: string) => total.concat(findChildSessions(termGroups, childUid)), []);
|
2016-10-03 18:00:50 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Get the index of the next or previous group,
|
|
|
|
|
// depending on the movement direction:
|
2019-10-19 08:59:56 -08:00
|
|
|
const getNeighborIndex = (groups: string[], uid: string, type: string) => {
|
2016-10-03 18:00:50 -08:00
|
|
|
if (type === UI_MOVE_NEXT_PANE) {
|
|
|
|
|
return (groups.indexOf(uid) + 1) % groups.length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (groups.indexOf(uid) + groups.length - 1) % groups.length;
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-02 09:27:27 -09:00
|
|
|
function moveToNeighborPane(type: typeof UI_MOVE_NEXT_PANE | typeof UI_MOVE_PREV_PANE) {
|
|
|
|
|
return () => (dispatch: HyperDispatch, getState: () => HyperState) => {
|
2016-10-03 18:00:50 -08:00
|
|
|
dispatch({
|
|
|
|
|
type,
|
|
|
|
|
effect() {
|
|
|
|
|
const {sessions, termGroups} = getState();
|
2019-10-19 08:59:56 -08:00
|
|
|
const {uid} = findBySession(termGroups, sessions.activeUid!)!;
|
|
|
|
|
const childGroups = findChildSessions(termGroups.termGroups, termGroups.activeRootGroup!);
|
2016-10-03 18:00:50 -08:00
|
|
|
if (childGroups.length === 1) {
|
|
|
|
|
console.log('ignoring move for single group');
|
|
|
|
|
} else {
|
2021-03-28 09:07:04 -08:00
|
|
|
const index = getNeighborIndex(childGroups, uid, type);
|
2016-10-03 18:00:50 -08:00
|
|
|
const {sessionUid} = termGroups.termGroups[childGroups[index]];
|
2019-10-19 08:59:56 -08:00
|
|
|
dispatch(setActiveSession(sessionUid!));
|
2016-10-03 18:00:50 -08:00
|
|
|
}
|
|
|
|
|
}
|
2020-01-02 09:27:27 -09:00
|
|
|
} as HyperActions);
|
2016-10-03 18:00:50 -08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const moveToNextPane = moveToNeighborPane(UI_MOVE_NEXT_PANE);
|
|
|
|
|
export const moveToPreviousPane = moveToNeighborPane(UI_MOVE_PREV_PANE);
|
|
|
|
|
|
2019-10-19 08:59:56 -08:00
|
|
|
const getGroupUids = (state: HyperState) => {
|
2016-10-03 18:00:50 -08:00
|
|
|
const rootGroups = getRootGroups(state);
|
|
|
|
|
return rootGroups.map(({uid}) => uid);
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
export function moveLeft() {
|
2020-01-02 09:27:27 -09:00
|
|
|
return (dispatch: HyperDispatch, getState: () => HyperState) => {
|
2016-07-13 12:44:24 -08:00
|
|
|
dispatch({
|
|
|
|
|
type: UI_MOVE_LEFT,
|
2016-09-21 06:27:11 -08:00
|
|
|
effect() {
|
2016-10-03 18:00:50 -08:00
|
|
|
const state = getState();
|
2019-11-11 06:21:42 -09:00
|
|
|
const uid = state.termGroups.activeRootGroup!;
|
2016-10-03 18:00:50 -08:00
|
|
|
const groupUids = getGroupUids(state);
|
|
|
|
|
const index = groupUids.indexOf(uid);
|
2019-10-02 16:08:40 -08:00
|
|
|
const next = groupUids[index - 1] || groupUids[groupUids.length - 1];
|
2016-07-13 12:44:24 -08:00
|
|
|
if (!next || uid === next) {
|
|
|
|
|
console.log('ignoring left move action');
|
|
|
|
|
} else {
|
2016-10-03 18:00:50 -08:00
|
|
|
dispatch(setActiveGroup(next));
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
export function moveRight() {
|
2020-01-02 09:27:27 -09:00
|
|
|
return (dispatch: HyperDispatch, getState: () => HyperState) => {
|
2016-07-13 12:44:24 -08:00
|
|
|
dispatch({
|
|
|
|
|
type: UI_MOVE_RIGHT,
|
2016-09-21 06:27:11 -08:00
|
|
|
effect() {
|
2016-10-03 18:00:50 -08:00
|
|
|
const state = getState();
|
|
|
|
|
const groupUids = getGroupUids(state);
|
2019-11-11 06:21:42 -09:00
|
|
|
const uid = state.termGroups.activeRootGroup!;
|
2016-10-03 18:00:50 -08:00
|
|
|
const index = groupUids.indexOf(uid);
|
|
|
|
|
const next = groupUids[index + 1] || groupUids[0];
|
2016-07-13 12:44:24 -08:00
|
|
|
if (!next || uid === next) {
|
|
|
|
|
console.log('ignoring right move action');
|
|
|
|
|
} else {
|
2016-10-03 18:00:50 -08:00
|
|
|
dispatch(setActiveGroup(next));
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 08:59:56 -08:00
|
|
|
export function moveTo(i: number | 'last') {
|
2020-01-02 09:27:27 -09:00
|
|
|
return (dispatch: HyperDispatch, getState: () => HyperState) => {
|
2017-10-05 10:39:39 -08:00
|
|
|
if (i === 'last') {
|
|
|
|
|
// Finding last tab index
|
|
|
|
|
const {termGroups} = getState().termGroups;
|
|
|
|
|
i =
|
|
|
|
|
Object.keys(termGroups)
|
2020-03-25 02:15:08 -08:00
|
|
|
.map((uid) => termGroups[uid])
|
2017-10-05 10:39:39 -08:00
|
|
|
.filter(({parentUid}) => !parentUid).length - 1;
|
|
|
|
|
}
|
2016-07-13 12:44:24 -08:00
|
|
|
dispatch({
|
|
|
|
|
type: UI_MOVE_TO,
|
|
|
|
|
index: i,
|
2016-09-21 06:27:11 -08:00
|
|
|
effect() {
|
2016-10-03 18:00:50 -08:00
|
|
|
const state = getState();
|
|
|
|
|
const groupUids = getGroupUids(state);
|
|
|
|
|
const uid = state.termGroups.activeRootGroup;
|
2019-10-19 08:59:56 -08:00
|
|
|
if (uid === groupUids[i as number]) {
|
2016-07-13 12:44:24 -08:00
|
|
|
console.log('ignoring same uid');
|
2019-10-19 08:59:56 -08:00
|
|
|
} else if (groupUids[i as number]) {
|
|
|
|
|
dispatch(setActiveGroup(groupUids[i as number]));
|
2016-09-21 06:27:11 -08:00
|
|
|
} else {
|
2016-10-03 18:00:50 -08:00
|
|
|
console.log('ignoring inexistent index', i);
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 08:59:56 -08:00
|
|
|
export function windowMove(window: any) {
|
2020-01-02 09:27:27 -09:00
|
|
|
return (dispatch: HyperDispatch) => {
|
2016-07-19 10:30:57 -08:00
|
|
|
dispatch({
|
|
|
|
|
type: UI_WINDOW_MOVE,
|
2019-02-26 14:22:52 -09:00
|
|
|
window,
|
2016-09-21 06:27:11 -08:00
|
|
|
effect() {
|
2016-07-19 10:30:57 -08:00
|
|
|
dispatch(setFontSmoothing());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
2016-08-01 14:52:21 -08:00
|
|
|
|
2017-01-10 20:45:49 -09:00
|
|
|
export function windowGeometryChange() {
|
2020-01-02 09:27:27 -09:00
|
|
|
return (dispatch: HyperDispatch) => {
|
2017-01-10 20:45:49 -09:00
|
|
|
dispatch({
|
|
|
|
|
type: UI_WINDOW_MOVE,
|
|
|
|
|
effect() {
|
|
|
|
|
dispatch(setFontSmoothing());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 08:59:56 -08:00
|
|
|
export function openFile(path: string) {
|
2020-01-02 09:27:27 -09:00
|
|
|
return (dispatch: HyperDispatch) => {
|
2016-08-01 14:52:21 -08:00
|
|
|
dispatch({
|
|
|
|
|
type: UI_OPEN_FILE,
|
2016-09-21 06:27:11 -08:00
|
|
|
effect() {
|
2019-10-19 08:59:56 -08:00
|
|
|
stat(path, (err: any, stats: Stats) => {
|
2016-08-01 14:52:21 -08:00
|
|
|
if (err) {
|
2018-05-02 00:10:44 -08:00
|
|
|
notify('Unable to open path', `"${path}" doesn't exist.`, {error: err});
|
2016-08-01 14:52:21 -08:00
|
|
|
} else {
|
2017-05-13 06:19:03 -08:00
|
|
|
let command = escapeShellCmd(path).replace(/ /g, '\\ ');
|
2016-08-01 14:52:21 -08:00
|
|
|
if (stats.isDirectory()) {
|
|
|
|
|
command = `cd ${command}\n`;
|
|
|
|
|
} else if (stats.isFile() && isExecutable(stats)) {
|
|
|
|
|
command += '\n';
|
|
|
|
|
}
|
2016-09-21 06:27:11 -08:00
|
|
|
rpc.once('session add', ({uid}) => {
|
2016-08-01 14:52:21 -08:00
|
|
|
rpc.once('session data', () => {
|
2019-10-19 08:59:56 -08:00
|
|
|
dispatch(sendSessionData(uid, command, null));
|
2016-08-01 14:52:21 -08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
dispatch(requestSession());
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
2017-11-02 18:51:18 -08:00
|
|
|
|
2020-01-02 09:27:27 -09:00
|
|
|
export function enterFullScreen(): HyperActions {
|
2019-05-08 17:43:50 -08:00
|
|
|
return {
|
|
|
|
|
type: UI_ENTER_FULLSCREEN
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-02 09:27:27 -09:00
|
|
|
export function leaveFullScreen(): HyperActions {
|
2019-05-08 17:43:50 -08:00
|
|
|
return {
|
|
|
|
|
type: UI_LEAVE_FULLSCREEN
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 08:59:56 -08:00
|
|
|
export function openSSH(url: string) {
|
2020-01-02 09:27:27 -09:00
|
|
|
return (dispatch: HyperDispatch) => {
|
2018-02-12 11:20:45 -09:00
|
|
|
dispatch({
|
|
|
|
|
type: UI_OPEN_SSH_URL,
|
|
|
|
|
effect() {
|
2019-10-19 08:59:56 -08:00
|
|
|
const parsedUrl = parseUrl(url, true);
|
2021-03-28 09:48:36 -08:00
|
|
|
let command = `${parsedUrl.protocol} ${parsedUrl.user ? `${parsedUrl.user}@` : ''}${parsedUrl.resource}`;
|
2018-02-12 11:20:45 -09:00
|
|
|
|
2021-03-28 09:48:36 -08:00
|
|
|
if (parsedUrl.port) command += ` -p ${parsedUrl.port}`;
|
2018-02-12 11:20:45 -09:00
|
|
|
|
|
|
|
|
command += '\n';
|
|
|
|
|
|
|
|
|
|
rpc.once('session add', ({uid}) => {
|
|
|
|
|
rpc.once('session data', () => {
|
2019-10-19 08:59:56 -08:00
|
|
|
dispatch(sendSessionData(uid, command, null));
|
2018-02-12 11:20:45 -09:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
dispatch(requestSession());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-19 04:51:34 -08:00
|
|
|
export function execCommand(command: string, fn: (e: any, dispatch: HyperDispatch) => void, e: any) {
|
2020-01-02 09:27:27 -09:00
|
|
|
return (dispatch: HyperDispatch) =>
|
2017-11-02 18:51:18 -08:00
|
|
|
dispatch({
|
|
|
|
|
type: UI_COMMAND_EXEC,
|
|
|
|
|
command,
|
|
|
|
|
effect() {
|
|
|
|
|
if (fn) {
|
2020-03-22 01:27:40 -08:00
|
|
|
fn(e, dispatch);
|
2017-11-02 18:51:18 -08:00
|
|
|
} else {
|
|
|
|
|
rpc.emit('command', command);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|