forked from zsh-users/zsh-apple-touchbar
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate.js
157 lines (133 loc) · 3.52 KB
/
generate.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import YAML from "yaml";
import fs from "fs/promises";
import crypto from "crypto";
import os from "os";
const userInfo = os.userInfo();
const uid = userInfo.uid;
const colorMap = {
red: "1",
green: "2",
yellow: "3",
blue: "4",
magenta: "5",
cyan: "6",
rose: "9",
chartreuse: "10",
orange: "11",
azure: "12",
violet: "13",
springgreen: "14",
};
function getRandomColor() {
const keys = Object.values(colorMap);
return keys[Math.floor(Math.random() * keys.length)];
}
const md5 = (str) => crypto.createHash("md5").update(str).digest("hex");
const generateCacheFile = async () => {
const yaml = await fs.readFile(
`${process.env.HOME}/.tmux-keys.yaml`,
"utf-8"
);
const config = YAML.parse(yaml);
const viewsFunctions = Object.entries(config.views).map(([view, value]) => {
const prepareKey = (key) => (Array.isArray(value) ? +key + 1 : +key);
let subfn = `
${md5(view)}_view() {
unbind_keys
${Object.entries(value)
.map(([key, action]) => {
const color = action.color ? colorMap[action.color] : getRandomColor();
const key_string = `
create_key ${prepareKey(key)} "${
action.title_exec
? `$(${action.title_exec})`
: action.title
? action.title
: action.action
}" ${
action.type === "view"
? `'${md5(action.action)}_view' 'view'`
: action.type === "exec"
? `'${action.action}' 'exec'`
: action.type === "insert"
? `'${action.action}' 'insert'`
: action.type === "tmux"
? `'${action.action}' 'tmux'`
: `'${action.action}' 'popup'`
} ${color} ${
action.sh === false || action.type === "view" ? "false" : "true"
}
left_status+="#[bg=colour8,fg=colour15,bold] ${prepareKey(
key
)} #[bg=colour${color},fg=colour0,bold] ${
action.title_exec
? `$(${action.title_exec})`
: action.title
? action.title
: action.action
} #[fg=default,bg=default]"
`;
return key_string;
})
.join(` left_status+=' '`)}
set_status
}
`;
return subfn;
});
let script = `
left_status=''
function unbind_keys() {
tmux unbind-key -n F1
tmux unbind-key -n F2
tmux unbind-key -n F3
tmux unbind-key -n F4
tmux unbind-key -n F5
tmux unbind-key -n F6
tmux unbind-key -n F7
tmux unbind-key -n F8
tmux unbind-key -n F9
tmux unbind-key -n F10
tmux unbind-key -n F11
tmux unbind-key -n F12
}
function create_key() {
display_message=''
if [ "$6" = 'true' ]; then
display_message="display -d 200 '#[fill=colour0 bg=colour\${5} align=centre] \${2} '"
fi
tmux_action=''
if [ "$4" = "view" ]; then
tmux_action="run-shell 'zsh \${TMPDIR:-/tmp}/zsh-\${UID}/tmux-keys.zsh $3'"
elif [ "$4" = "insert" ]; then
tmux_action="send-keys $keys[$1] '$3'"
elif [ "$4" = "exec" ]; then
tmux_action="send-keys $keys[$1] '$3\n'"
elif [ "$4" = "popup" ]; then
tmux_action="display-popup -w '80%' -h '80%' $3"
elif [ "$4" = "tmux" ]; then
tmux_action="$3"
fi
if [ "$display_message" = "" ]; then
tmux bind-key -n F\${1} "$tmux_action"
else
tmux bind-key -n F\${1} "$display_message ; $tmux_action"
fi
}
function set_status() {
tmux set -g status-right "$left_status"
}
${viewsFunctions.join("")}
case $1 in
${Object.entries(config.views)
.map(([view, val]) => `\t\t${md5(view)}_view) ${md5(view)}_view ;;`)
.join("\n")}
*) ${md5(config.default_view)}_view
esac
`;
fs.writeFile(
`${process.env.TMPDIR || "/tmp"}/zsh-${uid}/tmux-keys.zsh`,
script
);
};
generateCacheFile();