-
Notifications
You must be signed in to change notification settings - Fork 99
/
cheatset_keybindings.rb
executable file
·276 lines (231 loc) · 6.71 KB
/
cheatset_keybindings.rb
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/ruby
# encoding: utf-8
require 'shellwords'
require 'fileutils'
require 'optparse'
def class_exists?(class_name)
klass = Module.const_get(class_name)
klass.is_a?(Class)
rescue NameError
false
end
if class_exists? 'Encoding'
Encoding.default_external = Encoding::UTF_8 if Encoding.respond_to?('default_external')
Encoding.default_internal = Encoding::UTF_8 if Encoding.respond_to?('default_internal')
end
class ::String
def esc
gsub(/'/, '\\\\\'').gsub(/ \(new\)/i, '').gsub(/(#+)/, '`\1`').gsub(/\\(?=[\[!])/, '').strip
end
def create_command
gsub(/ +/, ' ').esc
end
end
##
## Generate a cheatset input file
##
class DashGenerator
attr_writer :modifier_format
def initialize(options)
@options = options
infile = @options[:bindings_file]
begin
@input = IO.read(infile).force_encoding('utf-8')
rescue
@input = IO.read(infile)
end
@bindings = {
id: 'General Commands',
bindings: [],
groups: []
}
end
def parse
level = 0
prefix = false
group_command = ''
group_desc = ''
subgroup_command = ''
subgroup_desc = ''
desc = ''
command = ''
note = ''
group = nil
@input.split("\n").each do |line|
next if line =~ /^\s*$/ || line =~ %r{^\s*//\s*(TODO)}
case line
when %r{^\s*//\s*>\s*(.*)$}
note += Regexp.last_match(1)
when /^\s*\};\s*$/
level -= 1
case level
when 1
subgroup_command = ''
subgroup_desc = ''
when 0
group_command = ''
group_desc = ''
end
when %r{^\s*//\s*(.*)}
desc = Regexp.last_match(1)
when %r{^\s*"([^"]+)"\s*=\s*\{.*?//\s*(.*)}
unless group.nil?
@bindings[:groups].push(group)
group = nil
end
m = Regexp.last_match
level += 1
case level
when 1
group_command = translate_command(m[1])
group_desc = m[2]
group = {
id: group_desc,
command: group_command,
bindings: []
}
when 2
subgroup_command = translate_command(m[1])
subgroup_desc = m[2]
group = {
id: subgroup_desc,
command: subgroup_command,
bindings: []
}
else
prefix = m[1]
end
when /^\s*"([^"]+)"\s*=\s*\(/
command = translate_command(Regexp.last_match(1))
case level
when 0
entry = {
command: "#{subgroup_command} #{command}",
name: desc,
note: note
}
@bindings[:bindings].push(entry)
else
command = "#{prefix} #{command}" if prefix
entry = {
command: "#{group_command} #{subgroup_command} #{command}",
name: desc,
note: note
}
group[:bindings].push(entry)
end
note = ''
end
end
end
def translate_command(str)
case @options[:format]
when :symbol
translate_command_symbol(str)
else
translate_command_name(str)
end
end
def translate_command_name(str)
str = str.gsub(/~/, 'OPT+').gsub(/@/, 'CMD+').gsub(/\$/, 'SHIFT+').gsub(/\^/, 'CTRL+')
str = str.gsub('\UF700', 'UP').gsub('\UF701', 'DOWN').gsub('\UF703', 'RIGHT').gsub('\UF702', 'LEFT')
str = str.gsub('\U0009', 'TAB').gsub('\U000D', 'RETURN').gsub('\U001B', 'ESC').gsub('\U000A', 'ENTER')
str = str.gsub('\UF728', 'DEL').gsub('\177', 'BACKSPACE')
str.gsub('\040', 'SPACE').gsub(/(?<=\+)([A-Z])$/, 'SHIFT+\\1').gsub(/([a-z])$/, &:upcase)
end
def translate_command_symbol(str)
str = str.gsub(/~/, '⌥').gsub(/@/, '⌘').gsub(/\$/, '⇧').gsub(/\^/, '⌃')
str = str.gsub('\UF700', '↑').gsub('\UF701', '↓').gsub('\UF703', '→').gsub('\UF702', '←')
str = str.gsub('\U0009', '⇥').gsub('\U000D', '↩').gsub('\U001B', '⎋').gsub('\U000A', '␍')
str = str.gsub('\UF728', '⌦').gsub('\177', '⌫').gsub('\040', '␣')
str.gsub(/([\[\]|])/, '\\\\\1').gsub(/([A-Z])/, '⇧\\1').downcase
end
def create_entry(bnd)
out = <<EOENTRY
entry do
command '#{bnd[:command].create_command}'
name '#{bnd[:name].esc}'
end
EOENTRY
out
end
def create_category(grp)
out = <<EOCAT
category do
id '#{grp[:id].esc}'
EOCAT
grp[:bindings].each { |bnd| out += create_entry(bnd) }
out += <<EOCAT
end
EOCAT
out
end
def create_cheatsheet
out = <<EOCHEAT
cheatsheet do
title '#{@options[:title]}'
docset_file_name '#{@options[:title].gsub(/ +/, '')}'
keyword 'kb'
# resources 'resources_dir'
introduction '#{@options[:description]}'
EOCHEAT
out += create_category(@bindings)
@bindings[:groups].each { |grp| out += create_category(grp) }
out += <<EOCHEAT
end
EOCHEAT
out
end
def render
parse
target = File.expand_path(@options[:path])
File.open(target, 'w') { |f| f.puts create_cheatsheet }
if @options[:save]
$stdout.puts %(Template saved. Generate a docset with `cheatset generate "#{target}"`)
else
docset = File.expand_path("#{@options[:title].gsub(/ +/, '')}.docset")
`cheatset generate "#{target}"`
FileUtils.rm(target)
$stdout.puts %(Docset generated: #{docset})
`open "#{docset}"` if @options[:open]
end
end
end
options = {
format: :name,
save: false,
path: File.expand_path('keybindings-dash.rb'),
open: false,
title: 'Keybindings',
description: 'Brett\\\'s Key Bindings',
bindings_file: File.expand_path('~/Library/KeyBindings/DefaultKeyBinding.dict')
}
opt_parser = OptionParser.new do |opt|
opt.banner = %(Usage: #{File.basename(__FILE__)} [options] [DefaultKeyBinding.dict]
If dict file is not specified, ~/Library/KeyBindings/DefaultKeyBinding.dict will be used)
opt.separator ''
opt.separator 'Options:'
opt.on('-f', '--format=FORMAT', 'Format for modifier keys: "symbol" or "name" (default)') do |fmt|
options[:format] = fmt =~ /^s/i ? :symbol : :name
end
opt.on('--save=FILE', 'Save cheatset template to file (don\'t generate docset automatically)') do |path|
options[:save] = true
options[:path] = File.expand_path(path)
end
opt.on('-d', '--description=FILE', 'Description of cheat sheet') do |desc|
options[:description] = desc
end
opt.on('-t', '--title=TITLE', 'Title of cheat sheet') do |title|
options[:title] = title
end
opt.on('-o', '--open', 'Open the docset in Dash after generating') do
options[:open] = true
end
opt.on('-h', '--help', 'Display help') do
puts opt_parser
exit
end
end
opt_parser.parse!
options[:bindings_file] = File.expand_path(ARGV[0]) if ARGV.count.positive?
DashGenerator.new(options).render