From f490e6e8ef07c4bb9f5ae531ca00b47997c574e5 Mon Sep 17 00:00:00 2001 From: lanxu Date: Sat, 3 Feb 2018 13:41:40 +0200 Subject: [PATCH] Moved from luasec to pure curl implementation. mcstatus is now async. --- .config/awesome/lanxu/README.md | 1 - .config/awesome/lanxu/mcstatus.lua | 91 +++++++++++++++--------------- 2 files changed, 47 insertions(+), 45 deletions(-) diff --git a/.config/awesome/lanxu/README.md b/.config/awesome/lanxu/README.md index c167708..8eb3628 100644 --- a/.config/awesome/lanxu/README.md +++ b/.config/awesome/lanxu/README.md @@ -4,7 +4,6 @@ lanxu awesome scripts Requirements ------------ -- luasec (for https connections) - dkjson (for parsing json. Available with this widget) How to use diff --git a/.config/awesome/lanxu/mcstatus.lua b/.config/awesome/lanxu/mcstatus.lua index fd7a404..ba4afbc 100644 --- a/.config/awesome/lanxu/mcstatus.lua +++ b/.config/awesome/lanxu/mcstatus.lua @@ -1,9 +1,9 @@ -local naughty = require("naughty") -local wibox = require("wibox") -local json = require("lanxu/dkjson") -local https = require("ssl.https") -local gears = require("gears") -local focused = require("awful.screen").focused +local naughty = require("naughty") +local wibox = require("wibox") +local json = require("lanxu/dkjson") +local gears = require("gears") +local focused = require("awful.screen").focused +local easy_async = require("awful.spawn").easy_async local function factory(args) local args = args or {} @@ -26,11 +26,11 @@ local function factory(args) end minecraft.notification = naughty.notify({ - text = minecraft.notification_text, - -- icon = minecraft.icon - timeout = 0, - preset = notification_preset, - }) + text = minecraft.notification_text, + -- icon = minecraft.icon + timeout = 0, + preset = notification_preset, + }) end function minecraft.hide() @@ -55,47 +55,50 @@ local function factory(args) end function minecraft.update() - local response, error = https.request(calluri) - local obj, pos, err = json.decode (response, 1, nil) + local cmd = string.format("curl -s " .. "'" .. calluri .. "'") - totalplayers = 0 + easy_async(cmd, function(response, stderr, reason, exit_code) + local obj, pos, err = json.decode(response, 1, nil) - if not err then - -- 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 - local tmp = string.len(v.server_name) - if tmp > max_len then - max_len = tmp - end - end - end + totalplayers = 0 - 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 .. ' > ' .. p .. '\n' + if not err then + -- 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 + local tmp = string.len(v.server_name) + if tmp > max_len then + max_len = tmp end end end - totalplayers = totalplayers + players - end + local fmt = string.format("%%-%ds %%-8s %%-7s\n", max_len) + local text = string.format(fmt, "Server", "Version", "Players") - minecraft.notification_text = text - minecraft.totalplayers = totalplayers; - widget = minecraft.widget - settings() - end + 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 .. ' > ' .. p .. '\n' + end + end + end + + totalplayers = totalplayers + players + end + + minecraft.notification_text = text + minecraft.totalplayers = totalplayers; + widget = minecraft.widget + settings() + end + end) end minecraft.attach(minecraft.widget)