tiny.smg.cli.output
Defined in cli.
API (31)
Actions
Public operations.
clickUsageLineWidthdisplayWidthhelpColorModewriteBoxBottomwriteBoxLinewriteBoxLineDisplaywriteBoxTopwriteByteLimitExceededwriteClickBoxErrorwriteClickUsageErrorwriteDocHardlinewriteDocTextwriteErrwriteErrFmtwriteNoSuchOptionwriteOutwriteOutFmtwritePaddedAsciiLinewritePaddedDisplayLinewriteProcessErrorwriteRepeatedDocwriteRootUsageErrorwriteSpacesDocwriteStaleSourceHeadwriteStyledBoxBottomwriteStyledBoxLineDisplaywriteStyledBoxTopwriteStyledTextwriteStyledUsageLinewriteWrappedError
Values and defaults
Public values and defaults.
Source
Source: tools/smg/src/cli/output.zig
zig
const std = @import("std");const alloc = @import("alloc");const pretty = @import("pretty");const pretty_usage = @import("pretty_usage");pub const stale_source_head_message = "Error: .smg/ changed while this command was running. No update was applied; retry the command.\n";pub fn writeOut(data: []const u8) !u8 { var stdout_buffer: [4096]u8 = undefined; var stdout = std.Io.File.stdout().writer(std.Options.debug_io, &stdout_buffer); try pretty.write(&stdout.interface, .{ .text = data }, .{ .width = 88 }); try stdout.interface.flush(); return 0;}pub fn writeOutFmt(allocator: std.mem.Allocator, comptime fmt: []const u8, args: anytype) !u8 { return try writeOut(try std.fmt.allocPrint(allocator, fmt, args));}pub fn writeErr(data: []const u8) !void { var stderr_buffer: [4096]u8 = undefined; var stderr = std.Io.File.stderr().writer(std.Options.debug_io, &stderr_buffer); try pretty.write(&stderr.interface, .{ .text = data }, .{ .width = 88 }); try stderr.interface.flush();}pub fn writeErrFmt(allocator: std.mem.Allocator, comptime fmt: []const u8, args: anytype) !void { const text_value = try std.fmt.allocPrint(allocator, fmt, args); defer allocator.free(text_value); try writeErr(text_value);}pub fn writeStaleSourceHead() !void { try writeErr(stale_source_head_message);}pub fn writeByteLimitExceeded( allocator: std.mem.Allocator, limit_name: []const u8, value: usize, option: []const u8,) !u8 { try writeErrFmt( allocator, "Error: {s} is {d} bytes. Raise it with {s} SIZE.\n", .{ limit_name, value, option }, ); return 2;}pub fn writeWrappedError(allocator: std.mem.Allocator, message: []const u8) !void { const text_value = try std.fmt.allocPrint(allocator, "Error: {s}", .{message}); defer allocator.free(text_value); var out: std.Io.Writer.Allocating = .init(allocator); defer out.deinit(); var words = std.mem.tokenizeScalar(u8, text_value, ' '); var width: usize = 0; while (words.next()) |word| { if (width == 0) { try out.writer.writeAll(word); width = word.len; } else if (width + 1 + word.len > 80) { if (width < 80) try out.writer.writeByte(' '); try out.writer.writeByte('\n'); try out.writer.writeAll(word); width = word.len; } else { try out.writer.writeByte(' '); try out.writer.writeAll(word); width += 1 + word.len; } } try out.writer.writeByte('\n'); try writeErr(out.written());}pub fn writeProcessError(err: anyerror) void { pretty_usage.Terminal.stderr(alloc.standaloneProcessAllocator(), .{}).writeErrorTextFmt("smg", "{s}", .{@errorName(err)}) catch {};}pub fn writeNoSuchOption(allocator: std.mem.Allocator, command: []const u8, signature: []const u8, option: []const u8) !void { try writeClickUsageError(allocator, command, signature, try std.fmt.allocPrint(allocator, "No such option: {s}", .{option}));}pub fn writeClickUsageError(allocator: std.mem.Allocator, command: []const u8, signature: []const u8, message: []const u8) !void { var out: std.Io.Writer.Allocating = .init(allocator); const usage_width = clickUsageLineWidth(command); try writePaddedAsciiLine(&out.writer, "", 80); try writePaddedAsciiLine(&out.writer, try std.fmt.allocPrint(allocator, " Usage: smg {s} {s}", .{ command, signature }), usage_width); try writePaddedAsciiLine(&out.writer, "", 80); try writePaddedAsciiLine(&out.writer, try std.fmt.allocPrint(allocator, " Try 'smg {s} --help' for help", .{command}), usage_width); try writeBoxTop(&out.writer, "Error", 80); try writeBoxLine(&out.writer, message, 80); try writeBoxBottom(&out.writer, 80); try writePaddedAsciiLine(&out.writer, "", 80); try writeErr(out.written());}pub fn clickUsageLineWidth(command: []const u8) usize { return if (std.mem.eql(u8, command, "blame")) 70 else 80;}pub fn writeRootUsageError(allocator: std.mem.Allocator, command_name: []const u8, message: []const u8) !void { var out: std.Io.Writer.Allocating = .init(allocator); try writePaddedAsciiLine(&out.writer, "", 80); try writePaddedAsciiLine(&out.writer, try std.fmt.allocPrint(allocator, " Usage: {s} [OPTIONS] COMMAND [ARGS]...", .{command_name}), 80); try writePaddedAsciiLine(&out.writer, "", 80); try writePaddedAsciiLine(&out.writer, try std.fmt.allocPrint(allocator, " Try '{s} --help' for help", .{command_name}), 80); try writeBoxTop(&out.writer, "Error", 80); try writeBoxLine(&out.writer, message, 80); try writeBoxBottom(&out.writer, 80); try writePaddedAsciiLine(&out.writer, "", 80); try writeErr(out.written());}pub fn writeClickBoxError(allocator: std.mem.Allocator, message: []const u8) !void { var out: std.Io.Writer.Allocating = .init(allocator); try writeBoxTop(&out.writer, "Error", 80); try writeBoxLine(&out.writer, message, 80); try writeBoxBottom(&out.writer, 80); try writePaddedAsciiLine(&out.writer, "", 80); try writeErr(out.written());}pub fn writeDocText(writer: *std.Io.Writer, value: []const u8) !void { try pretty.write(writer, .{ .text = value }, .{ .width = 88 });}pub fn helpColorMode() pretty.ColorMode { return pretty_usage.terminalColorMode(std.Io.File.stdout());}pub fn writeStyledText(writer: *std.Io.Writer, style: pretty.Style, value: []const u8, mode: pretty.ColorMode) !void { const text_doc = pretty.Doc{ .text = value }; const styled_doc = pretty.Doc{ .styled = .{ .style = style, .doc = &text_doc } }; try pretty.write(writer, styled_doc, .{ .width = 88, .color = mode });}const box_rule_glyph = "\xE2\x94\x80";const box_rule_capacity = 96;fn boxRule(buffer: *[box_rule_capacity * box_rule_glyph.len]u8, count: usize) []const u8 { std.debug.assert(count <= box_rule_capacity); for (0..count) |index| { @memcpy(buffer[index * box_rule_glyph.len ..][0..box_rule_glyph.len], box_rule_glyph); } return buffer[0 .. count * box_rule_glyph.len];}pub fn writeStyledBoxTop(writer: *std.Io.Writer, title: []const u8, width: usize, mode: pretty.ColorMode) !void { var rule_buffer: [box_rule_capacity * box_rule_glyph.len]u8 = undefined; try writeStyledText(writer, .muted, "\xE2\x95\xAD\xE2\x94\x80 ", mode); try writeStyledText(writer, .title, title, mode); try writeStyledText(writer, .muted, " ", mode); try writeStyledText(writer, .muted, boxRule(&rule_buffer, width - title.len - 5), mode); try writeStyledText(writer, .muted, "\xE2\x95\xAE", mode); try writeDocHardline(writer);}pub fn writeStyledBoxLineDisplay(writer: *std.Io.Writer, message: []const u8, width: usize, mode: pretty.ColorMode) !void { try writeStyledText(writer, .muted, "\xE2\x94\x82 ", mode); try writeBoxRowContent(writer, message, mode); try writeSpacesDoc(writer, width -| displayWidth(message) -| 3); try writeStyledText(writer, .muted, "\xE2\x94\x82", mode); try writeDocHardline(writer);}fn writeBoxRowContent(writer: *std.Io.Writer, message: []const u8, mode: pretty.ColorMode) !void { const name_len = boxRowNameLength(message); if (name_len == 0) { try writeDocText(writer, message); return; } const style: pretty.Style = if (message[0] == '-') .type_name else .name; try writeStyledText(writer, style, message[0..name_len], mode); try writeDocText(writer, message[name_len..]);}fn boxRowNameLength(message: []const u8) usize { if (message.len == 0 or message[0] == ' ') return 0; var index: usize = 0; while (index + 1 < message.len) : (index += 1) { if (message[index] == ' ' and message[index + 1] == ' ') return index; } return 0;}pub fn writeStyledBoxBottom(writer: *std.Io.Writer, width: usize, mode: pretty.ColorMode) !void { var rule_buffer: [box_rule_capacity * box_rule_glyph.len]u8 = undefined; try writeStyledText(writer, .muted, "\xE2\x95\xB0", mode); try writeStyledText(writer, .muted, boxRule(&rule_buffer, width - 2), mode); try writeStyledText(writer, .muted, "\xE2\x95\xAF", mode); try writeDocHardline(writer);}pub fn writeStyledUsageLine(writer: *std.Io.Writer, label: []const u8, rest: []const u8, width: usize, mode: pretty.ColorMode) !void { try writeDocText(writer, " "); try writeStyledText(writer, .keyword, label, mode); try writeStyledText(writer, .source, rest, mode); const used = 1 + displayWidth(label) + displayWidth(rest); try writeSpacesDoc(writer, width -| used); try writeDocHardline(writer);}pub fn writeDocHardline(writer: *std.Io.Writer) !void { try pretty.write(writer, pretty.hardline, .{ .width = 88 });}pub fn writeSpacesDoc(writer: *std.Io.Writer, count: usize) !void { var rest = count; var spaces: [128]u8 = undefined; @memset(&spaces, ' '); while (rest != 0) { const chunk = @min(rest, spaces.len); try writeDocText(writer, spaces[0..chunk]); rest -= chunk; }}pub fn writeRepeatedDoc(writer: *std.Io.Writer, value: []const u8, count: usize) !void { for (0..count) |_| try writeDocText(writer, value);}pub fn writePaddedAsciiLine(writer: *std.Io.Writer, value: []const u8, width: usize) !void { try writeDocText(writer, value); try writeSpacesDoc(writer, width -| value.len); try writeDocHardline(writer);}pub fn writePaddedDisplayLine(writer: *std.Io.Writer, value: []const u8, width: usize) !void { try writeDocText(writer, value); try writeSpacesDoc(writer, width -| displayWidth(value)); try writeDocHardline(writer);}pub fn writeBoxTop(writer: *std.Io.Writer, title: []const u8, width: usize) !void { try writeDocText(writer, "\xE2\x95\xAD\xE2\x94\x80 "); try writeDocText(writer, title); try writeDocText(writer, " "); try writeRepeatedDoc(writer, "\xE2\x94\x80", width - title.len - 5); try writeDocText(writer, "\xE2\x95\xAE"); try writeDocHardline(writer);}pub fn writeBoxLine(writer: *std.Io.Writer, message: []const u8, width: usize) !void { try writeDocText(writer, "\xE2\x94\x82 "); try writeDocText(writer, message); try writeSpacesDoc(writer, width -| message.len -| 3); try writeDocText(writer, "\xE2\x94\x82"); try writeDocHardline(writer);}pub fn writeBoxLineDisplay(writer: *std.Io.Writer, message: []const u8, width: usize) !void { try writeDocText(writer, "\xE2\x94\x82 "); try writeDocText(writer, message); try writeSpacesDoc(writer, width -| displayWidth(message) -| 3); try writeDocText(writer, "\xE2\x94\x82"); try writeDocHardline(writer);}pub fn writeBoxBottom(writer: *std.Io.Writer, width: usize) !void { try writeDocText(writer, "\xE2\x95\xB0"); try writeRepeatedDoc(writer, "\xE2\x94\x80", width - 2); try writeDocText(writer, "\xE2\x95\xAF"); try writeDocHardline(writer);}pub fn displayWidth(value: []const u8) usize { var width: usize = 0; var index: usize = 0; while (index < value.len) { const byte = value[index]; if (byte < 0x80) { width += 1; index += 1; continue; } const sequence_len = std.unicode.utf8ByteSequenceLength(byte) catch 1; width += 1; index += @min(sequence_len, value.len - index); } return width;}test "display width counts utf8 codepoints" { try std.testing.expectEqual(@as(usize, 5), displayWidth("abcde")); try std.testing.expectEqual(@as(usize, 5), displayWidth("a\xE2\x80\x94bcd"));}test "usage error width preserves blame compatibility" { try std.testing.expectEqual(@as(usize, 70), clickUsageLineWidth("blame")); try std.testing.expectEqual(@as(usize, 80), clickUsageLineWidth("context"));}Source: tools/smg/src/cli/root.zig:6
zig
pub const output = @import("output.zig");Complete caller list for cli.output.writeClickUsageError
18 direct callers.
tiny.smg.cli.output.writeNoSuchOption[function] attools/smg/src/cli/output.zig:81tiny.smg.cli.validation.validateQueryNameCommand[function] attools/smg/src/cli/validation/query.zig:11tiny.smg.cli.validation.rejectUnexpectedOptions[function] attools/smg/src/cli/validation/unexpected.zig:9tiny.smg.cli.validation.rejectUnexpectedOptionsPlain[function] attools/smg/src/cli/validation/unexpected.zig:20tiny.smg.cli.validation.rejectUnexpectedScanOptions[function] attools/smg/src/cli/validation/unexpected.zig:31tiny.smg.cli.validation.configuredLimitsOrReject[function] attools/smg/src/cli/validation/value.zig:77tiny.smg.cli.validation.rejectInvalidChoiceOption[function] attools/smg/src/cli/validation/value.zig:43tiny.smg.cli.validation.rejectInvalidFloatOption[function] attools/smg/src/cli/validation/value.zig:68tiny.smg.cli.validation.rejectInvalidIntegerOption[function] attools/smg/src/cli/validation/value.zig:59tiny.smg.cli.validation.rejectInvalidIntegerRangeOption[function] attools/smg/src/cli/validation/value.zig:119tiny.smg.cli.validation.rejectInvalidLine[function] attools/smg/src/cli/validation/value.zig:130tiny.smg.cli.validation.rejectMissingPaths[function] attools/smg/src/cli/validation/value.zig:110tiny.smg.command.inspection.run[function] attools/smg/src/command/inspection.zig:5tiny.smg.command.node.list[function] attools/smg/src/command/node.zig:17tiny.smg.command.node.show[function] attools/smg/src/command/node.zig:49tiny.smg.command.search.index[function] attools/smg/src/command/search.zig:47tiny.smg.command.status.run[function] attools/smg/src/command/status.zig:12tiny.smg.command.usages.run[function] attools/smg/src/command/usages.zig:24
Complete caller list for cli.output.writeDocHardline
13 direct callers.
tiny.smg.cli.output.writeBoxBottom[function] attools/smg/src/cli/output.zig:261tiny.smg.cli.output.writeBoxLine[function] attools/smg/src/cli/output.zig:245tiny.smg.cli.output.writeBoxLineDisplay[function] attools/smg/src/cli/output.zig:253tiny.smg.cli.output.writeBoxTop[function] attools/smg/src/cli/output.zig:236tiny.smg.cli.output.writePaddedAsciiLine[function] attools/smg/src/cli/output.zig:224tiny.smg.cli.output.writePaddedDisplayLine[function] attools/smg/src/cli/output.zig:230tiny.smg.cli.output.writeStyledBoxBottom[function] attools/smg/src/cli/output.zig:188tiny.smg.cli.output.writeStyledBoxLineDisplay[function] attools/smg/src/cli/output.zig:160tiny.smg.cli.output.writeStyledBoxTop[function] attools/smg/src/cli/output.zig:150tiny.smg.cli.output.writeStyledUsageLine[function] attools/smg/src/cli/output.zig:196tools.smg.src.cli.table.rich.writeBorder[function] — private; no exact target attools/smg/src/cli/table/rich.zig:65in nearest public ownertiny.smg.cli.table.richtools.smg.src.cli.table.rich.writeRow[function] — private; no exact target attools/smg/src/cli/table/rich.zig:85in nearest public ownertiny.smg.cli.table.richtools.smg.src.command.scan.text.writeTrimmedLine[function] — private; no exact target attools/smg/src/command/scan/text.zig:104in nearest public ownertiny.smg.command.scan.text
Complete caller list for cli.output.writeDocText
15 direct callers.
tiny.smg.cli.output.writeBoxBottom[function] attools/smg/src/cli/output.zig:261tiny.smg.cli.output.writeBoxLine[function] attools/smg/src/cli/output.zig:245tiny.smg.cli.output.writeBoxLineDisplay[function] attools/smg/src/cli/output.zig:253tools.smg.src.cli.output.writeBoxRowContent[function] — private; no exact target attools/smg/src/cli/output.zig:168in nearest public ownertiny.smg.cli.outputtiny.smg.cli.output.writeBoxTop[function] attools/smg/src/cli/output.zig:236tiny.smg.cli.output.writePaddedAsciiLine[function] attools/smg/src/cli/output.zig:224tiny.smg.cli.output.writePaddedDisplayLine[function] attools/smg/src/cli/output.zig:230tiny.smg.cli.output.writeRepeatedDoc[function] attools/smg/src/cli/output.zig:220tiny.smg.cli.output.writeSpacesDoc[function] attools/smg/src/cli/output.zig:209tiny.smg.cli.output.writeStyledUsageLine[function] attools/smg/src/cli/output.zig:196tools.smg.src.cli.table.rich.writeBorder[function] — private; no exact target attools/smg/src/cli/table/rich.zig:65in nearest public ownertiny.smg.cli.table.richtools.smg.src.cli.table.rich.writeCell[function] — private; no exact target attools/smg/src/cli/table/rich.zig:102in nearest public ownertiny.smg.cli.table.richtools.smg.src.cli.table.rich.writeRow[function] — private; no exact target attools/smg/src/cli/table/rich.zig:85in nearest public ownertiny.smg.cli.table.richtools.smg.src.command.scan.text.writeRow[function] — private; no exact target attools/smg/src/command/scan/text.zig:73in nearest public ownertiny.smg.command.scan.texttools.smg.src.command.scan.text.writeTrimmedLine[function] — private; no exact target attools/smg/src/command/scan/text.zig:104in nearest public ownertiny.smg.command.scan.text
Complete caller list for cli.output.writeErr
9 direct callers.
tiny.smg.cli.output.writeClickBoxError[function] attools/smg/src/cli/output.zig:116tiny.smg.cli.output.writeClickUsageError[function] attools/smg/src/cli/output.zig:85tiny.smg.cli.output.writeErrFmt[function] attools/smg/src/cli/output.zig:27tiny.smg.cli.output.writeRootUsageError[function] attools/smg/src/cli/output.zig:103tiny.smg.cli.output.writeStaleSourceHead[function] attools/smg/src/cli/output.zig:33tiny.smg.cli.output.writeWrappedError[function] attools/smg/src/cli/output.zig:51tools.smg.src.cli.resolve.writeAmbiguous[function] — private; no exact target attools/smg/src/cli/resolve.zig:57in nearest public ownertiny.smg.cli.resolvetools.smg.src.cli.resolve.writeNotFound[function] — private; no exact target attools/smg/src/cli/resolve.zig:46in nearest public ownertiny.smg.cli.resolvetiny.smg.command.analyze.support.writeScopeWarning[function] attools/smg/src/command/analyze/support.zig:21
Complete caller list for cli.output.writeErrFmt
9 direct callers.
tiny.smg.cli.output.writeByteLimitExceeded[function] attools/smg/src/cli/output.zig:37tiny.smg.cli.validation.rejectInvalidMeta[function] attools/smg/src/cli/validation/value.zig:140tiny.smg.command.check.run[function] attools/smg/src/command/check/run.zig:13tools.smg.src.command.rules.add.addKind[function] — private; no exact target attools/smg/src/command/rules/add.zig:29in nearest public ownertiny.smg.command.rules.addtools.smg.src.command.rules.add.denyRule[function] — private; no exact target attools/smg/src/command/rules/add.zig:77in nearest public ownertiny.smg.command.rules.addtools.smg.src.command.rules.add.invariantRule[function] — private; no exact target attools/smg/src/command/rules/add.zig:98in nearest public ownertiny.smg.command.rules.addtools.smg.src.command.rules.add.quantifiedRule[function] — private; no exact target attools/smg/src/command/rules/add.zig:122in nearest public ownertiny.smg.command.rules.addtiny.smg.command.search.run[function] attools/smg/src/command/search.zig:13tiny.smg.command.session.noteGraphAge[function] attools/smg/src/command/session.zig:104
Complete caller list for cli.output.writeOut
15 direct callers.
tiny.smg.cli.json.edgeArray[function] attools/smg/src/cli/json.zig:165tiny.smg.cli.json.stringArray[function] attools/smg/src/cli/json.zig:152tiny.smg.cli.output.writeOutFmt[function] attools/smg/src/cli/output.zig:16tiny.smg.command.check.run[function] attools/smg/src/command/check/run.zig:13tiny.smg.command.concepts.render.writeList[function] attools/smg/src/command/concepts/render.zig:9tiny.smg.command.inspection.run[function] attools/smg/src/command/inspection.zig:5tiny.smg.command.node.list[function] attools/smg/src/command/node.zig:17tiny.smg.command.node.listJson[function] attools/smg/src/command/node.zig:127tiny.smg.command.node.show[function] attools/smg/src/command/node.zig:49tiny.smg.command.rules.render.writeList[function] attools/smg/src/command/rules/render.zig:9tiny.smg.command.search.run[function] attools/smg/src/command/search.zig:13tiny.smg.command.search.writeJson[function] attools/smg/src/command/search.zig:59tiny.smg.command.status.run[function] attools/smg/src/command/status.zig:12tiny.smg.command.usages.run[function] attools/smg/src/command/usages.zig:24tiny.smg.command.usages.writeJson[function] attools/smg/src/command/usages.zig:105
Complete caller list for cli.output.writeSpacesDoc
8 direct callers.
tiny.smg.cli.output.writeBoxLine[function] attools/smg/src/cli/output.zig:245tiny.smg.cli.output.writeBoxLineDisplay[function] attools/smg/src/cli/output.zig:253tiny.smg.cli.output.writePaddedAsciiLine[function] attools/smg/src/cli/output.zig:224tiny.smg.cli.output.writePaddedDisplayLine[function] attools/smg/src/cli/output.zig:230tiny.smg.cli.output.writeStyledBoxLineDisplay[function] attools/smg/src/cli/output.zig:160tiny.smg.cli.output.writeStyledUsageLine[function] attools/smg/src/cli/output.zig:196tools.smg.src.cli.table.rich.writeCell[function] — private; no exact target attools/smg/src/cli/table/rich.zig:102in nearest public ownertiny.smg.cli.table.richtools.smg.src.command.scan.text.writeRow[function] — private; no exact target attools/smg/src/command/scan/text.zig:73in nearest public ownertiny.smg.command.scan.text
Audit
| Definitions | 32 |
|---|---|
| Public names | 32 |
| Members | 0 |
| Version | 26.7.0 |
| Revision | daab053ee433 |