refactor: add Cursor char_at, char_left/_right, test_left/_right functions

This commit is contained in:
CJ van den Berg 2025-11-27 16:11:53 +01:00
parent 3db11a43c9
commit bb53ba0fc1
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -193,10 +193,39 @@ pub fn egc_at(self: *const Self, root: Buffer.Root, metrics: Metrics) error{NotF
return root.egc_at(self.row, self.col, metrics);
}
pub fn char_at(self: *const Self, root: Buffer.Root, metrics: Metrics) []const u8 {
const char, _, _ = root.egc_at(self.row, self.col, metrics) catch return &.{};
return char;
}
pub fn char_left(self: *const Self, root: Buffer.Root, metrics: Metrics) []const u8 {
var tmp = self.*;
tmp.move_left(root, metrics) catch return &.{};
return tmp.char_at(root, metrics);
}
pub fn char_right(self: *const Self, root: Buffer.Root, metrics: Metrics) []const u8 {
var tmp = self.*;
tmp.move_right(root, metrics) catch return &.{};
return tmp.char_at(root, metrics);
}
pub fn test_at(self: *const Self, root: Buffer.Root, pred: *const fn (c: []const u8) bool, metrics: Metrics) bool {
return root.test_at(pred, self.row, self.col, metrics);
}
pub fn test_left(self: *const Self, root: Buffer.Root, pred: *const fn (c: []const u8) bool, metrics: Metrics) bool {
var tmp = self.*;
tmp.move_left(root, metrics) catch return false;
return root.test_at(pred, tmp.row, tmp.col, metrics);
}
pub fn test_right(self: *const Self, root: Buffer.Root, pred: *const fn (c: []const u8) bool, metrics: Metrics) bool {
var tmp = self.*;
tmp.move_right(root, metrics) catch return false;
return root.test_at(pred, tmp.row, tmp.col, metrics);
}
pub fn write(self: *const Self, writer: *std.Io.Writer) !void {
try cbor.writeValue(writer, .{
self.row,