| 1 | -- contrib/countfiles_all.lua |
| 2 | -- Copyright (C) 2017 Jörg Thalheim <[email protected]> |
| 3 | -- |
| 4 | -- This file is part of Vicious. |
| 5 | -- |
| 6 | -- Vicious is free software: you can redistribute it and/or modify |
| 7 | -- it under the terms of the GNU General Public License as |
| 8 | -- published by the Free Software Foundation, either version 2 of the |
| 9 | -- License, or (at your option) any later version. |
| 10 | -- |
| 11 | -- Vicious is distributed in the hope that it will be useful, |
| 12 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | -- GNU General Public License for more details. |
| 15 | -- |
| 16 | -- You should have received a copy of the GNU General Public License |
| 17 | -- along with Vicious. If not, see <https://www.gnu.org/licenses/>. |
| 18 | |
| 19 | -- {{{ Grab environment |
| 20 | local io = { popen = io.popen } |
| 21 | local setmetatable = setmetatable |
| 22 | local pairs = pairs |
| 23 | -- }}} |
| 24 | |
| 25 | -- countfiles: provides a number of files in several directories |
| 26 | -- @warg.paths a table with the paths which should be checked |
| 27 | -- @warg.pattern a global regex to match files (Default: match all) |
| 28 | -- use posix-egrep style instead of the default (less familiar) emacs regex |
| 29 | |
| 30 | -- Be carefull with directories, who contains a mass of files. |
| 31 | -- "find" is usally fast, but will also produce delays, if the inodes get to big. |
| 32 | -- So if you want to count your music library, you may want to use locate/updatedb instead. |
| 33 | |
| 34 | -- vicious.contrib.countfiles |
| 35 | local countfiles_all = {} |
| 36 | |
| 37 | -- {{{ Sum up widget type |
| 38 | local function worker(format, warg) |
| 39 | if not warg then return end |
| 40 | -- Initialise counter table |
| 41 | local store = {} |
| 42 | |
| 43 | -- Match by default all files |
| 44 | warg.pattern = warg.pattern or ".*" |
| 45 | |
| 46 | for key, value in pairs(warg.paths) do |
| 47 | local f = io.popen("find '"..value.."'".. |
| 48 | " -type f -regextype posix-egrep".. |
| 49 | " -regex '"..warg.pattern.."'") |
| 50 | |
| 51 | local lines = 0 |
| 52 | for line in f:lines() do |
| 53 | lines = lines + 1 |
| 54 | end |
| 55 | |
| 56 | store[key] = (store[key] or 0) + lines |
| 57 | |
| 58 | f:close() |
| 59 | end |
| 60 | return store |
| 61 | end |
| 62 | -- }}} |
| 63 | |
| 64 | setmetatable(countfiles_all, { __call = function(_, ...) return worker(...) end }) |