refactor(wezterm): superior table handling

This commit is contained in:
Price Hiller 2022-07-09 14:15:46 -05:00
parent 98da4f7142
commit f3e5818a47
2 changed files with 33 additions and 50 deletions

View File

@ -1,4 +1,4 @@
local Table = require("wlib").new_table()
local wlib = require("wlib")
-- NOTE: Should be merged
local events = require("config.events")
@ -9,5 +9,5 @@ local rendering = require("config.rendering")
local keybinds = require("config.keybinds")
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

View File

@ -1,55 +1,38 @@
local os = require("os")
local M = {}
--- Creates a new custom table that allows
--- usage of lua's `table` attributes
---
--- This is done really oddly such that we create a local table instance for
--- each invocation, but also get methods for each new table in a lua style
---
M.Table = {}
--- Dump a table into a json-ish string format,
--- shamelessly taken from https://stackoverflow.com/a/27028488
---@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
function M.new_table()
local t = {}
--- Dump a table into a json-ish string format,
--- shamelessly taken from https://stackoverflow.com/a/27028488
---@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)
function M.Table.merge(...)
local ret = {}
for _, tbl in ipairs({ ... }) do
for k, v in pairs(tbl) do
ret[k] = v
end
end
--- 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
return ret
end
--- 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
-- if we are unable to find their home dir
-- from a environment variable
local checked_env_vars = M.new_table()
local checked_env_vars = {}
-- Attempt to get UNIX 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
error(
"Unable to determine user's home directory, checked environment variables:\n\n"
.. checked_env_vars.dump()
.. M.Table.dump(checked_env_vars)
.. "\n\n"
)
end