]> 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:

lain is now as asynchronous as possible
[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 shell        = require("awful.util").shell
11 local focused      = require("awful.screen").focused
12 local escape_f     = require("awful.util").escape
13 local naughty      = require("naughty")
14 local wibox        = require("wibox")
15 local os           = { getenv = os.getenv }
16 local string       = { format = string.format,
17                        gmatch = string.gmatch }
18 local setmetatable = setmetatable
19
20 -- MOC audio player
21 -- lain.widgets.contrib.moc
22 local moc = helpers.make_widget_textbox()
23
24 local function worker(args)
25     local args          = args or {}
26     local timeout       = args.timeout or 2
27     local music_dir     = args.music_dir or os.getenv("HOME") .. "/Music"
28     local cover_pattern = args.cover_pattern or "*\\.(jpg|jpeg|png|gif)$"
29     local cover_size    = args.cover_size or 100
30     local default_art   = args.default_art or ""
31     local followtag     = args.followtag or false
32     local settings      = args.settings or function() end
33
34     moc_notification_preset = { title = "Now playing", timeout = 6 }
35
36     helpers.set_map("current moc track", nil)
37
38     function moc.update()
39         helpers.async("mocp -i", function(f)
40             moc_now = {
41                 state   = "N/A",
42                 file    = "N/A",
43                 artist  = "N/A",
44                 title   = "N/A",
45                 album   = "N/A",
46                 elapsed = "N/A",
47                 total   = "N/A"
48             }
49
50             for line in string.gmatch(f, "[^\n]+") do
51                 for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
52                     if     k == "State"       then moc_now.state   = v
53                     elseif k == "File"        then moc_now.file    = v
54                     elseif k == "Artist"      then moc_now.artist  = escape_f(v)
55                     elseif k == "SongTitle"   then moc_now.title   = escape_f(v)
56                     elseif k == "Album"       then moc_now.album   = escape_f(v)
57                     elseif k == "CurrentTime" then moc_now.elapsed = escape_f(v)
58                     elseif k == "TotalTime"   then moc_now.total   = escape_f(v)
59                     end
60                 end
61             end
62
63             moc_notification_preset.text = string.format("%s (%s) - %s\n%s", moc_now.artist,
64                                            moc_now.album, moc_now.total, moc_now.title)
65             widget = moc.widget
66             settings()
67
68             if moc_now.state == "PLAY" then
69                 if moc_now.title ~= helpers.get_map("current moc track") then
70                     helpers.set_map("current moc track", moc_now.title)
71
72                     if followtag then moc_notification_preset.screen = focused() end
73
74                     local common =  {
75                         preset      = moc_notification_preset,
76                         icon        = default_art,
77                         icon_size   = cover_size,
78                         replaces_id = moc.id,
79                     }
80
81                     local path   = string.format("%s/%s", music_dir, string.match(moc_now.file, ".*/"))
82                     local cover  = string.format("find '%s' -maxdepth 1 -type f | egrep -i -m1 '%s'", path, cover_pattern)
83                     helpers.async({ shell, "-c", cover }, function(current_icon)
84                         common.icon = current_icon:gsub("\n", "")
85                         moc.id = naughty.notify(common).id
86                     end)
87                 end
88             elseif  moc_now.state ~= "PAUSE" then
89                 helpers.set_map("current moc track", nil)
90             end
91         end)
92     end
93
94     helpers.newtimer("moc", timeout, moc.update)
95
96     return setmetatable(moc, { __index = moc.widget })
97 end
98
99 return setmetatable(moc, { __call = function(_, ...) return worker(...) end })