Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sub-command category #600

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Hook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ local validT =
-- to moduleT.lua to make it safe on
-- shared filesystems.
avail = false, -- Map directory names to labels
category = false, -- Hook to change output of category
restore = false, -- This hook is run after restore operation
startup = false, -- This hook is run when Lmod is called
finalize = false, -- This hook is run just before Lmod generates its output before exiting
Expand Down
126 changes: 126 additions & 0 deletions src/cmdfuncs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,132 @@ function Avail(...)
end
end

------------------------------------------------------------------------
function Category(...)
dbg.start{"Category(", concatTbl({...},", "),")"}
local shell = _G.Shell


local cache = Cache:singleton{buildCache = true}
local moduleT, dbT = cache:build()

local categoryT = {}

local function add_module(cat, name)
if (cat == "") then return
elseif (categoryT[cat] == nil) then
dbg.print{"found new category: ", cat, "\n"}
categoryT[cat] = {}
end

if (not categoryT[cat][name]) then
dbg.print{"found new sn: ", name, " in category: ", cat, "\n"}
categoryT[cat][name] = 1
else
categoryT[cat][name] = categoryT[cat][name] + 1
end
end

for sn, v in pairs(dbT) do
for _, info in pairs(v) do
local category = info.Category or ""

for entry in category:split(",") do
entry = entry:trim()
add_module(entry, sn)
end
end
end

local masterTbl = masterTbl()
local search = ... and true
local twidth = TermWidth()
local cwidth = masterTbl.rt and LMOD_COLUMN_TABLE_WIDTH or twidth
local banner = Banner:singleton()

local a = {}

if (not search) then
dbg.print{"printing category block\n"}
a[#a+1] = "\n"
a[#a+1] = [[
To get a list of every module in a category execute:
$ module category Foo
where "Foo" is the name of a category.
]]
rtmclay marked this conversation as resolved.
Show resolved Hide resolved
a[#a+1] = "\n"
a[#a+1] = banner:bannerStr("List of Categories")
a[#a+1] = "\n"

local b = {}
for cat, _ in pairsByKeys(categoryT) do
b[#b+1] = cat .. "\n"
end

b = hook.apply("category", "simple", b) or b

for i = 1, #b do
a[#a+1] = b[i]
end

a[#a+1] = "\n"
else
local argA = pack(...)
local searchA = {}

for i = 1, argA.n do
searchA[i] = argA[i]:caseIndependent()
end
searchA.n = argA.n

local match = hook.apply("category", "complex", categoryT) or {}

if (next(match) == nil) then
for cat, v in pairsByKeys(categoryT) do
for i = 1, searchA.n do
if (cat:find(searchA[i])) then
match[cat] = v
end
end
end
end

a[#a+1] = "\n"
a[#a+1] = [[
To learn more about a package and how to load it execute:
$ module spider Bar
where "Bar" is the name of a package.
]]
Copy link

@ocaisa ocaisa Oct 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
]]
where "Bar" is the name of a module.
]]

This is more like how it is done for module keyword

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe package instead of module here since that is the word you used previously.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the line with the word package as a new commit


if (next(match) == nil) then
a[#a+1] = "\n"
a[#a+1] = "No matching category found."
a[#a+1] = "\n"
end

for cat, v in pairsByKeys(match) do
local b = {}

for sn, count in pairsByKeys(v) do
b[#b+1] = { sn, "(" .. tostring(count) .. ") " }
end

dbg.print{"printing category block\n"}
local ct = ColumnTable:new{tbl = b, gap = 1, len = length, width = cwidth}
a[#a+1] = "\n"
a[#a+1] = banner:bannerStr(cat)
a[#a+1] = "\n"
a[#a+1] = ct:build_tbl()
a[#a+1] = "\n"
end
end

if (next(a) ~= nil) then
shell:echo(concatTbl(a, ""))
end
dbg.fini("Category")
end

------------------------------------------------------------------------
-- Just convert the vararg into an actual array and call
-- master.overview to do the real work.
Expand Down
4 changes: 4 additions & 0 deletions src/lmod.in.lua
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ function Usage()
a[#a+1] = { " list", "s1 s2 ...", i18n("list2") }
a[#a+1] = { " avail | av", "", i18n("list3") }
a[#a+1] = { " avail | av", "string", i18n("list4") }
a[#a+1] = { " category | cat","", "List all categories" }
a[#a+1] = { " category | cat","s1 s2 ...", "List all categories that match the pattern and display their modules" }
a[#a+1] = { " overview | ov", "", i18n("ov1") }
a[#a+1] = { " overview | ov", "string", i18n("ov2") }
a[#a+1] = { " spider", "", i18n("list5") }
Expand Down Expand Up @@ -276,10 +278,12 @@ function main()
local whatisTbl = { name = "whatis", checkMPATH = false, cmd = Whatis }
local isLoadedTbl = { name = "isLoaded", checkMPATH = false, cmd = IsLoaded }
local isAvailTbl = { name = "isAvail", checkMPATH = false, cmd = IsAvail }
local categoryTbl = { name = "category", checkMPATH = true, cmd = Category }

local lmodCmdA = {
{cmd = 'add', min = 2, action = loadTbl },
{cmd = 'avail', min = 2, action = availTbl },
{cmd = 'category', min = 3, action = categoryTbl },
{cmd = 'delete', min = 3, action = unloadTbl },
{cmd = 'describe', min = 3, action = mcTbl },
{cmd = 'disable', min = 4, action = disableTbl },
Expand Down
1 change: 1 addition & 0 deletions src/ml_cmd.in.lua
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ function main()

local lmodCmdT = {
avail="avail", av="avail", available="avail",
category="category", cat="category",
describe="describe", mcc="describe",
disable="disable",
getdefault="getdefault", gd="getdefault",
Expand Down