From 4f1b87181db8b1767d9e504fe1664659375194ce Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Tue, 22 Oct 2024 01:07:26 -0400 Subject: [PATCH] format::desktop_entry: Add untested formatting function --- format/desktop_entry/line.ha | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/format/desktop_entry/line.ha b/format/desktop_entry/line.ha index 2eb27ee..c9fdaaf 100644 --- a/format/desktop_entry/line.ha +++ b/format/desktop_entry/line.ha @@ -1,3 +1,4 @@ +use fmt; use locale; use strings; @@ -42,3 +43,19 @@ export fn entry_finish(entr: entry) void = { free(entr.value); locale::finish(entr.locale); }; + +// Formats a [[line]] and writes it to an [[io::handle]]. +export fn fprint(output: io::handle, lin: line) (size | io::error) = match (lin) { +case blank => yield fmt::fprintln(output, lin); +case let lin: comment => yield fmt::fprintf(output, "#{}\n", lin: string); +case let lin: group_header => yield fmt::fprintf(output, "[{}]\n", lin: string); +case let lin: entry => + let wrote = fmt::print(lin.key)?; + if (!locale::equal(locale::c, lin.locale)) { + let localestr = locale::format(lin.locale); + defer free(localestr); + wrote += fmt::printf("[{}]", localestr)?; + }; + wrote += fmt::printf("={}\n", lin.value)?; + yield wrote; +};