Skip to content

Commit bf4175e

Browse files
committed
(docs) add documentation about echo
1 parent e417da9 commit bf4175e

File tree

1 file changed

+155
-2
lines changed

1 file changed

+155
-2
lines changed

docs/docs/deep-dive/echo.mdx

+155-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,158 @@
11
---
2-
sidebar_position: 1
2+
sidebar_position: 2
33
---
44

5-
# What you can echo?
5+
import CodePen from '@site/src/CodePen';
6+
7+
# What You Can Echo?
8+
9+
`echo` is the most important method of jQuery Terminal. You use it to print stuff on the terminal.
10+
11+
## String and other primitives
12+
13+
You can `echo` strings and basic datatypes that can be converted to string.
14+
15+
This includes:
16+
17+
* Numbers
18+
* Booleans
19+
* Regular Expressions
20+
* Null
21+
22+
## Promises
23+
24+
You can print a promise of the object that can be printed on the terminal. It can be any promise like object,
25+
same as with [async/await](https://javascript.info/async-await). Which means that it can be real promise,
26+
[jQuery Deferred](https://api.jquery.com/jquery.deferred/), or even any object that have then then method.
27+
28+
### ES6 Promise
29+
30+
```javascript
31+
const promise = new Promise(resolve => {
32+
setTimeout(() => resolve('hello'), 100):
33+
});
34+
35+
term.echo(promise);
36+
```
37+
38+
### Promise like object
39+
40+
```javascript
41+
const promise = {
42+
then(fn) {
43+
fn('hello');
44+
return promise;
45+
}
46+
};
47+
48+
term.echo(promise);
49+
```
50+
51+
This object will work the same with async/await:
52+
53+
```javascript
54+
(async () => {
55+
term.echo(await promise);
56+
})();
57+
```
58+
59+
## Array
60+
61+
When you echo an array it will print it in columns.
62+
63+
```javascript
64+
const array = [
65+
"Lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit", "Huic",
66+
"mori", "optimum", "esse", "propter", "desperationem", "sapientiae", "illi",
67+
"propter", "spem", "vivere", "Duo", "Reges", "constructio", "interrete"
68+
];
69+
70+
term.echo(array);
71+
```
72+
73+
<CodePen id="OPLpOLM" title="Echo Array" height={400} />
74+
75+
## Function
76+
77+
You can echo function that will be called on each render, that happen when you resize the
78+
terminal. With function you can create respnsive text that addapt to the size of the terminal. This
79+
is how default Greetings is created. It shows different [ASCII
80+
Art](https://en.wikipedia.org/wiki/ASCII_art) based on the size of the terminal.
81+
82+
With function, you can print dynamic text using [Figlet library](https://github.com/patorjk/figlet.js),
83+
that renders ASCII text using different fonts.
84+
85+
```javascript
86+
term.echo(function() {
87+
const cols = this.cols();
88+
if (cols > 100) {
89+
return 'Large ASCII';
90+
} else if (cols > 50) {
91+
return 'Medium ASCII';
92+
} else (cols > 20) {
93+
return 'Small ASCII'
94+
}
95+
});
96+
97+
```
98+
99+
### Using figlet
100+
101+
First you need to include the figlet library:
102+
103+
```html
104+
<script src="https://cdn.jsdelivr.net/npm/figlet/lib/figlet.js"></script>
105+
```
106+
107+
And jQuery Terminal figlet helper:
108+
109+
```html
110+
<script src="https://cdn.jsdelivr.net/gh/jcubic/jquery.terminal@devel/js/figlet.js"></script>
111+
```
112+
113+
Then you can create a helper function that will display
114+
115+
```javascript
116+
const term = $('body').terminal({}, {
117+
greetings: false
118+
});
119+
120+
(async () => {
121+
await $.terminal.figlet.load(['Slant']);
122+
term.echo($.terminal.figlet('Slant', 'jQuery Terminal'));
123+
})();
124+
```
125+
126+
`$.terminal.figlet.load` async function load all specified fonts, you can see the list of available
127+
fonts, on [figlet demo website](https://patorjk.com/software/taag/) (type some text and click test
128+
all to see all fonts).
129+
130+
`$.terminal.figlet` return a function that renders the text using figlet, because of this you have
131+
responsive text. See below demo how it works, you can resize the window, to see how the ASCII Art
132+
change.
133+
134+
<CodePen id="xbKqpZG" title="Terminal Figlet" />
135+
136+
## renderHandler
137+
138+
When you have renderHandler you can print basciall anything, any kind of objects. Even [React
139+
components](#???). Below code use renderHandler to print object literals that by default are
140+
converted to string and display `[object Object]`.
141+
142+
```javascript
143+
const term = $('body').terminal(/* your interpreter */, {
144+
renderHandler(value) {
145+
if ($.isPlainObject(value)) {
146+
this.echo(JSON.stringify(value, null, 2));
147+
return false;
148+
}
149+
}
150+
});
151+
152+
term.echo({
153+
name: 'James Bond',
154+
code: '007'
155+
});
156+
```
157+
158+
<CodePen id="ByBWJRV" title="Echo Object" height={340} />

0 commit comments

Comments
 (0)