Search code examples
luaawesome-wm

Activate vicious.widgets.net widget only if interface is available


I have the following widget using vicious in my rc.lua:

-- Initialize widget Ethernet
ethwidget = wibox.widget.textbox()                                                                                                                            
-- Register widget       
    vicious.register(ethwidget, vicious.widgets.net, 'Eth0: <span color="#CC9933">down: ${eth0 down_kb} kB/s</span> <span color="#7F9F7F"> up: ${eth0 up_kb}  kB/s</span><span color="#cccccc"> [ ${eth0 rx_gb} GB //  ${eth0 tx_gb} GB ] | </span>', 2)

The thing is that I have a recent macbook pro, and if I don't have plugged the ethernet adapter, then I only see e.g. ${eth0 down_kb} printed on my wibox. How could I add a conditional so that the widget is inserted only if the ethernet adapter is plugged?.


Solution

  • I came with a solution that is working fine. I found this link http://awesome.naquadah.narkive.com/wpn4XSWl/netwidget-with-two-interfaces and my code is as follows now:

    eths = { 'eth0', 'wlp3s0' }
    netwidget = wibox.widget.textbox()
    vicious.register( netwidget, vicious.widgets.net,
    function(widget,args)
    t=''  
    for i = 1, #eths do
    e = eths[i]       
    if args["{"..e.." carrier}"] == 1 then
        if e == 'wlp3s0' then
            t=t..'|'..'Wifi: <span color="#CC9933"> down: '..args['{'..e..' down_kb}']..' kbps</span>  <span color="#7F9F7F">up: ' ..args['{'..e..' up_kb}']..'   kbps </span>'..'[ '..args['{'..e..' rx_gb}'].. ' GB // ' ..args['{'..e..' tx_gb}']..' GB ] '
        else          
            t=t..'|'..'Eth0: <span color="#CC9933"> down: '..args['{'..e..' down_kb}']..' kbps</span>  <span color="#7F9F7F">up: ' ..args['{'..e..' up_kb}']..'   kbps </span>'..'[ '..args['{'..e..' rx_gb}'].. ' GB // ' ..args['{'..e..' tx_gb}']..' GB ] '
        end
    end
    end               
    if string.len(t)>0 then -- remove leading '|'
    return string.sub(t,2,-1)
    end               
    return 'No network'
    end                                                                                                                                                           
    , 1 )
    

    And the you add your widget as follows (I am on awesome v3.5.9 (Mighty Ravendark)):

    bottom_layout:add(netwidget)