From 565d74cc8bc4df9a0044f64de1fb960ef3912ab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Gia=20Phong?= Date: Sat, 22 Jun 2019 17:25:18 +0700 Subject: [PATCH] [fs] Deprecate io.popen Also clarify unit prefix convention --- Changes.md | 2 +- README.md | 1 + widgets/fs_all.lua | 64 +++++++++++++++++----------------------------- 3 files changed, 26 insertions(+), 41 deletions(-) diff --git a/Changes.md b/Changes.md index 85ba41d..0836c31 100644 --- a/Changes.md +++ b/Changes.md @@ -20,7 +20,7 @@ Fixed: - Deprecate the use of `io.popen` in following widgets: * wifi_linux, wifiiw_linux, hwmontemp_linux * bat_freebsd, mem_freebsd, net_freebsd - * volume, gmail, mdir, mpd + * volume, gmail, mdir, mpd, fs - [mpd] Lua 5.3 compatibility (for real this time); also correct a typo - [pkg,weather,contrib/btc] Allow function call without Awesome - [pkg] Use more updated front-ends for Debian/Ubuntu (apt) and Fedora (dnf) diff --git a/README.md b/README.md index 56da125..f88b39a 100644 --- a/README.md +++ b/README.md @@ -236,6 +236,7 @@ Supported platforms: platform independent. * Returns a table with string keys, using mount points as a base, e.g. `${/ size_mb}`, `${/ size_gb}`, `${/ used_mb}`, `${/ used_gb}`, `${/ used_p}`, `${/ avail_mb}`, `${/ avail_gb}`, `${/ avail_p}`, `${/home size_mb}`, etc. + mb and gb refer to mebibyte and gibibyte respectively. ### vicious.widgets.gmail diff --git a/widgets/fs_all.lua b/widgets/fs_all.lua index b577431..9c4e5d0 100644 --- a/widgets/fs_all.lua +++ b/widgets/fs_all.lua @@ -6,50 +6,34 @@ -- {{{ Grab environment local tonumber = tonumber -local io = { popen = io.popen } -local setmetatable = setmetatable -local string = { match = string.match } -local helpers = require("vicious.helpers") + +local helpers = require"vicious.helpers" +local spawn = require"vicious.spawn" -- }}} +-- Mebibyte and gibibyte respectively, because backward compatibility +local UNIT = { mb = 1024, gb = 1024^2 } -- FS: provides file system disk space usage -- vicious.widgets.fs -local fs_all = {} +return helpers.setasyncall{ + async = function(format, warg, callback) + local fs_info = {} -- Get data from df + spawn.with_line_callback_with_shell( + warg and "LC_ALL=C df -kP" or "LC_ALL=C df -klP", + { stdout = function (line) + -- (1024-blocks) (Used) (Available) (Capacity)% (Mounted on) + local s, u, a, p, m = line:match( + "^.-%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%%%s+([%p%w]+)") + if u and m then -- Handle 1st line and broken regexp + helpers.uformat(fs_info, m .. " size", s, UNIT) + helpers.uformat(fs_info, m .. " used", u, UNIT) + helpers.uformat(fs_info, m .. " avail", a, UNIT) --- Variable definitions -local unit = { ["mb"] = 1024, ["gb"] = 1024^2 } - --- {{{ Filesystem widget type -local function worker(format, warg) - local cmd = "LC_ALL=C df -kP" - if not warg then - -- List only local filesystems by default - cmd = cmd .. " -l" - end - - local fs_info = {} -- Get data from df - local f = io.popen(cmd) - - for line in f:lines() do -- Match: (size) (used)(avail)(use%) (mount) - local s = string.match(line, "^.-[%s]([%d]+)") - local u,a,p = string.match(line, "([%d]+)[%D]+([%d]+)[%D]+([%d]+)%%") - local m = string.match(line, "%%[%s]+([%p%w]+)") - - if u and m then -- Handle 1st line and broken regexp - helpers.uformat(fs_info, m .. " size", s, unit) - helpers.uformat(fs_info, m .. " used", u, unit) - helpers.uformat(fs_info, m .. " avail", a, unit) - - fs_info["{" .. m .. " used_p}"] = tonumber(p) - fs_info["{" .. m .. " avail_p}"] = 100 - tonumber(p) - end - end - f:close() - - return fs_info -end --- }}} - -return setmetatable(fs_all, { __call = function(_, ...) return worker(...) end }) + fs_info["{" .. m .. " used_p}"] = tonumber(p) + fs_info["{" .. m .. " avail_p}"] = 100 - tonumber(p) + end + end, + output_done = function () callback(fs_info) end }) + end }