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

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