2009-09-29 22:33:19 +02:00
|
|
|
---------------------------------------------------
|
|
|
|
-- Licensed under the GNU General Public License v2
|
2010-01-02 21:21:54 +01:00
|
|
|
-- * (c) 2010, Adrian C. <anrxc@sysphere.org>
|
2009-09-29 22:33:19 +02:00
|
|
|
---------------------------------------------------
|
Import of vicious source tree.
Vicious is a modular widget library for 'awesome' window manager,
derived from the 'Wicked' widget library.
Summary of changes:
* Original wicked code modularized
* Widgets ported from Wicked:
- CPU, MEM, FS, NET, Date, Uptime, MPD
* CPU widget rewritten, uses pattern matching
* MEM widget rewritten, uses pattern matching
- Swap widget merged with MEM widget type
* FS widget rewritten, uses pattern matching
- Also fixed padding in the process
* NET widget rewritten, uses pattern matching
* MPD widget rewritten, a bit more versatile
* Removed deprecated helper functions
* Widgets written for Vicious:
- Thermal, Battery, Mbox, OrgMode, Volume, Entropy,
Disk I/O, System Load, Wireless, Pacman, Maildir
2009-07-29 17:59:32 +02:00
|
|
|
|
|
|
|
-- {{{ Grab environment
|
2010-03-12 21:56:17 +01:00
|
|
|
local tonumber = tonumber
|
Import of vicious source tree.
Vicious is a modular widget library for 'awesome' window manager,
derived from the 'Wicked' widget library.
Summary of changes:
* Original wicked code modularized
* Widgets ported from Wicked:
- CPU, MEM, FS, NET, Date, Uptime, MPD
* CPU widget rewritten, uses pattern matching
* MEM widget rewritten, uses pattern matching
- Swap widget merged with MEM widget type
* FS widget rewritten, uses pattern matching
- Also fixed padding in the process
* NET widget rewritten, uses pattern matching
* MPD widget rewritten, a bit more versatile
* Removed deprecated helper functions
* Widgets written for Vicious:
- Thermal, Battery, Mbox, OrgMode, Volume, Entropy,
Disk I/O, System Load, Wireless, Pacman, Maildir
2009-07-29 17:59:32 +02:00
|
|
|
local io = { popen = io.popen }
|
2009-08-01 23:11:41 +02:00
|
|
|
local setmetatable = setmetatable
|
2010-03-12 21:56:17 +01:00
|
|
|
local string = { gmatch = string.gmatch }
|
2009-11-03 01:19:37 +01:00
|
|
|
local helpers = require("vicious.helpers")
|
2019-03-04 04:23:23 +01:00
|
|
|
local math = { floor = math.floor }
|
Import of vicious source tree.
Vicious is a modular widget library for 'awesome' window manager,
derived from the 'Wicked' widget library.
Summary of changes:
* Original wicked code modularized
* Widgets ported from Wicked:
- CPU, MEM, FS, NET, Date, Uptime, MPD
* CPU widget rewritten, uses pattern matching
* MEM widget rewritten, uses pattern matching
- Swap widget merged with MEM widget type
* FS widget rewritten, uses pattern matching
- Also fixed padding in the process
* NET widget rewritten, uses pattern matching
* MPD widget rewritten, a bit more versatile
* Removed deprecated helper functions
* Widgets written for Vicious:
- Thermal, Battery, Mbox, OrgMode, Volume, Entropy,
Disk I/O, System Load, Wireless, Pacman, Maildir
2009-07-29 17:59:32 +02:00
|
|
|
-- }}}
|
|
|
|
|
|
|
|
|
2010-03-12 21:56:17 +01:00
|
|
|
-- Mpd: provides Music Player Daemon information
|
2012-06-15 18:07:05 +02:00
|
|
|
-- vicious.widgets.mpd
|
2017-01-25 17:53:46 +01:00
|
|
|
local mpd_all = {}
|
Import of vicious source tree.
Vicious is a modular widget library for 'awesome' window manager,
derived from the 'Wicked' widget library.
Summary of changes:
* Original wicked code modularized
* Widgets ported from Wicked:
- CPU, MEM, FS, NET, Date, Uptime, MPD
* CPU widget rewritten, uses pattern matching
* MEM widget rewritten, uses pattern matching
- Swap widget merged with MEM widget type
* FS widget rewritten, uses pattern matching
- Also fixed padding in the process
* NET widget rewritten, uses pattern matching
* MPD widget rewritten, a bit more versatile
* Removed deprecated helper functions
* Widgets written for Vicious:
- Thermal, Battery, Mbox, OrgMode, Volume, Entropy,
Disk I/O, System Load, Wireless, Pacman, Maildir
2009-07-29 17:59:32 +02:00
|
|
|
|
|
|
|
|
2018-12-14 11:26:36 +01:00
|
|
|
-- {{{ Return true if number is nonzero
|
|
|
|
local function cbool(number)
|
|
|
|
return type(number) == "number" and number ~= 0 or number
|
|
|
|
end
|
|
|
|
-- }}}
|
|
|
|
|
2018-12-14 16:31:08 +01:00
|
|
|
-- {{{ Format playing progress
|
|
|
|
function format_progress(elapsed, duration)
|
2019-03-04 04:23:23 +01:00
|
|
|
local em, es = math.floor(elapsed / 60), math.floor(elapsed % 60)
|
|
|
|
local dm, ds = math.floor(duration / 60), math.floor(duration % 60)
|
2018-12-14 16:31:08 +01:00
|
|
|
|
|
|
|
if dm < 10 then
|
|
|
|
return ("%d:%02d"):format(em, es), ("%d:%02d"):format(dm, ds)
|
|
|
|
elseif dm < 60 then
|
|
|
|
return ("%02d:%02d"):format(em, es), ("%02d:%02d"):format(dm, ds)
|
|
|
|
elseif dm < 600 then
|
2019-03-15 14:34:48 +01:00
|
|
|
return ("%d:%02d:%02d"):format(math.floor(em / 60), math.floor(em % 60), es),
|
|
|
|
("%d:%02d:%02d"):format(math.floor(dm / 60), math.floor(dm % 60), ds)
|
2018-12-14 16:31:08 +01:00
|
|
|
else
|
2019-03-15 14:34:48 +01:00
|
|
|
return ("%02d:%02d:%02d"):format(math.floor(em / 60), math.floor(em % 60), es),
|
|
|
|
("%02d:%02d:%02d"):format(math.floor(dm / 60), math.floor(dm % 60), ds)
|
2018-12-14 16:31:08 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
-- }}}
|
2018-12-14 11:26:36 +01:00
|
|
|
|
Import of vicious source tree.
Vicious is a modular widget library for 'awesome' window manager,
derived from the 'Wicked' widget library.
Summary of changes:
* Original wicked code modularized
* Widgets ported from Wicked:
- CPU, MEM, FS, NET, Date, Uptime, MPD
* CPU widget rewritten, uses pattern matching
* MEM widget rewritten, uses pattern matching
- Swap widget merged with MEM widget type
* FS widget rewritten, uses pattern matching
- Also fixed padding in the process
* NET widget rewritten, uses pattern matching
* MPD widget rewritten, a bit more versatile
* Removed deprecated helper functions
* Widgets written for Vicious:
- Thermal, Battery, Mbox, OrgMode, Volume, Entropy,
Disk I/O, System Load, Wireless, Pacman, Maildir
2009-07-29 17:59:32 +02:00
|
|
|
-- {{{ MPD widget type
|
2009-11-03 01:19:37 +01:00
|
|
|
local function worker(format, warg)
|
2018-12-14 11:26:36 +01:00
|
|
|
-- Fallback values
|
|
|
|
local mpd_state = {
|
|
|
|
["{volume}"] = 0,
|
|
|
|
["{bitrate}"] = 0,
|
|
|
|
["{elapsed}"] = 0,
|
|
|
|
["{duration}"] = 0,
|
|
|
|
["{repeat}"] = false,
|
|
|
|
["{random}"] = false,
|
|
|
|
["{state}"] = "N/A",
|
|
|
|
["{Artist}"] = "N/A",
|
|
|
|
["{Title}"] = "N/A",
|
|
|
|
["{Album}"] = "N/A",
|
|
|
|
["{Genre}"] = "N/A",
|
|
|
|
--["{Name}"] = "N/A",
|
|
|
|
--["{file}"] = "N/A",
|
2010-03-12 21:56:17 +01:00
|
|
|
}
|
|
|
|
|
2018-12-14 11:26:36 +01:00
|
|
|
-- Construct MPD client options, fallback to defaults when necessary
|
|
|
|
local query = ("printf 'password %s\nstatus\ncurrentsong\nclose\n'"):format(
|
|
|
|
warg and (warg.password or warg[1]) or '""')
|
|
|
|
local connect = ("curl --connect-timeout 1 -fsm 3 telnet://%s:%s"):format(
|
|
|
|
warg and (warg.host or warg[2]) or "127.0.0.1",
|
|
|
|
warg and (warg.port or warg[3]) or "6600")
|
2010-03-12 21:56:17 +01:00
|
|
|
|
|
|
|
-- Get data from MPD server
|
2018-12-14 11:26:36 +01:00
|
|
|
local f = io.popen(query .. "|" .. connect)
|
2010-03-12 21:56:17 +01:00
|
|
|
for line in f:lines() do
|
|
|
|
for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
|
2018-10-21 19:20:01 +02:00
|
|
|
local key = "{" .. k .. "}"
|
2018-12-14 11:26:36 +01:00
|
|
|
if k == "volume" or k == "bitrate" or
|
|
|
|
k == "elapsed" or k == "duration" then
|
2018-10-21 19:20:01 +02:00
|
|
|
mpd_state[key] = v and tonumber(v)
|
2018-12-14 11:26:36 +01:00
|
|
|
elseif k == "repeat" or k == "random" then
|
|
|
|
mpd_state[key] = cbool(v)
|
2018-10-21 19:20:01 +02:00
|
|
|
elseif k == "state" then
|
|
|
|
mpd_state[key] = helpers.capitalize(v)
|
2018-12-14 11:26:36 +01:00
|
|
|
elseif k == "Artist" or k == "Title" or
|
2018-10-21 19:20:01 +02:00
|
|
|
--k == "Name" or k == "file" or
|
|
|
|
k == "Album" or k == "Genre" then
|
|
|
|
mpd_state[key] = v
|
2010-03-12 21:56:17 +01:00
|
|
|
end
|
2009-11-03 01:19:37 +01:00
|
|
|
end
|
|
|
|
end
|
2010-03-12 21:56:17 +01:00
|
|
|
f:close()
|
Import of vicious source tree.
Vicious is a modular widget library for 'awesome' window manager,
derived from the 'Wicked' widget library.
Summary of changes:
* Original wicked code modularized
* Widgets ported from Wicked:
- CPU, MEM, FS, NET, Date, Uptime, MPD
* CPU widget rewritten, uses pattern matching
* MEM widget rewritten, uses pattern matching
- Swap widget merged with MEM widget type
* FS widget rewritten, uses pattern matching
- Also fixed padding in the process
* NET widget rewritten, uses pattern matching
* MPD widget rewritten, a bit more versatile
* Removed deprecated helper functions
* Widgets written for Vicious:
- Thermal, Battery, Mbox, OrgMode, Volume, Entropy,
Disk I/O, System Load, Wireless, Pacman, Maildir
2009-07-29 17:59:32 +02:00
|
|
|
|
2018-12-14 11:26:36 +01:00
|
|
|
-- Formatted elapsed and duration
|
2018-12-14 16:31:08 +01:00
|
|
|
mpd_state["{Elapsed}"], mpd_state["{Duration}"] = format_progress(
|
2018-12-14 11:26:36 +01:00
|
|
|
mpd_state["{elapsed}"], mpd_state["{duration}"])
|
|
|
|
|
2010-03-12 21:56:17 +01:00
|
|
|
return mpd_state
|
Import of vicious source tree.
Vicious is a modular widget library for 'awesome' window manager,
derived from the 'Wicked' widget library.
Summary of changes:
* Original wicked code modularized
* Widgets ported from Wicked:
- CPU, MEM, FS, NET, Date, Uptime, MPD
* CPU widget rewritten, uses pattern matching
* MEM widget rewritten, uses pattern matching
- Swap widget merged with MEM widget type
* FS widget rewritten, uses pattern matching
- Also fixed padding in the process
* NET widget rewritten, uses pattern matching
* MPD widget rewritten, a bit more versatile
* Removed deprecated helper functions
* Widgets written for Vicious:
- Thermal, Battery, Mbox, OrgMode, Volume, Entropy,
Disk I/O, System Load, Wireless, Pacman, Maildir
2009-07-29 17:59:32 +02:00
|
|
|
end
|
|
|
|
-- }}}
|
2009-08-01 23:11:41 +02:00
|
|
|
|
2017-01-25 17:53:46 +01:00
|
|
|
return setmetatable(mpd_all, { __call = function(_, ...) return worker(...) end })
|