1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
/* curses Yorick plugin, version 0.1
* Francois Rigaut, nov2004
* last revision/addition: 2004Nov30
*
* Copyright (c) 2003, Francois RIGAUT (frigaut@maumae.net)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details (to receive a copy of the GNU
* General Public License, write to the Free Software Foundation, Inc., 675
* Mass Ave, Cambridge, MA 02139, USA).
*/
#include "utils.i"
func curses{}
/* DOCUMENT curses package
The curses plugin wraps some of the curses routine, hopefully enough
of them to do something useful in a one terminal environment (i.e.
I did not wrap multi-terminal curses control routines like newterm).
Curses allow control of the screen and keyboard in a unbuffered way.
you can position and write string wherever you want, without or with
scroll, and you can monitor keyboard events. This can be useful to
report results of large computations without the rapid screen scrolling
and/or steer simulations as they go.
You can see examples of use in the check-plug.i file or in the
"progressbar" or "countdown" routines below.
NOTE: This routine uses the "curses" library. Thus, it will work
on Linux and maxosx machines (and probably unix) but *NOT* on windows
machine (unless curses has been ported to windows, which I am ignorant
of).
Also, curses may not have all the term files. In particular, I have
noticed that it doesn't like to be ran in an emacs terminal/environment.
SEE ALSO:
*/
if (strmatch(get_env("TERM"),"emacs")) \
error,"curses will not work in an emacs environement";
plug_in, "curses";
extern kbdinit;
/* xPROTOTYPE
void kbdinit(void)
*/
extern kbd;
/* xPROTOTYPE
int kbd(int wait)
*/
extern kbdend;
/* xPROTOTYPE
void kbdend(void)
*/
extern cgetch;
/* xPROTOTYPE
int getch(void)
*/
extern cinitscr;
/* xPROTOTYPE
long initscr(void)
*/
extern cendwin;
/* xPROTOTYPE
int endwin(void)
*/
extern cisendwin;
/* xPROTOTYPE
int isendwin(void)
*/
extern ccbreak;
/* xPROTOTYPE
int cbreak(void)
*/
extern cnocbreak;
/* xPROTOTYPE
int nocbreak(void)
*/
extern chalfdelay;
/* xPROTOTYPE
int halfdelay(int tenths)
*/
extern craw;
/* xPROTOTYPE
int raw(void)
*/
extern cnoraw;
/* xPROTOTYPE
int noraw(void)
*/
extern ctimeout;
/* xPROTOTYPE
void timeout(int delay)
*/
extern cnl;
/* xPROTOTYPE
int nl(void)
*/
extern cnonl;
/* xPROTOTYPE
int nonl(void)
*/
extern cprintw;
/* xPROTOTYPE
int printw(string fmt, string str)
*/
extern cmvprintw;
/* xPROTOTYPE
int mvprintw(int y, int x, string fmt, string str)
*/
extern crefresh;
/* xPROTOTYPE
int refresh(void)
*/
extern caddch;
/* xPROTOTYPE
int addch(char ch)
*/
extern cmvaddch;
/* xPROTOTYPE
int mvaddch(int y, int x, char ch)
*/
extern cerase;
/* xPROTOTYPE
int erase(void)
*/
extern cclear;
/* xPROTOTYPE
int clear(void)
*/
extern cclrtobot;
/* xPROTOTYPE
int clrtobot(void)
*/
extern cclrtoeol;
/* xPROTOTYPE
int clrtoeol(void)
*/
extern cbkgdset;
/* xPROTOTYPE
void bkgdset(char ch)
*/
extern cgetlines;
/* xPROTOTYPE
int getlines(void)
*/
extern cgetcols;
/* xPROTOTYPE
int getcols(void)
*/
extern ccurs_set;
/* xPROTOTYPE
int curs_set(int visibility)
*/
extern ccurs_move;
/* xPROTOTYPE
int move(int y, int x)
*/
extern cbeep;
/* xPROTOTYPE
int beep(void)
*/
extern cflash;
/* xPROTOTYPE
int flash(void)
*/
extern cecho;
/* xPROTOTYPE
int echo(void)
*/
extern cnoecho;
/* xPROTOTYPE
int noecho(void)
*/
extern cgetstr;
/* xPROTOTYPE
int getstr(string str);
*/
extern cmvgetstr;
/* xPROTOTYPE
int mvgetstr(int y, int x, string str);
*/
extern cnodelay;
/* xPROTOTYPE
int ynodelay(int condition)
*/
extern cnotimeout;
/* xPROTOTYPE
int ynotimeout(int condition)
*/
extern ckeypad;
/* xPROTOTYPE
int ykeypad(int condition)
*/
extern cmeta;
/* xPROTOTYPE
int ymeta(int condition)
*/
extern cintrflush;
/* xPROTOTYPE
int yintrflush(int condition)
*/
extern cscrollok;
/* xPROTOTYPE
int yscrollok(int condition)
*/
extern cleaveok;
/* xPROTOTYPE
int yleaveok(int condition)
*/
extern cclearok;
/* xPROTOTYPE
int yclearok(int condition)
*/
extern cidlok;
/* xPROTOTYPE
int yidlok(int condition)
*/
func countdown(sec,interval)
/* DOCUMENT func countdown(sec,interval)
sec: number of seconds to coundown
interval: time to pause between refresh, in ms
SEE ALSO:
*/
{
if (!interval) interval=10;
kbdinit;
cerase;
tic;
while ((elapsed=tac()) < sec) {
el = long(round(elapsed));
cmvprintw,0,0,"[%-"+swrite(format="%s",sec)+"s]",((el==0)?"":array("*",el)(sum));
cmvprintw,0,sec+3,"Time remaining: %s sec",swrite(format="%.3f",sec-elapsed);
cclrtobot;
crefresh;
pause,interval;
}
kbdend;
}
func progressbar(current,outof,nstars,counter=,line=)
/* DOCUMENT print out a progress bar in the terminal using curse
no init necessary. Just do a tic(counter) at the start of the loop.
term has to be in curse mode.
SEE ALSO:
*/
{
if (!counter) counter=1;
if (line==[]) line=0;
nc = cgetcols();
el = long(round(current*1./outof*nstars));
cmvprintw,line,nc-nstars-2,"[%-"+swrite(format="%d",nstars)+"s]",
((el==0)?"":array("*",el)(sum));
elapsed = tac(counter);
left = elapsed*(outof-current)/(current+1e-6);
cmvprintw,line+1,nc-nstars-2,"%s s left ",swrite(format="%.3f",left);
cmvprintw,line+2,nc-nstars-2,"%s",swrite(format="%d/%d",current,outof);
cclrtoeol;
}
|