summaryrefslogtreecommitdiff
path: root/src/File.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/File.zig')
-rw-r--r--src/File.zig90
1 files changed, 23 insertions, 67 deletions
diff --git a/src/File.zig b/src/File.zig
index 84e88dff..64614191 100644
--- a/src/File.zig
+++ b/src/File.zig
@@ -2,7 +2,7 @@
//! index, syntax highlights and the body it fills.
const panes = @import("panes.zig");
const std = @import("std");
-const uucode = @import("uucode");
+const vaxis = @import("vaxis");
const builtin = @import("builtin");
const pardes = @import("pardes.zig");
const config = @import("config.zig");
@@ -21,7 +21,6 @@ const Stream = @import("Stream.zig");
const Pane = panes.Pane;
const Output = panes.Output;
const Mini = panes.mini;
-const terminal = panes.terminal;
const Text = panes.Text;
const SYNTAX_CONTEXT_AFTER_ROWS: usize = 2;
@@ -128,11 +127,11 @@ pub const State = struct {
/// A +Pager whose text is a diff (`git diff`, `show`, `log -p`): drawn
/// and looked at as a diff (Pane.colorAlgo).
pager_diff: bool = false,
- /// A +Pager's colours, as its program printed them (pager.zig): drawn
+ /// A +Pager's colours, as its program printed them (Term.page): drawn
/// over its text by recolorSyntax, never read over 9P. ponytail: any
/// edit drops them (setContentSpan); shifting them through the splice,
/// as pane.zig's shiftOne moves dot, would keep them.
- pager_spans: []pardes.pager.Span = &.{},
+ pager_spans: []pardes.Term.Span = &.{},
/// Seconds of the last content change, for the filesystem's stat; zero until edited.
mtime: u32 = 0,
/// Non-null for a generated output buffer rather than an on-disk file.
@@ -186,8 +185,10 @@ pub const NodeStep = struct { ranges: []modal.Selection, primary: usize };
pub fn graphemeDisplayWidth(grapheme: []const u8) usize {
if (std.mem.eql(u8, grapheme, "\t")) return config.tab_now;
if (grapheme.len == 1 and grapheme[0] >= 0x20 and grapheme[0] < 0x7f) return 1;
- if (std.unicode.utf8ValidateSlice(grapheme)) return @max(1, @as(usize, utf8Width(grapheme)));
- // Each invalid byte is a cell, as surface.zig draws it, and each valid
+ if (std.unicode.utf8ValidateSlice(grapheme)) return @max(1, @as(usize, vaxis.gwidth.gwidth(grapheme, .unicode)));
+ // vaxis's gwidth counts back a replacement rune's 3 bytes over the one
+ // invalid byte it stood for (`\xee` before `e\u{301}` overflowed it):
+ // each invalid byte is a cell, as surface.zig draws it, and each valid
// run between is measured alone.
var width: usize = 0;
var run: usize = 0;
@@ -198,60 +199,15 @@ pub fn graphemeDisplayWidth(grapheme: []const u8) usize {
i += n;
continue;
}
- if (run < i) width += utf8Width(grapheme[run..i]);
+ if (run < i) width += vaxis.gwidth.gwidth(grapheme[run..i], .unicode);
width += 1;
i += 1;
run = i;
}
- if (run < grapheme.len) width += utf8Width(grapheme[run..]);
+ if (run < grapheme.len) width += vaxis.gwidth.gwidth(grapheme[run..], .unicode);
return @max(1, width);
}
-/// The cells valid UTF-8 takes, summed over its graphemes: a grapheme is
-/// as wide as its widest code point (a control, a nonspacing or enclosing
-/// mark and the joiners, selectors and other format characters below add
-/// none; East Asian wide and fullwidth two, the rest one), then at least
-/// one under the text selector U+FE0E, else at least two under the emoji
-/// selector U+FE0F, with an emoji-presentation code point, or as a flag's
-/// two regional indicators. It is libvaxis's `gwidth(.unicode)` over the
-/// same uucode tables, which the tui renders with in mode 2027.
-pub fn utf8Width(valid: []const u8) u16 {
- var total: u16 = 0;
- var widest: u16 = 0;
- var text_vs = false;
- var emoji = false;
- var flags: u8 = 0;
- var it = uucode.grapheme.utf8Iterator(valid);
- while (it.nextCodePoint()) |r| {
- const cp = r.code_point;
- if (cp == 0xfe0e) {
- text_vs = true;
- } else if (cp == 0xfe0f) {
- emoji = true;
- } else {
- if (uucode.get(.is_emoji_presentation, cp)) emoji = true;
- if (cp >= 0x1f1e6 and cp <= 0x1f1ff) flags += 1;
- widest = @max(widest, @as(u16, switch (cp) {
- 0...0x1f, 0x7f...0x9f, 0xad, 0x34f, 0x180b...0x180d, 0x200b...0x200d, 0x2060, 0xfe00...0xfe0d, 0xfeff, 0xe0100...0xe01ef => 0,
- else => switch (uucode.get(.general_category, cp)) {
- .mark_nonspacing, .mark_enclosing => 0,
- else => switch (uucode.get(.east_asian_width, cp)) {
- .fullwidth, .wide => 2,
- else => 1,
- },
- },
- }));
- }
- if (!r.is_break) continue;
- total += if (text_vs) @max(1, widest) else if (emoji or flags == 2) @max(2, widest) else widest;
- widest = 0;
- text_vs = false;
- emoji = false;
- flags = 0;
- }
- return total;
-}
-
pub fn displayWidth(text: []const u8) usize {
var width: usize = 0;
var at: usize = 0;
@@ -598,7 +554,7 @@ pub fn open(p: *Pardes, id: usize, path: []const u8, line: usize) !*Pane {
std.debug.assert(p.panes[id] == null and !p.reserved_slots[id]);
const path_copy = try p.gpa.dupe(u8, path);
errdefer p.gpa.free(path_copy);
- const pane = try terminal.createDoc(p.gpa, p.view().screen_w, p.view().screen_h);
+ const pane = try panes.createDoc(p.gpa, p.view().screen_w, p.view().screen_h);
errdefer p.gpa.destroy(pane);
const history = try History.create(p.gpa);
errdefer p.gpa.destroy(history);
@@ -1875,8 +1831,8 @@ pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes.
// A row whose first byte names a hue (syntax.ground_add, _del): its
// grounds, the theme's, worked out once a frame and only for one.
var grounds: ?Colors.Grounds = null;
- var filtered_storage: terminal.FilteredColors = undefined;
- const filtered: ?*terminal.FilteredColors = if (pane.tty_filter and spans.len > 0) blk: {
+ var filtered_storage: pardes.Term.Filter = undefined;
+ const filtered: ?*pardes.Term.Filter = if (pane.tty_filter and spans.len > 0) blk: {
filtered_storage = .init(p, null);
break :blk &filtered_storage;
} else null;
@@ -1923,11 +1879,11 @@ pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes.
const ground: ?[3]u8 = if (lead & syntax.ground_emph != 0) t.row else if (lead & syntax.ground_line != 0) t.line else null;
if (ground) |g| s.tint(tx + @as(u16, @intCast(prefix_width)), body_y + vr, tw -| @as(u16, @intCast(prefix_width)), g, null);
};
- // A +Pager's own colours (pager.zig) on every row but one in a hue:
+ // A +Pager's own colours (Term.page) on every row but one in a hue:
// an added or removed line is drawn as pardes draws a diff, and
// git's red and green on it would only fight the tint.
- var span_at = if (tint != null) spans.len else std.sort.partitionPoint(pardes.pager.Span, spans, base + hs, struct {
- fn before(at: usize, span: pardes.pager.Span) bool {
+ var span_at = if (tint != null) spans.len else std.sort.partitionPoint(pardes.Term.Span, spans, base + hs, struct {
+ fn before(at: usize, span: pardes.Term.Span) bool {
return span.end <= at;
}
}.before);
@@ -2004,8 +1960,8 @@ pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes.
if (st.fg != .default) cell.style.fg = colors.spanColor(st.fg, false);
if (st.bg != .default) cell.style.bg = colors.spanColor(st.bg, true);
} else {
- if (st.fg != .default) cell.style.fg = if (st.fg == .index) terminal.palColor(p, st.fg.index) else st.fg;
- if (st.bg != .default) cell.style.bg = if (st.bg == .index) terminal.palColor(p, st.bg.index) else st.bg;
+ if (st.fg != .default) cell.style.fg = if (st.fg == .index) pardes.Term.palColor(p, st.fg.index) else st.fg;
+ if (st.bg != .default) cell.style.bg = if (st.bg == .index) pardes.Term.palColor(p, st.bg.index) else st.bg;
}
cell.style.bold = cell.style.bold or st.bold;
cell.style.dim = st.dim;
@@ -2069,7 +2025,7 @@ pub const DiffOutput = struct {
e.used = o.clock;
return if (e.is_diff) e.rows else null;
};
- const shown = terminal.shellRows(p, pane) catch return null;
+ const shown = pardes.edit.shellRows(p, pane) catch return null;
const is_diff = diff.looksLikeDiff(shown);
var total: usize = 0;
if (is_diff) for (shown) |row| {
@@ -2131,7 +2087,7 @@ pub fn recolorDiffOutput(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, t
const line = shown.next() orelse continue;
const vr: i32 = @intCast(vi);
const w = pane.wrapAt(vr);
- const row_i: i32 = if (raw) terminal.gridOffset(pane) + vr else w.line;
+ const row_i: i32 = if (raw) @as(i32, @intCast(panes.Term.grid(pane.terminal).offset)) + vr else w.line;
if (row_i < 0 or row_i >= @as(i64, @intCast(rows.len))) continue;
const row: usize = @intCast(row_i);
const from: usize = if (raw) 0 else @intCast(@max(0, w.at));
@@ -2217,7 +2173,7 @@ pub fn paintWordSelection(
}
}
-test "an invalid byte in a grapheme is one cell, and never reaches utf8Width" {
+test "an invalid byte in a grapheme is one cell, and never reaches vaxis's width" {
try std.testing.expectEqual(@as(usize, 2), graphemeDisplayWidth("\xeee\xcc\x81"));
try std.testing.expectEqual(@as(usize, 3), graphemeDisplayWidth("\xee\xffe"));
try std.testing.expectEqual(@as(usize, 3), graphemeDisplayWidth("\u{4e16}\x80"));
@@ -2256,11 +2212,11 @@ test "indexed source rows match uncached scans across content changes" {
}
}
-test "the ASCII arm of graphemeDisplayWidth matches the utf8Width it skips" {
+test "the ASCII arm of graphemeDisplayWidth matches the gwidth it skips" {
const ref = struct {
fn width(grapheme: []const u8) usize {
if (std.mem.eql(u8, grapheme, "\t")) return config.tab_now;
- return @max(1, @as(usize, utf8Width(grapheme)));
+ return @max(1, @as(usize, vaxis.gwidth.gwidth(grapheme, .unicode)));
}
}.width;
@@ -2275,7 +2231,7 @@ test "the ASCII arm of graphemeDisplayWidth matches the utf8Width it skips" {
"\u{1f642}", "\u{1f1e6}\u{1f1e7}", "\xff",
}) |g| try std.testing.expectEqual(ref(g), graphemeDisplayWidth(g));
// Two invalid bytes are two cells, as surface.zig draws them, where
- // one U+FFFD for the pair would give one.
+ // vaxis's own count gives the pair one.
try std.testing.expectEqual(@as(usize, 2), graphemeDisplayWidth("\xe4\xb8"));
}