Commit: dc407f7e73545401aad580e6918a06fa8c497a1b
Parent: 30da6f3281d8d94b879cac754a53b48cff92ceee
Author: Randy Palamar
Date: Sun, 9 Mar 2025 07:50:20 -0600
add stream_reset to reset index and error flag
Diffstat:
3 files changed, 13 insertions(+), 6 deletions(-)
diff --git a/blocks/linux/backlight.c b/blocks/linux/backlight.c
@@ -25,7 +25,7 @@ static BLOCK_INIT_FN(backlight_init)
lbd->max_brightness = read_i64(stream_ensure_c_str(&path));
if (!lbd->max_brightness)
die("backlight_init: failed to read max brightness\n");
- path.write_index = sidx;
+ stream_reset(&path, sidx);
stream_push_s8(&path, s8("/brightness"));
path.buffer[path.write_index++] = 0;
diff --git a/blocks/linux/battery_info.c b/blocks/linux/battery_info.c
@@ -24,7 +24,7 @@ static BLOCK_UPDATE_FN(battery_info_update)
stream_push_s8(&lbd->path_base, s8("/energy_now"));
energy_now = read_i64(stream_ensure_c_str(&lbd->path_base));
- lbd->path_base.write_index = sidx;
+ stream_reset(&lbd->path_base, sidx);
f32 percent = 100 * (energy_now / (f64)lbd->energy_full) + 0.5;
b32 warn = percent < ba->thres;
@@ -35,7 +35,7 @@ static BLOCK_UPDATE_FN(battery_info_update)
(s8){.len = sizeof(state_buffer),
.data = (u8 *)state_buffer}));
if (state.len <= 0) state = s8("Unknown");
- lbd->path_base.write_index = sidx;
+ stream_reset(&lbd->path_base, sidx);
state.data[state.len] = 0;
i64 len;
@@ -45,7 +45,7 @@ static BLOCK_UPDATE_FN(battery_info_update)
stream_push_s8(&lbd->path_base, s8("/power_now"));
power_now = read_i64(stream_ensure_c_str(&lbd->path_base));
if (!power_now) power_now = 1;
- lbd->path_base.write_index = sidx;
+ stream_reset(&lbd->path_base, sidx);
timeleft = energy_now / (f64)ABS(power_now);
h = timeleft;
@@ -88,7 +88,7 @@ static BLOCK_INIT_FN(battery_info_init)
lbd->energy_full = read_i64(stream_ensure_c_str(&lbd->path_base));
if (!lbd->energy_full)
die("battery_info_init: failed to read battery capacity\n");
- lbd->path_base.write_index = sidx;
+ stream_reset(&lbd->path_base, sidx);
battery_info_update(b);
}
diff --git a/status.c b/status.c
@@ -205,6 +205,13 @@ s8_trim_space(s8 a)
return a;
}
+static void
+stream_reset(Stream *s, size position)
+{
+ s->errors = !BETWEEN(position, 0, s->capacity);
+ if (!s->errors) s->write_index = position;
+}
+
static Stream
stream_alloc(Arena *a, size capacity)
{
@@ -321,7 +328,7 @@ dispatch_file_watch_events(Arena a)
static void
update_status(void)
{
- statusline.write_index = 0;
+ stream_reset(&statusline, 0);
i32 block_index;
for (block_index = 0; block_index < dirty_block_index; block_index++)
statusline.write_index += blocks[block_index].len;