tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

POC for more compact default output of apropos



Hi all,
attached patch is based on compla^Wsuggestions from Jared. The goal is
to keep command and short description aligned and limit the snippet to a
single line. More work is needed to properly handle escape sequences and
a trailing elipse, but it would be nice to agree first that this is an
improvement. I'll leave the rest to someone else :)

Joerg
Index: apropos.c
===================================================================
RCS file: /home/joerg/repo/netbsd/src/usr.sbin/makemandb/apropos.c,v
retrieving revision 1.16
diff -u -p -r1.16 apropos.c
--- apropos.c	2 Apr 2013 17:16:50 -0000	1.16
+++ apropos.c	2 May 2015 23:45:17 -0000
@@ -33,7 +33,10 @@
 #include <sys/cdefs.h>
 __RCSID("$NetBSD: apropos.c,v 1.16 2013/04/02 17:16:50 christos Exp $");
 
+#include <sys/ioctl.h>
 #include <err.h>
+#include <fcntl.h>
+#include <paths.h>
 #include <search.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -44,6 +47,8 @@ __RCSID("$NetBSD: apropos.c,v 1.16 2013/
 #include "apropos-utils.h"
 #include "sqlite3.h"
 
+static int cols;
+
 typedef struct apropos_flags {
 	int sec_nums[SECMAX];
 	int nresults;
@@ -171,6 +176,21 @@ main(int argc, char *argv[])
 
 	parseargs(argc, argv, &aflags);
 
+	if (aflags.format == APROPOS_NONE || aflags.format == APROPOS_TERM) {
+		int ttyfd = open(_PATH_TTY, O_RDONLY);
+		if (ttyfd != -1) {
+			struct winsize ws;
+
+			if (ioctl(ttyfd, TIOCGWINSZ, &ws) == 0)
+				cols = ws.ws_col;
+			else
+				cols = -1;
+			close(ttyfd);
+		} else {
+			cols = -1;
+		}
+	}
+
 	/*
 	 * If the user specifies a section number as an option, the
 	 * corresponding index element in sec_nums is set to the string
@@ -265,15 +285,18 @@ query_callback(void *data, const char *s
 	FILE *out = cbdata->out;
 	cbdata->count++;
 	if (cbdata->aflags->format != APROPOS_HTML) {
-	    fprintf(out, cbdata->aflags->legacy ? "%s(%s) - %s\n" :
-		"%s (%s)\t%s\n", name, section, name_desc);
-	    if (cbdata->aflags->no_context == 0)
-		    fprintf(out, "%s\n\n", snippet);
+	    fprintf(out, "%s(%s) - %s\n", name, section, name_desc);
+		if (cbdata->aflags->no_context == 0) {
+			if (cols == -1)
+				fprintf(out, "    %s\n", snippet);
+			else
+				fprintf(out, "    %.*s\n", cols - 5, snippet);
+		}
 	} else {
-	    fprintf(out, "<tr><td>%s(%s)</td><td>%s</td></tr>\n", name,
-		section, name_desc);
-	    if (cbdata->aflags->no_context == 0)
-		    fprintf(out, "<tr><td colspan=2>%s</td></tr>\n", snippet);
+		fprintf(out, "<tr><td>%s(%s)</td><td>%s</td></tr>\n", name,
+		    section, name_desc);
+		if (cbdata->aflags->no_context == 0)
+			fprintf(out, "<tr><td colspan=2>%s</td></tr>\n", snippet);
 	}
 
 	return 0;


Home | Main Index | Thread Index | Old Index