From adfff8167a843febc845bd106ac025e4f53d1c1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20M=C3=A9sz=C3=A1ros=20=28Laptop=29?= Date: Fri, 20 Feb 2026 15:49:22 +0100 Subject: [PATCH] Fix inline style newline handling --- README.md | 4 ++++ src/style/style.zig | 4 ++++ tests/style_tests.zig | 14 ++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/README.md b/README.md index 22ee5a0..0dade7b 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/style/style.zig b/src/style/style.zig index e8abe17..b2a208f 100644 --- a/src/style/style.zig +++ b/src/style/style.zig @@ -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(); } diff --git a/tests/style_tests.zig b/tests/style_tests.zig index 5b98017..7e2c676 100644 --- a/tests/style_tests.zig +++ b/tests/style_tests.zig @@ -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" {