Merged changes made by masa_ with cleanups

This commit is contained in:
lanxu 2018-01-01 21:45:51 +02:00
parent c0f84f270b
commit 26b0d039a0

View File

@ -1,20 +1,20 @@
local naughty = require("naughty") local naughty = require("naughty")
local wibox = require("wibox") local wibox = require("wibox")
local json = require("dkjson") local json = require("lanxu/dkjson")
local https = require("ssl.https") local https = require("ssl.https")
local gears = require("gears") local gears = require("gears")
local focused = require("awful.screen").focused local focused = require("awful.screen").focused
local function factory(args) local function factory(args)
local args = args or {} 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 calluri = args.uri or "https://masa.dy.fi/api/games/stats/minecraft/?type=server_status"
local minecraft = { widget = wibox.widget.textbox() } local minecraft = { widget = wibox.widget.textbox() }
local settings = args.settings or function() end local settings = args.settings or function() end
local hideempty = args.hideempty or false local hideempty = args.hideempty or false
local hideoffline = args.hideoffline or true local hideoffline = args.hideoffline or true
local followtag = args.followtag 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") minecraft.widget:set_markup("N/A")
@ -47,6 +47,11 @@ local function factory(args)
obj:connect_signal("mouse::leave", function() obj:connect_signal("mouse::leave", function()
minecraft.hide() minecraft.hide()
end) end)
obj:connect_signal("button::press", function()
minecraft.update()
minecraft.show()
end)
end end
function minecraft.update() function minecraft.update()
@ -54,42 +59,54 @@ local function factory(args)
local obj, pos, err = json.decode (response, 1, nil) local obj, pos, err = json.decode (response, 1, nil)
totalplayers = 0 totalplayers = 0
if not err then 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 for k, v in pairs(obj) do
if (tonumber(v.num_players) > 0 or not hideempty) and (v.status == "online" or not hideoffline) then if (tonumber(v.num_players) > 0 or not hideempty) and (v.status == "online" or not hideoffline) then
text = text .. v.status .. "\t" local tmp = string.len(v.server_name)
text = text .. v.version .. "\t" if tmp > max_len then
text = text .. v.num_players .. "\t" max_len = tmp
text = text .. v.server_name .. "\t" end
text = text .. "\n" 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 if v.players then
for i, p in pairs(v.players) do for i, p in pairs(v.players) do
text = text .. '\t\t' .. p .. '\n' text = text .. ' > ' .. p .. '\n'
end end
end end
end end
local n = tonumber(v.num_players) totalplayers = totalplayers + players
totalplayers = totalplayers + n
end end
minecraft.notification_text = text minecraft.notification_text = text
minecraft.totalplayers = totalplayers; minecraft.totalplayers = totalplayers;
-- minecraft.widget:set_markup("N/A")
widget = minecraft.widget widget = minecraft.widget
settings() settings()
end end
end end
minecraft.attach(minecraft.widget) minecraft.attach(minecraft.widget)
gears.timer { gears.timer {
timeout = timeout, timeout = timeout,
autostart = true, autostart = true,
callback = minecraft.update callback = minecraft.update
} }
minecraft.update()
return minecraft return minecraft
end end