Skip to content

Commit

Permalink
only add config dir and folder rc.lua is located in
Browse files Browse the repository at this point in the history
  • Loading branch information
aarondill committed Aug 18, 2023
1 parent 99a1de8 commit 200c4d6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
12 changes: 4 additions & 8 deletions rc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
local awful = require("awful")
local beautiful = require("beautiful")
local gfile = require("gears.filesystem")
local gstring = require("gears.string")
-- Enable hotkeys help widget for VIM and other apps
-- when client with a matching name is opened:
-- Don't show the tmux keymaps
Expand All @@ -17,13 +16,10 @@ pcall(require, "awful.autofocus") -- Depreciated in V5

-- Add configuration directory to package.?path so awesome --config FILE works right
local conf_dir = gfile.get_configuration_dir():sub(1, -2) -- Remove slash

if not string.find(package.path, gstring.quote_pattern(conf_dir .. "/?.lua;"), nil, true) then -- contains
package.path = string.format("%s/?.lua;%s/?/init.lua;%s", conf_dir, conf_dir, package.path)
end
if not string.find(package.cpath, gstring.quote_pattern(conf_dir .. "/?.so;"), nil, true) then -- contains
package.cpath = string.format("%s/?.so;%s", conf_dir, package.cpath)
end
local this_dir = (debug.getinfo(1, "S").source:sub(2):match("(.*/)") or "./")
local package_path_utils = require("util.package_path")
package_path_utils.add_to_path(conf_dir)
package_path_utils.add_to_path(this_dir)

-- Set environment variables. (ONLY for POSIX systems)
require("configuration.environment")()
Expand Down
61 changes: 61 additions & 0 deletions util/package_path.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
local M = {}
---Whether package.path contains `dir`
---@param dir string
---@return boolean
---@nodiscard
function M.path_contains(dir)
local lua = dir .. "/?.lua;"
local init = dir .. "/?/init.lua;"
return not string.find(package.path, lua, 1, true) and not string.find(package.path, init, 1, true)
end
---Whether package.cpath contains `dir`
---@param dir string
---@return boolean
---@nodiscard
function M.cpath_contains(dir)
local so = dir .. "/?.so;"
return not string.find(package.path, so, 1, true)
end

---A utility for appending to the path and cpath
---@param prepend boolean prepend to the string?
---@param to string append or prepend to this string
---@param format string a format string for string.format
---@param args unknown[] a table of arguments to pass to string.format. This will be modified!
---@return string str the new formatted string. The parts will be semicolon delimited.
---@nodiscard
local function append_or_prepend(prepend, to, format, args)
assert(type(to) == "string")
assert(type(format) == "string")
assert(type(args) == "table")
table.insert(args, prepend and 1 or #args, to) -- order
format = prepend and (format .. ";%s") or ("%s;" .. format)
return format:format(table.unpack(args))
end

---Add to packge.cpath
---@param dir string
---@param prepend boolean? default true
---@return string cpath the new cpath (for convenience)
function M.add_to_cpath(dir, prepend)
assert(type(dir) == "string")
prepend = prepend == nil and true or not not prepend
if M.cpath_contains(dir) then return package.cpath end
package.cpath = append_or_prepend(prepend, package.cpath, "%s/?.so", { dir })
return package.cpath
end

---Add to packge.path
---@param dir string
---@param prepend boolean? default true
---@return string cpath the new cpath (for convenience)
function M.add_to_path(dir, prepend)
assert(type(dir) == "string")
prepend = prepend == nil and true or not not prepend
if M.path_contains(dir) then return package.cpath end
local format = "%s/?.lua;%s/?/init.lua"
package.path = append_or_prepend(prepend, package.cpath, format, { dir, dir })
return package.path
end

return M

0 comments on commit 200c4d6

Please sign in to comment.