Skip to content

Commit f3f829d

Browse files
authored
Merge pull request #8 from webscopeio/feature/history
Feature/history
2 parents bb595af + f555cb8 commit f3f829d

File tree

6 files changed

+381
-1201
lines changed

6 files changed

+381
-1201
lines changed

README.md

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ https://webscopeio.github.io/react-console/
3636
| wrapperClassName | string | className for the wrapper |
3737
| promptClassName | string | className for the prompt |
3838
| inputClassName | string | className for the input |
39+
| history | Array<string> | history array |
40+
| onAddHistoryItem | (entry: string) => void | callback called when a new history entry should be created |
3941

4042
\*_are mandatory_
4143

@@ -46,43 +48,62 @@ import React, { Component } from 'react'
4648

4749
import ReactConsole from 'react-console'
4850

49-
export default class App extends Component {
50-
render () {
51-
return (
52-
<div>
53-
<ReactConsole
54-
autoFocus
55-
welcomeMessage="Welcome"
56-
commands={{
57-
echo: {
58-
description: 'Echo',
59-
fn: (...args) => {
60-
return new Promise((resolve, reject) => {
61-
setTimeout(() => {
62-
resolve(`${args.join(' ')}`)
63-
}, 2000)
64-
})
65-
}
66-
},
67-
test: {
68-
description: 'Test',
69-
fn: (...args) => {
70-
return new Promise((resolve, reject) => {
71-
setTimeout(() => {
72-
resolve('Hello world \n\n hello \n')
73-
}, 2000)
74-
})
75-
}
51+
const App = () => {
52+
const [history, setHistory] = useState([
53+
"echo hello world",
54+
"sleep 1000",
55+
"sleep 2000",
56+
"sleep 3000",
57+
"echo ola",
58+
"not found",
59+
])
60+
61+
return (
62+
<div>
63+
<ReactConsole
64+
autoFocus
65+
welcomeMessage="Welcome"
66+
commands={{
67+
history: {
68+
description: 'History',
69+
fn: () => new Promise(resolve => {
70+
resolve(`${history.join('\r\n')}`)
71+
})
72+
},
73+
echo: {
74+
description: 'Echo',
75+
fn: (...args) => {
76+
return new Promise((resolve, reject) => {
77+
setTimeout(() => {
78+
resolve(`${args.join(' ')}`)
79+
}, 2000)
80+
})
7681
}
77-
}}
78-
/>
79-
</div>
80-
)
81-
}
82+
},
83+
test: {
84+
description: 'Test',
85+
fn: (...args) => {
86+
return new Promise((resolve, reject) => {
87+
setTimeout(() => {
88+
resolve('Hello world \n\n hello \n')
89+
}, 2000)
90+
})
91+
}
92+
}
93+
}}
94+
/>
95+
</div>
96+
)
8297
}
83-
98+
export default App
8499
```
85100

101+
## History implementation
102+
103+
You can provide your own history implementation by providing `onAddHistoryItem` and `history` properties.
104+
If you don't provide `history`, up/down arrows & reverse search won't work.
105+
106+
86107
## License
87108

88-
MIT © [jvorcak](https://github.com/jvorcak)
109+
IT © [jvorcak](https://github.com/jvorcak)

example/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
"private": true,
77
"dependencies": {
88
"prop-types": "^15.6.2",
9-
"react": "^16.4.1",
10-
"react-dom": "^16.4.1",
11-
"react-scripts": "^1.1.4",
12-
"react-console": "link:.."
9+
"react": "16.8.0",
10+
"react-console": "link:..",
11+
"react-dom": "16.8.0",
12+
"react-scripts": "^1.1.4"
1313
},
1414
"scripts": {
1515
"start": "react-scripts start",

example/src/App.js

Lines changed: 88 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,49 @@
1-
import React, { Component } from 'react'
1+
import React, { useState } from 'react'
22

33
import ReactConsole from 'react-console'
44

5-
export default class App extends Component {
6-
render() {
7-
return (
8-
<div>
9-
<ReactConsole
10-
autoFocus
11-
welcomeMessage="This is a <b>welcome</b> message 🎉🎉🎉"
12-
prompt={'$'}
13-
commands={{
14-
echo: {
15-
description: 'Echo',
16-
fn: (...args) => {
17-
return new Promise((resolve, reject) => {
18-
setTimeout(() => {
19-
resolve(`${args.join(' ')}`)
20-
}, 0)
21-
})
22-
}
23-
},
24-
c: {
25-
description: 'crashtest',
26-
fn: () => {
27-
return new Promise(resolve => {
28-
var x = `
5+
const App = () => {
6+
const [history, setHistory] = useState([
7+
"echo hello world",
8+
"sleep 1000",
9+
"sleep 2000",
10+
"sleep 3000",
11+
"echo ola",
12+
"not found",
13+
])
14+
15+
return (
16+
<div>
17+
<ReactConsole
18+
autoFocus
19+
welcomeMessage="This is a <b>welcome</b> message 🎉🎉🎉"
20+
prompt={'$'}
21+
history={history}
22+
onAddHistoryItem={(newEntry) => {
23+
setHistory([...history, newEntry])
24+
}}
25+
commands={{
26+
history: {
27+
description: 'History',
28+
fn: () => new Promise(resolve => {
29+
resolve(`${history.join('\r\n')}`)
30+
})
31+
},
32+
echo: {
33+
description: 'Echo',
34+
fn: (...args) => {
35+
return new Promise((resolve, reject) => {
36+
setTimeout(() => {
37+
resolve(`${args.join(' ')}`)
38+
}, 0)
39+
})
40+
}
41+
},
42+
c: {
43+
description: 'crashtest',
44+
fn: () => {
45+
return new Promise(resolve => {
46+
var x = `
2947
1234567890
3048
1234567890
3149
1234567890
@@ -82,36 +100,50 @@ export default class App extends Component {
82100
k fj asdpf234567890
83101
84102
`
85-
resolve(x)
86-
})
87-
}
88-
},
89-
sleep: {
90-
description: 'sleep',
91-
fn: (timeout) => {
92-
return new Promise((resolve, reject) => {
93-
setTimeout(() => {
94-
resolve('')
95-
}, timeout)
96-
})
97-
}
103+
resolve(x)
104+
})
98105
}
99-
}}
100-
noCommandFound={() => new Promise((resolve, reject) => {
101-
resolve('No command found')
102-
})}
103-
/>
104-
<table>
105-
<tr>
106-
<td><code>echo ...args</code></td>
107-
<td>Echo</td>
108-
</tr>
109-
<tr>
110-
<td><code>sleep `ms`</code></td>
111-
<td>Sleeps for a number of milliseconds</td>
112-
</tr>
113-
</table>
114-
</div>
115-
)
116-
}
106+
},
107+
sleep: {
108+
description: 'sleep',
109+
fn: (timeout) => {
110+
return new Promise((resolve, reject) => {
111+
setTimeout(() => {
112+
resolve('')
113+
}, timeout)
114+
})
115+
}
116+
}
117+
}}
118+
noCommandFound={() => new Promise((resolve, reject) => {
119+
resolve('No command found')
120+
})}
121+
/>
122+
<p>
123+
ctrl+r, ctrl+c shortcuts as well as up/down arrows are supported.
124+
</p>
125+
<table>
126+
<tbody>
127+
<tr>
128+
<td><code>echo ...args</code></td>
129+
<td>Echo</td>
130+
</tr>
131+
<tr>
132+
<td><code>sleep `ms`</code></td>
133+
<td>Sleeps for a number of milliseconds</td>
134+
</tr>
135+
<tr>
136+
<td><code>history</code></td>
137+
<td>Shows a history</td>
138+
</tr>
139+
<tr>
140+
<td><code>clear</code></td>
141+
<td>Clear the terminal screen</td>
142+
</tr>
143+
</tbody>
144+
</table>
145+
</div>
146+
)
117147
}
148+
149+
export default App

0 commit comments

Comments
 (0)