git.lucas.co / cce-compositor
Wayland compositor (wlroots)
git clone https://git.lucas.co/cce-compositor.git

commit9a9f597862e25d16648da0e0fc977bdc78bced20
parent1e4979b603
authorIsaac Freund <[email protected]>
date2021-02-02 18:42
riverctl: improve handling of null string options

Passing an empty string as the value argument for riverctl set-option or
declare-option will set the value to null. The riverctl get-option
command produces no output for both null and empty string values.

This is not perfect as it is unable to distinguish between null and
empty strings through the riverctl CLI. I don't see a better alternative
here however. Forbidding null strings in the river-options protocol
would be one solution, however null strings are useful and more pleasant
to use from code despite being problematic on the CLI.

 riverctl/options.zig | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/riverctl/options.zig b/riverctl/options.zig
index 99e0be5..41e3223 100644
--- a/riverctl/options.zig
+++ b/riverctl/options.zig
@@ -63,7 +63,7 @@ pub fn declareOption(display: *wl.Display, globals: *Globals) !void {
         .int => setIntValueRaw(handle, raw_value),
         .uint => setUintValueRaw(handle, raw_value),
         .fixed => setFixedValueRaw(handle, raw_value),
-        .string => handle.setStringValue(raw_value),
+        .string => handle.setStringValue(if (raw_value[0] == 0) null else raw_value),
     }
     _ = display.flush() catch os.exit(1);
 }
@@ -156,7 +156,7 @@ fn getOptionListener(
         .int_value => |ev| printOutputExit("{}", .{ev.value}),
         .uint_value => |ev| printOutputExit("{}", .{ev.value}),
         .fixed_value => |ev| printOutputExit("{d}", .{ev.value.toDouble()}),
-        .string_value => |ev| printOutputExit("{}", .{ev.value}),
+        .string_value => |ev| if (ev.value) |s| printOutputExit("{}", .{s}) else os.exit(0),
     }
 }
 
@@ -180,7 +180,7 @@ fn setOptionListener(
         .int_value => |ev| setIntValueRaw(handle, ctx.raw_value),
         .uint_value => |ev| setUintValueRaw(handle, ctx.raw_value),
         .fixed_value => |ev| setFixedValueRaw(handle, ctx.raw_value),
-        .string_value => |ev| handle.setStringValue(ctx.raw_value),
+        .string_value => |ev| handle.setStringValue(if (ctx.raw_value[0] == 0) null else ctx.raw_value),
     }
     _ = ctx.display.flush() catch os.exit(1);
     os.exit(0);