shithub: bar

Download patch

ref: a75dbcaded5dc4a53ae0ed872a0f7da20502610d
parent: f34cd2fe2f884b250bf0c4396cfe4186b34b2bf9
parent:
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Wed Nov 18 15:17:41 EST 2020

fix window sizing depending on the font; use tmdate(2)

--- a/bar.c	Wed Nov 18 14:46:02 2020
+++ b/bar.c	Wed Nov 18 15:17:41 2020
@@ -4,8 +4,6 @@
 #include <mouse.h>
 
 #define MAX(a,b) ((a)>=(b)?(a):(b))
-#define MIN(a,b) ((a)<=(b)?(a):(b))
-#define CLAMP(x,min,max) MAX(min, MIN(max, x))
 
 enum {
 	Off = 4,
@@ -16,21 +14,21 @@
 	int w, h;
 }scr;
 
-static char *wday[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
 static char *pos = "rb";
 static int wctl, width, bottom, bat;
+static Tzone *local;
 
 static void
 place(void)
 {
 	static int ow, oh;
 	char t[61], *a[5];
-	int f, n, w, h, minx, miny, maxx, maxy;
+	int fd, n, w, h, minx, miny, maxx, maxy;
 
-	if((f = open("/dev/screen", OREAD)) < 0)
+	if((fd = open("/dev/screen", OREAD)) < 0)
 		return;
-	n = read(f, t, sizeof(t)-1);
-	close(f);
+	n = read(fd, t, sizeof(t)-1);
+	close(fd);
 	t[sizeof(t)-1] = 0;
 	if(n != sizeof(t)-1 || tokenize(t, a, 5) != 5)
 		return;
@@ -40,16 +38,16 @@
 	if(ow != w || oh != h){
 		if(pos[0] == 't' || pos[1] == 't'){
 			miny = 0;
-			maxy = 48;
+			maxy = 3*f->height;
 		}else{
-			miny = h-48;
+			miny = h - 3*f->height;
 			maxy = h;
 		}
 		if(pos[0] == 'l' || pos[1] == 'l'){
 			minx = 0;
-			maxx = Borderwidth+width+Borderwidth;
+			maxx = MAX(100, Borderwidth+width+Borderwidth);
 		}else{
-			minx = w-Borderwidth-width-Borderwidth;
+			minx = MAX(100, w-Borderwidth-width-Borderwidth);
 			maxx = w;
 		}
 		fprint(wctl, "resize -r %d %d %d %d", minx, miny, maxx, maxy);
@@ -61,7 +59,7 @@
 static void
 redraw(void)
 {
-	Tm *tm;
+	Tm tm;
 	char bats[16], s[128], *t;
 	Point p;
 	Rectangle r;
@@ -77,15 +75,8 @@
 		t = strchr(bats, ' ');
 		strcpy(t, "% | ");
 	}
-	tm = localtime(time(nil));
-	snprint(
-		s, sizeof(s),
-		"%s%04d/%02d/%02d %s %02d:%02d:%02d",
-		bats,
-		tm->year+1900, tm->mon+1, tm->mday,
-		wday[tm->wday],
-		tm->hour, tm->min, tm->sec
-	);
+
+	snprint(s, sizeof(s), "%s%τ", bats, tmfmt(tmnow(&tm, local), "YYYY/MM/DD WW hh:mm:ss"));
 	width = Off + stringwidth(f, s) + Off;
 	p.x = r.max.x - width + Off;
 	p.y = (pos[0] == 't' || pos[1] == 't') ? r.max.y - (f->height + Off) : r.min.y + Off;
@@ -155,6 +146,10 @@
 	default:
 		usage();
 	}ARGEND
+
+	tmfmtinstall();
+	if((local = tzload("local")) == nil)
+		sysfatal("zone: %r");
 
 	if((wctl = open("/dev/wctl", ORDWR)) < 0)
 		sysfatal("%r");