]> git.madduck.net Git - etc/awesome.git/blob - widgets/contrib/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:

fixing typo mentioned in issues
[etc/awesome.git] / widgets / contrib / moc.lua
1
2 --[[
3                                                                   
4      Licensed under GNU General Public License v2                 
5       * (c) 2014, anticlockwise <http://github.com/anticlockwise> 
6                                                                   
7 --]]
8
9 local helpers  = require("lain.helpers")
10 local async    = require("lain.asyncshell")
11
12 local focused  = require("awful.screen").focused
13 local escape_f = require("awful.util").escape
14 local naughty  = require("naughty")
15 local wibox    = require("wibox")
16
17 local io       = { popen   = io.popen }
18 local os       = { execute = os.execute,
19                    getenv  = os.getenv }
20 local string   = { format  = string.format,
21                    gmatch  = string.gmatch }
22
23 local setmetatable = setmetatable
24
25 -- MOC audio player
26 -- lain.widgets.contrib.moc
27 local moc = {}
28
29 local function worker(args)
30     local args        = args or {}
31     local timeout     = args.timeout or 2
32     local music_dir   = args.music_dir or os.getenv("HOME") .. "/Music"
33     local cover_size  = args.cover_size or 100
34     local default_art = args.default_art or ""
35     local followtag    = args.followtag or false
36     local settings    = args.settings or function() end
37
38     local mpdcover = helpers.scripts_dir .. "mpdcover"
39
40     moc.widget = wibox.widget.textbox('')
41
42     moc_notification_preset = {
43         title   = "Now playing",
44         timeout = 6
45     }
46
47     helpers.set_map("current moc track", nil)
48
49     function moc.update()
50         -- mocp -i will produce output like:
51         -- Artist: Travis
52         -- Album: The Man Who
53         -- etc.
54         async.request("mocp -i", function(f)
55             moc_now = {
56                 state   = "N/A",
57                 file    = "N/A",
58                 artist  = "N/A",
59                 title   = "N/A",
60                 album   = "N/A",
61                 elapsed = "N/A",
62                 total   = "N/A"
63             }
64
65             for line in string.gmatch(f, "[^\n]+") do
66                 for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
67                     if k == "State" then moc_now.state = v
68                     elseif k == "File" then moc_now.file = v
69                     elseif k == "Artist" then moc_now.artist = escape_f(v)
70                     elseif k == "SongTitle" then moc_now.title = escape_f(v)
71                     elseif k == "Album" then moc_now.album = escape_f(v)
72                     elseif k == "CurrentTime" then moc_now.elapsed = escape_f(v)
73                     elseif k == "TotalTime" then moc_now.total = escape_f(v)
74                     end
75                 end
76             end
77
78             moc_notification_preset.text = string.format("%s (%s) - %s\n%s", moc_now.artist,
79                                            moc_now.album, moc_now.total, moc_now.title)
80             widget = moc.widget
81             settings()
82
83             if moc_now.state == "PLAY" then
84                 if moc_now.title ~= helpers.get_map("current moc track") then
85                     helpers.set_map("current moc track", moc_now.title)
86                     os.execute(string.format("%s %q %q %d %q", mpdcover, "",
87                                moc_now.file, cover_size, default_art))
88
89                     if followtag then
90                         moc_notification_preset.screen = focused()
91                     end
92
93                     moc.id = naughty.notify({
94                         preset = moc_notification_preset,
95                         icon = "/tmp/mpdcover.png",
96                         replaces_id = moc.id,
97                     }).id
98                 end
99             elseif  moc_now.state ~= "PAUSE" then
100                 helpers.set_map("current moc track", nil)
101             end
102         end)
103     end
104
105     helpers.newtimer("moc", timeout, moc.update)
106
107     return setmetatable(moc, { __index = moc.widget })
108 end
109
110 return setmetatable(moc, { __call = function(_, ...) return worker(...) end })