Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the editor area to be bigger than u16 (width and height remain u16) #4318

Merged
merged 1 commit into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions helix-tui/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl Buffer {

/// Returns a Buffer with all cells initialized with the attributes of the given Cell
pub fn filled(area: Rect, cell: &Cell) -> Buffer {
let size = area.area() as usize;
let size = area.area();
let mut content = Vec::with_capacity(size);
for _ in 0..size {
content.push(cell.clone());
Expand Down Expand Up @@ -222,7 +222,7 @@ impl Buffer {
y,
self.area
);
((y - self.area.y) * self.area.width + (x - self.area.x)) as usize
((y - self.area.y) as usize) * (self.area.width as usize) + ((x - self.area.x) as usize)
}

/// Returns the index in the Vec<Cell> for the given global (x, y) coordinates,
Expand Down Expand Up @@ -261,8 +261,8 @@ impl Buffer {
self.content.len()
);
(
self.area.x + i as u16 % self.area.width,
self.area.y + i as u16 / self.area.width,
(self.area.x as usize + (i % self.area.width as usize)) as u16,
(self.area.y as usize + (i / self.area.width as usize)) as u16,
)
}

Expand Down Expand Up @@ -463,7 +463,7 @@ impl Buffer {
/// Resize the buffer so that the mapped area matches the given area and that the buffer
/// length is equal to area.width * area.height
pub fn resize(&mut self, area: Rect) {
let length = area.area() as usize;
let length = area.area();
if self.content.len() > length {
self.content.truncate(length);
} else {
Expand Down Expand Up @@ -570,8 +570,8 @@ impl Buffer {
let mut to_skip: usize = 0;
for (i, (current, previous)) in next_buffer.iter().zip(previous_buffer.iter()).enumerate() {
if (current != previous || invalidated > 0) && to_skip == 0 {
let x = i as u16 % width;
let y = i as u16 / width;
let x = (i % width as usize) as u16;
let y = (i / width as usize) as u16;
updates.push((x, y, &next_buffer[i]));
}

Expand Down
6 changes: 3 additions & 3 deletions helix-tui/tests/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use helix_tui::{
};

#[test]
fn terminal_buffer_size_should_be_limited() {
fn terminal_buffer_size_should_not_be_limited() {
let backend = TestBackend::new(400, 400);
let terminal = Terminal::new(backend).unwrap();
let size = terminal.backend().size().unwrap();
assert_eq!(size.width, 255);
assert_eq!(size.height, 255);
assert_eq!(size.width, 400);
assert_eq!(size.height, 400);
}

// #[test]
Expand Down
49 changes: 5 additions & 44 deletions helix-view/src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,31 +95,19 @@ pub struct Rect {
}

impl Rect {
/// Creates a new rect, with width and height limited to keep the area under max u16.
/// If clipped, aspect ratio will be preserved.
/// Creates a new rect, with width and height
pub fn new(x: u16, y: u16, width: u16, height: u16) -> Rect {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does Rect need these fields to become usizes so that Rects can be drawn on very large terminal sizes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. 65 thousand characters as a width or height is already insanely large. Also crossterm only works with u16 for x/y positions.
Looking at termwiz (which I understand we might switch to eventually), they do use usize.
I just made sure that it was fine if the area which equals width * height is bigger than 2^16, which could plausibly happen with a terminal around 256 by 256.

let max_area = u16::max_value();
let (clipped_width, clipped_height) =
if u32::from(width) * u32::from(height) > u32::from(max_area) {
let aspect_ratio = f64::from(width) / f64::from(height);
let max_area_f = f64::from(max_area);
let height_f = (max_area_f / aspect_ratio).sqrt();
let width_f = height_f * aspect_ratio;
(width_f as u16, height_f as u16)
} else {
(width, height)
};
Rect {
x,
y,
width: clipped_width,
height: clipped_height,
width,
height,
}
}

#[inline]
pub fn area(self) -> u16 {
self.width * self.height
pub fn area(self) -> usize {
(self.width as usize) * (self.height as usize)
}

#[inline]
Expand Down Expand Up @@ -552,33 +540,6 @@ impl Style {
mod tests {
use super::*;

#[test]
fn test_rect_size_truncation() {
for width in 256u16..300u16 {
for height in 256u16..300u16 {
let rect = Rect::new(0, 0, width, height);
rect.area(); // Should not panic.
assert!(rect.width < width || rect.height < height);
// The target dimensions are rounded down so the math will not be too precise
// but let's make sure the ratios don't diverge crazily.
assert!(
(f64::from(rect.width) / f64::from(rect.height)
- f64::from(width) / f64::from(height))
.abs()
< 1.0
)
}
}

// One dimension below 255, one above. Area above max u16.
let width = 900;
let height = 100;
let rect = Rect::new(0, 0, width, height);
assert_ne!(rect.width, 900);
assert_ne!(rect.height, 100);
assert!(rect.width < width || rect.height < height);
}

#[test]
fn test_rect_size_preservation() {
for width in 0..256u16 {
Expand Down