aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--binaries/mi/mi.zig88
-rw-r--r--system/libraries/format/format.zig5
-rw-r--r--system/libraries/format/table.zig101
3 files changed, 136 insertions, 58 deletions
diff --git a/binaries/mi/mi.zig b/binaries/mi/mi.zig
index 837e7c4..6b7c2a8 100644
--- a/binaries/mi/mi.zig
+++ b/binaries/mi/mi.zig
@@ -61,74 +61,53 @@ fn display_stack(path: []const u8) !void {
return;
}
- var max_access_len: usize = 6;
- var max_size_len: usize = 4;
- var max_owner_len: usize = 7;
- const max_date_len: usize = 19;
-
- for (0..count) |i| {
- const entry = &entries[i];
- const perms = get_permissions(entry.permission_type);
- if (perms.len > max_access_len) max_access_len = perms.len;
-
- var size_buf: [32]u8 = undefined;
- const size_str = format.formatSize(entry.size, &size_buf);
- if (size_str.len > max_size_len) max_size_len = size_str.len;
-
- if (entry.owner_name_len > max_owner_len) {
- max_owner_len = entry.owner_name_len;
- }
- }
-
- format.print("Access");
- print_padding(max_access_len - 6 + 2);
- format.print("Size");
- print_padding(max_size_len - 4 + 2);
- format.print("Persona");
- print_padding(max_owner_len - 7 + 3);
- format.print("Modified");
- print_padding(max_date_len - 8 + 1);
- format.println("Name");
+ var table = format.Table.init(&[_]format.Column{
+ .{ .name = "Access", .color = colors.cyan },
+ .{ .name = "Size", .color = colors.green },
+ .{ .name = "Persona", .color = colors.yellow },
+ .{ .name = "Modified", .color = colors.blue },
+ .{ .name = "Name", .color = colors.white },
+ });
var stack_count: usize = 0;
var unit_count: usize = 0;
var total_size: u64 = 0;
+ var size_bufs: [128][32]u8 = undefined;
+ var date_bufs: [128][32]u8 = undefined;
+ var name_bufs: [128][65]u8 = undefined;
+
for (0..count) |i| {
const entry = &entries[i];
- const identity = entry.identity[0..entry.identity_len];
- const owner = entry.owner_name[0..entry.owner_name_len];
const perms = get_permissions(entry.permission_type);
- format.color(perms, colors.cyan);
- print_padding(max_access_len - perms.len + 2);
-
- var size_buf: [32]u8 = undefined;
- const size_str = format.formatSize(entry.size, &size_buf);
- format.color(size_str, colors.green);
- print_padding(max_size_len - size_str.len + 2);
-
- format.color(owner, colors.yellow);
- print_padding(max_owner_len - owner.len + 3);
-
- var date_buf: [32]u8 = undefined;
- const date_str = format.formatDate(entry.modified_time, &date_buf);
- format.color(date_str, colors.blue);
- print_padding(max_date_len - date_str.len + 2);
+ const size_str = format.formatSize(entry.size, &size_bufs[i]);
+ const date_str = format.formatDate(entry.modified_time, &date_bufs[i]);
+ const owner = entry.owner_name[0..entry.owner_name_len];
+ const identity = entry.identity[0..entry.identity_len];
+ // Build name with optional /
+ var name_len: usize = identity.len;
+ @memcpy(name_bufs[i][0..identity.len], identity);
if (entry.is_stack) {
+ name_bufs[i][name_len] = '/';
+ name_len += 1;
stack_count += 1;
- total_size += entry.size;
- format.color(identity, colors.cyan);
- format.color("/", colors.blue);
} else {
unit_count += 1;
- total_size += entry.size;
- format.print(identity);
}
- format.print("\n");
+ total_size += entry.size;
+
+ const name_color: u32 = if (entry.is_stack) colors.cyan else colors.white;
+
+ table.rowColored(
+ &[_][]const u8{ perms, size_str, owner, date_str, name_bufs[i][0..name_len] },
+ &[_]u32{ colors.cyan, colors.green, colors.yellow, colors.blue, name_color },
+ );
}
+ table.print();
+
format.print("\n");
var buf: [16]u8 = undefined;
@@ -148,10 +127,3 @@ fn get_permissions(perm_type: u8) []const u8 {
else => "Owner",
};
}
-
-fn print_padding(count: usize) void {
- var i: usize = 0;
- while (i < count) : (i += 1) {
- format.print(" ");
- }
-}
diff --git a/system/libraries/format/format.zig b/system/libraries/format/format.zig
index f764ebd..e516017 100644
--- a/system/libraries/format/format.zig
+++ b/system/libraries/format/format.zig
@@ -4,6 +4,7 @@ pub const int = @import("int.zig");
pub const size = @import("size.zig");
pub const date = @import("date.zig");
pub const printmod = @import("print.zig");
+pub const tablemod = @import("table.zig");
pub const intToStr = int.toStr;
pub const formatSize = size.format;
@@ -15,3 +16,7 @@ pub const printf = printmod.printf;
pub const color = printmod.color;
pub const colorln = printmod.colorln;
pub const colorf = printmod.colorf;
+
+pub const Table = tablemod.Table;
+pub const Column = tablemod.Column;
+pub const Alignment = tablemod.Alignment;
diff --git a/system/libraries/format/table.zig b/system/libraries/format/table.zig
new file mode 100644
index 0000000..e26be32
--- /dev/null
+++ b/system/libraries/format/table.zig
@@ -0,0 +1,101 @@
+//! Table formatting
+
+const io = @import("io");
+const colors = @import("colors");
+
+pub const Alignment = enum {
+ left,
+ right,
+};
+
+pub const Column = struct {
+ name: []const u8,
+ color: u32 = colors.white,
+ alignment: Alignment = .left,
+};
+
+const MAX_COLUMNS = 8;
+const MAX_ROWS = 256;
+
+pub const Table = struct {
+ columns: []const Column,
+ col_widths: [MAX_COLUMNS]usize,
+ rows: [MAX_ROWS][MAX_COLUMNS][]const u8,
+ row_colors: [MAX_ROWS][MAX_COLUMNS]u32,
+ row_count: usize,
+
+ pub fn init(cols: []const Column) Table {
+ var t = Table{
+ .columns = cols,
+ .col_widths = [_]usize{0} ** MAX_COLUMNS,
+ .rows = undefined,
+ .row_colors = undefined,
+ .row_count = 0,
+ };
+
+ for (cols, 0..) |col, i| {
+ t.col_widths[i] = col.name.len;
+ }
+
+ return t;
+ }
+
+ pub fn row(self: *Table, cells: []const []const u8) void {
+ self.rowColored(cells, null);
+ }
+
+ pub fn rowColored(self: *Table, cells: []const []const u8, cell_colors: ?[]const u32) void {
+ if (self.row_count >= MAX_ROWS) return;
+
+ for (cells, 0..) |cell, i| {
+ if (i >= self.columns.len) break;
+ self.rows[self.row_count][i] = cell;
+
+ if (cell_colors) |cc| {
+ self.row_colors[self.row_count][i] = cc[i];
+ } else {
+ self.row_colors[self.row_count][i] = self.columns[i].color;
+ }
+
+ if (cell.len > self.col_widths[i]) {
+ self.col_widths[i] = cell.len;
+ }
+ }
+
+ self.row_count += 1;
+ }
+
+ pub fn print(self: *Table) void {
+ for (self.columns, 0..) |col, i| {
+ _ = io.mark(io.stream, col.name, colors.white) catch {};
+ self.printPadding(self.col_widths[i] - col.name.len + 2);
+ }
+ _ = io.mark(io.stream, "\n", colors.white) catch {};
+
+ for (0..self.row_count) |r| {
+ for (0..self.columns.len) |c| {
+ const cell = self.rows[r][c];
+ const col = self.columns[c];
+ const color = self.row_colors[r][c];
+
+ if (col.alignment == .right) {
+ self.printPadding(self.col_widths[c] - cell.len);
+ _ = io.mark(io.stream, cell, color) catch {};
+ self.printPadding(2);
+ } else {
+ _ = io.mark(io.stream, cell, color) catch {};
+ self.printPadding(self.col_widths[c] - cell.len + 2);
+ }
+ }
+ _ = io.mark(io.stream, "\n", colors.white) catch {};
+ }
+ }
+
+ fn printPadding(self: *Table, count: usize) void {
+ _ = self;
+ var i: usize = 0;
+ while (i < count) : (i += 1) {
+ _ = io.mark(io.stream, " ", colors.white) catch {};
+ }
+ }
+};