shithub: riscv

Download patch

ref: dc5143fd4fbdad70ad346b4714cb5d9947662e60
parent: 3668c38e40b94bfa3fa634ab15f3b3b4746aa224
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Mon Oct 17 15:12:50 EDT 2022

zuke: use percentile to calculate relative widths of columns

--- a/sys/src/cmd/audio/zuke/icy.c
+++ b/sys/src/cmd/audio/zuke/icy.c
@@ -14,18 +14,6 @@
 	int metaint;
 };
 
-static char *
-truncate(char *s){
-	Rune … = L'…';
-	int n;
-
-	/* titles can be obnoxiously long */
-	if(strlen(s) > (n = 48-UTFmax))
-		s[n + runetochar(s+n, &…)] = 0;
-
-	return s;
-}
-
 static long
 Breadn(Biobufhdr *bp, void *addr, long nbytes)
 {
@@ -65,10 +53,8 @@
 				break;
 			p[n] = 0;
 			if((s = strstr(p, "StreamTitle='")) != nil && (e = strstr(s+13, "';")) != nil && e != s+13){
-				s += 13;
 				*e = 0;
-				s = strdup(truncate(s));
-				if(sendp(aux->newtitle, s) != 1){
+				if(sendp(aux->newtitle, strdup(s+13)) != 1){
 					free(s);
 					break;
 				}
@@ -121,7 +107,7 @@
 		}
 		s[n-2] = 0;
 		if(strncmp(s, "icy-name:", 9) == 0){
-			s = truncate(s+9);
+			s += 9;
 			if(newtitle != nil)
 				sendp(aux->newtitle, strdup(s));
 			else if(m->title == nil)
--- a/sys/src/cmd/audio/zuke/zuke.c
+++ b/sys/src/cmd/audio/zuke/zuke.c
@@ -927,11 +927,17 @@
 	return s;
 }
 
+static int
+cmpint(void *a, void *b)
+{
+	return *(int*)a - *(int*)b;
+}
+
 static Playlist *
 readplist(int fd, int mincolwidth[Ncol])
 {
 	char *raw, *s, *e, *a[5], *b;
-	int plsz, i, x;
+	int plsz, i, *w;
 	Playlist *pl;
 	Meta *m;
 
@@ -950,9 +956,6 @@
 		return nil;
 	}
 
-	for(i = 0; cols[i] != 0; i++)
-		mincolwidth[i] = 1;
-
 	pl->raw = raw;
 	for(s = pl->raw, m = pl->m, e = s; e != nil; s = e){
 		if((e = strchr(s, '\n')) == nil)
@@ -963,10 +966,6 @@
 		case 0:
 addit:
 			if(m->path != nil){
-				for(i = 0; cols[i] != 0; i++){
-					if((x = stringwidth(f, getcol(m, cols[i]))) > mincolwidth[i])
-						mincolwidth[i] = x;
-				}
 				if(m->filefmt == nil)
 					m->filefmt = "";
 				pl->n++;
@@ -1001,6 +1000,15 @@
 			break;
 		}
 	}
+
+	w = malloc(sizeof(int)*pl->n);
+	for(i = 0; cols[i] != 0; i++){
+		for(m = pl->m; m != pl->m + pl->n; m++)
+			w[m - pl->m] = stringwidth(f, getcol(m, cols[i]));
+		qsort(w, pl->n, sizeof(*w), cmpint);
+		mincolwidth[i] = w[93*(pl->n-1)/100];
+	}
+	free(w);
 
 	return pl;
 }