justinhj / battery.nvim

Neovim plugin to detect and view battery information
MIT License
58 stars 9 forks source link

Error on my Windows device with no battery #50

Closed tbrsvn closed 1 month ago

tbrsvn commented 1 month ago

I work on a laptop and a desktop, and I liked this plugin because it could make my config go in between the two with an extra QOL feature on my laptop. It works fine on my laptop, but on my desktop with no battery every time the plugin checks if I have a battery it gives this error:

Error executing luv callback: .../plugged/battery.nvim/lua/battery/parsers/powershell.lua:57: attempt to index a nil value stack traceback: .../plugged/battery.nvim/lua/battery/parsers/powershell.lua:57: in function 'parse_powershell_battery_info' .../plugged/battery.nvim/lua/battery/parsers/powershell.lua:79: in function '_user_on_exit' ...Local/nvim-data/plugged/plenary.nvim/lua/plenary/job.lua:241: in function '_shutdown' ...Local/nvim-data/plugged/plenary.nvim/lua/plenary/job.lua:48: in function <...Local/nvim-data/plugged/plenary.nvim/lua/plenary/job.lua:39>

which makes sense because there's no battery, but I read on the ReadMe that it can detect if there isn't a battery and not cause harm. If that's only a feature in Linux, then I guess this is an enhancement issue.

tbrsvn commented 1 month ago

Im loading battery.nvim in lualine like this:

lua << EOF
local batterycolor = "#ec9ca4"
local battery = require("battery")
battery.setup({
    update_rate_seconds = 30,           -- Number of seconds between checking battery status
    show_status_when_no_battery = true, -- Don't show any icon or text when no battery found (desktop for example)
    show_plugged_icon = true,           -- If true show a cable icon alongside the battery icon when plugged in
    show_unplugged_icon = true,         -- When true show a diconnected cable icon when not plugged in
    show_percent = true,                -- Whether or not to show the percent charge remaining in digits
    vertical_icons = true,              -- When true icons are vertical, otherwise shows horizontal battery icon
    multiple_battery_selection = 1,     -- Which battery to choose when multiple found. "max" or "maximum", "min" or "minimum" or a number to pick the nth battery found (currently linux acpi only)
})
EOF
lua << EOF
require('lualine').setup {
    options = {
        theme = 'catppuccin',
        icons_enabled = true
    },
    nvimbattery = {
        function()
            return require("battery").get_status_line()
        end,
        color = { fg = batterycolor, bg = bg },
    },
    extensions = {
    'nerdtree'
    },
    sections = {
        lualine_a = {'mode'},
        lualine_b = {'branch', 'diff', 'diagnostics'},
        lualine_c = {'filename'},
        lualine_w = {'encoding', 'fileformat', 'filetype'},
        lualine_x = {'location'},
        lualine_y = {'ctime'},
        lualine_z = {'nvimbattery'}
  },
}
EOF
tbrsvn commented 1 month ago

I also tried running :lua vim.print(require("battery").get_status_line()) manually and the output was a battery with a question mark

tbrsvn commented 1 month ago

I just cleaned my syntax, but same issue

lua << EOF
local batterycolor = '#ec9ca4'
local battery = require('battery')
battery.setup({
    update_rate_seconds = 30,
    show_status_when_no_battery = true,
    show_plugged_icon = true,
    show_unplugged_icon = true,
    show_percent = true,
    vertical_icons = true,
    multiple_battery_selection = 1
})
EOF
lua << EOF
local nvimbattery = {
        function()
            return require('battery').get_status_line()
        end,
        color = { fg = batterycolor, bg = bg }
}
EOF
lua << EOF
require('lualine').setup {
    options = {
        theme = 'catppuccin',
        icons_enabled = true
    },
    extensions = {
    'nerdtree'
    },
    sections = {
        lualine_a = {'mode'},
        lualine_b = {'branch', 'diff', 'diagnostics'},
        lualine_c = {'filename'},
        lualine_w = {'encoding', 'fileformat', 'filetype'},
        lualine_x = {'location'},
        lualine_y = {'ctime'},
        lualine_z = {'nvimbattery'}
  },
}
EOF
tbrsvn commented 1 month ago

I updated to the latest version on my laptop and nothing is showing, no errors, but no battery

justinhj commented 1 month ago

Thank you for the report Norok I will investigate when I get time 👍🏽

tbrsvn commented 1 month ago

Sounds good

Agent-E11 commented 1 month ago

@Norok-The-Diablo I think I found the issue. I have a fork with the branch 50-fix-error-on-windows-no-battery with a fix

Agent-E11 commented 1 month ago

You can see the diff here https://github.com/justinhj/battery.nvim/pull/51/files

Agent-E11 commented 1 month ago

I am not sure how you manage your packages, but I will assume vim-plug since it looks like you are using .vimrc:

Plug 'agent-e11/battery.nvim', { 'branch': '50-fix-error-on-windows-no-battery' }
tbrsvn commented 1 month ago

Thanks so much!

On Thu, Jul 18, 2024, 12:36 PM Eamon Burns @.***> wrote:

