Repository navigation
Expand file tree
/
Copy pathgen-res
More file actions
executable file
·140 lines (118 loc) · 3.21 KB
/
Copy pathgen-res
File metadata and controls
executable file
·140 lines (118 loc) · 3.21 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
#!/usr/bin/env lua
-- vim: sw=2
-- A small script that outputs xresources based on some configurations. I made
-- this because managing a raw xresources file is very annoying - the C
-- preprocessor is annoying af.
local function tableSize(t)
local x = 0
for _, _ in pairs(t) do
x = x + 1
end
return x
end
local function tablePrintAll(t, fd)
fd = fd or io.stderr
for k, v in pairs(t) do
fd:write(k, " => ", v, "\n")
end
end
local function fileExists(name)
local f = io.open(name, "r")
if f ~= nil then
f:close()
return true
else
return false
end
end
local theme = {}
do
local theme_proc = assert(io.popen("load-base16-theme", "r"))
for line in theme_proc:lines() do
if line:match("^%s*$") then
-- skip
elseif line:match('^base0[0-9A-F]="#' .. string.rep("[0-9a-fA-F]", 6) .. '"$') then
local name = line:gsub('^(base0[0-9A-F]).*$', "%1")
local value = line:gsub(('^base0[0-9A-F]="(#' .. string.rep("[0-9a-fA-F]", 6) .. ')"$'), "%1")
theme[name] = value
else
io.stderr:write("Failed to parse (load-base16-theme) line: ", string.format("%q", line), "\n")
os.exit(1)
end
end
theme_proc:close()
end
if tableSize(theme) ~= 16 then
io.stderr:write("Failed to get all 16 colors. Table contents:\n")
tablePrintAll(theme, io.stderr)
os.exit(1)
end
local DOTFILES = os.getenv("DOTFILES")
if DOTFILES == nil then
io.stderr:write("Failed to get DOTFILES env var.\n")
os.exit(1)
end
local resources_file = DOTFILES .. "/config/dots/resources.lua"
if not fileExists(resources_file) then
io.stderr:write("File ", string.format("%q", resources_file), " doesn't seem to exist. The configuration should be there.\n")
os.exit(1)
end
local _meta = {}
_G._meta = _meta
-- Make the current base16 theme accessible
_meta.theme = theme
-- The different available targets. This table store functions which are used as identifiers and also as converters.
_meta.targets = {}
_meta.targets.xresources = function(stream, key, val)
stream:write(key, ": ", val, "\n")
end
_meta.targets.dotcfg = function(stream, key, val)
stream:write("set:", key, ":", val, "\n")
end
local current_target
local dump_fonts = false
if arg[1] == "xresources" then
current_target = _meta.targets.xresources
elseif arg[1] == "dotcfg" then
current_target = _meta.targets.dotcfg
elseif arg[1] == "dump-fonts" then
current_target = nil
dump_fonts = true
else
io.stderr:write("Bad argument #1 (expected { xresources | dotcfg | dump-fonts })\n")
os.exit(1)
end
local containedIn = function(haystack, needle)
for _, x in ipairs(haystack) do
if x == needle then
return true
end
end
return false
end
local declarations = {}
local fonts = {}
_meta.decl = function(args)
local targets = assert(args.targets, "`targets` field not specified")
if not containedIn(targets, current_target) then
return
end
for _, e in ipairs(args) do
declarations[e[1]] = e[2]
end
end
_meta.declFonts = function(fonts_)
fonts = fonts_
end
assert(loadfile(resources_file))()
if current_target ~= nil then
for k, v in pairs(declarations) do
current_target(io.stdout, k, v)
end
elseif dump_fonts then
for k, _ in pairs(fonts) do
print(k)
end
else
error("???")
end