Include notionflux in the main repo instead of its own submodule
[notion/jeffpc.git] / contrib / statusd / statusd_moc.lua
blob3288d4153795de95789cc432304e6a3a6e57d343
1 -- Authors: Unknown
2 -- License: Public domain
3 -- Last Changed: Unknown
4 --
5 -- statusd_moc.lua
6 --
7 -- Public domain
8 --
9 -- keys:
10 -- moc (moc_title + "[paused]", "[stopped]", or "[not playing]")
11 -- moc_state ("PAUSE", "PLAY", "STOP", or "OFF")
12 -- moc_state2 ("paused", "playing", "stopped", or "off")
13 -- moc_file }
14 -- moc_title }
15 -- moc_artist }
16 -- moc_songtitle }
17 -- moc_album }
18 -- moc_totaltime }-- (as in "mocp -i")
19 -- moc_currenttime }
20 -- moc_timeleft }
21 -- moc_totalsec }
22 -- moc_currentsec }
23 -- moc_bitrate }
24 -- moc_rate }
25 -- moc_leftsec (moc_totalsec - moc_currentsec)
26 -- moc_percent (current time / total time)
27 -- moc_progress (a progress bar, made of characters---see settings below)
29 -- settings:
30 -- update_interval
31 -- moc_dir (relative to ion's working directory)
32 -- progress_char1 (character for unfilled progress bar area)
33 -- progress_char2 (character for filled progress bar area)
34 -- progress_length (length of the progress bar in characters)
35 -- color_hints (boolean-- use color hints for the "state" montitors)
37 local home=os.getenv("HOME")
38 home=(home and home.."/" or "")
40 local defaults={
41 update_interval=3*1000,
42 moc_dir=home..".moc",
43 progress_char1=" ",
44 progress_char2="|",
45 progress_length="50",
46 color_hints=false,
49 local settings=table.join(statusd.get_config("moc"), defaults)
51 function do_query_mocp()
52 local f=io.popen('mocp -i')
53 local moc_status = f:read('*a')
54 f:close()
56 local i, j, moc_state = string.find( moc_status, 'State: (%C*)')
57 local i, j, moc_file = string.find( moc_status, 'File: (%C*)')
58 local i, j, moc_title = string.find( moc_status, 'Title: (%C*)')
59 local i, j, moc_artist = string.find( moc_status, 'Artist: (%C*)')
60 local i, j, moc_songtitle = string.find( moc_status, 'SongTitle: (%C*)')
61 local i, j, moc_album = string.find( moc_status, 'Album: (%C*)')
62 local i, j, moc_totaltime = string.find( moc_status, 'TotalTime: (%C*)')
63 local i, j, moc_currenttime = string.find( moc_status, 'CurrentTime: (%C*)')
64 local i, j, moc_timeleft = string.find( moc_status, 'TimeLeft: (%C*)')
65 local i, j, moc_totalsec = string.find( moc_status, 'TotalSec: (%C*)')
66 local i, j, moc_currentsec = string.find( moc_status, 'CurrentSec: (%C*)')
67 local i, j, moc_bitrate = string.find( moc_status, 'Bitrate: (%C*)')
68 local i, j, moc_rate = string.find( moc_status, 'Rate: (%C*)')
70 if moc_state == "STOP"
71 then return "[stopped]", moc_state, "stopped", mocp_not_playing()
72 else
74 local moc_leftsect, moc_percent, moc_progress = "--", "--", "--"
75 if moc_totalsec and moc_currentsec then
76 moc_leftsec = tostring(moc_totalsec - moc_currentsec)
77 moc_percent = tostring(math.floor(moc_currentsec * 100 / moc_totalsec))
78 moc_progress = moc_make_progress_bar(moc_percent)
79 end
81 if moc_state == "PLAY"
82 then return moc_title, moc_state, "playing", moc_file, moc_title, moc_artist, moc_songtitle, moc_album, moc_totaltime, moc_currenttime, moc_timeleft, moc_totalsec, moc_currentsec, moc_bitrate, moc_rate, moc_leftsec, moc_percent, moc_progress
83 else return moc_title.." [paused]" , moc_state, "paused", moc_file, moc_title, moc_artist, moc_songtitle, moc_album, moc_totaltime, moc_currenttime, moc_timeleft, moc_totalsec, moc_currentsec, moc_bitrate, moc_rate, moc_leftsec, moc_percent, moc_progress
84 end
85 end
86 end
88 function moc_make_progress_bar(p)
89 local total_chars = settings.progress_length
90 local filled_chars = math.ceil(p * total_chars / 100)
91 local str = ""
92 for i=1,filled_chars do
93 str = str..settings.progress_char2
94 end
95 for i=filled_chars+1,total_chars do
96 str = str..settings.progress_char1
97 end
98 return str
99 end
101 function mocp_not_playing()
102 return "--", "--", "--", "--", "--", "--:--", "--:--", "--:--", "--", "--", "--", "--", "--", "--", moc_make_progress_bar(0)
105 function get_moc()
106 local f=io.open(settings.moc_dir.."/pid")
107 if f
108 then f:close()
109 return do_query_mocp()
110 else return "[not running]", "OFF", "off", mocp_not_playing()
114 function update_moc()
115 local moc, moc_state, moc_state2, moc_file, moc_title, moc_artist, moc_songtitle, moc_album, moc_totaltime, moc_currenttime, moc_timeleft, moc_totalsec, moc_currentsec, moc_bitrate, moc_rate, moc_leftsec, moc_percent, moc_progress = get_moc()
116 statusd.inform("moc", moc)
117 statusd.inform("moc_state", moc_state)
118 statusd.inform("moc_state2", moc_state2)
119 if settings.color_hints then
120 if moc_state2 == "playing"
121 then statusd.inform("moc_state_hint", "green")
122 statusd.inform("moc_state2_hint", "green")
123 elseif moc_state2 == "paused"
124 then statusd.inform("moc_state_hint", "yellow")
125 statusd.inform("moc_state2_hint", "yellow")
126 elseif moc_state2 == "stopped"
127 then statusd.inform("moc_state_hint", "red")
128 statusd.inform("moc_state2_hint", "red")
129 elseif moc_state2 == "off"
130 then statusd.inform("moc_state_hint", "grey")
131 statusd.inform("moc_state2_hint", "grey")
132 end end
133 statusd.inform("moc_file", moc_file)
134 statusd.inform("moc_title", moc_title)
135 statusd.inform("moc_artist", moc_artist)
136 statusd.inform("moc_songtitle", moc_songtitle)
137 statusd.inform("moc_album", moc_album)
138 statusd.inform("moc_totaltime", moc_totaltime)
139 statusd.inform("moc_currenttime", moc_currenttime)
140 statusd.inform("moc_timeleft", moc_timeleft)
141 statusd.inform("moc_totalsec", moc_totalsec)
142 statusd.inform("moc_currentsec", moc_currentsec)
143 statusd.inform("moc_bitrate", moc_bitrate)
144 statusd.inform("moc_rate", moc_rate)
145 statusd.inform("moc_leftsec", moc_leftsec)
146 statusd.inform("moc_percent", moc_percent)
147 statusd.inform("moc_progress", moc_progress)
148 moc_timer:set(settings.update_interval, update_moc)
151 moc_timer = statusd.create_timer()
152 update_moc()
154 -- vim:tw=0: