-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy paththeme.rb
More file actions
executable file
·221 lines (172 loc) · 5.83 KB
/
theme.rb
File metadata and controls
executable file
·221 lines (172 loc) · 5.83 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
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
#!/usr/bin/env ruby
# frozen_string_literal: true
# See README.md for purpose and usage.
require 'json'
require 'pathname'
require 'open-uri'
SCRIPT_DIR = Pathname.new(__FILE__).dirname
DOTFILES = SCRIPT_DIR
CONFIG = DOTFILES / 'config'
ZSH_CONFIG = CONFIG / 'zsh'
THEME_DIR = SCRIPT_DIR / 'theme'
TEMPLATE_DIR = THEME_DIR / 'template'
def usage
puts <<~USAGE
Usage: #{$PROGRAM_NAME} <theme-file.json>
Generate all themed config files from a theme definition.
Arguments:
theme-file.json - Theme definition file (e.g., theme/colors-tokyonight.json)
Generates:
- theme/template/current-theme.omp.json (Oh My Posh theme)
- theme/template/current-syntax-highlighting.zsh (ZSH syntax highlighting)
- config/current-theme.lua (Lua palette for nvim/wezterm)
- config/current-theme-env.zsh (Environment variables)
- ~/.config/eza/theme.yml (eza theme symlink, if eza-themes repo exists)
- ~/.config/ghostty/theme (ghostty theme)
Example:
USAGE
if THEME_DIR.exist?
theme_files = Dir[THEME_DIR / 'colors-*.json'].sort
if theme_files.any?
puts
puts 'Available themes:'
theme_files.each do |f|
puts " - #{f}"
end
end
end
exit 1
end
def generate_omp_theme(colors)
template_file = TEMPLATE_DIR / 'omp-template.json'
output_file = ZSH_CONFIG / 'current-theme.omp.json'
template = JSON.parse(File.read(template_file))
template['palette'].merge!(colors)
File.write(output_file, JSON.pretty_generate(template))
puts "- generated #{output_file}"
end
def generate_zsh_syntax(colors)
template_file = TEMPLATE_DIR / 'zsh-syntax-template.zsh'
output_file = ZSH_CONFIG / 'current-syntax-highlighting.zsh'
template = File.read(template_file)
colors.each do |name, value|
template.gsub!("{{#{name}}}", value)
end
unreplaced = template.scan(/\{\{(\w+)\}\}/).flatten.uniq
if unreplaced.any?
puts "Warning: Unreplaced placeholders in ZSH syntax: #{unreplaced.join(', ')}"
end
File.write(output_file, template)
puts "- generated #{output_file}"
end
def generate_lua_theme(colors, theme_names, theme_file)
output_file = CONFIG / 'current-theme.lua'
colors_lua = colors.map { |k, v| " #{k} = \"#{v}\"" }.join(",\n")
lua_content = <<~LUA
-- #{theme_file}
local M = {}
-- Color palette
M.colors = {
#{colors_lua}
}
-- Theme names for different tools
M.wezterm = "#{theme_names['wezterm']}"
M.nvim = "#{theme_names['nvim']}"
M.lualine = "#{theme_names['lualine']}"
M.vivid = "#{theme_names['vivid']}"
return M
LUA
File.write(output_file, lua_content)
puts "- generated #{output_file}"
end
def generate_zsh_env(theme_names)
output_file = CONFIG / 'current-theme-env.zsh'
# Convert simple color numbers to escape codes if needed
search_bg = theme_names['less_search_bg']
search_fg = theme_names['less_search_fg']
# If it's just a number, convert to 256-color escape code
search_bg = "\\e[48;5;#{search_bg}m" if search_bg =~ /^\d+$/
search_fg = "\\e[38;5;#{search_fg}m" if search_fg =~ /^\d+$/
zsh_content = <<~ZSH
export VIVID_THEME="#{theme_names['vivid']}"
export EZA_THEME="#{theme_names['eza']}"
export BAT_THEME="#{theme_names['bat']}"
# LESS_TERMCAP for search highlighting in pagers
export LESS_TERMCAP_so=$'#{search_bg}#{search_fg}'
export LESS_TERMCAP_se=$'\\e[0m'
ZSH
File.write(output_file, zsh_content)
puts "- generated #{output_file}"
end
def generate_eza_theme(theme_name)
theme_file = CONFIG / 'eza/theme.yml'
uri = "https://raw.githubusercontent.com/eza-community/eza-themes/refs/heads/main/themes/#{theme_name}.yml"
begin
File.open(theme_file, 'w') do |local|
local.write("# #{uri}\n\n")
local.write(URI.open(uri).read)
end
puts "- generated #{theme_file}"
rescue OpenURI::HTTPError => e
puts "HTTP error fetching eza theme #{uri}: #{e}"
rescue StandardError => e
puts "Error fetching eza theme: #{e}"
end
end
def generate_ghostty_theme(theme_names)
theme_file = CONFIG / 'ghostty/theme'
begin
File.open(theme_file, 'w') do |f|
f.puts "theme = #{theme_names['ghostty']}"
end
puts "- generated #{theme_file}"
rescue e
puts "Error generating ghostty theme: #{e}"
end
end
def generate_kitty_theme(colors, kitty_colors)
template_file = TEMPLATE_DIR / 'kitty-template.conf'
output_file = CONFIG / 'kitty/current-theme.conf'
template = File.read(template_file)
# Replace color placeholders from the main colors palette
colors.each do |name, value|
template.gsub!("{{#{name}}}", value)
end
# Replace kitty-specific colors
kitty_colors.each do |name, value|
template.gsub!("{{#{name}}}", value)
end
unreplaced = template.scan(/\{\{(\w+)\}\}/).flatten.uniq
if unreplaced.any?
puts "Warning: Unreplaced placeholders in kitty theme: #{unreplaced.join(', ')}"
end
File.write(output_file, template)
puts "- generated #{output_file}"
end
def main
usage if ARGV.length != 1
theme_file = Pathname.new(ARGV[0])
unless theme_file.exist?
theme_file = Pathname.new(THEME_DIR / "#{ARGV[0]}.json")
unless theme_file.exist?
puts "Error: Theme file not found: #{theme_file}"
exit 1
end
end
theme_data = JSON.parse(File.read(theme_file))
unless theme_data['colors']
puts "Error: Theme file must have a 'colors' field"
exit 1
end
colors = theme_data['colors']
kitty_colors = theme_data['kitty'] || {}
theme_names = theme_data.reject { |k, _| k == 'colors' || k == 'kitty' }
generate_omp_theme(colors)
generate_zsh_syntax(colors)
generate_lua_theme(colors, theme_names, theme_file)
generate_zsh_env(theme_names)
generate_eza_theme(theme_names['eza'])
generate_ghostty_theme(theme_names['eza'])
generate_kitty_theme(colors, kitty_colors)
end
main