]> git.madduck.net Git - etc/awesome.git/blob - widgets/moc.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

9b1380ae817b6249df380ea04d618ff3fb2979a9
[etc/awesome.git] / widgets / moc.lua
1 local helpers = require("lain.helpers")
2 local async   = require("lain.asyncshell")
3
4 local escape_f = require("awful.util").escape
5 local naughty  = require("naughty")
6 local wibox    = require("wibox")
7
8 local io     = { popen   = io.popen }
9 local os     = { execute = os.execute,
10                  getenv  = os.getenv }
11 local string = { format  = string.format,
12                  gmatch  = string.gmatch }
13
14 local setmetatable = setmetatable
15
16 local moc = {}
17
18 local function worker(args)
19     local args = args or {}
20     local timeout = args.timeout or 2
21     local music_dir = args.music_dir or os.getenv("HOME") .. "/Music"
22     local cover_size = args.cover_size or 100
23     local default_art = args.default_art or ""
24     local settings = args.settings or function() end
25
26     local mpdcover = helpers.scripts_dir .. "mpdcover"
27
28     moc.widget = wibox.widget.textbox('')
29
30     moc_notification_preset = {
31         title = "Now playing",
32         timeout = 6
33     }
34
35     helpers.set_map("current moc track", nil)
36
37     function moc.update()
38         async.request("mocp -i", function(f)
39             moc_now = {
40                 state = "N/A",
41                 file  = "N/A",
42                 artist = "N/A",
43                 title = "N/A",
44                 album = "N/A",
45                 elapsed = "N/A",
46                 total = "N/A"
47             }
48
49             for line in f:lines() do
50                 for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
51                     if k == "State" then moc_now.state = v
52                     elseif k == "File" then moc_now.file = v
53                     elseif k == "Artist" then moc_now.artist = escape_f(v)
54                     elseif k == "SongTitle" then moc_now.title = escape_f(v)
55                     elseif k == "Album" then moc_now.album = escape_f(v)
56                     elseif k == "CurrentTime" then moc_now.elapsed = escape_f(v)
57                     elseif k == "TotalTime" then moc_now.total = escape_f(v)
58                     end
59                 end
60             end
61
62             moc_notification_preset.text = string.format("%s (%s) - %s\n%s", moc_now.artist,
63                                            moc_now.album, moc_now.total, moc_now.title)
64             widget = moc.widget
65             settings()
66
67             if moc_now.state == "PLAY" then
68                 if moc_now.title ~= helpers.get_map("current moc track") then
69                     helpers.set_map("current moc track", moc_now.title)
70                     os.execute(string.format("%s %q %q %d %q", mpdcover, "",
71                                moc_now.file, cover_size, default_art))
72
73                     moc.id = naughty.notify({
74                         preset = moc_notification_preset,
75                         icon = "/tmp/mpdcover.png",
76                         replaces_id = moc.id,
77                     }).id
78                 end
79             elseif  moc_now.state ~= "PAUSE" then
80                 helpers.set_map("current moc track", nil)
81             end
82         end)
83     end
84
85     helpers.newtimer("moc", timeout, moc.update)
86
87     return setmetatable(moc, { __index = moc.widget })
88 end
89
90 return setmetatable(moc, { __call = function(_, ...) return worker(...) end })