shithub: zuke

Download patch

ref: e4a9c43404e2b63158cb59cef0d153dc320da5b0
parent: ebbddfea84efee38eb49ebc8165bc20c13f5950e
author: Sigrid Haflínudóttir <[email protected]>
date: Tue Jun 2 15:08:31 EDT 2020

add -c option to specify columns to show

--- a/README.md
+++ b/README.md
@@ -32,6 +32,21 @@
 % audio/mkplist /n/music | audio/zuke
 ```
 
+## Columns to display
+
+Zuke has an optional argument `-c` that specifies which columns to
+display, the default is `-c AatD`.
+
+```
+A  artist
+a  album
+t  title
+D  duration
+d  date
+T  track number
+p  file path
+```
+
 ## Hot keys
 
 ```
--- a/zuke.c
+++ b/zuke.c
@@ -55,8 +55,9 @@
 static Channel *ev;
 static Mousectl *mctl;
 static Keyboardctl *kctl;
-static int colwidth[3];
-static int mincolwidth[3];
+static int colwidth[7];
+static int mincolwidth[7];
+static char *cols = "AatD";
 static int *shuffle;
 static char *covers[] = {"folder", "cover", "Cover", "scans/CD", "Scans/Front", "Covers/Front"};
 
@@ -90,26 +91,58 @@
 	return fmtstrcpy(f, tmp);
 }
 
+static char *
+getcol(Meta *m, int c)
+{
+	static char tmp[32];
+
+	switch(c){
+	case Palbum: return m->album;
+	case Partist: return m->artist[0];
+	case Pdate: return m->date;
+	case Pduration: snprint(tmp, sizeof(tmp), "%8P", m->duration/1000); return tmp;
+	case Ptitle: return m->title;
+	case Ptrack: snprint(tmp, sizeof(tmp), "%4s", m->track); return m->track ? tmp : nil;
+	case Ppath: return m->path;
+	default: sysfatal("invalid column '%c'", c);
+	}
+
+	return nil;
+}
+
 static void
 adjustcolumns(void)
 {
-	int i, x, total;
+	int i, n, x, total, width;
 
 	if(mincolwidth[0] == 0){
-		mincolwidth[0] = mincolwidth[1] = mincolwidth[2] = 1;
-		for(i = 0; i < plnum; i++){
-			if((x = stringwidth(f, pl[i].artist[0])) > mincolwidth[0])
-				mincolwidth[0] = x;
-			if((x = stringwidth(f, pl[i].album)) > mincolwidth[1])
-				mincolwidth[1] = x;
-			if((x = stringwidth(f, pl[i].title)) > mincolwidth[2])
-				mincolwidth[2] = x;
+		for(i = 0; cols[i] != 0; i++)
+			mincolwidth[i] = 1;
+		for(n = 0; n < plnum; n++){
+			for(i = 0; cols[i] != 0; i++){
+				if((x = stringwidth(f, getcol(&pl[n], cols[i]))) > mincolwidth[i])
+					mincolwidth[i] = x;
+			}
 		}
 	}
 
-	total = mincolwidth[0] + mincolwidth[1] + mincolwidth[2];
-	for(i = 0; i < nelem(mincolwidth); i++)
-		colwidth[i] = (Dx(screen->r) - 8) * mincolwidth[i] / total;
+	total = 0;
+	n = 0;
+	width = Dx(screen->r);
+	for(i = 0; cols[i] != 0; i++){
+		if(cols[i] == Pduration || cols[i] == Pdate || cols[i] == Ptrack)
+			width -= mincolwidth[i] + 8;
+		else{
+			total += mincolwidth[i];
+			n++;
+		}
+	}
+	for(i = 0; cols[i] != 0; i++){
+		if(cols[i] == Pduration || cols[i] == Pdate || cols[i] == Ptrack)
+			colwidth[i] = mincolwidth[i];
+		else
+			colwidth[i] = (width - Scrollwidth - n*8) * mincolwidth[i] / total;
+	}
 }
 
 static Meta *
@@ -124,7 +157,7 @@
 	Image *col;
 	Point p, sp;
 	Rectangle sel, r;
-	int i, left, scrollcenter;
+	int i, j, left, scrollcenter;
 	char tmp[32];
 
 	lockdisplay(display);
@@ -154,16 +187,16 @@
 			left += Scrollwidth + 4;
 		}
 
-		p.x = sp.x = left + colwidth[0] + 4;
+		p.x = sp.x = left;
 		p.y = 0;
 		sp.y = screen->r.max.y;
-		line(screen, p, sp, Endsquare, Endsquare, 0, colors[Dfmed].im, ZP);
+		for(i = 0; cols[i+1] != 0; i++){
+			p.x += colwidth[i] + 4;
+			sp.x = p.x;
+			line(screen, p, sp, Endsquare, Endsquare, 0, colors[Dfmed].im, ZP);
+			p.x += 4;
+		}
 
-		p.x = sp.x = left + colwidth[0] + 8 + colwidth[1] + 4;
-		p.y = 0;
-		sp.y = screen->r.max.y;
-		line(screen, p, sp, Endsquare, Endsquare, 0, colors[Dfmed].im, ZP);
-
 		sp.x = sp.y = 0;
 		p.x = left + 2;
 		p.y = screen->r.min.y + 2;
@@ -186,25 +219,16 @@
 			}
 
 			sel = screen->r;
-			r = screen->r;
 
 			p.x = left + 2;
-			sel.max.x = p.x + colwidth[0];
-			replclipr(screen, 0, sel);
-			string(screen, p, col, sp, f, getmeta(i)->artist[0]);
-			p.x += colwidth[0] + 8;
-			sel.min.x = p.x;
-			sel.max.x = p.x + colwidth[1];
-			replclipr(screen, 0, sel);
-			string(screen, p, col, sp, f, getmeta(i)->album);
-			p.x += colwidth[1] + 8;
-			sel.min.x = p.x;
-			sel.max.x = p.x + colwidth[2];
-			replclipr(screen, 0, sel);
-			string(screen, p, col, sp, f, getmeta(i)->title);
+			for(j = 0; cols[j] != 0; j++){
+				sel.max.x = p.x + colwidth[j];
+				replclipr(screen, 0, sel);
+				string(screen, p, col, sp, f, getcol(getmeta(i), cols[j]));
+				p.x += colwidth[j] + 8;
+			}
+			replclipr(screen, 0, screen->r);
 
-			replclipr(screen, 0, r);
-
 			if(pcurplaying == i){
 				Point rightp, leftp;
 				leftp.y = rightp.y = p.y - 1;
@@ -797,6 +821,13 @@
 	}
 }
 
+static void
+usage(void)
+{
+	fprint(2, "usage: %s [-c aAdDtTp]\n", argv0);
+	sysfatal("usage");
+}
+
 void
 threadmain(int argc, char **argv)
 {
@@ -812,7 +843,16 @@
 	};
 	int fd, n, scrolling, oldpcur, oldbuttons, pnew;
 
-	USED(argc); USED(argv);
+	ARGBEGIN{
+	case 'c':
+		cols = EARGF(usage());
+		if(strlen(cols) >= nelem(colwidth))
+			sysfatal("max %d columns allowed", nelem(colwidth));
+		break;
+	default:
+		usage();
+		break;
+	}ARGEND;
 
 	audio = open("/dev/audio", OWRITE);
 	if(audio < 0)