Anonymous avatar Anonymous committed 298c4bd

cleaner wrap on word break

Comments (0)

Files changed (1)

 struct winsize ws;
 char margin = 0;
 
+int break_len(char *start, char *end, int def)
+{
+	char *p;
+
+	if (end - start < def)
+		return end - start;
+
+	for (p = start + def; p > start && *p != ' '; p--)
+		;
+	return (p == start) ? def : p - start + (*p == ' ');
+}
+
 void print_line(char *line)
 {
-	char *m, *p;
+	char *p;
 	int len, wlen, w, cols = ws.ws_col - 1;
 
 	len = strlen(line);
-	m = strchr(line, margin);
-	w = m - line;
+	p = strchr(line, margin);
+	w = p - line;
 
-	if (!margin || !m || w + 20 >= cols) { // 20, pretty random choice
+	if (!margin || !p || w + 20 >= cols) { // 20, pretty random choice
 		fputs(line, stdout);
 		return;
 	}
 	if (line[len - 1] == '\n')
 		line[--len] = '\0';
 
-	for (p = MIN(line + len, line + cols); p > line && p < line + len && *p != ' '; p--)
-		;
-	wlen = (p == line) ? cols : p - line + (*p == ' ');
+	wlen = break_len(line, line + len, cols);
 	printf("%.*s\n", wlen, line);
 
 	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 == ' ');
+		wlen = break_len(p, line + len, cols - 2 - w);
 		printf("%*s%c %.*s\n", w, "", margin, wlen, p);
 	}
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.