tiny.smg.command.concepts.render
Defined in command.concepts.
API (1)
Actions
Public operations.
Source
Source: tools/smg/src/command/concepts/render.zig
zig
const std = @import("std");const pretty_json = @import("pretty").json;const smg = @import("../../root.zig");const concepts_mod = smg.concepts;const output = smg.cli.output;const rich_table = smg.cli.table.rich;pub fn writeList(allocator: std.mem.Allocator, concept_list: []const concepts_mod.Concept, fmt: []const u8) !u8 { if (std.mem.eql(u8, fmt, "json")) { var out: std.Io.Writer.Allocating = .init(allocator); var writer = pretty_json.Writer.init(&out.writer, .minified); try writer.beginArray(); for (concept_list) |concept| try writer.raw(try concepts_mod.render(allocator, concept)); try writer.endArray(); try out.writer.writeByte('\n'); return try output.writeOut(out.written()); } if (concept_list.len == 0) return try output.writeOut("No concepts defined. Add one with smg concept add.\n"); var out: std.Io.Writer.Allocating = .init(allocator); try writeTable(allocator, &out.writer, concept_list); return try output.writeOut(out.written());}fn writeTable(allocator: std.mem.Allocator, writer: *std.Io.Writer, concept_list: []const concepts_mod.Concept) !void { var rows: std.ArrayList(rich_table.Row3) = .empty; for (concept_list) |concept| { try rows.append(allocator, .{ .a = concept.name, .b = try joinComma(allocator, concept.prefixes), .c = if (concept.sync_points.len == 0) "-" else try joinComma(allocator, concept.sync_points), }); } try rich_table.write3(writer, .{ .a = "Name", .b = "Prefixes", .c = "Sync Points" }, rows.items);}fn joinComma(allocator: std.mem.Allocator, values: []const []const u8) ![]const u8 { var out: std.Io.Writer.Allocating = .init(allocator); for (values, 0..) |value, index| { if (index != 0) try out.writer.writeAll(", "); try out.writer.writeAll(value); } return try out.toOwnedSlice();}test "concept table renders prefixes and sync points" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); const concept_list = [_]concepts_mod.Concept{.{ .name = "core", .prefixes = &.{"app"}, .sync_points = &.{"app.main"}, }}; var out: std.Io.Writer.Allocating = .init(allocator); try writeTable(allocator, &out.writer, &concept_list); try std.testing.expect(std.mem.indexOf(u8, out.written(), "Sync Points") != null); try std.testing.expect(std.mem.indexOf(u8, out.written(), "app.main") != null);}Source: tools/smg/src/command/concepts/root.zig:2
zig
pub const render = @import("render.zig");Audit
| Definitions | 2 |
|---|---|
| Public names | 3 |
| Members | 0 |
| Version | 26.7.0 |
| Revision | daab053ee433 |