Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,15 @@ const style = (zz.Style{})
.alignH(.center);

const output = try style.render(allocator, "Hello, World!");
// render() does not append an implicit trailing '\n'

// Text transforms
const upper_style = (zz.Style{}).transform(zz.transforms.uppercase);
const shouting = try upper_style.render(allocator, "hello"); // "HELLO"

// Inline mode is useful when embedding block-styled output in a single line
const inline = (zz.Style{}).fg(zz.Color.cyan()).inline_style(true);

// Whitespace formatting controls
const ws_style = (zz.Style{})
.underline(true)
Expand Down
4 changes: 4 additions & 0 deletions src/style/style.zig
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,10 @@ pub const Style = struct {
}
}

if (!self.inline_mode and result.items.len > 0 and result.items[result.items.len - 1] == '\n') {
_ = result.pop();
}

return result.toOwnedSlice();
}

Expand Down
14 changes: 14 additions & 0 deletions tests/style_tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ test "Style render" {
// Result should contain ANSI codes and the text
try testing.expect(result.len > 5); // "Hello" + ANSI codes
try testing.expect(std.mem.indexOf(u8, result, "Hello") != null);
try testing.expect(!std.mem.endsWith(u8, result, "\n"));
}

test "Style render keeps internal newlines but no trailing newline" {
const allocator = testing.allocator;

var style = zz.Style{};
style = style.fg(zz.Color.cyan());

const result = try style.render(allocator, "A\nB");
defer allocator.free(result);

try testing.expect(std.mem.indexOfScalar(u8, result, '\n') != null);
try testing.expect(!std.mem.endsWith(u8, result, "\n"));
}

test "Border styles exist" {
Expand Down