refactor(wezterm): superior table handling
This commit is contained in:
parent
98da4f7142
commit
f3e5818a47
@ -1,4 +1,4 @@
|
|||||||
local Table = require("wlib").new_table()
|
local wlib = require("wlib")
|
||||||
|
|
||||||
-- NOTE: Should be merged
|
-- NOTE: Should be merged
|
||||||
local events = require("config.events")
|
local events = require("config.events")
|
||||||
@ -9,5 +9,5 @@ local rendering = require("config.rendering")
|
|||||||
local keybinds = require("config.keybinds")
|
local keybinds = require("config.keybinds")
|
||||||
local misc = require("config.misc")
|
local misc = require("config.misc")
|
||||||
|
|
||||||
local config = Table.merge(events, fonts, theme, tabbar, misc, rendering, keybinds)
|
local config = wlib.Table.merge(events, fonts, theme, tabbar, misc, rendering, keybinds)
|
||||||
return config
|
return config
|
||||||
|
@ -1,55 +1,38 @@
|
|||||||
local os = require("os")
|
local os = require("os")
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
--- Creates a new custom table that allows
|
M.Table = {}
|
||||||
--- usage of lua's `table` attributes
|
|
||||||
---
|
--- Dump a table into a json-ish string format,
|
||||||
--- This is done really oddly such that we create a local table instance for
|
--- shamelessly taken from https://stackoverflow.com/a/27028488
|
||||||
--- each invocation, but also get methods for each new table in a lua style
|
---@param o table
|
||||||
---
|
function M.Table.dump(o)
|
||||||
|
if type(o) == "table" then
|
||||||
|
local s = "{\n"
|
||||||
|
for k, v in pairs(o) do
|
||||||
|
if type(k) ~= "number" then
|
||||||
|
k = '"' .. k .. '"'
|
||||||
|
end
|
||||||
|
s = s .. " [" .. k .. "] = " .. dump(v) .. ",\n"
|
||||||
|
end
|
||||||
|
return s .. "} "
|
||||||
|
else
|
||||||
|
return tostring(o)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Allows the combining of multiple tables, where the last table given has the highest
|
||||||
|
--- priority (it overrides the keys of other tables)
|
||||||
|
---@vararg table
|
||||||
---@return table
|
---@return table
|
||||||
function M.new_table()
|
function M.Table.merge(...)
|
||||||
local t = {}
|
local ret = {}
|
||||||
|
for _, tbl in ipairs({ ... }) do
|
||||||
--- Dump a table into a json-ish string format,
|
for k, v in pairs(tbl) do
|
||||||
--- shamelessly taken from https://stackoverflow.com/a/27028488
|
ret[k] = v
|
||||||
---@param o table
|
|
||||||
local function dump(o)
|
|
||||||
if type(o) == "table" then
|
|
||||||
local s = "{\n"
|
|
||||||
for k, v in pairs(o) do
|
|
||||||
if type(k) ~= "number" then
|
|
||||||
k = '"' .. k .. '"'
|
|
||||||
end
|
|
||||||
s = s .. " [" .. k .. "] = " .. dump(v) .. ",\n"
|
|
||||||
end
|
|
||||||
return s .. "} "
|
|
||||||
else
|
|
||||||
return tostring(o)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
return ret
|
||||||
--- Simple wrapper around dump function
|
|
||||||
---@return string
|
|
||||||
function t.dump()
|
|
||||||
return dump(t)
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Allows the combining of multiple tables, where the last table given has the highest
|
|
||||||
--- priority (it overrides the keys of other tables)
|
|
||||||
---@vararg table
|
|
||||||
---@return table
|
|
||||||
function t.merge(...)
|
|
||||||
local ret = {}
|
|
||||||
for _, tbl in ipairs({ ... }) do
|
|
||||||
for k, v in pairs(tbl) do
|
|
||||||
ret[k] = v
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return ret
|
|
||||||
end
|
|
||||||
|
|
||||||
return t
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Returns the path to the users home, note this string needs to be regularized as
|
--- Returns the path to the users home, note this string needs to be regularized as
|
||||||
@ -59,7 +42,7 @@ function M.determine_home_path()
|
|||||||
-- This helps give a better error message
|
-- This helps give a better error message
|
||||||
-- if we are unable to find their home dir
|
-- if we are unable to find their home dir
|
||||||
-- from a environment variable
|
-- from a environment variable
|
||||||
local checked_env_vars = M.new_table()
|
local checked_env_vars = {}
|
||||||
|
|
||||||
-- Attempt to get UNIX home
|
-- Attempt to get UNIX home
|
||||||
local unix_home_var = "HOME"
|
local unix_home_var = "HOME"
|
||||||
@ -81,7 +64,7 @@ function M.determine_home_path()
|
|||||||
-- NOTE: This will error if it cannot determine the home path
|
-- NOTE: This will error if it cannot determine the home path
|
||||||
error(
|
error(
|
||||||
"Unable to determine user's home directory, checked environment variables:\n\n"
|
"Unable to determine user's home directory, checked environment variables:\n\n"
|
||||||
.. checked_env_vars.dump()
|
.. M.Table.dump(checked_env_vars)
|
||||||
.. "\n\n"
|
.. "\n\n"
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user