feat: sanitize non utf-8 and display a status bar warning

This commit is contained in:
CJ van den Berg 2024-12-18 15:52:57 +01:00
parent e865a89ede
commit c0a9be21f5
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
5 changed files with 79 additions and 20 deletions

View file

@ -32,6 +32,7 @@ file_dirty: bool = false,
detailed: bool = false,
file: bool = false,
eol_mode: Buffer.EolMode = .lf,
utf8_sanitized: bool = false,
const project_icon = "";
const Self = @This();
@ -161,6 +162,11 @@ fn render_detailed(self: *Self, plane: *Plane, theme: *const Widget.Theme) void
_ = plane.print(" of {d} lines", .{self.lines}) catch {};
if (self.file_type.len > 0)
_ = plane.print(" ({s}){s}", .{ self.file_type, eol_mode }) catch {};
if (self.utf8_sanitized) {
plane.set_style(.{ .fg = theme.editor_error.fg.? });
_ = plane.putstr(" [UTF-8 sanitized]") catch {};
}
}
return;
}
@ -196,7 +202,7 @@ pub fn receive(self: *Self, _: *Button.State(Self), _: tp.pid_ref, m: tp.message
return false;
if (try m.match(.{ "E", "dirty", tp.extract(&file_dirty) })) {
self.file_dirty = file_dirty;
} else if (try m.match(.{ "E", "eol_mode", tp.extract(&eol_mode) })) {
} else if (try m.match(.{ "E", "eol_mode", tp.extract(&eol_mode), tp.extract(&self.utf8_sanitized) })) {
self.eol_mode = @enumFromInt(eol_mode);
} else if (try m.match(.{ "E", "save", tp.extract(&file_path) })) {
@memcpy(self.name_buf[0..file_path.len], file_path);

View file

@ -10,12 +10,15 @@ const EventHandler = @import("EventHandler");
const Widget = @import("../Widget.zig");
const Button = @import("../Button.zig");
const utf8_sanitized_warning = "  UTF";
line: usize = 0,
lines: usize = 0,
column: usize = 0,
buf: [256]u8 = undefined,
rendered: [:0]const u8 = "",
eol_mode: Buffer.EolMode = .lf,
utf8_sanitized: bool = false,
const Self = @This();
@ -36,7 +39,8 @@ fn on_click(_: *Self, _: *Button.State(Self)) void {
}
pub fn layout(self: *Self, btn: *Button.State(Self)) Widget.Layout {
const len = btn.plane.egc_chunk_width(self.rendered, 0, 1);
const warn_len = if (self.utf8_sanitized) btn.plane.egc_chunk_width(utf8_sanitized_warning, 0, 1) else 0;
const len = btn.plane.egc_chunk_width(self.rendered, 0, 1) + warn_len;
return .{ .static = len };
}
@ -47,6 +51,11 @@ pub fn render(self: *Self, btn: *Button.State(Self), theme: *const Widget.Theme)
btn.plane.set_style(if (btn.active) theme.editor_cursor else if (btn.hover) theme.statusbar_hover else theme.statusbar);
btn.plane.fill(" ");
btn.plane.home();
if (self.utf8_sanitized) {
btn.plane.set_style(.{ .fg = theme.editor_error.fg.? });
_ = btn.plane.putstr(utf8_sanitized_warning) catch {};
}
btn.plane.set_style(if (btn.active) theme.editor_cursor else if (btn.hover) theme.statusbar_hover else theme.statusbar);
_ = btn.plane.putstr(self.rendered) catch {};
return false;
}
@ -67,7 +76,7 @@ pub fn receive(self: *Self, _: *Button.State(Self), _: tp.pid_ref, m: tp.message
var eol_mode: Buffer.EolModeTag = @intFromEnum(Buffer.EolMode.lf);
if (try m.match(.{ "E", "pos", tp.extract(&self.lines), tp.extract(&self.line), tp.extract(&self.column) })) {
self.format();
} else if (try m.match(.{ "E", "eol_mode", tp.extract(&eol_mode) })) {
} else if (try m.match(.{ "E", "eol_mode", tp.extract(&eol_mode), tp.extract(&self.utf8_sanitized) })) {
self.eol_mode = @enumFromInt(eol_mode);
self.format();
} else if (try m.match(.{ "E", "open", tp.more })) {
@ -78,6 +87,7 @@ pub fn receive(self: *Self, _: *Button.State(Self), _: tp.pid_ref, m: tp.message
self.column = 0;
self.rendered = "";
self.eol_mode = .lf;
self.utf8_sanitized = false;
}
return false;
}