I am not sure how you manage your packages, but I will assume vim-plug since it looks like you are using .vimrc:

Plug 'agent-e11/battery.nvim', { 'branch': '50-fix-error-on-windows-no-battery' }

— Reply to this email directly, view it on GitHub https://github.com/justinhj/battery.nvim/issues/50#issuecomment-2237385192, or unsubscribe https://github.com/notifications/unsubscribe-auth/AY22GBWNKOY7VGCM5GUMYCTZNAKMDAVCNFSM6AAAAABLA6P2AWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMZXGM4DKMJZGI . You are receiving this because you were mentioned.Message ID: @.***>

Agent-E11 commented 1 month ago

@Norok-The-Diablo Did it fix it?

tbrsvn commented 1 month ago

Just got home, I will test right now

tbrsvn commented 1 month ago

No more error, it just shows nothing on desktop

tbrsvn commented 1 month ago

I will try this version on my laptop too

Agent-E11 commented 1 month ago

@Norok-The-Diablo Oh, I forgot to mention. I think the reason that it shows nothing is because in your config you wrote:

lualine_z = { 'nvimbattery' }

instead of

lualine_z = { nvimbattery }
tbrsvn commented 1 month ago

on my init I have it without quotes so that's not it

tbrsvn commented 1 month ago

I realized I did it wrong later

Agent-E11 commented 1 month ago

What is the output of require("battery").get_battery_status()?

tbrsvn commented 1 month ago

nothing

Agent-E11 commented 1 month ago

"Nothing" meaning:

{}

or

?

tbrsvn commented 1 month ago

nothing nothing

tbrsvn commented 1 month ago

nothing on my laptop either

tbrsvn commented 1 month ago

just tried it

tbrsvn commented 1 month ago

It's loaded like this:

lua << EOF
local batterycolor = '#ec9ca4'
local battery = require('battery')
battery.setup({
    update_rate_seconds = 30,
    show_status_when_no_battery = true,
    show_plugged_icon = true,
    show_unplugged_icon = true,
    show_percent = true,
    vertical_icons = true,
    multiple_battery_selection = 1
})
EOF
lua << EOF
local nvimbattery = {
        function()
            return require('battery').get_status_line()
        end,
        color = { fg = batterycolor, bg = bg }
}
EOF
lua << EOF
require('lualine').setup {
    options = {
        theme = 'catppuccin',
        icons_enabled = true
    },
    extensions = {
    'nerdtree'
    },
    sections = {
        lualine_a = {'mode'},
        lualine_b = {'branch', 'diff', 'diagnostics'},
        lualine_c = {'filename'},
        lualine_w = {'encoding', 'fileformat', 'filetype'},
        lualine_x = {'location'},
        lualine_y = {'ctime'},
        lualine_z = {nvimbattery}
  },
}
EOF
Agent-E11 commented 1 month ago

can you verify that you have the proper branch checked out?

Go to the directory the plugin is installed to and run:

git remote --verbose
git branch
tbrsvn commented 1 month ago

yeah everything's right

Agent-E11 commented 1 month ago

Ok, run this directly:

:lua =require"battery".get_battery_status()

Remember the =

tbrsvn commented 1 month ago
{
  ac_power = true,                                                                                                        battery_count = 0,                                                                                                      method = "powershell",                                                                                                  percent_charge_remaining = 100                                                                                        }                 
tbrsvn commented 1 month ago

thats the output

Agent-E11 commented 1 month ago

Yeah, that is what it's supposed to be. 🤔

Try this:

:lua =require"battery".get_status_line()
tbrsvn commented 1 month ago

there's a desktop icon

tbrsvn commented 1 month ago

so weird

Agent-E11 commented 1 month ago

Now, replace this:

lualine_z = {nvimbattery}

with this:

lualine_z = { { require("battery").get_status_line } }
tbrsvn commented 1 month ago

It works now

tbrsvn commented 1 month ago

What the heck

tbrsvn commented 1 month ago

so how I'm loading nvimbattery in the function must be wrong

Agent-E11 commented 1 month ago

maybe just replace this:

local nvimbattery = {
        function()
            return require('battery').get_status_line()
        end,
        color = { fg = batterycolor, bg = bg }
}

with this:

local nvimbattery = {
        require("battery").get_status_line,
        color = { fg = batterycolor, bg = bg }
}
tbrsvn commented 1 month ago

doesn't work

tbrsvn commented 1 month ago

If I remove the color variable it doesn't work either

tbrsvn commented 1 month ago

Does it have to be nested in the lualine setup?

Agent-E11 commented 1 month ago

That is really strange

Agent-E11 commented 1 month ago

Actually, remove the

EOF
lua<<EOF

between the declaration of nvimbattery and require('lualine').setup

tbrsvn commented 1 month ago

sounds good

tbrsvn commented 1 month ago

works just fine!

tbrsvn commented 1 month ago

thanks so much

justinhj commented 1 month ago

Thanks to you both. I just sat down and updated my Windows desktop since it was still on Neovim 0.7 lol By the time I had replicated the issue and create a branch to fix it I saw @Agent-E11 had fixed already! Thanks again