[BusyBox-cvs] busybox.stable cmdedit.c,1.67,1.68

Erik Andersen andersen at codepoet.org
Tue Jan 14 18:20:08 UTC 2003


Update of /var/cvs/busybox.stable
In directory winder:/tmp/cvs-serv19199

Modified Files:
	cmdedit.c 
Log Message:
The cmdedit part of vodz' last_patch75 which improves terminal 
width handling when hitting tab-tab within a shell 


Index: cmdedit.c
===================================================================
RCS file: /var/cvs/busybox.stable/cmdedit.c,v
retrieving revision 1.67
retrieving revision 1.68
diff -u -d -r1.67 -r1.68
--- cmdedit.c	2 Jan 2003 07:25:47 -0000	1.67
+++ cmdedit.c	14 Jan 2003 18:20:02 -0000	1.68
@@ -965,6 +965,44 @@
 	return command_mode;
 }
 
+/*
+   display by column original ideas from ls applet,
+   very optimize by my :)
+*/
+static void showfiles(char **matches, int nfiles)
+{
+	int ncols, row;
+	int column_width = 0;
+	int nrows = nfiles;
+
+	/* find the longest file name-  use that as the column width */
+	for (row = 0; row < nrows; row++) {
+		int l = strlen(matches[row]);
+
+		if (column_width < l)
+			column_width = l;
+	}
+	column_width += 2;              /* min space for columns */
+	ncols = cmdedit_termw / column_width;
+
+	if (ncols > 1) {
+		nrows /= ncols;
+		if(nfiles % ncols)
+			nrows++;        /* round up fractionals */
+		column_width = -column_width;   /* for printf("%-Ns", ...); */
+	} else {
+		ncols = 1;
+	}
+	for (row = 0; row < nrows; row++) {
+		int n = row;
+		int nc;
+
+		for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++)
+			printf("%*s", column_width, matches[n]);
+		printf("%s\n", matches[n]);
+	}
+}
+
 
 static void input_tab(int *lastWasTab)
 {
@@ -1092,29 +1130,11 @@
 		 * just hit TAB again, print a list of all the
 		 * available choices... */
 		if (matches && num_matches > 0) {
-			int i, col, l;
 			int sav_cursor = cursor;	/* change goto_new_line() */
 
 			/* Go to the next line */
 			goto_new_line();
-			for (i = 0, col = 0; i < num_matches; i++) {
-				l = strlen(matches[i]);
-				if (l < 14)
-					l = 14;
-				printf("%-14s  ", matches[i]);
-				col+=l;
-				if ((l += 2) > 16)
-					while (l % 16) {
-						putchar(' ');
-						l++;
-					}
-				if (col > (cmdedit_termw-l-l) && matches[i + 1] != NULL) {
-					putchar('\n');
-					col = 0;
-				}
-			}
-			/* Go to the next line and rewrite */
-			putchar('\n');
+			showfiles(matches, num_matches);
 			redraw(0, len - sav_cursor);
 		}
 	}




More information about the busybox-cvs mailing list