shithub: castor9

Download patch

ref: 645dac214007d2edd60ea495a1e073cccdf482fc
parent: d872057643707f83e541f736178427fc17033eb7
author: Julien Blanchard <[email protected]>
date: Fri Nov 27 10:58:30 EST 2020

Wrap text, many tweaks

--- a/castor.c
+++ b/castor.c
@@ -50,6 +50,14 @@
 };
 
 
+char* replace_char(char* str, char find, char replace){
+    char *current_pos = strchr(str,find);
+    while (current_pos){
+        *current_pos = replace;
+        current_pos = strchr(current_pos,find);
+    }
+    return str;
+}
 
 char *
 cleanup(char *line)
@@ -61,6 +69,7 @@
     }
     *dst = '\0';
 	
+	replace_char(line, '\t', ' ');
 	return line;
 }
 
@@ -150,35 +159,52 @@
 	fprint(fd, "%s\r\n", url->url);
 	Binit(&body, fd, OREAD);
 
-	while((line = Brdstr(&body, '\n', 0)) != nil){
+	while((line = Brdstr(&body, '\n', 0)) != nil)
+	{
 		if (strstr(line, "=>") == nil) {
-			//String *lines[];
-			//int line_count = 0;
-			//String *cut_line;
-			//cut_line = s_new();
-			//char delim[] = " ";
-			
-			//char *ptr = strtok(line, delim);
+			/* Not a link so wrapping text */
+			char *base,*right_margin;
+    		int length,width;
 
-			//while(ptr != NULL)
-			//{
-			//	if(s_len(cut_line)+strlen(ptr)<80){
-			//		s_append(cut_line, strcat(ptr, " "));
-			//	}else{
-			//		lines[line_count] = cut_line;
-			//		line_count++;
-			//		cut_line = s_new();
-			//		s_append(cut_line, ptr);
-			//	}
-			//	ptr = strtok(NULL, delim);
-			//}
+    		length = strlen(strdup(line));
+    		base = strdup(line);
+    		width = 80;
 
-			//for (int i = 0; i < line_count; i++){
-			//	plrtstr(&m->text, 1000000, 8, 0, font, strdup(s_to_c(lines[i])), 0, 0);
-			//}
-			plrtstr(&m->text, 1000000, 8, 0, font, strdup(cleanup(line)), 0, 0);
+			while(*base)
+    		{
+        		if(length <= width)
+        		{
+					plrtstr(&m->text, 1000000, 8, 0, font, strdup(cleanup(base)), 0, 0);
+					break;
+        		}
+        		right_margin = base+width;
+        		while(!isspace(*right_margin))
+        		{
+            		right_margin--;
+            		if( right_margin == base)
+            		{
+                		right_margin += width;
+                		while(!isspace(*right_margin))
+                		{
+                    		if( *right_margin == '\0')
+                        		break;
+                    		right_margin++;
+                		}
+            		}
+        		}
+        		*right_margin = '\0';
+				plrtstr(&m->text, 1000000, 8, 0, font, strdup(cleanup(base)), 0, 0);
+        		length -= right_margin-base+1;      /* +1 for the space */
+        		base = right_margin+1;
+    		}
 		} else {
-			plrtstr(&m->text, 1000000, 8, 0, font, strdup(cleanup(line)), PL_HOT, 0);
+			/* a link */
+			char *copy = strdup(cleanup(line));
+			strtok(copy, " ");
+			char *link = strtok(NULL, " ");
+			char *rest = strtok(NULL, "\0");
+			char *label = smprint("→ %s", rest);
+			plrtstr(&m->text, 1000000, 8, 0, font, strdup(label), PL_HOT, strdup(link));
 		}
 		free(line);
 	}
@@ -191,7 +217,6 @@
 	plinitlabel(urlp, PACKN|FILLX, strdup(url->url));
 	pldraw(urlp, screen);
 	message("Castor!");
-	message("%d", textp->r.max.x);
 }
 
 void
@@ -248,31 +273,28 @@
 void
 texthit(Panel *p, int b, Rtext *rt)
 {
-	message("text hit %s", rt->text);
-	char *t, *u, *next_url;
-	char *s = rt->text;
+	//message("text hit %s", rt->user);
+	char *copy, *next_url;
+	char *link = rt->user;
 
-	next_url = nil;
-	if (s[0] == '=' && s[1] == '>') {
-		t = s + 2;
-		while (isspace(*t)) {
-			t++;
-			u = t;
-			if ((t = strpbrk(t, " :/")) == nil || t[0] != ':' || t[1] != '/' || t[2] != '/') { /* no scheme */
-				if (*u == '/') { /* absolute */
-					char *path = strtok(u, " ");
-					next_url = smprint("gemini://%s:%s/%s", current_url->server, current_url->port, path+1);
-				} else {
-					char *path = strtok(u, " ");
-					next_url = smprint("gemini://%s:%s/%s", current_url->server, current_url->port, path);
-				}
-			} else {
-				next_url = strtok(u, " ");
-			}
+	copy = link;
+	if ((strpbrk(link, " :/") == nil) || link[0] == '/') {
+		/* assuming relative URL */
+		if (*copy == '/') {
+			next_url = smprint("gemini://%s:%s/%s", current_url->server, current_url->port, copy+1);
+		} else {
+			next_url = smprint("gemini://%s:%s/%s", current_url->server, current_url->port, copy);
 		}
-		//free(s);
+	} else {
+		/* absolute URL */
+		next_url = strdup(link);
+	}
+	free(link);
+
+	if(strstr(next_url, "gemini://") != nil) {
 		gemini_get(parseurl(next_url));
-		//message(next_url);
+	} else {
+		message("%s protocol not supported yet!", next_url);
 	}
 }