1
Description: renamed waitbar to text_waitbar to not shadow core function
2
Author: Carnë Draug <carandraug+dev@gmail.com>
4
Last-Update: 2012-03-09
6
This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
10
-/**************************************************************************
11
- * Waitbar function -- displays progress of lengthy calculations
12
- * -------------------------------------------------------------
13
- * Copyright (c) 2002 Quentin Spencer <qspencer@ieee.org>
15
- * This library is free software; you can redistribute it and/or
16
- * modify it under the terms of the GNU Library General Public
17
- * License as published by the Free Software Foundation; either
18
- * version 2 of the License, or (at your option) any later version.
20
- * This library is distributed in the hope that it will be useful,
21
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23
- * Library General Public License for more details.
25
- * You should have received a copy of the GNU Library General Public
26
- * License along with this library; if not, see <http://www.gnu.org/licenses/>.
28
- *************************************************************************/
30
-#include <octave/oct.h>
32
-// Note the extern "C" is need for mingw with a version of termcap.h
33
-// without the extern "C" explicitly included. Doing it twice should be
36
-#if defined (HAVE_TERM_H)
38
-#elif defined (HAVE_TERMCAP_H)
39
-# include <termcap.h>
45
-#define DEFAULT_LEN 50
49
-static bool no_terminal=false;
51
-DEFUN_DLD(waitbar, args, nargout,
53
- WAITBAR displays a text-based wait bar. This function\n\
54
- is similar to the Matlab waitbar command, but it is\n\
55
- a text, rather than graphical function.\n\n\
56
- A typical usage of WAITBAR in a lengthy computation\n\
57
- (inside a FOR loop, for example) is as follows:\n\n\
62
- WAITBAR(X,TITLE), where 0 <= X <= 1, sets the position of\n\
63
- the waitbar to the fractional length X. Values of X exactly\n\
64
- equal to 0 or 1 clear the waitbar. The optional second\n\
65
- argument TITLE sets the waitbar caption to TITLE.\n\n\
66
- If Octave is running in a smart terminal, the width is\n\
67
- automatically detected, and the title is displayed in the\n\
68
- waitbar (and truncated if it is too long). Otherwise, the\n\
69
- title is not displayed and the width is initialized to a\n\
70
- default of 50 characters, or it can be set to N characters\n\
71
- with WAITBAR(0,N). If no terminal is detected (such as when\n\
72
- Octave is run in batch mode and output is redirected), no\n\
73
- output is generated.\n\n\
74
- For compatibility with the Matlab version of this function\n\
75
- (which is graphical rather than text-based), additional\n\
76
- arguments are ignored, but there are no guarantees of perfect\n\
79
- static char print_buf[BUF_SIZE];
80
- static int n_chars_old;
81
- static int pct_int_old;
83
-#if defined(USE_TERM)
84
- static char term_buffer[1024];
85
- static char *begin_rv, *end_rv;
86
- static int brvlen, ervlen, pct_pos;
87
- static bool smart_term;
88
- static bool newtitle = false;
89
- static charMatrix title;
98
- octave_value_list retval;
99
- retval(0) = Matrix(0,0);
100
- int nargin = args.length();
109
- pct = args(0).double_value();
110
- if(pct>1.0) pct = 1.0; // to prevent overflow
112
-#if defined(USE_TERM)
113
- if(nargin==2 && args(1).is_string())
116
- title = args(1).string_value();
121
- title = args(2).string_value();
125
- if(pct==0.0 || pct==1.0)
128
- term = getenv("TERM");
131
- no_terminal = true;
134
-#if defined (USE_TERM)
136
- smart_term = i ? true : false;
137
- if(nargin==1 || args(1).is_string())
138
- length = smart_term ? i-1 : DEFAULT_LEN;
141
- length = DEFAULT_LEN;
144
- if(nargin==2 && !(args(1).is_string()))
146
- length = args(1).int_value();
147
- if(length>MAX_LEN) length = MAX_LEN;
148
- if(length<=0) length = DEFAULT_LEN;
150
-#if defined (USE_TERM)
151
- pct_pos = length/2-2;
154
- // get terminal strings ("rv"="reverse video")
155
- char* buf_ptr = term_buffer;
156
- begin_rv = tgetstr("so", &buf_ptr);
157
- end_rv = tgetstr("se", &buf_ptr);
159
- // Display a progress bar, but only if the current terminal has a
161
- if (begin_rv && end_rv)
164
- buf_ptr = begin_rv;
165
- while(buf_ptr[++brvlen]);
166
- ervlen = 0; buf_ptr = end_rv;
167
- while(buf_ptr[++ervlen]);
170
- // initialize print buffer
171
- for(i=0; i<BUF_SIZE; ++i)
172
- print_buf[i] = ' ';
173
- print_buf[length+brvlen+ervlen+1] = '\r';
174
- print_buf[length+brvlen+ervlen+2] = '\0';
175
- for(i=0; i<brvlen; ++i)
176
- print_buf[i] = begin_rv[i];
177
- for(i=0; i<ervlen; ++i)
178
- print_buf[i+brvlen] = end_rv[i];
179
- fputs(print_buf,stdout);
186
- for(i=0; i<BUF_SIZE; ++i)
187
- print_buf[i] = ' ';
188
- print_buf[length+8] = '\r';
189
- print_buf[length+9] = '\0';
190
- fputs(print_buf,stdout);
191
- print_buf[0] = '[';
192
- print_buf[length+1] = ']';
193
-#if defined (USE_TERM)
202
- // calculate position
203
- int n_chars=(int)(pct*length+0.5);
204
- int pct_int=(int)(pct*100.0+0.5);
206
- // check to see if we got this far without initialization
209
- Fwaitbar(octave_value(0.0),0);
210
- fputs(print_buf,stdout);
214
- // check to see of output needs to be updated
215
-#if !defined (USE_TERM)
216
- if(n_chars!=n_chars_old || pct_int!=pct_int_old)
219
- if(n_chars!=n_chars_old || pct_int!=pct_int_old || newtitle)
223
- static char pct_str[5];
224
- sprintf(pct_str,"%3i%%",pct_int);
226
- // Insert the title
229
- pct_pos = length-SPACING*2;
230
- for(i=SPACING+brvlen; i<pct_pos+brvlen-SPACING; ++i)
231
- print_buf[ i>=n_chars_old+brvlen ? i+ervlen : i ] = ' ';
232
- for(i=SPACING+brvlen, j=0; j<title.length(); ++i, ++j)
233
- if(i<pct_pos+brvlen-SPACING)
234
- print_buf[ i>=n_chars_old ? i+ervlen : i ] = title(0,j);
238
- // Insert the percentage string
239
- for(i=pct_pos+brvlen, j=0; j<4; ++i, ++j)
240
- print_buf[ i>=n_chars_old+brvlen ? i+ervlen : i ] = pct_str[j];
242
- // Move print_buf characters
243
- if(n_chars_old<n_chars)
244
- for(i=n_chars_old+brvlen; i<n_chars+brvlen; ++i)
245
- print_buf[i] = print_buf[i+ervlen];
247
- for(i=n_chars_old+brvlen-1; i>=n_chars+brvlen; --i)
248
- print_buf[i+ervlen] = print_buf[i];
250
- // Insert end of reverse video
251
- for(i=n_chars+brvlen, j=0; j<ervlen; ++i, ++j)
252
- print_buf[i] = end_rv[j];
257
- if(n_chars>=n_chars_old)
258
- for(int i=n_chars_old+1; i<=n_chars; ++i)
259
- print_buf[i] = BAR_CHAR;
261
- for(int i=n_chars+1; i<=n_chars_old; ++i)
262
- print_buf[i] = ' ';
263
- sprintf(&(print_buf[length+3])," %3i%%\r",pct_int);
264
-#if defined (USE_TERM)
267
- fputs(print_buf,stdout);
269
- n_chars_old = n_chars;
270
- pct_int_old = pct_int;
276
+++ b/src/text_waitbar.cc
278
+/**************************************************************************
279
+ * Waitbar function -- displays progress of lengthy calculations
280
+ * -------------------------------------------------------------
281
+ * Copyright (c) 2002 Quentin Spencer <qspencer@ieee.org>
283
+ * This library is free software; you can redistribute it and/or
284
+ * modify it under the terms of the GNU Library General Public
285
+ * License as published by the Free Software Foundation; either
286
+ * version 2 of the License, or (at your option) any later version.
288
+ * This library is distributed in the hope that it will be useful,
289
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
290
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
291
+ * Library General Public License for more details.
293
+ * You should have received a copy of the GNU Library General Public
294
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
296
+ *************************************************************************/
298
+#include <octave/oct.h>
300
+// Note the extern "C" is need for mingw with a version of termcap.h
301
+// without the extern "C" explicitly included. Doing it twice should be
304
+#if defined (HAVE_TERM_H)
306
+#elif defined (HAVE_TERMCAP_H)
307
+# include <termcap.h>
311
+#define BUF_SIZE 256
313
+#define DEFAULT_LEN 50
314
+#define BAR_CHAR '#'
317
+static bool no_terminal=false;
319
+DEFUN_DLD(text_waitbar, args, nargout,
320
+"text_waitbar(...);\n\
321
+ TEXT_WAITBAR displays a text-based wait bar. This function\n\
322
+ is similar to the Matlab waitbar command, but it is\n\
323
+ a text, rather than graphical function.\n\n\
324
+ A typical usage of TEXT_WAITBAR in a lengthy computation\n\
325
+ (inside a FOR loop, for example) is as follows:\n\n\
328
+ text_waitbar(i/1000);\n\
330
+ TEXT_WAITBAR(X,TITLE), where 0 <= X <= 1, sets the position of\n\
331
+ the waitbar to the fractional length X. Values of X exactly\n\
332
+ equal to 0 or 1 clear the waitbar. The optional second\n\
333
+ argument TITLE sets the waitbar caption to TITLE.\n\n\
334
+ If Octave is running in a smart terminal, the width is\n\
335
+ automatically detected, and the title is displayed in the\n\
336
+ waitbar (and truncated if it is too long). Otherwise, the\n\
337
+ title is not displayed and the width is initialized to a\n\
338
+ default of 50 characters, or it can be set to N characters\n\
339
+ with TEXT_WAITBAR(0,N). If no terminal is detected (such as when\n\
340
+ Octave is run in batch mode and output is redirected), no\n\
341
+ output is generated.\n\n\
342
+ For compatibility with the Matlab version of this function\n\
343
+ (which is graphical rather than text-based), additional\n\
344
+ arguments are ignored, but there are no guarantees of perfect\n\
347
+ static char print_buf[BUF_SIZE];
348
+ static int n_chars_old;
349
+ static int pct_int_old;
351
+#if defined(USE_TERM)
352
+ static char term_buffer[1024];
353
+ static char *begin_rv, *end_rv;
354
+ static int brvlen, ervlen, pct_pos;
355
+ static bool smart_term;
356
+ static bool newtitle = false;
357
+ static charMatrix title;
366
+ octave_value_list retval;
367
+ retval(0) = Matrix(0,0);
368
+ int nargin = args.length();
377
+ pct = args(0).double_value();
378
+ if(pct>1.0) pct = 1.0; // to prevent overflow
380
+#if defined(USE_TERM)
381
+ if(nargin==2 && args(1).is_string())
384
+ title = args(1).string_value();
389
+ title = args(2).string_value();
393
+ if(pct==0.0 || pct==1.0)
396
+ term = getenv("TERM");
399
+ no_terminal = true;
402
+#if defined (USE_TERM)
404
+ smart_term = i ? true : false;
405
+ if(nargin==1 || args(1).is_string())
406
+ length = smart_term ? i-1 : DEFAULT_LEN;
409
+ length = DEFAULT_LEN;
412
+ if(nargin==2 && !(args(1).is_string()))
414
+ length = args(1).int_value();
415
+ if(length>MAX_LEN) length = MAX_LEN;
416
+ if(length<=0) length = DEFAULT_LEN;
418
+#if defined (USE_TERM)
419
+ pct_pos = length/2-2;
422
+ // get terminal strings ("rv"="reverse video")
423
+ char* buf_ptr = term_buffer;
424
+ begin_rv = tgetstr("so", &buf_ptr);
425
+ end_rv = tgetstr("se", &buf_ptr);
427
+ // Display a progress bar, but only if the current terminal has a
429
+ if (begin_rv && end_rv)
432
+ buf_ptr = begin_rv;
433
+ while(buf_ptr[++brvlen]);
434
+ ervlen = 0; buf_ptr = end_rv;
435
+ while(buf_ptr[++ervlen]);
438
+ // initialize print buffer
439
+ for(i=0; i<BUF_SIZE; ++i)
440
+ print_buf[i] = ' ';
441
+ print_buf[length+brvlen+ervlen+1] = '\r';
442
+ print_buf[length+brvlen+ervlen+2] = '\0';
443
+ for(i=0; i<brvlen; ++i)
444
+ print_buf[i] = begin_rv[i];
445
+ for(i=0; i<ervlen; ++i)
446
+ print_buf[i+brvlen] = end_rv[i];
447
+ fputs(print_buf,stdout);
454
+ for(i=0; i<BUF_SIZE; ++i)
455
+ print_buf[i] = ' ';
456
+ print_buf[length+8] = '\r';
457
+ print_buf[length+9] = '\0';
458
+ fputs(print_buf,stdout);
459
+ print_buf[0] = '[';
460
+ print_buf[length+1] = ']';
461
+#if defined (USE_TERM)
470
+ // calculate position
471
+ int n_chars=(int)(pct*length+0.5);
472
+ int pct_int=(int)(pct*100.0+0.5);
474
+ // check to see if we got this far without initialization
477
+ Ftext_waitbar(octave_value(0.0),0);
478
+ fputs(print_buf,stdout);
482
+ // check to see of output needs to be updated
483
+#if !defined (USE_TERM)
484
+ if(n_chars!=n_chars_old || pct_int!=pct_int_old)
487
+ if(n_chars!=n_chars_old || pct_int!=pct_int_old || newtitle)
491
+ static char pct_str[5];
492
+ sprintf(pct_str,"%3i%%",pct_int);
494
+ // Insert the title
497
+ pct_pos = length-SPACING*2;
498
+ for(i=SPACING+brvlen; i<pct_pos+brvlen-SPACING; ++i)
499
+ print_buf[ i>=n_chars_old+brvlen ? i+ervlen : i ] = ' ';
500
+ for(i=SPACING+brvlen, j=0; j<title.length(); ++i, ++j)
501
+ if(i<pct_pos+brvlen-SPACING)
502
+ print_buf[ i>=n_chars_old ? i+ervlen : i ] = title(0,j);
506
+ // Insert the percentage string
507
+ for(i=pct_pos+brvlen, j=0; j<4; ++i, ++j)
508
+ print_buf[ i>=n_chars_old+brvlen ? i+ervlen : i ] = pct_str[j];
510
+ // Move print_buf characters
511
+ if(n_chars_old<n_chars)
512
+ for(i=n_chars_old+brvlen; i<n_chars+brvlen; ++i)
513
+ print_buf[i] = print_buf[i+ervlen];
515
+ for(i=n_chars_old+brvlen-1; i>=n_chars+brvlen; --i)
516
+ print_buf[i+ervlen] = print_buf[i];
518
+ // Insert end of reverse video
519
+ for(i=n_chars+brvlen, j=0; j<ervlen; ++i, ++j)
520
+ print_buf[i] = end_rv[j];
525
+ if(n_chars>=n_chars_old)
526
+ for(int i=n_chars_old+1; i<=n_chars; ++i)
527
+ print_buf[i] = BAR_CHAR;
529
+ for(int i=n_chars+1; i<=n_chars_old; ++i)
530
+ print_buf[i] = ' ';
531
+ sprintf(&(print_buf[length+3])," %3i%%\r",pct_int);
532
+#if defined (USE_TERM)
535
+ fputs(print_buf,stdout);
537
+ n_chars_old = n_chars;
538
+ pct_int_old = pct_int;
549
-TARGETS=waitbar.oct $(LISTEN) xmlread.oct \
550
+TARGETS=text_waitbar.oct $(LISTEN) xmlread.oct \
551
csvexplode.oct csv2cell.oct csvconcat.oct cell2csv.oct \
552
cell2cell.oct partarray.oct
556
-waitbar.oct: waitbar.cc
557
- $(MKOCTFILE) $(MISCDEFS) waitbar.cc $(TERM_LIB)
558
+text_waitbar.oct: text_waitbar.cc
559
+ $(MKOCTFILE) $(MISCDEFS) text_waitbar.cc $(TERM_LIB)
561
server.oct: server.o listencanfork.o stringmatch.o
562
$(MKOCTFILE) server.o listencanfork.o stringmatch.o