From 1fc509f897ba8d8664f77c57ed3ecf70bbf04e7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m=20Kov=C3=A1cs?= Date: Wed, 4 Jun 2025 17:32:01 +0200 Subject: [PATCH] feat: refactor --- src/console/main.zig | 105 +++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 54 deletions(-) diff --git a/src/console/main.zig b/src/console/main.zig index 70900ee..3c2f19d 100644 --- a/src/console/main.zig +++ b/src/console/main.zig @@ -261,68 +261,65 @@ const Model = struct { } fn createItems( ctx: vxfw.DrawContext, - items1: *locked(?std.ArrayList(Arc(*models.Item))), + locked_items: *locked(?std.ArrayList(Arc(*models.Item))), current_full_name: ?models.FullName, ) !?[]vxfw.Widget { - const text_items = blk2: { - items1.mutex.lock(); - defer items1.mutex.unlock(); + const text_items = blk: { + locked_items.mutex.lock(); + defer locked_items.mutex.unlock(); - break :blk2 if (items1.data) |items| blk: { - const children = try ctx.arena.create(std.ArrayList(*vxfw.Text)); - children.* = std.ArrayList(*vxfw.Text).init(ctx.arena); + const items = locked_items.data orelse return null; + const children = try ctx.arena.create(std.ArrayList(*vxfw.Text)); + children.* = std.ArrayList(*vxfw.Text).init(ctx.arena); - for (items.items) |*original_arc_child| { - const arc_child = original_arc_child.retain(); - defer if (arc_child.releaseUnwrap()) |item| item.deinit(); + for (items.items) |*original_arc_child| { + const arc_child = original_arc_child.retain(); + defer if (arc_child.releaseUnwrap()) |item| item.deinit(); - const child = arc_child.value.*; - const is_active = if (current_full_name) |c_full_name| - models.FullName.eql(&c_full_name, &child.fullName) - else - false; + const child = arc_child.value.*; + const is_active = if (current_full_name) |c_full_name| + models.FullName.eql(&c_full_name, &child.fullName) + else + false; - const fg, const bg = colors: { - var fg: vaxis.Color = .default; - var bg: vaxis.Color = .default; - if (is_active) { - fg = switch (child.item) { - .container => .{ .index = 0 }, - .element => .{ .index = 0 }, - }; - bg = switch (child.item) { - .container => .{ .index = 4 }, - .element => .{ .index = 7 }, - }; - } else { - fg = switch (child.item) { - .container => .{ .index = 4 }, - .element => .default, - }; - bg = .default; - } - break :colors .{ fg, bg }; - }; + const fg, const bg = colors: { + var fg: vaxis.Color = .default; + var bg: vaxis.Color = .default; + if (is_active) { + fg = switch (child.item) { + .container => .{ .index = 0 }, + .element => .{ .index = 0 }, + }; + bg = switch (child.item) { + .container => .{ .index = 4 }, + .element => .{ .index = 7 }, + }; + } else { + fg = switch (child.item) { + .container => .{ .index = 4 }, + .element => .default, + }; + bg = .default; + } + break :colors .{ fg, bg }; + }; - //NOTE: the right padding is wrong, if the text is too long, the remainder of the text and also the space will be clipped - const text = try std.fmt.allocPrint(ctx.arena, " {s} ", .{child.displayName}); - const text_element = try ctx.arena.create(vxfw.Text); - text_element.* = vxfw.Text{ - .text = text, - .overflow = .clip, - .softwrap = false, - .style = .{ - .bg = bg, - .fg = fg, - }, - }; + //NOTE: the right padding is wrong, if the text is too long, the remainder of the text and also the space will be clipped + const text = try std.fmt.allocPrint(ctx.arena, " {s} ", .{child.displayName}); + const text_element = try ctx.arena.create(vxfw.Text); + text_element.* = vxfw.Text{ + .text = text, + .overflow = .clip, + .softwrap = false, + .style = .{ + .bg = bg, + .fg = fg, + }, + }; - try children.append(text_element); - } - break :blk children; - } else { - return null; - }; + try children.append(text_element); + } + break :blk children; }; const widgets = try ctx.arena.alloc(vxfw.Widget, text_items.items.len);