xaizek / pms (License: GPLv3+) (since 2018-12-07)
Older version of Practical Music Search written in C++.
Commit 572b9befb372b773df03287b52854e09b9354984

Shave away the control code parser from the curses print function.
Author: Kim Tore Jensen
Author date (UTC): 2012-08-14 18:15
Committer name: Kim Tore Jensen
Committer date (UTC): 2012-08-14 18:15
Parent(s): 9ab8be221431266c2f7a93b66d0e52d768d58213
Signing key:
Tree: 44a959f39e553a9b09ac82b0f74cb754ce3c2529
File Lines added Lines deleted
src/curses.cpp 6 108
File src/curses.cpp changed (mode: 100644) (index 51756ac..16c01f7)
... ... void Curses::bell()
160 160 void Curses::print(Rect * rect, Color * c, int y, int x, const char * fmt, ...) void Curses::print(Rect * rect, Color * c, int y, int x, const char * fmt, ...)
161 161 { {
162 162 va_list ap; va_list ap;
163 unsigned int i = 0;
164 double f = 0;
165 string output = "";
166 bool parse = false;
167 bool attr = false;
168 char buf[1024];
169 string colorstr;
170 unsigned int maxlen; // max allowed characters printed on screen
171 unsigned int printlen = 0; // num characters printed on screen
163 char buffer[1024];
172 164
173 if (!rect || !c)
165 if (!rect || !c) {
174 166 return; return;
167 }
175 168
176 169 va_start(ap, fmt); va_start(ap, fmt);
170 vsprintf(buffer, fmt, ap);
171 va_end(ap);
177 172
178 maxlen = rect->right - rect->left - x + 1;
179 173 move(rect->top + y, rect->left + x); move(rect->top + y, rect->left + x);
180 174 attron(c->pair); attron(c->pair);
181
182 while(*fmt && printlen < maxlen)
183 {
184 if (*fmt == '%' && !parse)
185 {
186 if (*(fmt + 1) == '%')
187 {
188 fmt += 2;
189 output = "%%";
190 printw(output.c_str());
191 continue;
192 }
193 parse = true;
194 attr = true;
195 ++fmt;
196 }
197
198 if (parse)
199 {
200 switch(*fmt)
201 {
202 case '/':
203 /* Turn off attribute, SGML style */
204 attr = false;
205 break;
206 case 'B':
207 if (attr)
208 attron(A_BOLD);
209 else
210 attroff(A_BOLD);
211 parse = false;
212 break;
213 case 'R':
214 if (attr)
215 attron(A_REVERSE);
216 else
217 attroff(A_REVERSE);
218 parse = false;
219 break;
220 case 'd':
221 parse = false;
222 i = va_arg(ap, int);
223 sprintf(buf, "%d", i);
224 printw(buf);
225 printlen += strlen(buf);
226 i = 0;
227 break;
228 case 'f':
229 parse = false;
230 f = va_arg(ap, double);
231 sprintf(buf, "%f", f);
232 printw(buf);
233 printlen += strlen(buf);
234 break;
235 case 's':
236 parse = false;
237 output = va_arg(ap, const char *);
238 sprintf(buf, "%s", output.c_str());
239 printw(buf);
240 printlen += strlen(buf);
241 break;
242 case 0:
243 parse = false;
244 continue;
245 default:
246 /* Use colors? */
247 i = atoi(fmt);
248 if (i >= 0)
249 {
250 if (attr)
251 {
252 attroff(c->pair);
253 attron(i);
254 }
255 else
256 {
257 attroff(i);
258 attron(c->pair);
259 }
260
261 /* Skip characters */
262 fmt += static_cast<int>(floor(log(i)) + 1);
263 }
264 parse = false;
265 break;
266 }
267 }
268 else
269 {
270 output = *fmt;
271 printw(output.c_str());
272 ++printlen;
273 }
274 ++fmt;
275 }
276
277 va_end(ap);
175 printw(buffer);
278 176 attroff(c->pair); attroff(c->pair);
279 177 } }
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/pms

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/pms

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master