luastatus icon indicating copy to clipboard operation
luastatus copied to clipboard

Could someone please verify/check my "new" mem-usage widget?

Open jaimet opened this issue 4 years ago • 2 comments

Hi.

At the moment: The mem-usage widget "requires" the mem-usage-linux plugin which "requires" the timer plugin.

But for me, personally, this middle layer of indirection (the mem-usage-linux plugin) doesn't help me. For me, it just makes the code harder to understand and follow (this might be because I am new to lua and luastatus).

So, I am trying to combine the mem-usage widget and the mem-usage-linux plugin into 1 file: a new mem-usage widget that "requires" the timer plugin.

Here's what I have so far:

widget = {
    plugin = './plugins/timer/plugin-timer.so',
    opts = {period = 2},
    cb = function()
        local f = assert(io.open('/proc/meminfo', 'r'))
        local r = {}
        for line in f:lines() do
            local key, value, unit = line:match('(%w+):%s+(%w+)%s+(%w+)')
            if key == 'MemTotal' then
                r.total = {value = tonumber(value), unit = unit}
            elseif key == 'MemAvailable' then
                r.avail = {value = tonumber(value), unit = unit}
            end
        end
        f:close()
        local used_kb = r.total.value - r.avail.value
        return string.format('[%3.2f GiB]', used_kb / 1024 / 1024)
    end
}

I can "successfully" run this new widget and it produces what appears to be correct output, thus:

[1.10 GiB]
[1.03 GiB]
[1.02 GiB]
[1.03 GiB]
[1.00 GiB]
[0.99 GiB]
[0.98 GiB]
[0.99 GiB]
[0.98 GiB]
[0.99 GiB]
[1.01 GiB]
[1.00 GiB]
[0.99 GiB]
[1.02 GiB]

and it appears to only produce new output when the value changes (which, I think, is like the other widgets), so I think this is correct. But... I am new to lua and luastatus, so I don't feel sure that my code is functionally equivalent. Could somebody who possesses more lua-fu than me please have a look at this code and confirm whether or not it is functionally equivalent to the original source, and also whether or not it can be made more simple/easier to understand?

Thank you! Jaime

jaimet avatar Feb 13 '20 07:02 jaimet

and it appears to only produce new output when the value changes (which, I think, is like the other widgets),

I've just discovered that the "no duplicate output lines" "thing" is built into the barlibs ("Temporary buffer for secondary buffering, to avoid unneeded redraws.")

I'll get my coat! :laughing:

jaimet avatar Feb 13 '20 14:02 jaimet

Could somebody who possesses more lua-fu than me please have a look at this code and confirm whether or not it is functionally equivalent to the original source,

It seems to be, except for this line:

    plugin = './plugins/timer/plugin-timer.so'

Note that it now depends on the current directory (the directory that the luastatus binary was launched at).

and also whether or not it can be made more simple/easier to understand?

Probably by getting rid of the r table and using two local variables instead:

    cb = function()
        local f = assert(io.open('/proc/meminfo', 'r'))
        local total_kb, avail_kb
        for line in f:lines() do
            local key, value, unit = line:match('(%w+):%s+(%w+)%s+(%w+)')
            if key == 'MemTotal' then
                total_kb = tonumber(value)
            elseif key == 'MemAvailable' then
                avail_kb = tonumber(value)
            end
        end
        f:close()
        local used_kb = total_kb - avail_kb
        return string.format('[%3.2f GiB]', used_kb / 1024 / 1024)
    end

shdown avatar Feb 15 '20 19:02 shdown