Skip to content

Commit

Permalink
mercurywm: add alias command (#55)
Browse files Browse the repository at this point in the history
* mercurywm: add alias command

* mercurywm: add error messages to alias and env
  • Loading branch information
fg123 authored and MichaelKim committed Jan 24, 2019
1 parent eda9909 commit b65d6e4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
31 changes: 31 additions & 0 deletions src/background/commands/alias.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* @flow strict */

import type { StoreState } from 'types';

/* This command has essentially the same logic as env.js */
export default function alias(state: StoreState, params: Array<string>) {
if (!state.wsh.aliases) {
state.wsh.aliases = {};
}
if (params.length === 0) {
Object.keys(state.wsh.aliases).forEach(e => {
this.output(e + ': ' + state.wsh.aliases[e], false, false);
});
} else if (params.length === 1) {
if (!state.wsh.aliases[params[0]]) {
this.output('\'' + params[0] + '\' is not aliased to anything!');
} else {
this.output(params[0] + ': ' + state.wsh.aliases[params[0]], false, false);
}
} else if (params.length === 2) {
// $FlowFixMe: command can mutate state
if (!params[1]) {
delete state.wsh.aliases[params[0]];
} else {
state.wsh.aliases[params[0]] = params[1];
}
} else {
this.output('Invalid number of parameters');
}
return state;
}
6 changes: 5 additions & 1 deletion src/background/commands/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ export default function env(state: StoreState, params: Array<string>) {
this.output(e + ': ' + state.wsh.env[e], false, false);
});
} else if (params.length === 1) {
this.output(params[0] + ': ' + state.wsh.env[params[0]], false, false);
if (!state.wsh.env[params[0]]) {
this.output('Environment variable \'' + params[0] + '\' does not exist!');
} else {
this.output(params[0] + ': ' + state.wsh.env[params[0]], false, false);
}
} else if (params.length === 2) {
// $FlowFixMe: command can mutate state
state.wsh.env[params[0]] = params[1];
Expand Down
8 changes: 7 additions & 1 deletion src/background/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function Command(state, command, params) {

export function isCommand(name: string) {
const names = [
'alias',
'cat',
'cd',
'clear',
Expand All @@ -87,7 +88,12 @@ export function isCommand(name: string) {
export function executeCommand(state: StoreState, input: string): StoreState {
if (input === '') return state;

const [name, ...params] = parseInput(input);
let [name, ...params] = parseInput(input);
while (state.wsh.aliases && state.wsh.aliases[name]) {
const [newName, ...newParams] = parseInput(state.wsh.aliases[name]);
name = newName;
params.unshift(...newParams);
}

const command = new Command(state, name, params);
if (name === 'reset') {
Expand Down

0 comments on commit b65d6e4

Please sign in to comment.