Skip to content

Commit e4d5527

Browse files
author
Zack Argyle
committed
Add whoami command
1 parent 5c0f870 commit e4d5527

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ prop | description
1919
`theme` | A string representing which `theme` to use (Terminal.Themes.LIGHT, Terminal.Themes.DARK)
2020
`prefix` | The string used to prefix commands in history: defaults to `hacker@default`
2121

22+
### Currently supported commands and args
23+
command | args/flags | description
24+
------------ | args/flags | -----------
25+
`help` | | lists all available commands
26+
`clear` | | clears history
27+
`ls` | path | lists all file and dirs at path or `cwd`
28+
`cat` | path/file | prints out the contents of a file
29+
`mkdir` | path/dir | makes a new dir at path
30+
`cd` | path | change directory to relative path
31+
`pwd` | | prints out the `cwd`
32+
`echo` | any | prints out all args with env variables
33+
`printenv` | | prints out env variables
34+
`whoami` | | prints out current user's username
35+
2236
### Extending the command list
2337
The `extension` prop is an easy way to extend the bash commands that can be parsed from the terminal input. In essence, each command is a state reducer returning a new terminal state. This provides a lot of flexibility. Each command has access to the `structure`, `history`, and `cwd`, and expects the object returned to be applied in `setState` of the React component. Note that each extension should keep the state immutable, otherwise the component will not update. If we were to extend the commands with and existing command like 'clear, here's how we could do it.
2438

@@ -94,10 +108,7 @@ script | description
94108
>✌⊂(✰‿✰)つ✌
95109
96110
**Some ideas for contributions:**
97-
* Add `echo` command with environment variables?
98111
* Add `grep` command that walks/searches the `structure`
99-
* Add `whoami` command
100-
* Add handles for the three circles at the top left of the terminal
101112
* Add multiline support / text formatting for `cat`
102113

103114
## License

src/commands.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as Util from './util';
22
import { Errors } from './const';
33

4-
const helpCommands = ['clear', 'ls', 'cat', 'mkdir', 'cd', 'pwd', 'echo', 'printenv'];
4+
const helpCommands = ['clear', 'ls', 'cat', 'mkdir', 'cd', 'pwd', 'echo', 'printenv', 'whoami'];
55

66
export const help = {
77
exec: (state) => {
@@ -141,3 +141,12 @@ export const printenv = {
141141
});
142142
},
143143
};
144+
145+
export const whoami = {
146+
exec: (state) => {
147+
const value = state.settings.user.username;
148+
return Object.assign({}, state, {
149+
history: state.history.concat({ value }),
150+
});
151+
},
152+
};

tests/commands.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ describe('bash commands', () => {
300300
const state = stateFactory();
301301

302302
it('should exist', () => {
303-
chai.assert.isFunction(bash.commands.echo.exec);
303+
chai.assert.isFunction(bash.commands.printenv.exec);
304304
});
305305

306306
it('should print out the environment variables', () => {
@@ -310,4 +310,19 @@ describe('bash commands', () => {
310310

311311
});
312312

313+
describe('whoami', () => {
314+
const state = stateFactory();
315+
316+
it('should exist', () => {
317+
chai.assert.isFunction(bash.commands.whoami.exec);
318+
});
319+
320+
it('should print out the environment variables', () => {
321+
const { history } = bash.commands.whoami.exec(state, {});
322+
chai.assert.strictEqual(history.length, 1);
323+
chai.assert.strictEqual(history[0].value, state.settings.user.username);
324+
});
325+
326+
});
327+
313328
});

0 commit comments

Comments
 (0)