Something went wrong. Try again.
Charm-like tui library
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313//! Horizontal tab stops.//!//! Port of ultraviolet's `tabstop.go`. Tracks which columns are tab stops so//! the renderer can emit `\t` / backspace-tab moves instead of a longer//! cursor-forward sequence. Stops are stored as a bitmask per 8-column slot://! slot index is `col >> 3`, the bit position is `col & (interval - 1)`. This//! requires `interval` to be a power of two (the default is 8).//!//! All column APIs take and return `usize`; the Go original used `int` but//! every call site in the renderer passes non-negative columns.
const std = @import("std");
const Allocator = std.mem.Allocator;const t = std.testing;
const TabStops = @This();
/// The default tab interval.const default_tab_interval = 8;
/// Horizontal tab stops for a screen of `width` columns.stops: []u32, // one bitmask per 8-column slotinterval: usize, // power of two (>= 1)cols: usize, // screen width in columnsallocator: Allocator,
/// Creates a new set of tab stops for `w` columns at the given interval./// New stops are set at every column `n * interval` (col 0 is always a/// stop). Mirrors Go `NewTabStops`.pub fn init(allocator: Allocator, w: usize, interval: usize) !TabStops { const len = (w + interval - 1) / interval; var ts: TabStops = .{ .stops = try allocator.alloc(u32, len), .interval = interval, .cols = w, .allocator = allocator, }; for (ts.stops) |*s| s.* = 0; ts.initBits(0, w); return ts;}
/// Creates a new set of tab stops with the default interval. Mirrors Go/// `DefaultTabStops`.pub fn default(allocator: Allocator, cols: usize) !TabStops { return init(allocator, cols, default_tab_interval);}
pub fn deinit(self: *TabStops) void { self.allocator.free(self.stops); self.* = undefined;}
/// Resizes the tab stops to the given width. Mirrors Go `Resize`.pub fn resize(self: *TabStops, w: usize) !void { if (w == self.cols) return;
const new_len = (w + self.interval - 1) / self.interval; if (new_len != self.stops.len) { const new_stops = try self.allocator.alloc(u32, new_len); const n = @min(new_len, self.stops.len); @memcpy(new_stops[0..n], self.stops[0..n]); for (new_stops[n..]) |*s| s.* = 0; self.allocator.free(self.stops); self.stops = new_stops; }
self.initBits(self.cols, w); self.cols = w;}
/// Returns the width of the screen that the tab stops are set for.pub fn width(self: TabStops) usize { return self.cols;}
/// Returns true if the given column is a tab stop. Mirrors Go `IsStop`.pub fn isStop(self: TabStops, col: usize) bool { const i = col >> 3; if (i >= self.stops.len) return false; return self.stops[i] & self.mask(col) != 0;}
/// Returns the next tab stop after the given column. Mirrors Go `Next`.pub fn next(self: TabStops, col: usize) usize { return self.find(col, 1);}
/// Returns the previous tab stop before the given column. Mirrors Go/// `Prev`.pub fn prev(self: TabStops, col: usize) usize { return self.find(col, -1);}
/// Returns the prev/next tab stop before/after the given column and delta./// A positive delta advances forward by that many stops, a negative delta/// moves backward, and a zero delta returns the column unchanged. Columns/// are clamped to the screen: forward stops stop at `width - 1`, backward/// stops stop at column 0. Mirrors Go `Find`.fn find(self: TabStops, col: usize, delta: isize) usize { if (delta == 0) return col; if (self.width() == 0) return col;
var count: isize = delta; const backwards = count < 0; if (backwards) count = -count;
var c = col; while (count > 0) { if (!backwards) { if (c >= self.width() - 1) return c; c += 1; } else { if (c < 1) return c; c -= 1; } if (self.isStop(c)) count -= 1; } return c;}
/// Adds a tab stop at the given column.pub fn set(self: *TabStops, col: usize) void { self.stops[col >> 3] |= self.mask(col);}
/// Removes the tab stop at the given column.pub fn reset(self: *TabStops, col: usize) void { self.stops[col >> 3] &= ~self.mask(col);}
/// Removes all tab stops.pub fn clear(self: *TabStops) void { for (self.stops) |*s| s.* = 0;}
/// Returns the bitmask for the given column.fn mask(self: TabStops, col: usize) u32 { return @as(u32, 1) << @intCast(col & (self.interval - 1));}
/// Initializes the tab stops from `col` up to `w`: multiples of the/// interval become stops, everything else is cleared.fn initBits(self: *TabStops, col: usize, w: usize) void { var x = col; while (x < w) : (x += 1) { if (x % self.interval == 0) { self.set(x); } else { self.reset(x); } }}
test "tab stops default interval" { // default interval of 8 { var ts: TabStops = try .init(t.allocator, 24, default_tab_interval); defer ts.deinit();
try t.expect(ts.isStop(0)); // first tab stop try t.expect(!ts.isStop(7)); try t.expect(ts.isStop(8)); // second tab stop try t.expect(!ts.isStop(15)); try t.expect(ts.isStop(16)); // third tab stop try t.expect(!ts.isStop(23)); }
// custom interval of 4 { var ts: TabStops = try .init(t.allocator, 16, 4); defer ts.deinit();
try t.expect(ts.isStop(0)); try t.expect(!ts.isStop(3)); try t.expect(ts.isStop(4)); try t.expect(!ts.isStop(7)); try t.expect(ts.isStop(8)); try t.expect(ts.isStop(12)); // fourth tab stop try t.expect(!ts.isStop(15)); }}
test "tab stops set and reset" { var ts: TabStops = try .init(t.allocator, 24, default_tab_interval); defer ts.deinit();
// Set a custom tab stop. ts.set(9); try t.expect(ts.isStop(9));
// Reset a regular tab stop. ts.reset(8); try t.expect(!ts.isStop(8));}
test "tab stops navigation" { var ts: TabStops = try .init(t.allocator, 24, default_tab_interval); defer ts.deinit();
try t.expectEqual(8, ts.next(0)); try t.expectEqual(0, ts.prev(0));
try t.expectEqual(8, ts.next(4)); try t.expectEqual(0, ts.prev(4));
try t.expectEqual(16, ts.next(8)); try t.expectEqual(0, ts.prev(8));
try t.expectEqual(23, ts.next(20)); // clamps at width-1 try t.expectEqual(16, ts.prev(20));}
test "tab stops clear" { var ts: TabStops = try .init(t.allocator, 24, default_tab_interval); defer ts.deinit();
// Verify initial state. try t.expect(ts.isStop(0)); try t.expect(ts.isStop(8)); try t.expect(ts.isStop(16));
// Clear all tab stops. ts.clear();
var i: usize = 0; while (i < 24) : (i += 1) { try t.expect(!ts.isStop(i)); }}
test "tab stops resize" { // grow buffer 16 -> 24 { var ts: TabStops = try .default(t.allocator, 16); defer ts.deinit();
try t.expectEqual(16, ts.width()); try ts.resize(24); try t.expectEqual(24, ts.width());
try t.expect(ts.isStop(0)); try t.expect(ts.isStop(8)); try t.expect(ts.isStop(16)); // new tab stop try t.expect(!ts.isStop(23)); }
// same size - no change { var ts: TabStops = try .default(t.allocator, 16); defer ts.deinit();
try ts.resize(16); try t.expectEqual(16, ts.width()); try t.expect(ts.isStop(0)); try t.expect(ts.isStop(8)); try t.expect(!ts.isStop(15)); }
// resize with custom interval 8 -> 16 at interval 4 { var ts: TabStops = try .init(t.allocator, 8, 4); defer ts.deinit();
try ts.resize(16); try t.expectEqual(16, ts.width()); try t.expect(ts.isStop(0)); try t.expect(ts.isStop(4)); try t.expect(ts.isStop(8)); try t.expect(ts.isStop(12)); try t.expect(!ts.isStop(15));
try t.expectEqual(@as(usize, (16 + (ts.interval - 1)) / ts.interval), ts.stops.len); }}
test "tab stops resize edge cases" {
// resize to zero { var ts: TabStops = try .default(t.allocator, 8); defer ts.deinit();
try ts.resize(0); try t.expectEqual(0, ts.width()); try t.expect(!ts.isStop(0)); // no tab stops accessible at zero width }
// resize to very large width { var ts: TabStops = try .default(t.allocator, 8); defer ts.deinit();
try ts.resize(1000); try t.expect(ts.isStop(992)); // multiple of 8 try t.expect(!ts.isStop(999)); }
// multiple resizes { var ts: TabStops = try .default(t.allocator, 8); defer ts.deinit();
const sizes = [_]usize{ 16, 8, 24, 4 }; for (sizes) |size| { try ts.resize(size); try t.expectEqual(size, ts.width()); try t.expect(ts.isStop(0)); // first tab stop always set } }}