aux/statusmsg: use libbio for textmode output

calling write(1, "\b", 1); for each rune to be removed is a lot of
overhead, and we don’t want rio to turn each of these writes into a
draw operation.

also, it now prints to stderr before exiting if initdraw() fails
This commit is contained in:
BurnZeZ 2017-05-05 21:43:30 +00:00
parent 74b6d9bda3
commit 59e8bc0e23

View file

@ -10,7 +10,8 @@ int newwin(char*);
int nokill; int nokill;
int textmode; int textmode;
char *title = nil; char *title = nil;
char message[1024]; char *message = nil;
Biobuf *bout;
Image *light; Image *light;
Image *text; Image *text;
@ -30,8 +31,9 @@ drawmsg(void)
static int last = 0; static int last = 0;
while(last-- > 0) while(last-- > 0)
write(1, "\b", 1); Bputc(bout, '\b');
write(1, message, strlen(message)); Bwrite(bout, message, strlen(message));
Bflush(bout);
last = utflen(message); last = utflen(message);
return; return;
} }
@ -70,8 +72,9 @@ msg(Biobuf *b)
if(textmode){ if(textmode){
child = -1; child = -1;
if(title){ if(title){
write(1, title, strlen(title)); Bwrite(bout, title, strlen(title));
write(1, ": ", 2); Bwrite(bout, ": ", 2);
Bflush(bout);
} }
} else } else
switch(child = rfork(RFMEM|RFPROC)) { switch(child = rfork(RFMEM|RFPROC)) {
@ -86,12 +89,14 @@ msg(Biobuf *b)
} }
_exits(0); _exits(0);
} }
while(!die && (p = Brdline(b, '\n'))) { while(!die && (p = Brdline(b, '\n'))){
snprint(message, sizeof(message), "%.*s", Blinelen(b)-1, p); snprint(message, Bsize, "%.*s", Blinelen(b)-1, p);
drawmsg(); drawmsg();
} }
if(textmode) if(textmode){
write(1, "\n", 1); Bwrite(bout, "\n", 1);
Bterm(bout);
}
postnote(PNPROC, child, "kill"); postnote(PNPROC, child, "kill");
} }
@ -140,11 +145,16 @@ main(int argc, char **argv)
while(q = strchr(p, ',')) while(q = strchr(p, ','))
*q = ' '; *q = ' ';
Binit(&b, lfd, OREAD); Binit(&b, lfd, OREAD);
if((message = malloc(Bsize)) == nil)
sysfatal("malloc: %r");
memset(message, 0, Bsize);
if(textmode || newwin(p) < 0){ if(textmode || newwin(p) < 0){
textmode = 1; textmode = 1;
if((bout = Bfdopen(1, OWRITE)) == nil)
sysfatal("Bfdopen: %r");
}else{ }else{
if(initdraw(0, 0, title) < 0) if(initdraw(0, 0, title) < 0)
exits("initdraw"); sysfatal("initdraw: %r");
initcolor(); initcolor();
einit(Emouse|Ekeyboard); einit(Emouse|Ekeyboard);
eresized(0); eresized(0);