shithub: vexed

Download patch

ref: c59ad62914f2ca6f43eb5301e5917a62d12bc8c6
parent: 84925069665705be5b8909d767f4638d37b62dce
author: phil9 <telephil9@gmail.com>
date: Thu Dec 30 01:47:37 EST 2021

report errors through a message box

	for non-fatal errors, show a box with the error message
	for the user to know something wrong happened

--- a/a.h
+++ b/a.h
@@ -41,6 +41,9 @@
 void pushundo(int, int, uchar, uchar, int);
 void patchundo(uchar);
 
+/* ERROR */
+void showerr(const char*, Mousectl*, Keyboardctl*);
+
 /* COLORS */
 enum
 {
--- a/buf.c
+++ b/buf.c
@@ -1,6 +1,8 @@
 #include <u.h>
 #include <libc.h>
 #include <draw.h>
+#include <mouse.h>
+#include <keyboard.h>
 #include "a.h"
 
 int
--- a/cols.c
+++ b/cols.c
@@ -1,6 +1,8 @@
 #include <u.h>
 #include <libc.h>
 #include <draw.h>
+#include <mouse.h>
+#include <keyboard.h>
 #include "a.h"
 
 void
@@ -19,11 +21,5 @@
 		cols[ASCII]  = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DGreygreen);
 		cols[SCROLL] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x999999FF);
 	}
-/*
-	bg  = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x282828ff);
-	fg  = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xebdbb2ff);
-	ofg = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x83a598ff);
-	scrlbg = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x504945ff); 
-*/
 }
 
--- /dev/null
+++ b/err.c
@@ -1,0 +1,84 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <thread.h>
+#include <mouse.h>
+#include <keyboard.h>
+
+enum { Padding = 12, };
+
+void
+showerr(const char *message, Mousectl *mctl, Keyboardctl *kctl)
+{
+	Alt alts[3];
+	Rectangle r, sc;
+	Point o, p;
+	Image *b, *save, *bg, *fg;
+	int done, h, w;
+	Mouse m;
+	Rune k;
+
+	alts[0].op = CHANRCV;
+	alts[0].c  = mctl->c;
+	alts[0].v  = &m;
+	alts[1].op = CHANRCV;
+	alts[1].c  = kctl->c;
+	alts[1].v  = &k;
+	alts[2].op = CHANEND;
+	alts[2].c  = nil;
+	alts[2].v  = nil;
+	while(nbrecv(kctl->c, nil)==1)
+		;
+	bg = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xf8d7daff);
+	fg = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x721c24ff);
+	done = 0;
+	save = nil;
+	h = Padding + font->height + Padding;
+	w = Padding + stringwidth(font, message) + Padding;
+	b = screen;
+	sc = b->clipr;
+	replclipr(b, 0, b->r);
+	while(!done){
+		o = addpt(screen->r.min, Pt((Dx(screen->r)-w)/2, (Dy(screen->r)-h)/2));
+		r = Rect(o.x, o.y, o.x+w, o.y+h);
+		if(save==nil){
+			save = allocimage(display, r, b->chan, 0, DNofill);
+			if(save==nil)
+				break;
+			draw(save, r, b, nil, r.min);
+		}
+		draw(b, r, bg, nil, ZP);
+		border(b, r, 2, fg, ZP);
+		p = addpt(o, Pt(Padding, Padding));
+		string(b, p, fg, ZP, font, message);
+		flushimage(display, 1);
+		if(b!=screen || !eqrect(screen->clipr, sc)){
+			freeimage(save);
+			save = nil;
+		}
+		b = screen;
+		sc = b->clipr;
+		replclipr(b, 0, b->r);
+		switch(alt(alts)){
+		default:
+			continue;
+			break;
+		case 1:
+			done = (k=='\n' || k==Kesc);
+			break;
+		case 0:
+			done = m.buttons&1 && ptinrect(m.xy, r);
+			break;
+		}
+		if(save){
+			draw(b, save->r, save, nil, save->r.min);
+			freeimage(save);
+			save = nil;
+		}
+			
+	}
+	replclipr(b, 0, sc);
+	flushimage(display, 1);
+	freeimage(bg);
+	freeimage(fg);
+}
--- a/mkfile
+++ b/mkfile
@@ -2,7 +2,7 @@
 
 BIN=/$objtype/bin
 TARG=vexed
-OFILES=vexed.$O buf.$O undo.$O cols.$O
+OFILES=vexed.$O buf.$O undo.$O err.$O cols.$O
 HFILES=a.h
 
 </sys/src/cmd/mkone
--- a/undo.c
+++ b/undo.c
@@ -1,6 +1,8 @@
 #include <u.h>
 #include <libc.h>
 #include <draw.h>
+#include <mouse.h>
+#include <keyboard.h>
 #include "a.h"
 
 enum { Stacksize = 1024 };
--- a/vexed.c
+++ b/vexed.c
@@ -212,12 +212,21 @@
 	int n, i;
 
 	n = enter("Look:", tmp, sizeof tmp, mctl, kctl, nil);
-	if(n <= 0 || n%2 != 0)
+	if(n <= 0)
 		return;
+	if(n%2 != 0){
+		showerr("invalid byte sequence", mctl, kctl);
+		return;
+	}
 	nsbuf = 0;
 	sindex = -1;
-	for(i = 0; i < n; i += 2)
+	for(i = 0; i < n; i += 2){
+		if(!(isxdigit(tmp[i]) && isxdigit(tmp[i+1]))){
+			showerr("invalid character in byte sequence", mctl, kctl);
+			return;
+		}
 		sbuf[nsbuf++] = 16*hexval(tmp[i]) + hexval(tmp[i+1]);
+	}
 	sbuf[nsbuf] = 0;
 	snprint(sstr, sizeof sstr, "/%s", tmp);
 	if(!search(sel)){
@@ -315,9 +324,10 @@
 	Point p;
 	int x;
 
+	draw(screen, statusr, cols[BACK], nil, ZP);
 	p = string(screen, Pt(statusr.min.x + Padding, statusr.min.y), cols[HEX], ZP, font, filename);
 	if(modified)
-		string(screen, p, cols[HEX], ZP, font, " (modified)");
+		string(screen, p, cols[SCROLL], ZP, font, " (modified)");
 	snprint(b, sizeof b, "%d%%", (int)((100.0 * sel) / buf.count + 0.5));
 	x = statusr.max.x - stringwidth(font, b) - Padding;
 	string(screen, Pt(x, statusr.min.y), cols[HEX], ZP, font, b);