shithub: castor9

Download patch

ref: e29ac5e534dc9ec859b5698331fc4d6cf3ef191c
parent: 69cadae851b48fc297cb5293c7899dd43dd499e5
author: Julien Blanchard <[email protected]>
date: Fri Jan 1 06:15:47 EST 2021

Add/Show bookmarks

--- a/castor.c
+++ b/castor.c
@@ -44,6 +44,7 @@
 void gemini_put(Response *r);
 void texthit(Panel *p, int b, Rtext *t);
 void addbookmark(void);
+void showbookmarks(void);
 void message(char *s, ...);
 
 Panel *root;
@@ -569,7 +570,7 @@
 		search();
 		break;
 	case Mbookmarks:
-		//showbookmarks();
+		showbookmarks();
 		break;
 	case Maddbookmark:
 		addbookmark();
@@ -580,13 +581,75 @@
 	}
 }
 
-void
-addbookmark(void)
+char*
+getbookmarkspath(void)
 {
+	char *home, *bpath;
+	home = getenv("home");
+	if(home==0)
+		sysfatal("getenv(home): %r");
+
+	bpath = smprint("%s/lib/castorbookmarks", home);
+	return bpath;
+}
+
+int
+createbookmarks(void)
+{
 	int fd;
-	if((fd = create("bookmarks", OWRITE, 0600 | DMAPPEND)) < 0)
+	if((fd = create(getbookmarkspath(), OWRITE, 0600 | DMAPPEND)) < 0)
 		sysfatal("create(bookmarks): %r");
+	return fd;
+}
 
+void
+showbookmarks(void)
+{
+	char *line;
+	Biobuf *bfile;
+
+	bfile = Bopen(getbookmarkspath(), OREAD);
+	if(bfile==nil){
+		message("You must bookmark a page first!");
+		return;
+	}
+
+	Ctx *c;
+	c = malloc(sizeof *c);
+	if(c==nil)
+		sysfatal("malloc: %r");
+	c->text = nil;
+	c->url = urlparse(nil, "file://bookmarks");
+
+	Hist *h;
+	h = malloc(sizeof *h);
+	if(h == nil)
+		sysfatal("malloc: %r");
+
+	plrtstr(&c->text, 1000000, 0, 0, font, strdup(" "), 0, 0);
+
+	message("loading bookmarks...");
+	
+	while((line = Brdstr(bfile, '\n', 0)) != nil){
+		render_link(c, line);
+		free(line);
+	}
+
+	Bflush(bfile);
+
+	h->p = hist;
+	h->n = nil;
+	h->c = c;
+	hist = h;
+
+	show(c);
+}
+
+void
+addbookmark(void)
+{
+	int fd;
+	fd = createbookmarks();
 	fprint(fd, "=> %s\n", hist->c->url->raw);
 	close(fd);
 	message("Bookmark added!");