|
5 | 5 | load their configuration. |
6 | 6 | ]] |
7 | 7 |
|
| 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 | + |
8 | 38 | --[[ |
9 | 39 | Setup the environment for the user's configuration. |
10 | 40 | ]] |
@@ -102,13 +132,67 @@ for _, dir in ipairs(config_dirs) do |
102 | 132 | package.path = package.path .. ";" .. dir .. "/waywall/?.lua" |
103 | 133 | end |
104 | 134 |
|
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 |
107 | 191 |
|
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) |
112 | 196 | end |
113 | 197 |
|
114 | 198 | return user_config |
0 commit comments