Copy Special Character
Find a special character, then copy the character or its hex value
This command illustrates an interactive List, as well as using Tab to navigate to another location within the command.
In this case, hitting Enter on an Option will copy the special character, while hitting Tab will show another view letting someone choose to copy the hex value instead.
1
/*
2
* A command that shows special characters. Hitting Enter
3
* will copy character, hitting Tab will give other ways
4
* to copy the character: HTML hex code, for example.
5
*/
6
7
/**
8
* Gets arguments passed in to script, like: --character=control
9
* and stores it into an array, like: args['character'] = "control".
10
*/
11
const args = process.argv.slice(2).reduce((agg, arg) => {
12
const match = arg.match(/^--(?<key>\w+)=(?<value>.+)$/);
13
return match ? { ...agg, [match.groups.key]: match.groups.value } : agg;
14
}, {});
15
16
let response;
17
if (args["character"]) {
18
response = showOptionsForCharacter(args["character"]);
19
} else {
20
response = showAllCharacters();
21
}
22
console.log(JSON.stringify(response));
23
24
/*
25
* The view that is shown when someone runs the command.
26
*/
27
function showAllCharacters() {
28
const response = {
29
view: {
30
type: "list",
31
options: [
32
{
33
title: "Command",
34
action: {
35
type: "copy",
36
value: "⌘",
37
},
38
moveAction: {
39
type: "add-param",
40
name: "character",
41
value: "command",
42
},
43
},
44
{
45
title: "Option",
46
action: {
47
type: "copy",
48
value: "⌥",
49
},
50
moveAction: {
51
type: "add-param",
52
name: "character",
53
value: "option",
54
},
55
},
56
{
57
title: "Control",
58
action: {
59
type: "copy",
60
value: "️️⌃",
61
},
62
moveAction: {
63
type: "add-param",
64
name: "character",
65
value: "control",
66
},
67
},
68
],
69
},
70
};
71
return response;
72
}
73
74
/**
75
* The view that is shown when someone Tabs on a character.
76
*/
77
function showOptionsForCharacter(character) {
78
const charactersToHex = {
79
command: "&#8984;",
80
option: "&#8997;",
81
control: "&#8963;",
82
};
83
return {
84
tokens: [
85
{
86
paramName: "character",
87
label: character,
88
icon: "🎹",
89
},
90
],
91
view: {
92
type: "list",
93
options: [
94
{
95
title: "Copy Hex",
96
action: {
97
type: "copy",
98
value: charactersToHex[character],
99
},
100
},
101
],
102
},
103
};
104
}
Copied!
Last modified 6d ago
Copy link