From 26b0d039a093a130cfbcff651f09a3da115333cc Mon Sep 17 00:00:00 2001 From: lanxu Date: Mon, 1 Jan 2018 21:45:51 +0200 Subject: [PATCH] Merged changes made by masa_ with cleanups --- .config/awesome/lanxu/mcstatus.lua | 49 ++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/.config/awesome/lanxu/mcstatus.lua b/.config/awesome/lanxu/mcstatus.lua index 6e24f96..fd7a404 100644 --- a/.config/awesome/lanxu/mcstatus.lua +++ b/.config/awesome/lanxu/mcstatus.lua @@ -1,20 +1,20 @@ local naughty = require("naughty") local wibox = require("wibox") -local json = require("dkjson") +local json = require("lanxu/dkjson") local https = require("ssl.https") local gears = require("gears") local focused = require("awful.screen").focused local function factory(args) local args = args or {} - local timeout = args.timeout or 120 + local timeout = args.timeout or 600 local calluri = args.uri or "https://masa.dy.fi/api/games/stats/minecraft/?type=server_status" local minecraft = { widget = wibox.widget.textbox() } local settings = args.settings or function() end local hideempty = args.hideempty or false local hideoffline = args.hideoffline or true local followtag = args.followtag or true - local notification_preset = args.notification_preset or {} + local notification_preset = args.notification_preset or { font = "Monospace 10" } minecraft.widget:set_markup("N/A") @@ -24,7 +24,7 @@ local function factory(args) if followtag then notification_preset.screen = focused() end - + minecraft.notification = naughty.notify({ text = minecraft.notification_text, -- icon = minecraft.icon @@ -43,10 +43,15 @@ local function factory(args) obj:connect_signal("mouse::enter", function() minecraft.show() end) - + obj:connect_signal("mouse::leave", function() minecraft.hide() end) + + obj:connect_signal("button::press", function() + minecraft.update() + minecraft.show() + end) end function minecraft.update() @@ -54,42 +59,54 @@ local function factory(args) local obj, pos, err = json.decode (response, 1, nil) totalplayers = 0 + if not err then - local text = "Status" .. "\t" .. "Version" .. "\t" .. "#" .. "\t" .. "Server" .. "\t" .. "\n" + -- First get the length of the longest server name, for proper column alignment + local max_len = 6 -- Initialize to the length of "Server" for k, v in pairs(obj) do if (tonumber(v.num_players) > 0 or not hideempty) and (v.status == "online" or not hideoffline) then - text = text .. v.status .. "\t" - text = text .. v.version .. "\t" - text = text .. v.num_players .. "\t" - text = text .. v.server_name .. "\t" - text = text .. "\n" + local tmp = string.len(v.server_name) + if tmp > max_len then + max_len = tmp + end + end + end + + local fmt = string.format("%%-%ds %%-8s %%-7s\n", max_len) + local text = string.format(fmt, "Server", "Version", "Players") + + for k, v in pairs(obj) do + local players = tonumber(v.num_players) + + if (players > 0 or not hideempty) and (v.status == "online" or not hideoffline) then + text = text .. string.format(fmt, v.server_name, v.version, v.num_players) if v.players then for i, p in pairs(v.players) do - text = text .. '\t\t' .. p .. '\n' + text = text .. ' > ' .. p .. '\n' end end end - local n = tonumber(v.num_players) - totalplayers = totalplayers + n + totalplayers = totalplayers + players end + minecraft.notification_text = text minecraft.totalplayers = totalplayers; - -- minecraft.widget:set_markup("N/A") widget = minecraft.widget settings() end end minecraft.attach(minecraft.widget) - gears.timer { timeout = timeout, autostart = true, callback = minecraft.update } + minecraft.update() + return minecraft end