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

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

Open
jaimet opened this issue Feb 13, 2020 · 2 comments
Open

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

jaimet opened this issue Feb 13, 2020 · 2 comments

Comments

@jaimet
Copy link

jaimet commented Feb 13, 2020

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
Copy link
Author

jaimet commented Feb 13, 2020

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! 😆

@shdown
Copy link
Owner

shdown commented Feb 15, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants