diff options
Diffstat (limited to 'src/Messages.zig')
| -rw-r--r-- | src/Messages.zig | 69 |
1 files changed, 45 insertions, 24 deletions
diff --git a/src/Messages.zig b/src/Messages.zig index cb52907a..4cf81ac5 100644 --- a/src/Messages.zig +++ b/src/Messages.zig @@ -31,6 +31,7 @@ len: usize = 0, pub const LoggedMessage = struct { pub const cap = 256; + pub const replica = .{ .len = .{.{ "text", "len" }} }; text: [cap]u8 = undefined, len: u16 = 0, /// The serial of the pane that said it, or 0 for none in particular. A @@ -117,6 +118,24 @@ pub const Message = struct { }; extern "c" fn localtime_r(timep: *const libc.time_t, result: *Tm) ?*Tm; + fn timeOfDay() [8]u8 { + var clock: [8]u8 = "--:--:--".*; + const notime = if (libc.getenv("PARDES_NOTIME")) |v| std.mem.span(v).len != 0 else false; + if (notime) return clock; + var ts: libc.timespec = undefined; + _ = libc.clock_gettime(.REALTIME, &ts); + const secs: libc.time_t = ts.sec; + var tm: Tm = undefined; + if (localtime_r(&secs, &tm) != null) { + _ = std.fmt.bufPrint(&clock, "{d:0>2}:{d:0>2}:{d:0>2}", .{ + @as(u32, @intCast(tm.hour)), + @as(u32, @intCast(tm.min)), + @as(u32, @intCast(tm.sec)), + }) catch {}; + } + return clock; + } + pub fn body(text: []const u8) []const u8 { if (text.len < 10) return text; if (text[2] != ':' or text[5] != ':' or text[8] != ' ' or text[9] != ' ') return text; @@ -127,21 +146,9 @@ pub const Message = struct { } pub fn stamp(buf: []u8, verb: []const u8, subject: []const u8) []const u8 { - var clock: [8]u8 = "--:--:--".*; - const notime = if (libc.getenv("PARDES_NOTIME")) |v| std.mem.span(v).len != 0 else false; - if (!notime) { - var ts: libc.timespec = undefined; - _ = libc.clock_gettime(.REALTIME, &ts); - const secs: libc.time_t = ts.sec; - var tm: Tm = undefined; - if (localtime_r(&secs, &tm) != null) { - _ = std.fmt.bufPrint(&clock, "{d:0>2}:{d:0>2}:{d:0>2}", .{ - @as(u32, @intCast(tm.hour)), - @as(u32, @intCast(tm.min)), - @as(u32, @intCast(tm.sec)), - }) catch {}; - } - } + // The time of day as the session that said it read it, in its + // zone: a replica's clock and TZ are its own (src/tape.zig). + const clock = pardes.tape.value(.clock, timeOfDay, .{}); const head = std.fmt.bufPrint(buf, "{s} {s} ", .{ &clock, verb }) catch return buf[0..0]; const room = buf.len - head.len; const tail = if (subject.len <= room) subject else subject[subject.len - room ..]; @@ -153,10 +160,14 @@ pub const Message = struct { /// A status updates the message line in place (a progress count does not /// pile up); a message is an event, and stacks. pub fn setStatus(p: *Pardes, id: usize, text: []const u8) void { + const taped = pardes.tape.enter(p, .{ .set_status = .{ .id = id, .text = text } }); + defer pardes.tape.leave(p, taped); showMessage(p, id, text, false); } pub fn setMessage(p: *Pardes, id: usize, text: []const u8) void { + const taped = pardes.tape.enter(p, .{ .set_message = .{ .id = id, .text = text } }); + defer pardes.tape.leave(p, taped); logMessage(p, id, text); showMessage(p, id, text, true); } @@ -186,7 +197,7 @@ fn showMessage(p: *Pardes, id: usize, text: []const u8, stack: bool) void { const arrives = pane.msg_len == 0 or pane.msg_life.phase == .leaving; for (text[0..n], pane.msg[0..n]) |c, *cell| cell.* = if (c < 0x20 or c == 0x7f) ' ' else c; pane.msg_len = @intCast(n); - pane.msg_said_ms = nowMs(); + pane.msg_said_ms = @intCast(p.now_ns / std.time.ns_per_ms); p.needs_frame = true; if (arrives) { pane.msg_life = .{ .phase = if (p.settings.message_animation and n > 0) .entering else .shown }; @@ -200,13 +211,6 @@ fn showMessage(p: *Pardes, id: usize, text: []const u8, stack: bool) void { } } -fn nowMs() i64 { - if (comptime !pardes.hosted) return 0; - var ts: std.c.timespec = undefined; - if (std.c.clock_gettime(.MONOTONIC, &ts) != 0) return 0; - return @as(i64, ts.sec) * 1000 + @divTrunc(@as(i64, ts.nsec), 1_000_000); -} - /// With no frontend attached no input dismisses a message, and a detached /// session's /screen and messages kept what was said an hour ago: each /// pane's lines go once its newest has been up `message_linger_ms`, on the @@ -217,7 +221,9 @@ fn nowMs() i64 { /// clock; the render pipeline's clock will give every line its own. pub fn expireUnattended(p: *Pardes) void { if (!p.unattended) return; - const now = nowMs(); + // The session's clock (`advance`), as every tick's: no replica reads + // its own. + const now: i64 = @intCast(p.now_ns / std.time.ns_per_ms); for (p.panes) |slot| { const pane = slot orelse continue; if (pane.msg_len == 0 and pane.msg_older_len == 0) continue; @@ -453,6 +459,8 @@ pub fn dialReason(err: anyerror) ?[]const u8 { } pub fn reportError(p: *Pardes, id: usize, operation: []const u8, err: anyerror) void { + const taped = pardes.tape.enter(p, .{ .report_error = .{ .id = id, .operation = operation, .err = err } }); + defer pardes.tape.leave(p, taped); // Room for an operation naming the longest path and the reason after // it: a shorter one cut the reason off (`Save /long…`), and the reason // is what a failure is for. Where it must be shorter, the path gives. @@ -575,6 +583,19 @@ test "an error reads as words and a failure is cut between them" { /// so it does not paint under them, and the paint pass so it can stack /// them instead of letting the last one win the row. pub fn collectNotices(p: *Pardes, pane: *Pane, r: Rect, id: usize) void { + collectBands(p, pane, r, id); + // Each band's chip at the right edge, as wide as its own text: a click + // finds it whether or not a frame has drawn it. + const tx = r.x + config.GUTTER; + const tw = r.w -| config.GUTTER; + var leader_buf: [16]u8 = undefined; + for (0..pane.notices.len) |i| { + const chip = noticeCols(p, noticeText(p, pane, i, &leader_buf), tw); + pane.notices.left[i] = if (chip == 0) std.math.maxInt(u16) else tx + tw - chip; + } +} + +fn collectBands(p: *Pardes, pane: *Pane, r: Rect, id: usize) void { pane.notices.len = 0; if (r.w <= config.GUTTER or r.h <= pane.tag_rows) return; // Oldest message line on top, so lines already up do not move when a |
