TACC / Lmod

Lmod: An Environment Module System based on Lua, Reads TCL Modules, Supports a Software Hierarchy
http://lmod.readthedocs.org
Other
486 stars 125 forks source link

Change color of version numbers in `module avail`? #603

Closed simonLeary42 closed 1 year ago

simonLeary42 commented 1 year ago

module av has colors but they're rarely used and I think it would be nice if the versions of things were blue while the names stayed white. Is this possible with some .modulerc configuration?

rtmclay commented 1 year ago

There is no current way to tell Lmod to do that. Please feel free to create a Pull Request that can do that. I believe that a new hook that might allow a site to do that would be the way to go.

simonLeary42 commented 1 year ago

I did this first with a message hook on avail, but all I had to work with was a list of strings and I had to use regex. It worked but it's a bit hacky

simonLeary42 commented 1 year ago

I put this in my /etc/lmod/lmod_config.lua:

function myMsgHook(kind,a)
    -- regex match the version in "name/version" and colorize the version
    if (kind == "avail") then
        local version_color = getenv("LMOD_AVAIL_VERSION_COLOR")
        -- any word that contains a `/`, the stuff after the `/` cannot contain another `/`
        local pattern = "([^%s]+)/([^%s/]+)"
        for i, elem in ipairs(a) do
            local replaced = string.gsub(elem, pattern, "%1/"..colorize(version_color, "%2"))
            a[i] = replaced
        end
    end
    return a
end
simonLeary42 commented 1 year ago

@rtmclay do you know why the above lmod_config.lua might cause Lmod site messages to no longer be displayed? Does my hook need to pass control back to some other hook?

simonLeary42 commented 1 year ago

Since this only overrides avail, can I access the message files and manually display the avail message here?

simonLeary42 commented 1 year ago
local hook   = require("Hook")
local getenv = os.getenv
local i18n   = require("i18n")

-- default message hook from Lmod source code
-- https://github.com/TACC/Lmod/blob/b95765ea9b1504fcaaaa28d0e863734edc908f88/src/StandardPackage.lua#LL68C16-L68C16
function add_site_msg(kind, a)
    local twidth = TermWidth()
    local s      = i18n(kind,{}) or ""
    if (s:len() > 0) then
       for line in s:split("\n") do
          a[#a+1] = "\n"
          a[#a+1] = line:fillWords("",twidth)
       end
       a[#a+1] = "\n"
    end
    a[#a+1] = "\n"
    return a
 end

function myMsgHook(kind,a)
    -- kind is a string that indicates what Lmod is doing right now
    -- a is a list of strings

    -- call the default message hook first, then do our thing on top of that
    a = add_site_msg(kind, a)

    if (kind == "avail") then
        -- regex match the version in "name/version" and colorize the version
        local version_color = getenv("LMOD_AVAIL_VERSION_COLOR")
        if version_color == "" or version_color == nil then
            version_color = "cyan"
        end
        -- (not whitespace) / (not whitespace and also not a slash)
        local pattern = "([^%s]+)/([^%s/]+)"
        for i, elem in ipairs(a) do
            -- gsub() will return the original string if the pattern doesn't match
            -- colorize() will use `plain` if `version_color` is not a valid color`
            local replaced = string.gsub(elem, pattern, "%1"..colorize(version_color, "/%2"))
            a[i] = replaced
        end
    end
    return a
end

hook.register("msgHook", myMsgHook)
wpoely86 commented 1 year ago

A hook should go into SitePackage.lua. It's cool example, maybe open a PR and add it under the contrib directory as example?

simonLeary42 commented 1 year ago

It should be noted that default pager more does not like ANSI colors, and the column alignment gets all messed up. But if you set LMOD_PAGER=less then it works fine