shithub: irc.myr

Download patch

ref: 3aa41b98d677101317487d504fefc49e433b16bf
parent: 7e2b193e327849b76e0f5ae1a1185d09f9e38886
author: Ori Bernstein <ori@eigenstate.org>
date: Fri Aug 25 20:37:18 EDT 2017

Fix some rendering bugs.

--- a/irc.myr
+++ b/irc.myr
@@ -6,8 +6,8 @@
 use fileutil	/* for homedir() */
 
 pkg =
-	const status	: (cli : client#, fmt : byte[:], args : ... -> void)
 	const send	: (cli : client#, srv : server#, fmt : byte[:], args : ... -> void)
+	const status	: (cli : client#, chan : chan#, fmt : byte[:], args : ... -> void)
 ;;
 
 type client = struct
@@ -138,7 +138,7 @@
 		| "PING":	send(cli, srv, "PONG :{}\r\n", args[0])
 		| "JOIN":	joined(cli, srv, src, args)
 		| c:	
-			status(cli, "unknown server command {}", ln)
+			status(cli, cli.self, "unknown server command {}", ln)
 		;;
 		std.slfree(args)
 		std.slfree(ln)
@@ -150,7 +150,6 @@
 
 	if args.len == 1
 		c = name2chan(srv, args[0])
-		status(cli, "joined {}: {}", args[0], src)
 		match std.strfind(src, "!")
 		| `std.Some i:	name = src[:i]
 		| `std.None:	name = src
@@ -159,6 +158,7 @@
 		| `std.None:	std.slpush(&c.users, std.sldup(name))
 		| `std.Some _:	/* ignore */
 		;;
+		std.slpush(&c.hist, (std.now(), `Join std.sldup(name)))
 	;;
 }
 
@@ -189,8 +189,8 @@
 	for n : std.bysplit(args[3], " ")
 		n = std.strstrip(n)
 		match std.lsearch(c.users, n, std.strcmp)
-		| `std.None:	std.slpush(&c.users, std.sldup(n))
-		| `std.Some _:	/* ignore */
+		| `std.None:    std.slpush(&c.users, std.sldup(n))
+		| `std.Some _:  /* ignore */
 		;;
 		c.gutter = std.max(c.gutter, n.len + 1)
 	;;
@@ -238,7 +238,7 @@
 const closed = {cli, srv, msg
 	if srv.live
 		srv.live = false
-		status(cli, "{} closed: {}", srv.ds, msg)
+		status(cli, cli.self, "{} closed: {}", srv.ds, msg)
 	;;
 }
 
@@ -293,6 +293,7 @@
 		.hist = [][:], /* FIXME: read back from log */
 		.name = std.sldup(name),
 		.topic = std.sldup(topic),
+		.gutter = 8
 	])
 }
 
@@ -332,7 +333,7 @@
 	| "help":	help(cli, sp[1:])
 	| "nick":	changenick(cli, sp[1:])
 	| "user":	changeuser(cli, sp[1:])
-	| c:		status(cli, "unknown command: /{}", text)
+	| c:		status(cli, cli.self, "unknown command: /{}", text)
 	;;
 	std.slfree(sp)
 }
@@ -339,7 +340,7 @@
 
 const changenick = {cli, args
 	if args.len != 1
-		status(cli, "/nick: invalid args {j= }", args)
+		status(cli, cli.self, "/nick: invalid args {j= }", args)
 		-> void
 	;;
 
@@ -347,16 +348,18 @@
 	| `std.None:
 		std.slfree(cli.nick)
 		cli.nick = std.sldup(args[0])
+		status(cli, cli.self, "default nick changed: {}", args[0])
 	| `std.Some srv:
 		std.slfree(cli.nick)
 		srv.nick = std.sldup(args[0])
 		send(cli, srv, "NICK {}\r\n", srv.nick)
+		status(cli, cli.self, "nick changed for {}: {}", srv.ds, args[0])
 	;;
 }
 
 const changeuser = {cli, args
 	if args.len != 1
-		status(cli, "/nick: invalid args {j= }", args)
+		status(cli, cli.self, "/nick: invalid args {j= }", args)
 		-> void
 	;;
 
@@ -377,8 +380,8 @@
 	ds = ""
 	match args.len
 	| 1:	ds = std.netaddr(args[0], "tcp", "ircd")
-	| 0:	status(cli, "join: missing server")
-	| _:	status(cli, "join: invalid arguments '{j= }'", args)
+	| 0:	status(cli, cli.self, "join: missing server")
+	| _:	status(cli, cli.self, "join: invalid arguments '{j= }'", args)
 	;;
 	if ds.len == 0
 		-> void
@@ -386,12 +389,12 @@
 
 	for s : cli.srv
 		if std.sleq(s.ds, ds)
-			status(cli, "already connected to {j= }", args)
+			status(cli, cli.self, "already connected to {j= }", args)
 			-> void
 		;;
 	;;
 
-	status(cli, "dialing {}", ds)
+	status(cli, cli.self, "dialing {}", ds)
 	match std.dial(ds)
 	| `std.Ok fd:
 		srv = mksrv(cli, fd, ds)
@@ -401,30 +404,28 @@
 				cli.focus = cli.srv.len - 1
 				srv.live = true
 			;;
-			status(cli, "connected to {}", ds)
+			status(cli, cli.self, "connected to {}", ds)
 		else
-			status(cli, "could not handshake with {j= }", args)
+			status(cli, cli.self, "could not handshake with {j= }", args)
 			std.close(srv.fd)
 			std.free(srv)
 		;;
 	| `std.Err e:
-		status(cli, "failed to connect: {}", e)
+		status(cli, cli.self, "failed to connect: {}", e)
 	;;
 }
 
 const shownames = {cli, args
-	var s, c
+	var c
 
 	if args.len == 0
 		c = curchan(cli)
 	elif args.len == 1
-		-> status(cli, "names: invalid arguments: {j= }", args)
+		-> status(cli, cli.self, "names: invalid arguments: {j= }", args)
 	;;
 	c = curchan(cli)
 	if c != cli.self
-		s = std.fmt("{j= }", c.users)
-		chanstatus(cli, c, s)
-		std.slfree(s)
+		status(cli, c, "{j= }", c.users)
 	;;
 }
 
@@ -438,15 +439,15 @@
 	if args.len == 2
 		match findsrv(cli, args[1])
 		| `std.Some s:	send(cli, s, "JOIN {}\r\n", args[0])
-		| `std.None:	status(cli, "no server '{}", args[1])
+		| `std.None:	status(cli, cli.self, "no server '{}", args[1])
 		;;
 	elif args.len == 1
 		match cursrv(cli)
 		| `std.Some s:	send(cli, s, "JOIN {}\r\n", args[0])
-		| `std.None:	status(cli, "no server focused")
+		| `std.None:	status(cli, cli.self, "no server focused")
 		;;
 	else
-		-> status(cli, "invalid arguments: {j= }", args)
+		-> status(cli, cli.self, "invalid arguments: {j= }", args)
 	;;
 }
 
@@ -462,7 +463,7 @@
 	elif args.len == 1
 		name = args[0]
 	else
-		status(cli, "leave: invalid arguments: {j= }", args)
+		status(cli, cli.self, "leave: invalid arguments: {j= }", args)
 		-> void
 	;;
 
@@ -485,7 +486,7 @@
 	var ch, srv
 
 	if args.len != 1
-		status(cli, "invalid arguments: {j= }", args)
+		status(cli, cli.self, "invalid arguments: {j= }", args)
 	else
 		srvidx = -1
 		idx = -1
@@ -501,7 +502,7 @@
 				| `std.None:	/* ok */
 				| `std.Some n:
 					if idx != -1
-						status(cli, "ambiguous channel name {}", args[0])
+						status(cli, cli.self, "ambiguous channel name {}", args[0])
 						-> void
 					else
 						srvidx = i
@@ -524,7 +525,7 @@
 
 const quit = {cli, args
 	if args.len != 0
-		status(cli, "invalid argument for quit: {j= }", args)
+		status(cli, cli.self, "invalid argument for quit: {j= }", args)
 		-> void
 	;;
 	termdraw.free(cli.term)
@@ -532,14 +533,15 @@
 }
 
 const help = {cli, args
-	status(cli, "irc.myr commands")
-	status(cli, "\t/connect dialstr: connect to server")
-	status(cli, "\t/help [cmd...]:   get help [on cmd...]")
-	status(cli, "\t/join chan [srv]: join channel on server")
-	status(cli, "\t/leave [chan]:    leave current channel")
-	status(cli, "\t/chan name:       switch to channnel 'name'")
-	status(cli, "\t/win name:        switch to channnel 'name'")
-	status(cli, "\t/quit:            exit irc.myr")
+	status(cli, cli.self, "irc.myr commands")
+	status(cli, cli.self, "\t/connect dialstr: connect to server")
+	status(cli, cli.self, "\t/help [cmd...]:   get help [on cmd...]")
+	status(cli, cli.self, "\t/join chan [srv]: join channel on server")
+	status(cli, cli.self, "\t/leave [chan]:    leave current channel")
+	status(cli, cli.self, "\t/chan name:       switch to channnel 'name'")
+	status(cli, cli.self, "\t/win name:        switch to channnel 'name'")
+	status(cli, cli.self, "\t/names:           list nicks in current channel")
+	status(cli, cli.self, "\t/quit:            exit irc.myr")
 }
 
 const send = {cli, srv, fmt, args
@@ -560,13 +562,13 @@
 	| `std.Some s:
 		c = curchan(cli)
 		if c == cli.self
-			status(cli, "can't send to status channel")
+			status(cli, cli.self, "can't send to status channel")
 		else
 			send(cli, s, "PRIVMSG {} :{}\r\n", c.name, msg)
 			chanmsg(cli, c, s.nick, msg)
 		;;
 	| `std.None:
-		status(cli, "no connected server")
+		status(cli, cli.self, "no connected server")
 	;;
 }
 
@@ -689,6 +691,10 @@
 	off = 0
 	dx = x1 - x0
 	dy = y1 - y0
+	/* nothing worth drawing... */
+	if dx <= c.gutter
+		-> void
+	;;
 	for (tm, h) : std.byreverse(c.hist)
 		margin = 0
 		match h
@@ -724,6 +730,7 @@
 		match h
 		| `Msg (m, ln):
 			(x, y) = draw(t, m, x0, y, x1, y1)
+			(x, y) = draw(t, ">", x0 + c.gutter - 2, y, x1, y1)
 			(x, y) = draw(t, ln[off:], x0 + c.gutter, y, x1, y1)
 		| `Join user:
 			(x, y) = draw(t, "#joined ", x0, y, x1, y1)
@@ -762,11 +769,6 @@
 		if x >= x1
 			x = x0
 			y++
-
-			termdraw.move(t, x, y)
-			termdraw.putc(t, '|')
-			termdraw.putc(t, ' ')
-			x += 2
 		;;
 	;;
 	-> (x, y)
@@ -805,20 +807,19 @@
 
 const srvmsg = {cli, args
 	if args.len >= 2
-		status(cli, "{}", args[1])
+		status(cli, cli.self, "{}", args[1])
 	;;
 }
 
-const status = {cli, fmt, args : ...
-	var c, s, ap
+const status = {cli, chan, fmt, args : ...
+	var s, ap
 
-	c = cli.self
 	ap = std.vastart(&args)
 	s = std.fmtv(fmt, &ap)
-	std.slpush(&c.hist, (std.now(), `Status s))
+	std.slpush(&chan.hist, (std.now(), `Status s))
 
-	if c != curchan(cli)
-		c.stale = true
+	if chan != curchan(cli)
+		chan.stale = true
 	;;
 }
 
@@ -852,7 +853,7 @@
 		match std.strfind(cli.srv[i].ds, name)
 		| `std.Some o:
 			if idx != -1
-				status(cli, "ambiguous server name {}", name)
+				status(cli, cli.self, "ambiguous server name {}", name)
 				-> `std.None
 			else
 				idx = i
@@ -905,17 +906,14 @@
 	var nick
 
 	match std.strfind(sender, "!")
-	| `std.Some i:	nick = sender[:i]
-	| `std.None:	nick = sender
+	| `std.Some i:	nick = std.sldup(sender[:i])
+	| `std.None:	nick = std.sldup(sender)
 	;;
-	std.slpush(&chan.hist, (std.now(), `Msg (nick, msg)))
+	std.slpush(&chan.hist, (std.now(), `Msg (nick, std.sldup(msg))))
 	if chan != curchan(cli)
 		chan.stale = true
 	;;
 
-}
-
-const chanstatus = {cli, chan, msg
 }
 
 const writeall = {fd, buf
--