shithub: mongrel

Download patch

ref: 1d467fd2cb9f0bfdaa3722992da7d509fb2c7f64
parent: 04e7a77895b3f00e513f70d5bfd956391bfc04e2
author: phil9 <telephil9@gmail.com>
date: Sun Feb 13 02:56:39 EST 2022

show a progress bar when loading a mailbox

	this gives a visual feedback that something is actually happening

--- a/a.h
+++ b/a.h
@@ -65,7 +65,7 @@
 };
 
 Mailbox* mboxinit(char *name);
-void mboxload(Mailbox*);
+void mboxload(Mailbox*, Channel *c);
 void mesgloadbody(Message*);
 int mesgmarkseen(Mailbox*, Message*);
 int mboxadd(Mailbox *mbox, char *path);
--- a/main.c
+++ b/main.c
@@ -30,6 +30,8 @@
 	BACK,
 	TEXT,
 	BORD,
+	PBRD,
+	PBCK,
 	NCOLS,
 };
 
@@ -38,6 +40,7 @@
 Channel *showc;
 Channel *selc;
 Channel *eventc;
+Channel *loadc;
 Mailbox *mboxes[16];
 int nmboxes;
 char *mbmenustr[16] = {0};
@@ -97,6 +100,59 @@
 }
 
 void
+loadproc(void *v)
+{
+	Channel *c;
+
+	c = v;
+	mboxload(mbox, c);
+}
+
+void
+drawprogress(Rectangle r, Rectangle pr)
+{
+	draw(screen, pr, cols[PBCK], nil, ZP);
+	border(screen, r, 2, cols[PBRD], ZP);	
+	flushimage(display, 1);
+}
+
+void
+loadmbox(void)
+{
+	ulong total, count, n;
+	Point sp, p;
+	Rectangle r, pr;
+	int pc;
+	char buf[255] = {0};
+
+	draw(screen, screen->r, cols[BACK], nil, ZP);
+	p.x = (Dx(screen->r)-200)/2;
+	p.y = (Dy(screen->r)-25)/2;
+	r = rectaddpt(rectaddpt(Rect(0, 0, 200, 25), p), screen->r.min);
+	snprint(buf, sizeof buf, "Loading %s...", mbox->name);
+	sp.x = (Dx(screen->r)-stringwidth(font, buf))/2;
+	sp.y = r.min.y - font->height - 12 - screen->r.min.y;
+	string(screen, addpt(screen->r.min, sp), cols[TEXT], ZP, font, buf);
+	replclipr(screen, 1, insetrect(r, -2));
+	loadc = chancreate(sizeof(ulong), 1);
+	proccreate(loadproc, loadc, 8192);
+	total = recvul(loadc);
+	count = 0;
+	for(;;){
+		n = recvul(loadc);
+		if(n == 0)
+			break;
+		count += 1;
+		pc = 200*((double)count/total);
+		pr = rectaddpt(rectaddpt(Rect(0, 0, pc, 25), p), screen->r.min);
+		if(count % (total/100) == 0)
+			drawprogress(r, pr);
+	}
+	chanfree(loadc);
+	replclipr(screen, 1, screen->r);
+}
+
+void
 switchmbox(int n)
 {
 	if(mbox==mboxes[n])
@@ -103,7 +159,7 @@
 		return;
 	mbox = mboxes[n];
 	if(!mbox->loaded)
-		mboxload(mbox);
+		loadmbox();
 	indexswitch(mbox);
 	collapsed = 0;
 	resize();
@@ -194,6 +250,8 @@
 		cols[BACK] = theme->back;
 		cols[TEXT] = theme->text;
 		cols[BORD] = theme->title;
+		cols[PBRD] = theme->menubord;
+		cols[PBCK] = theme->menuback;
 	}else{
 		r = Rect(0, 0, 1, 1);
 		cols[BACK] = allocimage(display, r, screen->chan, 1, 0xFFFFFFFF);
--- a/mbox.c
+++ b/mbox.c
@@ -200,7 +200,7 @@
 }
 
 void
-mboxload(Mailbox *mb)
+mboxload(Mailbox *mb, Channel *c)
 {
 	Dir *d;
 	int n, fd, i;
@@ -214,6 +214,7 @@
 	close(fd);
 	qsort(d, n, sizeof *d, (int(*)(void*,void*))dircmp);
 	mb->list = mkmlist(n*1.5);
+	sendul(c, n-1); /* don't count ctl file */
 	for(i = 1; i < n; i++){
 		snprint(buf, sizeof buf, "%s/%s", mb->path, d[i].name);
 		if((d[i].qid.type & QTDIR)==0)
@@ -224,9 +225,11 @@
 		if((m->flags & Fseen) == 0)
 			++mb->unseen;
 		++mb->count;
+		sendul(c, 1);
 	}
 	free(d);
 	mb->loaded = 1;
+	sendul(c, 0);
 }
 
 int