fix: +list-themes crashing when there's search result is empty

This commit is contained in:
furtidev
2024-11-16 20:03:58 +06:00
parent a6bdd2f201
commit fb2510c2ed

View File

@ -256,7 +256,10 @@ const Preview = struct {
fn updateFiltered(self: *Preview) !void {
const relative = self.current -| self.window;
const selected = self.themes[self.filtered.items[self.current]].theme;
var selected: []const u8 = undefined;
if (self.filtered.items.len > 0) {
selected = self.themes[self.filtered.items[self.current]].theme;
}
const hash_algorithm = std.hash.Wyhash;
@ -714,41 +717,41 @@ const Preview = struct {
const x_off: u16 = @intCast(x_off_unconverted);
const width: u16 = win.width - x_off;
if (self.filtered.items.len > 0) {
const theme = self.themes[self.filtered.items[self.current]];
var config = try Config.default(alloc);
defer config.deinit();
config.loadFile(config._arena.?.allocator(), theme.path) catch |err| {
const theme_path_len: u16 = @intCast(theme.path.len);
const child = win.child(
.{
.x_off = x_off,
.y_off = 0,
.width = .{
.limit = width,
},
.height = .{
.limit = win.height,
},
.width = width,
.height = win.height,
},
);
child.fill(.{ .style = self.ui_standard() });
const middle = child.height / 2;
{
const text = try std.fmt.allocPrint(alloc, "Unable to open {s} from:", .{theme.theme});
_ = try child.printSegment(
const text_len: u16 = @intCast(text.len);
_ = child.printSegment(
.{
.text = text,
.style = self.ui_err(),
},
.{
.row_offset = middle -| 1,
.col_offset = child.width / 2 -| text.len / 2,
.col_offset = child.width / 2 -| text_len / 2,
},
);
}
{
_ = try child.printSegment(
_ = child.printSegment(
.{
.text = theme.path,
.style = self.ui_err(),
@ -758,27 +761,28 @@ const Preview = struct {
},
.{
.row_offset = middle,
.col_offset = child.width / 2 -| theme.path.len / 2,
.col_offset = child.width / 2 -| theme_path_len / 2,
},
);
}
{
const text = try std.fmt.allocPrint(alloc, "{}", .{err});
_ = try child.printSegment(
const text_len: u16 = @intCast(text.len);
_ = child.printSegment(
.{
.text = text,
.style = self.ui_err(),
},
.{
.row_offset = middle + 1,
.col_offset = child.width / 2 -| text.len / 2,
.col_offset = child.width / 2 -| text_len / 2,
},
);
}
return;
};
var next_start: usize = 0;
var next_start: u16 = 0;
const fg: vaxis.Color = .{
.rgb = [_]u8{
@ -841,20 +845,18 @@ const Preview = struct {
};
{
const theme_len: u16 = @intCast(theme.theme.len);
const theme_path_len: u16 = @intCast(theme.path.len);
const child = win.child(
.{
.x_off = x_off,
.y_off = next_start,
.width = .{
.limit = width,
},
.height = .{
.limit = 4,
},
.width = width,
.height = 4,
},
);
child.fill(.{ .style = standard });
_ = try child.printSegment(
_ = child.printSegment(
.{
.text = theme.theme,
.style = standard_bold_italic,
@ -864,10 +866,10 @@ const Preview = struct {
},
.{
.row_offset = 1,
.col_offset = child.width / 2 -| theme.theme.len / 2,
.col_offset = child.width / 2 -| theme_len / 2,
},
);
_ = try child.printSegment(
_ = child.printSegment(
.{
.text = theme.path,
.style = standard,
@ -877,7 +879,7 @@ const Preview = struct {
},
.{
.row_offset = 2,
.col_offset = child.width / 2 -| theme.path.len / 2,
.col_offset = child.width / 2 -| theme_path_len / 2,
.wrap = .none,
},
);
@ -885,37 +887,36 @@ const Preview = struct {
}
if (config._diagnostics.items().len > 0) {
const diagnostic_items_len: u16 = @intCast(config._diagnostics.items().len);
const child = win.child(
.{
.x_off = x_off,
.y_off = next_start,
.width = .{
.limit = width,
},
.height = .{
.limit = if (config._diagnostics.items().len == 0) 0 else 2 + config._diagnostics.items().len,
},
.width = width,
.height = if (config._diagnostics.items().len == 0) 0 else 2 + diagnostic_items_len,
},
);
{
const text = "Problems were encountered trying to load the theme:";
_ = try child.printSegment(
const text_len: u16 = @intCast(text.len);
_ = child.printSegment(
.{
.text = text,
.style = self.ui_err(),
},
.{
.row_offset = 0,
.col_offset = child.width / 2 -| (text.len / 2),
.col_offset = child.width / 2 -| (text_len / 2),
},
);
}
var buf = std.ArrayList(u8).init(alloc);
defer buf.deinit();
for (config._diagnostics.items(), 0..) |diag, i| {
for (config._diagnostics.items(), 0..) |diag, captured_i| {
const i: u16 = @intCast(captured_i);
try diag.write(buf.writer());
_ = try child.printSegment(
_ = child.printSegment(
.{
.text = buf.items,
.style = self.ui_err(),
@ -933,24 +934,21 @@ const Preview = struct {
const child = win.child(.{
.x_off = x_off,
.y_off = next_start,
.width = .{
.limit = width,
},
.height = .{
.limit = 6,
},
.width = width,
.height = 6,
});
child.fill(.{ .style = standard });
for (0..16) |i| {
for (0..16) |captured_i| {
const i: u16 = @intCast(captured_i);
const r = i / 8;
const c = i % 8;
const text = if (self.hex)
try std.fmt.allocPrint(alloc, " {x:0>2}", .{i})
else
try std.fmt.allocPrint(alloc, "{d:3}", .{i});
_ = try child.printSegment(
_ = child.printSegment(
.{
.text = text,
.style = standard,
@ -960,7 +958,7 @@ const Preview = struct {
.col_offset = c * 8,
},
);
_ = try child.printSegment(
_ = child.printSegment(
.{
.text = "████",
.style = .{
@ -973,7 +971,7 @@ const Preview = struct {
.col_offset = 4 + c * 8,
},
);
_ = try child.printSegment(
_ = child.printSegment(
.{
.text = "████",
.style = .{
@ -994,12 +992,8 @@ const Preview = struct {
.{
.x_off = x_off,
.y_off = next_start,
.width = .{
.limit = width,
},
.height = .{
.limit = 24,
},
.width = width,
.height = 24,
},
);
const bold: vaxis.Style = .{
@ -1049,7 +1043,7 @@ const Preview = struct {
.bg = bg,
};
child.fill(.{ .style = standard });
_ = try child.print(
_ = child.print(
&.{
.{ .text = "", .style = color2 },
.{ .text = " ", .style = standard },
@ -1063,7 +1057,7 @@ const Preview = struct {
},
);
{
_ = try child.print(
_ = child.print(
&.{
.{
.text = "───────┬",
@ -1076,8 +1070,9 @@ const Preview = struct {
},
);
if (child.width > 10) {
for (10..child.width) |col| {
_ = try child.print(
for (10..child.width) |captured_col| {
const col: u16 = @intCast(captured_col);
_ = child.print(
&.{
.{
.text = "",
@ -1092,7 +1087,7 @@ const Preview = struct {
}
}
}
_ = try child.print(
_ = child.print(
&.{
.{
.text = "",
@ -1115,7 +1110,7 @@ const Preview = struct {
},
);
{
_ = try child.print(
_ = child.print(
&.{
.{
.text = "───────┼",
@ -1128,8 +1123,9 @@ const Preview = struct {
},
);
if (child.width > 10) {
for (10..child.width) |col| {
_ = try child.print(
for (10..child.width) |captured_col| {
const col: u16 = @intCast(captured_col);
_ = child.print(
&.{
.{
.text = "",
@ -1144,7 +1140,7 @@ const Preview = struct {
}
}
}
_ = try child.print(
_ = child.print(
&.{
.{ .text = " 1 │ ", .style = color238 },
.{ .text = "const", .style = color5 },
@ -1159,7 +1155,7 @@ const Preview = struct {
.col_offset = 2,
},
);
_ = try child.print(
_ = child.print(
&.{
.{ .text = " 2 │", .style = color238 },
},
@ -1168,7 +1164,7 @@ const Preview = struct {
.col_offset = 2,
},
);
_ = try child.print(
_ = child.print(
&.{
.{ .text = " 3 │ ", .style = color238 },
.{ .text = "pub ", .style = color5 },
@ -1184,7 +1180,7 @@ const Preview = struct {
.col_offset = 2,
},
);
_ = try child.print(
_ = child.print(
&.{
.{ .text = " 4 │ ", .style = color238 },
.{ .text = "const ", .style = color5 },
@ -1197,7 +1193,7 @@ const Preview = struct {
.col_offset = 2,
},
);
_ = try child.print(
_ = child.print(
&.{
.{ .text = " 5 │ ", .style = color238 },
.{ .text = "var ", .style = color5 },
@ -1212,7 +1208,7 @@ const Preview = struct {
.col_offset = 2,
},
);
_ = try child.print(
_ = child.print(
&.{
.{ .text = " 6 │ ", .style = color238 },
.{ .text = "while ", .style = color5 },
@ -1229,7 +1225,7 @@ const Preview = struct {
.col_offset = 2,
},
);
_ = try child.print(
_ = child.print(
&.{
.{ .text = " 7 │ ", .style = color238 },
.{ .text = "if ", .style = color5 },
@ -1245,7 +1241,7 @@ const Preview = struct {
.col_offset = 2,
},
);
_ = try child.print(
_ = child.print(
&.{
.{ .text = " 8 │ ", .style = color238 },
.{ .text = "try ", .style = color5 },
@ -1260,7 +1256,7 @@ const Preview = struct {
.col_offset = 2,
},
);
_ = try child.print(
_ = child.print(
&.{
.{ .text = " 9 │ ", .style = color238 },
.{ .text = "} ", .style = standard },
@ -1277,7 +1273,7 @@ const Preview = struct {
.col_offset = 2,
},
);
_ = try child.print(
_ = child.print(
&.{
.{ .text = " 10 │ ", .style = color238 },
.{ .text = "try ", .style = color5 },
@ -1292,7 +1288,7 @@ const Preview = struct {
.col_offset = 2,
},
);
_ = try child.print(
_ = child.print(
&.{
.{ .text = " 11 │ ", .style = color238 },
.{ .text = "} ", .style = standard },
@ -1309,7 +1305,7 @@ const Preview = struct {
.col_offset = 2,
},
);
_ = try child.print(
_ = child.print(
&.{
.{ .text = " 12 │ ", .style = color238 },
.{ .text = "try ", .style = color5 },
@ -1324,7 +1320,7 @@ const Preview = struct {
.col_offset = 2,
},
);
_ = try child.print(
_ = child.print(
&.{
.{ .text = " 13 │ ", .style = color238 },
.{ .text = "} ", .style = standard },
@ -1336,7 +1332,7 @@ const Preview = struct {
.col_offset = 2,
},
);
_ = try child.print(
_ = child.print(
&.{
.{ .text = " 14 │ ", .style = color238 },
.{ .text = "try ", .style = color5 },
@ -1351,7 +1347,7 @@ const Preview = struct {
.col_offset = 2,
},
);
_ = try child.print(
_ = child.print(
&.{
.{ .text = " 15 │ ", .style = color238 },
.{ .text = "}", .style = standard },
@ -1361,7 +1357,7 @@ const Preview = struct {
.col_offset = 2,
},
);
_ = try child.print(
_ = child.print(
&.{
.{ .text = " 16 │ ", .style = color238 },
.{ .text = "}", .style = standard },
@ -1371,7 +1367,7 @@ const Preview = struct {
.col_offset = 2,
},
);
_ = try child.print(
_ = child.print(
&.{
.{ .text = " 17 │ ", .style = color238 },
.{ .text = "}", .style = standard },
@ -1382,7 +1378,7 @@ const Preview = struct {
},
);
{
_ = try child.print(
_ = child.print(
&.{
.{
.text = "───────┴",
@ -1395,8 +1391,9 @@ const Preview = struct {
},
);
if (child.width > 10) {
for (10..child.width) |col| {
_ = try child.print(
for (10..child.width) |captured_col| {
const col: u16 = @intCast(captured_col);
_ = child.print(
&.{
.{
.text = "",
@ -1411,7 +1408,7 @@ const Preview = struct {
}
}
}
_ = try child.print(
_ = child.print(
&.{
.{ .text = "ghostty ", .style = color6 },
.{ .text = "on ", .style = standard },
@ -1427,7 +1424,7 @@ const Preview = struct {
.col_offset = 2,
},
);
_ = try child.print(
_ = child.print(
&.{
.{ .text = "", .style = color4 },
.{ .text = "at ", .style = standard },
@ -1446,23 +1443,20 @@ const Preview = struct {
.{
.x_off = x_off,
.y_off = next_start,
.width = .{
.limit = width,
},
.height = .{
.limit = win.height - next_start,
},
.width = width,
.height = win.height - next_start,
},
);
child.fill(.{ .style = standard });
var it = std.mem.splitAny(u8, lorem_ipsum, " \n");
var row: usize = 1;
var col: usize = 2;
var row: u16 = 1;
var col: u16 = 2;
while (row < child.height) {
const word = it.next() orelse line: {
it.reset();
break :line it.next() orelse unreachable;
};
const word_len: u16 = @intCast(word.len);
if (col + word.len > child.width) {
row += 1;
col = 2;
@ -1479,7 +1473,7 @@ const Preview = struct {
if (std.mem.eql(u8, "odio", word)) break :style standard_curly_underline;
break :style standard;
};
_ = try child.printSegment(
_ = child.printSegment(
.{
.text = word,
.style = style,
@ -1489,9 +1483,28 @@ const Preview = struct {
.col_offset = col,
},
);
col += word.len + 1;
col += word_len + 1;
}
}
} else {
const child = win.child(.{
.x_off = 0,
.y_off = 0,
.width = win.width,
.height = win.height,
});
child.fill(.{
.style = self.ui_standard(),
});
_ = child.printSegment(.{
.text = "No theme found!",
.style = self.ui_standard(),
}, .{
.row_offset = win.height / 2 - 1,
.col_offset = win.width / 2 - 7,
});
}
}
};