shithub: orca

Download patch

ref: 2981b0bf46bae9a4bfab4463c97aadd51551f00e
parent: f274c92b3e2b60f2c2b0b56cf94cd8f0a758748b
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Wed Feb 12 06:15:03 EST 2020

plan9: more info on the screen, more keys supported

--- a/plan9.c
+++ b/plan9.c
@@ -22,9 +22,22 @@
 static Rune *linebuf;
 static Usz tick;
 static int gridw = 8, gridh = 8;
+static int bpm = 120, insert = 1;
 static int curx, cury;
 static Image *curbg;
+static int charw, charh;
+static Field field;
 
+static char *menu3i[] = {
+	"load",
+	"save",
+	nil,
+};
+
+static Menu menu3 = {
+	.item = menu3i,
+};
+
 Usz
 orca_round_up_power2(Usz x)
 {
@@ -96,11 +109,11 @@
 }
 
 static void
-redraw(Field *f)
+redraw(void)
 {
 	Point p, top;
 	Rune cursor;
-	int x, y;
+	int x, y, len;
 
 	draw(screen, screen->r, display->black, nil, ZP);
 	p = screen->r.min;
@@ -107,9 +120,9 @@
 	p.x += Txtoff;
 	p.y += Txtoff;
 	top = p;
-	for (y = 0; y < f->height; y++) {
-		for (x = 0; x < f->width; x++) {
-			Rune c = f->buffer[f->width*y + x];
+	for (y = 0; y < field.height; y++) {
+		for (x = 0; x < field.width; x++) {
+			Rune c = field.buffer[field.width*y + x];
 			if (c == L'.')
 				c = L'·';
 			linebuf[x] = c;
@@ -122,14 +135,40 @@
 	}
 
 	p.y += 2 * font->height;
+
+	/* field size */
 	p.x = screen->r.min.x + Txtoff;
-	runesprint(linebuf, "%udx%ud", f->width, f->height);
+	len = runesprint(linebuf, "%udx%ud", field.width, field.height);
 	runestring(screen, p, display->white, ZP, font, linebuf);
-	p.x += stringwidth(font, "99999x99999  ");
+
+	/* cursor position */
+	p.y += font->height;
+	runesprint(linebuf, "%ud,%ud", curx, cury);
+	runestring(screen, p, display->white, ZP, font, linebuf);
+
+	/* grid size */
+	p.y -= font->height;
+	p.x += charw * (len + 3);
+	len = runesprint(linebuf, "%d/%d", gridw, gridh);
+	runestring(screen, p, display->white, ZP, font, linebuf);
+
+	/* ticks */
+	p.x += charw * (len + 3);
 	runesprint(linebuf, "%ludf", tick);
 	runestring(screen, p, display->white, ZP, font, linebuf);
 
-	/* cursor */
+	/* insert/append mode */
+	p.y += font->height;
+	len = runesprint(linebuf, "%s", insert ? "insert" : "append");
+	runestring(screen, p, display->white, ZP, font, linebuf);
+
+	/* bpm */
+	p.y -= font->height;
+	p.x += charw * (len + 3);
+	runesprint(linebuf, "%d", bpm);
+	runestring(screen, p, display->white, ZP, font, linebuf);
+
+	/* cursor bg */
 	p = top;
 	p.x += curx*stringwidth(font, " ");
 	p.y += cury*font->height;
@@ -139,11 +178,11 @@
 }
 
 static int
-loadfield(Field *field, char *path)
+fieldload(char *path)
 {
 	Field_load_error e;
 
-	if ((e = field_load_file(path, field)) != Field_load_error_ok) {
+	if ((e = field_load_file(path, &field)) != Field_load_error_ok) {
 		werrstr(field_load_error_string(e));
 		return -1;
 	}
@@ -152,10 +191,18 @@
 }
 
 static void
+fieldset(Rune key)
+{
+	field.buffer[curx + field.width*cury] = key;
+	if (!insert && curx < field.width-1)
+		curx++;
+}
+
+static void
 screensize(int *w, int *h)
 {
-	*w = (Dx(screen->r) - 2*Txtoff) / stringwidth(font, "X");
-	*h = ((Dy(screen->r) - 2*Txtoff) - 3*font->height) / font->height;
+	*w = (Dx(screen->r) - 2*Txtoff) / charw;
+	*h = ((Dy(screen->r) - 2*Txtoff) - 3*charh) / charh;
 }
 
 static void
@@ -163,22 +210,11 @@
 {
 }
 
-static char *menu3i[] = {
-	"load",
-	"save",
-	nil,
-};
-
-static Menu menu3 = {
-	.item = menu3i,
-};
-
 void
 threadmain(int argc, char **argv)
 {
 	Mbuf_reusable mbuf;
 	Oevent_list events;
-	Field field;
 	Mousectl *mctl;
 	Keyboardctl *kctl;
 	Rune key;
@@ -209,6 +245,8 @@
 	a[2].c = kctl->c;
 
 	curbg = allocimage(display, Rect(0, 0, 1, 1), RGBA32, 1, DYellow);
+	charw = stringwidth(font, "X");
+	charh = font->height;
 
 	screensize(&w, &h);
 	field_init_fill(&field, h, w, '.');
@@ -224,7 +262,7 @@
 		oevent_list_clear(&events);
 		orca_run(field.buffer, mbuf.buffer, h, w, tick, &events, 0);
 		process(&events);
-		redraw(&field);
+		redraw();
 
 		oldw = w = field.width;
 		oldh = h = field.height;
@@ -242,7 +280,7 @@
 				case 0:
 					tmp[0] = 0;
 					if (enter("load file:", tmp, sizeof(tmp), mctl, kctl, nil) > 0) {
-						if (loadfield(&field, tmp) == 0) {
+						if (fieldload(tmp) == 0) {
 							w = field.width;
 							h = field.height;
 						} else {
@@ -261,15 +299,19 @@
 
 		case 2: /* keyboard */
 			switch (key) {
+			case 0x0b: /* C-k */
 			case Kup:
 				cury = MAX(0, cury-1);
 				break;
+			case '\n': /* C-j */
 			case Kdown:
 				cury = MIN(h-1, cury+1);
 				break;
+			case Kbs: /* C-h */
 			case Kleft:
 				curx = MAX(0, curx-1);
 				break;
+			case 0x0c: /* C-l */
 			case Kright:
 				curx = MIN(w-1, curx+1);
 				break;
@@ -290,11 +332,14 @@
 			case '+':
 				h += gridh;
 				break;
+			case Kins:
+				insert = !insert;
+				break;
 			default:
 				if (key == Kdel)
 					key = '.';
 				if (orca_is_valid_glyph(key))
-					field.buffer[curx + w*cury] = key;
+					fieldset(key);
 				else
 					fprint(2, "unhandled key %04x\n", key);
 				break;