mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-15 00:06:09 +03:00
screen: fix negative scrolling from zero
This commit is contained in:
@ -100,19 +100,25 @@ pub fn rowIndex(self: Screen, idx: usize) usize {
|
|||||||
return val - self.storage.len;
|
return val - self.storage.len;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Scroll the screen up (negative) or down (positive). Scrolling direction
|
/// Scroll the screen up (positive) or down (negiatve). Scrolling direction
|
||||||
/// is the direction opposite text would move. For example, scrolling down would
|
/// is the direction text would move. For example, scrolling down would
|
||||||
/// move existing text upward. This sounds confusing but is the natural way
|
/// move existing text downward.
|
||||||
/// that humans scroll a screen.
|
|
||||||
pub fn scroll(self: *Screen, count: isize) void {
|
pub fn scroll(self: *Screen, count: isize) void {
|
||||||
|
// TODO: this math is super dumb, it doesn't work if count is a
|
||||||
|
// a multiple of rows
|
||||||
if (count < 0) {
|
if (count < 0) {
|
||||||
self.zero -|= @intCast(usize, -count);
|
const amount = @intCast(usize, -count);
|
||||||
|
if (amount > self.zero) {
|
||||||
|
self.zero = self.rows - amount;
|
||||||
|
} else {
|
||||||
|
self.zero -|= amount;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
self.zero += @intCast(usize, count);
|
self.zero += @intCast(usize, count);
|
||||||
}
|
|
||||||
if (self.zero > self.storage.len) {
|
if (self.zero > self.storage.len) {
|
||||||
self.zero -= self.storage.len;
|
self.zero -= self.storage.len;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Copy row at src to dst.
|
/// Copy row at src to dst.
|
||||||
@ -218,6 +224,26 @@ test "Screen: scrolling" {
|
|||||||
try testing.expectEqualStrings("2EFGH\n3IJKL\n1ABCD", contents);
|
try testing.expectEqualStrings("2EFGH\n3IJKL\n1ABCD", contents);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "Screen: scroll down from 0" {
|
||||||
|
const testing = std.testing;
|
||||||
|
const alloc = testing.allocator;
|
||||||
|
|
||||||
|
var s = try init(alloc, 3, 5);
|
||||||
|
defer s.deinit(alloc);
|
||||||
|
s.testWriteString("1ABCD\n2EFGH\n3IJKL");
|
||||||
|
s.scroll(-1);
|
||||||
|
|
||||||
|
// Test our row index
|
||||||
|
try testing.expectEqual(@as(usize, 10), s.rowIndex(0));
|
||||||
|
try testing.expectEqual(@as(usize, 0), s.rowIndex(1));
|
||||||
|
try testing.expectEqual(@as(usize, 5), s.rowIndex(2));
|
||||||
|
|
||||||
|
// Test our contents rotated
|
||||||
|
var contents = try s.testString(alloc);
|
||||||
|
defer alloc.free(contents);
|
||||||
|
try testing.expectEqualStrings("3IJKL\n1ABCD\n2EFGH", contents);
|
||||||
|
}
|
||||||
|
|
||||||
test "Screen: row copy" {
|
test "Screen: row copy" {
|
||||||
const testing = std.testing;
|
const testing = std.testing;
|
||||||
const alloc = testing.allocator;
|
const alloc = testing.allocator;
|
||||||
|
Reference in New Issue
Block a user