Improve detection for songs not in playlists
[ncmpcpp.git] / src / statusbar.cpp
blobe72667cdec668e52f0cae4c32462d7df62c13bb8
1 /***************************************************************************
2 * Copyright (C) 2008-2016 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "global.h"
22 #include "settings.h"
23 #include "status.h"
24 #include "statusbar.h"
25 #include "bindings.h"
26 #include "screens/playlist.h"
27 #include "utility/wide_string.h"
29 using Global::wFooter;
31 namespace {
33 bool progressbar_block_update = false;
35 boost::posix_time::ptime statusbar_lock_time;
36 boost::posix_time::seconds statusbar_lock_delay(-1);
38 bool statusbar_block_update = false;
39 bool statusbar_allow_unlock = true;
43 Progressbar::ScopedLock::ScopedLock() noexcept
45 progressbar_block_update = true;
48 Progressbar::ScopedLock::~ScopedLock() noexcept
50 progressbar_block_update = false;
53 bool Progressbar::isUnlocked()
55 return !progressbar_block_update;
58 void Progressbar::draw(unsigned int elapsed, unsigned int time)
60 unsigned pb_width = wFooter->getWidth();
61 unsigned howlong = time ? pb_width*elapsed/time : 0;
62 *wFooter << Config.progressbar_color;
63 if (Config.progressbar[2] != '\0')
65 wFooter->goToXY(0, 0);
66 for (unsigned i = 0; i < pb_width; ++i)
67 *wFooter << Config.progressbar[2];
68 wFooter->goToXY(0, 0);
70 else
71 mvwhline(wFooter->raw(), 0, 0, 0, pb_width);
72 *wFooter << NC::FormattedColor::End(Config.progressbar_color);
73 if (time)
75 *wFooter << Config.progressbar_elapsed_color;
76 pb_width = std::min(size_t(howlong), wFooter->getWidth());
77 for (unsigned i = 0; i < pb_width; ++i)
78 *wFooter << Config.progressbar[0];
79 if (howlong < wFooter->getWidth())
80 *wFooter << Config.progressbar[1];
81 *wFooter << NC::FormattedColor::End(Config.progressbar_elapsed_color);
85 Statusbar::ScopedLock::ScopedLock() noexcept
87 // lock
88 if (Config.statusbar_visibility)
89 statusbar_block_update = true;
90 else
91 progressbar_block_update = true;
92 statusbar_allow_unlock = false;
95 Statusbar::ScopedLock::~ScopedLock() noexcept
97 // unlock
98 statusbar_allow_unlock = true;
99 if (statusbar_lock_delay.is_negative())
101 if (Config.statusbar_visibility)
102 statusbar_block_update = false;
103 else
104 progressbar_block_update = false;
106 if (Status::State::player() == MPD::psStop)
108 switch (Config.design)
110 case Design::Classic:
111 put(); // clear statusbar
112 break;
113 case Design::Alternative:
114 Progressbar::draw(Status::State::elapsedTime(), Status::State::totalTime());
115 break;
117 wFooter->refresh();
121 bool Statusbar::isUnlocked()
123 return !statusbar_block_update;
126 void Statusbar::tryRedraw()
128 using Global::Timer;
129 if (statusbar_lock_delay > boost::posix_time::seconds(0)
130 && Timer - statusbar_lock_time > statusbar_lock_delay)
132 statusbar_lock_delay = boost::posix_time::seconds(-1);
134 if (Config.statusbar_visibility)
135 statusbar_block_update = !statusbar_allow_unlock;
136 else
137 progressbar_block_update = !statusbar_allow_unlock;
139 if (!statusbar_block_update && !progressbar_block_update)
141 switch (Config.design)
143 case Design::Classic:
144 switch (Status::State::player())
146 case MPD::psUnknown:
147 case MPD::psStop:
148 put(); // clear statusbar
149 break;
150 case MPD::psPlay:
151 case MPD::psPause:
152 Status::Changes::elapsedTime(false);
153 break;
155 break;
156 case Design::Alternative:
157 Progressbar::draw(Status::State::elapsedTime(), Status::State::totalTime());
158 break;
160 wFooter->refresh();
165 NC::Window &Statusbar::put()
167 *wFooter << NC::XY(0, Config.statusbar_visibility ? 1 : 0) << NC::TermManip::ClearToEOL;
168 return *wFooter;
171 void Statusbar::print(int delay, const std::string &message)
173 if (statusbar_allow_unlock)
175 statusbar_lock_time = Global::Timer;
176 statusbar_lock_delay = boost::posix_time::seconds(delay);
177 if (Config.statusbar_visibility)
178 statusbar_block_update = true;
179 else
180 progressbar_block_update = true;
181 wFooter->goToXY(0, Config.statusbar_visibility);
182 *wFooter << message << NC::TermManip::ClearToEOL;
183 wFooter->refresh();
187 void Statusbar::Helpers::mpd()
189 Status::update(Mpd.noidle());
192 bool Statusbar::Helpers::mainHook(const char *)
194 Status::trace();
195 return true;
198 std::string Statusbar::Helpers::promptReturnOneOf(std::vector<std::string> values)
200 Statusbar::Helpers::ImmediatelyReturnOneOf prompt_hook(std::move(values));
201 NC::Window::ScopedPromptHook hook(*wFooter, prompt_hook);
202 int x = wFooter->getX(), y = wFooter->getY();
203 std::string result;
206 wFooter->goToXY(x, y);
207 result = wFooter->prompt();
209 while (!prompt_hook.isOneOf(result));
210 return result;
213 bool Statusbar::Helpers::ImmediatelyReturnOneOf::operator()(const char *s) const
215 Status::trace();
216 return !isOneOf(s);
219 bool Statusbar::Helpers::ApplyFilterImmediately::operator()(const char *s)
221 using Global::myScreen;
222 Status::trace();
223 try {
224 if (m_w->allowsFiltering() && m_w->currentFilter() != s)
226 m_w->applyFilter(s);
227 if (myScreen == myPlaylist)
228 myPlaylist->enableHighlighting();
229 myScreen->refreshWindow();
231 } catch (boost::bad_expression &) { }
232 return true;
235 bool Statusbar::Helpers::FindImmediately::operator()(const char *s)
237 using Global::myScreen;
238 Status::trace();
239 try {
240 if (m_w->allowsSearching() && m_w->searchConstraint() != s)
242 m_w->setSearchConstraint(s);
243 m_w->search(m_direction, Config.wrapped_search, false);
244 if (myScreen == myPlaylist)
245 myPlaylist->enableHighlighting();
246 myScreen->refreshWindow();
248 } catch (boost::bad_expression &) { }
249 return true;
252 bool Statusbar::Helpers::TryExecuteImmediateCommand::operator()(const char *s)
254 bool continue_ = true;
255 if (m_s != s)
257 m_s = s;
258 auto cmd = Bindings.findCommand(m_s);
259 if (cmd && cmd->immediate())
260 continue_ = false;
262 Status::trace();
263 return continue_;