forked from sindresorhus/emoj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·95 lines (76 loc) · 2.13 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env node
'use strict';
const dns = require('dns');
const readline = require('readline');
const meow = require('meow');
const logUpdate = require('log-update');
const chalk = require('chalk');
const debounce = require('lodash.debounce');
const hasAnsi = require('has-ansi');
const mem = require('mem');
const emoj = require('./');
// limit it to 7 results so not to overwhelm the user
// this also reduces the chance of showing unrelated emojis
const fetch = mem(str => emoj(str).then(arr => arr.slice(0, 7).join(' ')));
const debouncer = debounce(cb => cb(), 200);
const cli = meow(`
Usage
$ emoj [text]
Example
$ emoj 'i love unicorns'
🦄 🎠 🐴 🐎 ❤ ✨ 🌈
Run it without arguments to enter the live search
`);
if (cli.input.length > 0) {
fetch(cli.input[0]).then(console.log);
return;
}
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
const pre = `\n${chalk.bold.cyan('›')} `;
const query = [];
let prevResult = '';
dns.lookup('emoji.getdango.com', err => {
if (err && err.code === 'ENOTFOUND') {
logUpdate(`\n${chalk.bold.red('› ')}${chalk.dim('Please check your internet connection')}\n\n`);
process.exit(1);
} else {
logUpdate(`${pre}${chalk.dim('Relevant emojis will appear when you start writing')}\n\n`);
}
});
process.stdin.on('keypress', (ch, key) => {
key = key || {};
if (hasAnsi(key.sequence)) {
return;
}
if (key.name === 'escape' || (key.ctrl && key.name === 'c')) {
if (query.length <= 1) {
logUpdate();
readline.moveCursor(process.stdout, 0, -1);
}
process.exit();
}
if (key.name === 'backspace') {
query.pop();
} else if (key.name === 'return' || (key.ctrl && key.name === 'u')) {
query.length = 0;
} else {
query.push(ch);
}
const queryStr = query.join('');
logUpdate(`${pre}${chalk.bold(queryStr)}\n${prevResult}\n`);
if (query.length <= 1) {
prevResult = '';
logUpdate(`${pre}${chalk.bold(queryStr)}\n\n`);
return;
}
debouncer(() => {
fetch(queryStr).then(emojis => {
if (query.length <= 1) {
return;
}
prevResult = emojis;
logUpdate(`${pre}${chalk.bold(query.join(''))}\n${emojis}\n`);
});
});
});