From de2a747e36a39a528d0a957f8dc71e5a909de0ae Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sun, 14 Jun 2026 19:40:27 +0100 Subject: [PATCH] runtime: format: only add trailing newline to string if one isn't already present --- bshell.runtime/format/value-writer.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/bshell.runtime/format/value-writer.c b/bshell.runtime/format/value-writer.c index b626779..69b1444 100644 --- a/bshell.runtime/format/value-writer.c +++ b/bshell.runtime/format/value-writer.c @@ -28,12 +28,25 @@ static void write_iterable(struct bshell_value_writer *w, const fx_value *value) fx_iterator_unref(it); } +static bool string_has_trailing_newline(const char *s) +{ + long len = strlen(s); + if (len == 0) { + return false; + } + + return s[len - 1] == '\n'; +} + static void write_string(struct bshell_value_writer *w, const fx_value *value) { const char *str = NULL; fx_value_get_cstr(value, &str); fx_stream_write_cstr(w->w_dest, str, NULL); - fx_stream_write_char(w->w_dest, '\n'); + + if (!string_has_trailing_newline(str)) { + fx_stream_write_char(w->w_dest, '\n'); + } } static void write_value(struct bshell_value_writer *w, const fx_value *value)