shithub: irc.myr

Download patch

ref: 0bea72e0c4734953884f544694de47cb74563e3f
parent: 56df36161464f68b5139ddb11b4f6677ba241eea
author: Ori Bernstein <ori@eigenstate.org>
date: Thu Nov 23 16:21:29 EST 2017

Allow setting logdir, and fix division by zero.

--- a/irc.myr
+++ b/irc.myr
@@ -61,24 +61,29 @@
 	`Status	byte[:]
 ;;
 
-var opt_rcfile = ".ircrc"
-
 const main = {args
 	var irc : irc#
+	var home, rcfile, logdir
 	var c
 	var fd
 	var cmd
 
+	home = fileutil.homedir()
+	rcfile = std.pathcat(home, ".ircrc")
+	logdir = std.pathcat(home, ".irclogs")
 	cmd = std.optparse(args, &[
 		.maxargs=-1,
 		.opts=[
 			[.opt='c', .arg="cfg",
-			 .desc="use config file cfg",
-			 .dest=`std.Some &opt_rcfile],
+			 .desc="use config file 'cfg'",
+			 .dest=`std.Some &rcfile],
+			[.opt='l', .arg="log",
+			 .desc="use log dir 'log'",
+			 .dest=`std.Some &logdir],
 		][:]
 	])
 
-	irc = mkirc()
+	irc = mkirc(rcfile, logdir)
 	while true
 		redraw(irc)
 		c = curchan(irc)
@@ -323,14 +328,13 @@
 	;;
 }
 
-const mkirc = {
-	var irc, home, path, t
+const mkirc = {rcfile, logdir
+	var irc, term
 	var nick, user
 
-	t = termdraw.mk(std.In)
-	termdraw.raw(t)
-	termdraw.cursoron(t)
-	home = fileutil.homedir()
+	term = termdraw.mk(std.In)
+	termdraw.raw(term)
+	termdraw.cursoron(term)
 	user = std.getenvv("user", std.getenvv("USER", "user"))
 	nick = user
 	irc = std.mk([
@@ -338,15 +342,14 @@
 		.user = std.sldup(user),
 		.nick = std.sldup(nick),
 		.focus = -1,
-		.term = t,
-		.logdir = std.pathcat(home, ".irclogs"),
+		.term = term,
+		.logdir = logdir,
 		.chandirty = true,
 		.cmddirty = true,
 	])
 	irc.self = mkchan(irc, "status", "status", "status")
 
-	path = std.pathcat(home, opt_rcfile)
-	match bio.open(path, bio.Rd)
+	match bio.open(rcfile, bio.Rd)
 	| `std.Ok cfg:
 		for ln : bio.byline(cfg)
 			do(irc, ln)
@@ -353,7 +356,6 @@
 		;;
 	| `std.Err e:
 	;;
-	std.slfree(path)
 
 	-> irc
 }
@@ -984,9 +986,9 @@
 	if dx <= c.gutter
 		-> void
 	;;
-	for (tm, h) : std.byreverse(c.hist[:c.hist.len - c.scroll])
+	for (tm, ent) : std.byreverse(c.hist[:c.hist.len - c.scroll])
 		margin = 0
-		match h
+		match ent
 		| `Msg (m, ln):
 			margin = c.gutter + termdraw.strwidth(t, " | ")
 			width = termdraw.strwidth(t, ln)
@@ -1000,12 +1002,13 @@
 			margin = termdraw.strwidth(t, "! ")
 			width = termdraw.strwidth(t, msg)
 		;;
-		if dx - margin + 1 <=  0
+		/* no point in drawing if all we draw is gutter */
+		if dx - margin <=  0
 			-> void
 		;;
 		height++
 		if width > 0
-			height += (width - 1) / (dx - margin)
+			height += width / (dx - margin)
 		;;
 		count++
 		if height == dy
--