Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131//! Content a tool renderer produces, and the owned form the core sends to a UI.
const std = @import("std");
/// Styled spans. A newline inside a span starts a new line.pub const Content = []const Span;
pub const Span = struct { text: []const u8, style: Style = .{},};
/// Styling a tool may set on its own words. The UI owns the background, the/// width, the wrapping and the placement.pub const Style = struct { bold: bool = false, italic: bool = false, /// Palette index for the text, or null for the terminal default. color: ?u8 = null,};
/// One plain span.pub fn text(arena: std.mem.Allocator, value: []const u8) std.mem.Allocator.Error!Content { return spans(arena, &.{.{ .text = value }});}
/// A call line: the tool name in bold, then its parameters.pub fn callLine(arena: std.mem.Allocator, name: []const u8, params: []const u8) std.mem.Allocator.Error!Content { return spans(arena, &.{ .{ .text = name, .style = .{ .bold = true } }, .{ .text = " " }, .{ .text = params }, });}
pub fn spans(arena: std.mem.Allocator, items: []const Span) std.mem.Allocator.Error!Content { return arena.dupe(Span, items);}
/// The text of every span, joined.pub fn plain(arena: std.mem.Allocator, content: Content) std.mem.Allocator.Error![]const u8 { var out: std.ArrayList(u8) = .empty; for (content) |span| try out.appendSlice(arena, span.text); return out.items;}
/// Content in one owned text buffer, so it can cross threads and free in one/// step. The text is valid UTF-8 and breaks lines with `\n` only. An empty/// block draws nothing.pub const Block = struct { text: []const u8 = "", runs: []const Run = &.{},
pub const Run = struct { start: u32, len: u32, style: Style, };
/// Copies `content`. Invalid UTF-8 becomes U+FFFD, and a CR or a CRLF /// becomes a line break. pub fn init(gpa: std.mem.Allocator, content: Content) std.mem.Allocator.Error!Block { var out: std.ArrayList(u8) = .empty; defer out.deinit(gpa); var runs: std.ArrayList(Run) = try .initCapacity(gpa, content.len); defer runs.deinit(gpa); for (content) |span| { const start = out.items.len; try out.print(gpa, "{f}", .{std.unicode.fmtUtf8(span.text)}); var write = start; var read = start; while (read < out.items.len) : (read += 1) { var byte = out.items[read]; if (byte == '\r') { if (read + 1 < out.items.len and out.items[read + 1] == '\n') { read += 1; } byte = '\n'; } out.items[write] = byte; write += 1; } out.shrinkRetainingCapacity(write); if (write > start) { runs.appendAssumeCapacity(.{ .start = @intCast(start), .len = @intCast(write - start), .style = span.style }); } } const owned = try out.toOwnedSlice(gpa); errdefer gpa.free(owned); return .{ .text = owned, .runs = try runs.toOwnedSlice(gpa) }; }
pub fn deinit(self: Block, gpa: std.mem.Allocator) void { gpa.free(self.text); gpa.free(self.runs); }
pub fn slice(self: Block, run: Run) []const u8 { return self.text[run.start..][0..run.len]; }};
const testing = std.testing;
test "a block keeps each span's text and style in order and drops empty spans" { const block: Block = try .init(testing.allocator, &.{ .{ .text = "bash", .style = .{ .bold = true } }, .{ .text = "" }, .{ .text = " ls", .style = .{ .color = 3 } }, }); defer block.deinit(testing.allocator);
try testing.expectEqualStrings("bash ls", block.text); try testing.expectEqual(@as(usize, 2), block.runs.len); try testing.expectEqualStrings("bash", block.slice(block.runs[0])); try testing.expect(block.runs[0].style.bold); try testing.expectEqualStrings(" ls", block.slice(block.runs[1])); try testing.expectEqual(@as(?u8, 3), block.runs[1].style.color);}
test "a block breaks lines at a CR or a CRLF and replaces invalid UTF-8" { const block: Block = try .init(testing.allocator, &.{ .{ .text = "one\rtwo\r\nthree" }, .{ .text = "a\xffb" }, }); defer block.deinit(testing.allocator);
try testing.expectEqualStrings("one\ntwo\nthree" ++ "a\u{FFFD}b", block.text); try testing.expectEqualStrings("a\u{FFFD}b", block.slice(block.runs[1]));}