1 /***************************************************************************
2  *   Copyright (C) 2008-2021 by Andrzej Rybczak                            *
3  *   andrzej@rybczak.net                                                   *
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  ***************************************************************************/
20 
21 #ifndef NCMPCPP_STATUSBAR_H
22 #define NCMPCPP_STATUSBAR_H
23 
24 #include <boost/format.hpp>
25 #include "curses/window.h"
26 #include "settings.h"
27 #include "gcc.h"
28 #include "interfaces.h"
29 
30 namespace Progressbar {
31 
32 struct ScopedLock
33 {
34 	ScopedLock() noexcept;
35 	~ScopedLock() noexcept;
36 };
37 
38 /// @return true if progressbar is unlocked
39 bool isUnlocked();
40 
41 /// draws progressbar
42 void draw(unsigned elapsed, unsigned time);
43 
44 }
45 
46 namespace Statusbar {
47 
48 struct ScopedLock
49 {
50 	ScopedLock() noexcept;
51 	~ScopedLock() noexcept;
52 };
53 
54 /// @return true if statusbar is unlocked
55 bool isUnlocked();
56 
57 /// tries to clear current message put there using Statusbar::printf if there is any
58 void tryRedraw();
59 
60 /// clears statusbar and move cursor to beginning of line
61 /// @return window object that represents statusbar
62 NC::Window &put();
63 
64 namespace Helpers {
65 
66 /// called when statusbar window detects incoming idle notification
67 void mpd();
68 
69 /// called each time user types another character while inside Window::getString
70 bool mainHook(const char *);
71 
72 /// prompt and return one of the characters specified in the vector
73 char promptReturnOneOf(const std::vector<char> &values);
74 
75 struct ImmediatelyReturnOneOf
76 {
ImmediatelyReturnOneOfImmediatelyReturnOneOf77 	ImmediatelyReturnOneOf(std::vector<std::string> arg)
78 	: m_values(std::move(arg))
79 	{ }
80 
81 	bool operator()(const char *s) const;
82 
83 	template <typename StringT>
isOneOfImmediatelyReturnOneOf84 	bool isOneOf(StringT &&s) const {
85 		return std::find(m_values.begin(), m_values.end(), std::forward<StringT>(s)) != m_values.end();
86 	}
87 
88 private:
89 	std::vector<std::string> m_values;
90 };
91 
92 struct ApplyFilterImmediately
93 {
ApplyFilterImmediatelyApplyFilterImmediately94 	ApplyFilterImmediately(Filterable *w)
95 		: m_w(w)
96 	{ }
97 
98 	bool operator()(const char *s);
99 
100 private:
101 	Filterable *m_w;
102 };
103 
104 struct FindImmediately
105 {
FindImmediatelyFindImmediately106 	FindImmediately(Searchable *w, SearchDirection direction)
107 		: m_w(w), m_direction(direction)
108 	{ }
109 
110 	bool operator()(const char *s);
111 
112 private:
113 	Searchable *m_w;
114 	const SearchDirection m_direction;
115 };
116 
117 struct TryExecuteImmediateCommand
118 {
119 	bool operator()(const char *s);
120 
121 private:
122 	std::string m_s;
123 };
124 
125 }
126 
127 /// displays message in statusbar for a given period of time
128 void print(int delay, const std::string& message);
129 
130 /// displays message in statusbar for period of time set in configuration file
print(const std::string & message)131 inline void print(const std::string &message)
132 {
133 	print(Config.message_delay_time, message);
134 }
135 
136 /// displays formatted message in statusbar for period of time set in configuration file
137 template <typename FormatT>
printf(FormatT && fmt)138 void printf(FormatT &&fmt)
139 {
140 	print(Config.message_delay_time, boost::format(std::forward<FormatT>(fmt)).str());
141 }
142 template <typename FormatT, typename ArgT, typename... Args>
printf(FormatT && fmt,ArgT && arg,Args &&...args)143 void printf(FormatT &&fmt, ArgT &&arg, Args&&... args)
144 {
145 	printf(boost::format(std::forward<FormatT>(fmt)) % std::forward<ArgT>(arg),
146 		std::forward<Args>(args)...
147 	);
148 }
149 
150 /// displays formatted message in statusbar for a given period of time
151 template <typename FormatT>
printf(int delay,FormatT && fmt)152 void printf(int delay, FormatT &&fmt)
153 {
154 	print(delay, boost::format(std::forward<FormatT>(fmt)).str());
155 }
156 template <typename FormatT, typename ArgT, typename... Args>
printf(int delay,FormatT && fmt,ArgT && arg,Args &&...args)157 void printf(int delay, FormatT &&fmt, ArgT &&arg, Args&&... args)
158 {
159 	printf(delay, boost::format(std::forward<FormatT>(fmt)) % std::forward<ArgT>(arg),
160 		std::forward<Args>(args)...
161 	);
162 }
163 
164 }
165 
166 #endif // NCMPCPP_STATUSBAR_H
167