-- Licensed under GNU General Public License v2 2014 Yauheni Kirylau -

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
--[[
Licensed under GNU General Public License v2
* (c) 2014, Yauheni Kirylau
--]]
local awful = require("awful")
local string = { format = string.format,
match = string.match }
local helpers = require("actionless.helpers")
local parse = require("actionless.parse")
local async = require("actionless.async")
local mpd = {
player_cmd = 'urxvt -e ncmpcpp'
}
local cover_script = helpers.scripts_dir .. "mpdcover"
function mpd.init(args)
local args = args or {}
mpd.music_dir = args.music_dir or os.getenv("HOME") .. "/Music"
mpd.host = args.host or "127.0.0.1"
mpd.port = args.port or "6600"
mpd.password = args.password or [[""]]
end
-------------------------------------------------------------------------------
function mpd.toggle()
awful.util.spawn_with_shell(
"mpc toggle || ncmpcpp toggle")
end
function mpd.next_song()
awful.util.spawn_with_shell(
"mpc next || ncmpcpp next")
end
function mpd.prev_song()
awful.util.spawn_with_shell(
"mpc prev || ncmpcpp prev")
end
-------------------------------------------------------------------------------
function mpd.update(parse_status_callback)
async.execute(
[[mpc --format "file:%file%
Artist:%artist%
Title:%title%
Album:%album%
Date:%date%"
]],
-- "function( -- <==workaround for syntax highlighter :) @TODO
function(str) mpd.parse_metadata(str, parse_status_callback) end)
end
-------------------------------------------------------------------------------
function mpd.parse_metadata(result_string, parse_status_callback)
local player_status = {}
local state = nil
if result_string:match("%[playing%]") then
state = 'play'
elseif result_string:match("%[paused%]") then
state = 'pause'
end
if state then
player_status = parse.find_values_in_result_stringing(
result_string, "([%w]+):(.*)$", {
file='file',
artist='Artist',
title='Title',
album='Album',
date='Date'})
end
player_status.state = state
parse_status_callback(player_status)
end
-------------------------------------------------------------------------------
function mpd.resize_cover(
player_status, cover_size, default_art, notification_callback
)
async.execute(
string.format(
"%s %q %q %d %q",
cover_script,
mpd.music_dir,
player_status.file,
cover_size,
default_art
),
function(f) notification_callback() end
)
end
return mpd