| 1 | -- filesystem widget type |
| 2 | -- Copyright (C) 2009 Lucas de Vries <[email protected]> |
| 3 | -- Copyright (C) 2010 Adrian C. <[email protected]> |
| 4 | -- Copyright (C) 2017 Joerg Thalheim <[email protected]> |
| 5 | -- Copyright (C) 2017 mutlusun <[email protected]> |
| 6 | -- Copyright (C) 2019 Nguyễn Gia Phong <[email protected]m> |
| 7 | -- |
| 8 | -- This file is part of Vicious. |
| 9 | -- |
| 10 | -- Vicious is free software: you can redistribute it and/or modify |
| 11 | -- it under the terms of the GNU General Public License as |
| 12 | -- published by the Free Software Foundation, either version 2 of the |
| 13 | -- License, or (at your option) any later version. |
| 14 | -- |
| 15 | -- Vicious is distributed in the hope that it will be useful, |
| 16 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | -- GNU General Public License for more details. |
| 19 | -- |
| 20 | -- You should have received a copy of the GNU General Public License |
| 21 | -- along with Vicious. If not, see <https://www.gnu.org/licenses/>. |
| 22 | |
| 23 | -- {{{ Grab environment |
| 24 | local tonumber = tonumber |
| 25 | |
| 26 | local helpers = require"vicious.helpers" |
| 27 | local spawn = require"vicious.spawn" |
| 28 | -- }}} |
| 29 | |
| 30 | -- Mebibyte and gibibyte respectively, because backward compatibility |
| 31 | local UNIT = { mb = 1024, gb = 1024^2 } |
| 32 | |
| 33 | -- FS: provides file system disk space usage |
| 34 | -- vicious.widgets.fs |
| 35 | return helpers.setasyncall{ |
| 36 | async = function(format, warg, callback) |
| 37 | local fs_info = {} -- Get data from df |
| 38 | spawn.with_line_callback_with_shell( |
| 39 | warg and "LC_ALL=C df -kP" or "LC_ALL=C df -klP", |
| 40 | { stdout = function (line) |
| 41 | -- (1024-blocks) (Used) (Available) (Capacity)% (Mounted on) |
| 42 | local s, u, a, p, m = line:match( |
| 43 | "^.-%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%%%s+([%p%w]+)") |
| 44 | |
| 45 | if u and m then -- Handle 1st line and broken regexp |
| 46 | helpers.uformat(fs_info, m .. " size", s, UNIT) |
| 47 | helpers.uformat(fs_info, m .. " used", u, UNIT) |
| 48 | helpers.uformat(fs_info, m .. " avail", a, UNIT) |
| 49 | |
| 50 | fs_info["{" .. m .. " used_p}"] = tonumber(p) |
| 51 | fs_info["{" .. m .. " avail_p}"] = 100 - tonumber(p) |
| 52 | end |
| 53 | end, |
| 54 | output_done = function () callback(fs_info) end }) |
| 55 | end } |