this repo has no description
3
fork

Configure Feed

Select the types of activity you want to include in your feed.

irc: refactor formatted prints

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>

+20 -34
+20 -34
src/irc.zig
··· 7 7 8 8 const testing = std.testing; 9 9 10 + const Allocator = std.mem.Allocator; 10 11 pub const MessagePool = bytepool.BytePool(max_raw_msg_size * 4); 11 12 pub const Slice = MessagePool.Slice; 12 13 ··· 206 207 self.has_unread_highlight = false; 207 208 const last_msg = self.messages.getLast(); 208 209 const time_tag = last_msg.getTag("time") orelse return; 209 - var write_buf: [128]u8 = undefined; 210 - const mark_read = try std.fmt.bufPrint( 211 - &write_buf, 210 + try self.client.print( 212 211 "MARKREAD {s} timestamp={s}\r\n", 213 212 .{ 214 213 self.name, 215 214 time_tag, 216 215 }, 217 216 ); 218 - try self.client.queueWrite(mark_read); 219 217 } 220 218 }; 221 219 ··· 657 655 } 658 656 } 659 657 658 + pub fn print(self: *Client, comptime fmt: []const u8, args: anytype) Allocator.Error!void { 659 + const msg = try std.fmt.allocPrint(self.alloc, fmt, args); 660 + self.write_queue.push(.{ .write = .{ 661 + .client = self, 662 + .msg = msg, 663 + .allocator = self.alloc, 664 + } }); 665 + } 666 + 660 667 /// push a write request into the queue. The request should include the trailing 661 668 /// '\r\n'. queueWrite will dupe the message and free after processing. 662 - pub fn queueWrite(self: *Client, msg: []const u8) !void { 669 + pub fn queueWrite(self: *Client, msg: []const u8) Allocator.Error!void { 663 670 self.write_queue.push(.{ .write = .{ 664 671 .client = self, 665 672 .msg = try self.alloc.dupe(u8, msg), ··· 688 695 self.stream = try std.net.tcpConnectToHost(self.alloc, self.config.server, port); 689 696 } 690 697 691 - var buf: [4096]u8 = undefined; 692 - 693 698 try self.queueWrite("CAP LS 302\r\n"); 694 699 695 700 const cap_names = std.meta.fieldNames(Capabilities); 696 701 for (cap_names) |cap| { 697 - const cap_req = try std.fmt.bufPrint( 698 - &buf, 702 + try self.print( 699 703 "CAP REQ :{s}\r\n", 700 704 .{cap}, 701 705 ); 702 - try self.queueWrite(cap_req); 703 706 } 704 707 705 - const nick = try std.fmt.bufPrint( 706 - &buf, 708 + try self.print( 707 709 "NICK {s}\r\n", 708 710 .{self.config.nick}, 709 711 ); 710 - try self.queueWrite(nick); 711 712 712 - const user = try std.fmt.bufPrint( 713 - &buf, 713 + try self.print( 714 714 "USER {s} 0 * {s}\r\n", 715 715 .{ self.config.user, self.config.real_name }, 716 716 ); 717 - try self.queueWrite(user); 718 717 } 719 718 720 719 pub fn getOrCreateChannel(self: *Client, name: []const u8) !*Channel { ··· 772 771 self.caps.@"away-notify" and 773 772 !channel.in_flight.who) 774 773 { 775 - var write_buf: [64]u8 = undefined; 776 774 channel.in_flight.who = true; 777 - const who = try std.fmt.bufPrint( 778 - &write_buf, 775 + try self.print( 779 776 "WHO {s} %cnfr\r\n", 780 777 .{channel.name}, 781 778 ); 782 - try self.queueWrite(who); 783 779 } else { 784 - var write_buf: [64]u8 = undefined; 785 780 channel.in_flight.names = true; 786 - const names = try std.fmt.bufPrint( 787 - &write_buf, 781 + try self.print( 788 782 "NAMES {s}\r\n", 789 783 .{channel.name}, 790 784 ); 791 - try self.queueWrite(names); 792 785 } 793 786 } 794 787 ··· 799 792 800 793 channel.history_requested = true; 801 794 802 - var buf: [128]u8 = undefined; 803 795 if (channel.messages.items.len == 0) { 804 - const hist = try std.fmt.bufPrint( 805 - &buf, 796 + try self.print( 806 797 "CHATHISTORY LATEST {s} * 50\r\n", 807 798 .{channel.name}, 808 799 ); 809 800 channel.history_requested = true; 810 - try self.queueWrite(hist); 811 801 return; 812 802 } 813 803 ··· 817 807 const first = channel.messages.items[0]; 818 808 const time = first.getTag("time") orelse 819 809 return error.NoTimeTag; 820 - const hist = try std.fmt.bufPrint( 821 - &buf, 810 + try self.print( 822 811 "CHATHISTORY BEFORE {s} timestamp={s} 50\r\n", 823 812 .{ channel.name, time }, 824 813 ); 825 814 channel.history_requested = true; 826 - try self.queueWrite(hist); 827 815 }, 828 816 .after => { 829 817 assert(channel.messages.items.len > 0); 830 818 const last = channel.messages.getLast(); 831 819 const time = last.getTag("time") orelse 832 820 return error.NoTimeTag; 833 - const hist = try std.fmt.bufPrint( 834 - &buf, 821 + try self.print( 835 822 // we request 500 because we have no 836 823 // idea how long we've been offline 837 824 "CHATHISTORY AFTER {s} timestamp={s} 500\r\n", 838 825 .{ channel.name, time }, 839 826 ); 840 827 channel.history_requested = true; 841 - try self.queueWrite(hist); 842 828 }, 843 829 } 844 830 }