Commits

Evan Gates  committed 63cc74a

wrap on word breaks

  • Participants
  • Parent commits 5150f2a

Comments (0)

Files changed (1)

 #define STDIN  0
 #define STDOUT 1
 #define STDERR 2
+#define MIN(a,b) (((a) < (b)) ? (a) : (b))
 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
 
 FILE *in, *out;
 
 void print_line(char *line)
 {
-	char *m;
-	int i, len, w, cols = ws.ws_col;
+	char *m, *p;
+	int len, wlen, w, cols = ws.ws_col - 1;
 
 	len = strlen(line);
 	m = strchr(line, margin);
 	if (line[len - 1] == '\n')
 		line[--len] = '\0';
 
-	printf("%.*s\n", cols, line);
+	for (p = MIN(line + len, line + cols); p > line && p < line + len && *p != ' '; p--)
+		;
+	wlen = (p == line) ? cols : p - line + (*p == ' ');
+	printf("%.*s\n", wlen, line);
 
-	for (i = cols; i < len; i += cols - 2 - w)
-		printf("%*s%c %.*s\n", w, "", margin, cols - 2 - w, line + i);
+	for (p = line + wlen; p < line + len; p += wlen) {
+		for (m = MIN(line + len, p + cols - 2 - w); m > p && m < line + len && *m != ' '; m--)
+			;
+		wlen = (m == p) ? cols - 2 - w : m - p + (*m == ' ');
+		printf("%*s%c %.*s\n", w, "", margin, wlen, p);
+	}
 }
 
 void sigwinch(int unused)