From f5060093c66e47a509ac2611cfea5af3c1c5c4e4 Mon Sep 17 00:00:00 2001 From: mutlusun Date: Wed, 31 Jul 2019 18:46:01 +0200 Subject: [PATCH] [os_all] Splitted into os_linux and os_bsd --- widgets/os_bsd.lua | 47 ++++++++++++++++++++++++++++ widgets/{os_all.lua => os_linux.lua} | 20 ++---------- 2 files changed, 50 insertions(+), 17 deletions(-) create mode 100644 widgets/os_bsd.lua rename widgets/{os_all.lua => os_linux.lua} (76%) diff --git a/widgets/os_bsd.lua b/widgets/os_bsd.lua new file mode 100644 index 0000000..cfe2170 --- /dev/null +++ b/widgets/os_bsd.lua @@ -0,0 +1,47 @@ +--------------------------------------------------- +-- Licensed under the GNU General Public License v2 +-- * (c) 2010, Adrian C. +--------------------------------------------------- + +-- {{{ Grab environment +local los = { getenv = os.getenv } +local string = { match = string.match } +local helpers = require("vicious.helpers") +local spawn = require("vicious.spawn") +-- }}} + + +-- OS: provides operating system information +-- vicious.widgets.os +local os_bsd = {} + + +-- {{{ Operating system widget type +local function parse(stdout, stderr, exitreason, exitcode) + local system = { + ["ostype"] = "N/A", + ["hostname"] = "N/A", + ["osrelease"] = "N/A", + ["username"] = "N/A", + ["entropy"] = "N/A", + ["entropy_p"] = "N/A" + } + + -- BSD manual page: uname(1) + system["ostype"], system["hostname"], system["osrelease"] = + string.match(stdout, "([%w]+)[%s]([%w%p]+)[%s]([%w%p]+)") + + -- Get user from the environment + system["username"] = los.getenv("USER") + + return {system["ostype"], system["osrelease"], system["username"], + system["hostname"], system["entropy"], system["entropy_p"]} +end + +function os_bsd.async(format, warg, callback) + spawn.easy_async("uname -snr", + function (...) callback(parse(...)) end) +end +-- }}} + +return helpers.setasyncall(os_bsd) diff --git a/widgets/os_all.lua b/widgets/os_linux.lua similarity index 76% rename from widgets/os_all.lua rename to widgets/os_linux.lua index 4c41338..1a6eec7 100644 --- a/widgets/os_all.lua +++ b/widgets/os_linux.lua @@ -6,21 +6,17 @@ -- {{{ Grab environment local pairs = pairs local tonumber = tonumber -local io = { popen = io.popen } local math = { ceil = math.ceil } local los = { getenv = os.getenv } local setmetatable = setmetatable local helpers = require("vicious.helpers") -local string = { - gsub = string.gsub, - match = string.match -} +local string = { gsub = string.gsub } -- }}} -- OS: provides operating system information -- vicious.widgets.os -local os_all = {} +local os_linux = {} -- {{{ Operating system widget type @@ -42,16 +38,6 @@ local function worker(format) end end - -- BSD manual page: uname(1) - if system["ostype"] == "N/A" then - local f = io.popen("uname -snr") - local uname = f:read("*line") - f:close() - - system["ostype"], system["hostname"], system["osrelease"] = - string.match(uname, "([%w]+)[%s]([%w%p]+)[%s]([%w%p]+)") - end - -- Linux manual page: random(4) if kernel.random then -- Linux 2.6 default entropy pool is 4096-bits @@ -70,4 +56,4 @@ local function worker(format) end -- }}} -return setmetatable(os_all, { __call = function(_, ...) return worker(...) end }) +return setmetatable(os_linux, { __call = function(_, ...) return worker(...) end })