Debian Codegraph @

obvious/basic_mpd/init.lua

raw symbols SCIP
1 ------------------------------------------
2 -- Author: Andrei "Garoth" Thorp --
3 -- Copyright 2009 Andrei "Garoth" Thorp --
4 ------------------------------------------
5
6 local setmetatable = setmetatable
7 local pairs = pairs
8 local type = type
9 local string = string
10 local awful = require("awful")
11 local wibox = require('wibox')
12 local naughty = require("naughty")
13 local lib = {
14 mpd = require("obvious.lib.mpd"),
15 markup = require("obvious.lib.markup"),
16 hooks = require("obvious.lib.hooks"),
17 }
18
19 local defaults = {
20 format = "$title - $album - $artist",
21 length = 75,
22 unknown = "(unknown)",
23 update_interval = 1,
24 }
25 local settings = {}
26 for key, value in pairs(defaults) do
27 settings[key] = value
28 end
29
30 local widget = wibox.widget.textbox()
31
32 local connection = lib.mpd.new()
33
34 local update -- forward declaration for update()
35
36 -- Utility function to handle the text for MPD
37 -- @param songinfo: a table with fields "artist", "album", "title" in text
38 -- @return formatted (settings.format) string to display on the widget. This
39 -- respects settings.length and tries to make fields as close to the same
40 -- lenghths as possible if shortening is required.
41 local function format_metadata(songinfo)
42 local format = settings.format or defaults.format
43
44 if (settings.length or defaults.length) <= 0 then
45 return ""
46 end
47
48 local used_keys = {}
49 local start = 1
50 local stop = 1
51 while start do
52 local key
53 start, stop = string.find(format, "%$%w+", stop)
54 key = string.match(format, "%$(%w+)", stop)
55 if key then
56 if songinfo[key] then
57 used_keys[key] = songinfo[key]
58 else
59 used_keys[key] = settings.unknown or defaults.unknown
60 end
61 end
62 end
63
64 local retval = ''
65 while true do
66 retval = string.gsub(format, "%$(%w+)", used_keys)
67 if #retval > (settings.length or defaults.length) then
68 local longest_key = nil
69 local longest_value = ''
70 for key, value in pairs(used_keys) do
71 if #value > #longest_value then
72 longest_key = key
73 longest_value = value
74 end
75 end
76 if longest_key then
77 -- shorten the longest by 1
78 used_keys[longest_key] = string.sub( used_keys[longest_key],
79 1,
80 #longest_value - 1)
81 else
82 -- Seems like the format itself's too long
83 local err = "obvious.basic_mpd: impossible to fit " ..
84 "output into " .. (settings.length or
85 defaults.length) .. " characters.\n" ..
86 "Widget paused."
87 naughty.notify({ text = err, timeout = 0 })
88 lib.hooks.timer.stop(update)
89 return ""
90 end
91 else
92 -- All good!
93 break
94 end
95 end
96 return awful.util.escape(retval)
97 end
98
99 -- Updates the widget's display
100 --[[local]] function update()
101 local status = connection:send("status")
102 local now_playing, songstats
103
104 if not status.state then
105 now_playing = "Music Off"
106 now_playing = lib.markup.fg.color("yellow", now_playing)
107 elseif status.state == "stop" then
108 now_playing = "Music Stopped"
109 else
110 songstats = connection:send("playlistid " .. status.songid)
111 local format = settings.format or defaults.format
112 if type(format) == "string" then
113 now_playing = format_metadata(songstats)
114 elseif type(format) == "function" then
115 now_playing = format(songstats)
116 else
117 naughty.notify({ text = "obvious.basic_mpd: Invalid " ..
118 "display format. Widget " ..
119 "paused." })
120 lib.hooks.timer.stop(update)
121 end
122 end
123
124 widget:set_markup(now_playing)
125 end
126 update()
127 lib.hooks.timer.register(settings.update_interval, 30, update, "basic_mpd widget refresh rate")
128
129 -- SETTINGS
130 -- Set the format string
131 -- @param format The format string
132 local function set_format(format)
133 settings.format = format or defaults.format
134 update()
135 end
136
137 -- Set the widget's text max length
138 -- @param format The max length (in characters) of the widget's text
139 local function set_length(length)
140 settings.length = length or defaults.length
141 update()
142 end
143
144 -- Set the string to use for unknown metadata
145 -- @param format The string to use for unknown metadata
146 local function set_unknown(unknown)
147 settings.unknown = unknown or defaults.unknown
148 update()
149 end
150
151 local function set_update_interval(t)
152 settings.update_interval = t or defaults.update_interval
153 update()
154 end
155
156 return setmetatable({
157 set_format = set_format,
158 set_length = set_length,
159 set_unknown = set_unknown,
160 set_update_interval = set_update_interval,
161 }, { __call = function () return widget end })
162
163 -- vim:ft=lua:ts=2:sw=2:sts=2:tw=80:et