feat: add support for CRLF EOL mode

This commit is contained in:
CJ van den Berg 2024-09-25 20:06:06 +02:00
parent 9a633aa2a9
commit 593b202b16
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
8 changed files with 119 additions and 50 deletions

View file

@ -2,6 +2,7 @@ const std = @import("std");
const Allocator = std.mem.Allocator;
const tp = @import("thespian");
const tracy = @import("tracy");
const Buffer = @import("Buffer");
const root = @import("root");
const Plane = @import("renderer").Plane;
@ -29,6 +30,7 @@ file_exists: bool,
file_dirty: bool = false,
detailed: bool = false,
file: bool = false,
eol_mode: Buffer.EolMode = .lf,
const project_icon = "";
const Self = @This();
@ -134,11 +136,16 @@ fn render_detailed(self: *Self, plane: *Plane, theme: *const Widget.Theme) void
const project_name = tp.env.get().str("project");
_ = plane.print("{s} ({s})", .{ self.name, project_name }) catch {};
} else {
const eol_mode = switch (self.eol_mode) {
.lf => " [↩ = ␊]",
.crlf => " [↩ = ␍␊]",
};
_ = plane.putstr(if (!self.file_exists) "󰽂" else if (self.file_dirty) "󰆓" else "󱣪") catch {};
_ = plane.print(" {s}:{d}:{d}", .{ self.name, self.line + 1, self.column + 1 }) catch {};
_ = plane.print(" of {d} lines", .{self.lines}) catch {};
if (self.file_type.len > 0)
_ = plane.print(" ({s})", .{self.file_type}) catch {};
_ = plane.print(" ({s}){s}", .{ self.file_type, eol_mode }) catch {};
}
return;
}
@ -169,10 +176,13 @@ pub fn receive(self: *Self, _: *Button.State(Self), _: tp.pid_ref, m: tp.message
var file_type: []const u8 = undefined;
var file_icon: []const u8 = undefined;
var file_dirty: bool = undefined;
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) }))
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) })) {
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);
self.name = self.name_buf[0..file_path.len];

View file

@ -1,6 +1,7 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const tp = @import("thespian");
const Buffer = @import("Buffer");
const Plane = @import("renderer").Plane;
@ -13,6 +14,7 @@ lines: usize = 0,
column: usize = 0,
buf: [256]u8 = undefined,
rendered: [:0]const u8 = "",
eol_mode: Buffer.EolMode = .lf,
const Self = @This();
@ -47,14 +49,21 @@ pub fn render(self: *Self, btn: *Button.State(Self), theme: *const Widget.Theme)
fn format(self: *Self) void {
var fbs = std.io.fixedBufferStream(&self.buf);
const writer = fbs.writer();
std.fmt.format(writer, " Ln {d}, Col {d} ", .{ self.line + 1, self.column + 1 }) catch {};
const eol_mode = switch (self.eol_mode) {
.lf => "",
.crlf => " [␍␊]",
};
std.fmt.format(writer, "{s} Ln {d}, Col {d} ", .{ eol_mode, self.line + 1, self.column + 1 }) catch {};
self.rendered = @ptrCast(fbs.getWritten());
self.buf[self.rendered.len] = 0;
}
pub fn receive(self: *Self, _: *Button.State(Self), _: tp.pid_ref, m: tp.message) error{Exit}!bool {
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) })) {
self.eol_mode = @enumFromInt(eol_mode);
} else if (try m.match(.{ "E", "close" })) {
self.lines = 0;
self.line = 0;