-
Notifications
You must be signed in to change notification settings - Fork 22
/
generate.rb
77 lines (62 loc) · 1.85 KB
/
generate.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
require 'yaml'
class Parser
def self.call(*args)
new(*args).call
end
def initialize(file_path = 'config.yml')
@file_path = file_path
end
def call
[].tap do |code|
code << 'source ${0:A:h}/functions.zsh'
code << "set_state '#{config['default_view']}'"
code << views_functions
code << define_views_functions
code << main_function
code << 'autoload -Uz add-zsh-hook'
code << "add-zsh-hook precmd precmd_apple_touchbar\n"
end.join("\n\n")
end
private
attr_reader :file_path
def create_key_pattern(key, value)
command = value['view'] ? "#{value['view']}_view" : value['command']
"\tcreate_key #{key} '#{value['text']}' '#{command}'".tap do |code|
code << " '-s'" if value['command']
end if (1..12).include?(key)
end
def function_pattern(view, commands, back = nil)
[].tap do |code|
code << "function #{view}_view() {"
code << "\tremove_and_unbind_keys\n"
code << "\tset_state '#{view}'\n"
code << commands.join("\n")
code << "\n\tset_state '#{back}'" if back
code << "}"
end.join("\n")
end
def views_functions
config['views'].map do |view, keys|
commands = keys.map { |k, v| create_key_pattern(k, v) }.compact
function_pattern(view, commands, keys['back'])
end.join("\n\n")
end
def define_views_functions
config['views'].keys.map do |view|
"zle -N #{view}_view"
end.join("\n")
end
def main_function
[].tap do |code|
code << 'precmd_apple_touchbar() {'
code << "\tcase $state in"
code << config['views'].keys.map { |view| "\t\t#{view}) #{view}_view ;;"}.join("\n")
code << "\tesac"
code << '}'
end.join("\n")
end
def config
@config ||= YAML.load(File.read(file_path))
end
end
File.open('zsh-apple-touchbar.zsh', 'w') {|f| f.write(Parser.call) }