-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
77 lines (61 loc) · 1.78 KB
/
cli.js
File metadata and controls
77 lines (61 loc) · 1.78 KB
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
#!/usr/bin/env node
'use strict'
const chalk = require('chalk')
const meow = require('meow')
const readline = require('readline')
const logUpdate = require('log-update')
const clipboardy = require('clipboardy')
const keycode = require('keycode')
const library = require('./package').name
const prompt = '›'
const prefixInput = `${chalk.bold.cyan(prompt)} `
const prefixOutput = '⌨ '
const notFoundMessage = chalk.red('Keycode not found')
const copiedMessage = chalk.italic.yellow('Copied to clipboard')
const exitMessage = chalk.dim('^C to exit')
const cli = meow(`
Usage
$ ${library}
Example
$ ${library}
${prefixInput}$
${prefixOutput}36
$ ${library} '#'
${prefixOutput}35
Options
--copy -c Copy the keycode to the clipboard
`, {
boolean: [
'copy'
],
alias: {
c: 'copy'
}
})
if (cli.input.length > 0) {
const keyname = cli.input[0]
const code = keycode(keyname)
let output = code ? chalk.bold.green(code) : notFoundMessage
if (cli.flags.copy && code) {
clipboardy.writeSync(code.toString())
output += `\n${copiedMessage}`
}
logUpdate(`${prefixInput}${keyname}\n${prefixOutput}${output}`)
process.exit()
}
readline.emitKeypressEvents(process.stdin)
process.stdin.setRawMode(true)
logUpdate(`${prefixInput}${chalk.dim('Keycodes will appear when you start typing')}\n\n${exitMessage}\n`)
process.stdin.on('keypress', (ch, key) => {
if (key.ctrl && key.name === 'c') {
process.exit()
}
const keyname = key.name || key.sequence
const code = keycode(keyname)
let output = code ? chalk.bold.green(code) : notFoundMessage
if (cli.flags.copy && code) {
clipboardy.write(code.toString())
output += `\n${copiedMessage}`
}
logUpdate(`${prefixInput}${keyname}\n${prefixOutput}${output}\n${exitMessage}`)
})