Skip to content

Commit bcd4d71

Browse files
committed
lua: auto-generate configuration when none exists
1 parent 68ff84c commit bcd4d71

File tree

2 files changed

+94
-10
lines changed

2 files changed

+94
-10
lines changed

doc/00_setup.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ you compiled earlier.
6767

6868
## Configuration
6969

70-
waywall will not start up without a configuration file. It will search for one
71-
in `$XDG_CONFIG_HOME/waywall` (typically `~/.config/waywall/`). You can create
72-
a file with the following contents at `~/.config/waywall/init.lua` as a starting
73-
point:
70+
waywall follows the [XDG Base Directory] specification and will search for a
71+
configuration file in `$XDG_CONFIG_HOME/waywall` (usually `~/.config/waywall`).
72+
If no configuration file exists, one will be automatically generated for you.
73+
You can use the following configuration as a starting point:
7474

7575
```lua
7676
local waywall = require("waywall")

waywall/lua/init.lua

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,36 @@
55
load their configuration.
66
]]
77

8+
local autogen_notice = [[
9+
Your configuration file is autogenerated!
10+
11+
You can remove this notice by opening it in a text
12+
editor and following the instructions in the file.
13+
]]
14+
15+
local default_config = [[
16+
-- This configuration file was auto-generated by waywall. Refer to the
17+
-- documentation for more information on how to configure waywall!
18+
--
19+
-- To remove the auto-generated notice, delete the 'config.autogen = true'
20+
-- line at the bottom of the file.
21+
22+
local waywall = require("waywall")
23+
local helpers = require("waywall.helpers")
24+
25+
local config = {
26+
theme = {
27+
background = "#303030ff",
28+
},
29+
}
30+
31+
config.actions = {}
32+
33+
config.autogen = true -- Delete me to remove the auto-generated notice
34+
35+
return config
36+
]]
37+
838
--[[
939
Setup the environment for the user's configuration.
1040
]]
@@ -102,13 +132,67 @@ for _, dir in ipairs(config_dirs) do
102132
package.path = package.path .. ";" .. dir .. "/waywall/?.lua"
103133
end
104134

105-
-- Run the user's configuration file.
106-
local user_config = require(priv.profile() or "init")
135+
-- Run the user's configuration file. If it doesn't exist, then create a default
136+
-- one for them.
137+
local profile = priv.profile() or "init"
138+
139+
local function load_config(autogenerate)
140+
local ok, result = pcall(require, profile)
141+
142+
if ok then
143+
if type(result) == "table" then
144+
print("loaded configuration from profile '" .. profile .. "'")
145+
return result
146+
end
147+
148+
error("expected table from user configuration, got " .. type(result))
149+
else
150+
if result:find("not found:") then
151+
if not autogenerate then
152+
error(result)
153+
end
154+
155+
print(
156+
"failed to find configuration file (profile = '"
157+
.. profile
158+
.. "', package.path = '"
159+
.. package.path
160+
.. "')"
161+
)
162+
163+
local path = assert(config_dirs[1], "no config dirs")
164+
.. "/waywall/"
165+
.. profile
166+
.. ".lua"
167+
local file, err = io.open(path, "a+")
168+
if err then
169+
error("failed to auto-generate a configuration file: " .. err)
170+
end
171+
file:write(default_config)
172+
file:close()
173+
174+
print("auto-generated configuration file at " .. path)
175+
176+
-- Try loading the new auto-generated configuration. Do not try to
177+
-- generate a new one if somehow loading the configuration still
178+
-- fails.
179+
return load_config(false)
180+
else
181+
error(result)
182+
end
183+
end
184+
end
185+
186+
local user_config = load_config(true)
187+
188+
if user_config.autogen then
189+
-- selene: allow(unused_variable)
190+
local autogen_text = nil
107191

108-
-- If the user's configuration does not return a table, then `require` will
109-
-- return a boolean to say whether or not the call was successful.
110-
if type(user_config) ~= "table" then
111-
return {}
192+
local waywall = require("waywall")
193+
waywall.listen("load", function()
194+
autogen_text = waywall.text(autogen_notice, { x = 10, y = 10, size = 1 })
195+
end)
112196
end
113197

114198
return user_config

0 commit comments

Comments
 (0)