Changes requested.
- Rename wrap to input_needs_wrap and providing documentation. - Standardize on min. - Optimization on subtracting col.
This commit is contained in:
parent
c106ee6601
commit
5642624f40
|
@ -15,7 +15,7 @@
|
||||||
//! Exports the `Term` type which is a high-level API for the Grid
|
//! Exports the `Term` type which is a high-level API for the Grid
|
||||||
use std::ops::{Deref, Range};
|
use std::ops::{Deref, Range};
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::cmp;
|
use std::cmp::min;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
use ansi::{self, Color, NamedColor, Attr, Handler};
|
use ansi::{self, Color, NamedColor, Attr, Handler};
|
||||||
|
@ -168,8 +168,9 @@ impl<'a> Iterator for RenderableCellsIter<'a> {
|
||||||
|
|
||||||
/// coerce val to be between min and max
|
/// coerce val to be between min and max
|
||||||
#[inline]
|
#[inline]
|
||||||
fn limit<T: PartialOrd + Ord>(val: T, min: T, max: T) -> T {
|
fn limit<T: PartialOrd + Ord>(val: T, min_limit: T, max_limit: T) -> T {
|
||||||
cmp::min(cmp::max(min, val), max)
|
use std::cmp::max;
|
||||||
|
min(max(min_limit, val), max_limit)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod mode {
|
pub mod mode {
|
||||||
|
@ -202,8 +203,12 @@ pub struct Term {
|
||||||
/// The grid
|
/// The grid
|
||||||
grid: Grid<Cell>,
|
grid: Grid<Cell>,
|
||||||
|
|
||||||
/// Wrap on next input
|
/// Tracks if the next call to input will need to first handle wrapping.
|
||||||
wrap: bool,
|
/// This is true after the last column is set with the input function. Any function that
|
||||||
|
/// implicitly sets the line or column needs to set this to false to avoid wrapping twice.
|
||||||
|
/// input_needs_wrap ensures that cursor.col is always valid for use into indexing into
|
||||||
|
/// arrays. Without it we wold have to sanitize cursor.col every time we used it.
|
||||||
|
input_needs_wrap: bool,
|
||||||
|
|
||||||
/// Alternate grid
|
/// Alternate grid
|
||||||
alt_grid: Grid<Cell>,
|
alt_grid: Grid<Cell>,
|
||||||
|
@ -274,8 +279,8 @@ impl SizeInfo {
|
||||||
let line = Line(y / (self.cell_height as usize));
|
let line = Line(y / (self.cell_height as usize));
|
||||||
|
|
||||||
Some(Point {
|
Some(Point {
|
||||||
line: cmp::min(line, self.lines() - 1),
|
line: min(line, self.lines() - 1),
|
||||||
col: cmp::min(col, self.cols() - 1)
|
col: min(col, self.cols() - 1)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -300,7 +305,7 @@ impl Term {
|
||||||
|
|
||||||
Term {
|
Term {
|
||||||
dirty: false,
|
dirty: false,
|
||||||
wrap: false,
|
input_needs_wrap: false,
|
||||||
grid: grid,
|
grid: grid,
|
||||||
alt_grid: alt,
|
alt_grid: alt,
|
||||||
alt: false,
|
alt: false,
|
||||||
|
@ -352,7 +357,7 @@ impl Term {
|
||||||
) -> Option<Range<Column>> {
|
) -> Option<Range<Column>> {
|
||||||
let line = &grid[line];
|
let line = &grid[line];
|
||||||
let line_length = line.line_length();
|
let line_length = line.line_length();
|
||||||
let line_end = cmp::min(line_length, cols.end + 1);
|
let line_end = min(line_length, cols.end + 1);
|
||||||
|
|
||||||
if cols.start >= line_end {
|
if cols.start >= line_end {
|
||||||
None
|
None
|
||||||
|
@ -617,7 +622,7 @@ impl ansi::Handler for Term {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn input(&mut self, c: char) {
|
fn input(&mut self, c: char) {
|
||||||
|
|
||||||
if self.wrap {
|
if self.input_needs_wrap {
|
||||||
|
|
||||||
debug_println!("wrapping");
|
debug_println!("wrapping");
|
||||||
|
|
||||||
|
@ -638,7 +643,7 @@ impl ansi::Handler for Term {
|
||||||
}
|
}
|
||||||
|
|
||||||
self.cursor.col = Column(0);
|
self.cursor.col = Column(0);
|
||||||
self.wrap = false;
|
self.input_needs_wrap = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -650,7 +655,7 @@ impl ansi::Handler for Term {
|
||||||
if (self.cursor.col + 1) < self.grid.num_cols() {
|
if (self.cursor.col + 1) < self.grid.num_cols() {
|
||||||
self.cursor.col += 1;
|
self.cursor.col += 1;
|
||||||
} else {
|
} else {
|
||||||
self.wrap = true;
|
self.input_needs_wrap = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -658,30 +663,30 @@ impl ansi::Handler for Term {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn goto(&mut self, line: Line, col: Column) {
|
fn goto(&mut self, line: Line, col: Column) {
|
||||||
debug_println!("goto: line={}, col={}", line, col);
|
debug_println!("goto: line={}, col={}", line, col);
|
||||||
self.cursor.line = cmp::min(line, self.grid.num_lines() - 1);
|
self.cursor.line = min(line, self.grid.num_lines() - 1);
|
||||||
self.cursor.col = cmp::min(col, self.grid.num_cols() - 1);
|
self.cursor.col = min(col, self.grid.num_cols() - 1);
|
||||||
self.wrap = false;
|
self.input_needs_wrap = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn goto_line(&mut self, line: Line) {
|
fn goto_line(&mut self, line: Line) {
|
||||||
debug_println!("goto_line: {}", line);
|
debug_println!("goto_line: {}", line);
|
||||||
self.cursor.line = cmp::min(line, self.grid.num_lines() - 1);
|
self.cursor.line = min(line, self.grid.num_lines() - 1);
|
||||||
self.wrap = false;
|
self.input_needs_wrap = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn goto_col(&mut self, col: Column) {
|
fn goto_col(&mut self, col: Column) {
|
||||||
debug_println!("goto_col: {}", col);
|
debug_println!("goto_col: {}", col);
|
||||||
self.cursor.col = cmp::min(col, self.grid.num_cols() - 1);
|
self.cursor.col = min(col, self.grid.num_cols() - 1);
|
||||||
self.wrap = false;
|
self.input_needs_wrap = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn insert_blank(&mut self, count: Column) {
|
fn insert_blank(&mut self, count: Column) {
|
||||||
// Ensure inserting within terminal bounds
|
// Ensure inserting within terminal bounds
|
||||||
|
|
||||||
let count = cmp::min(count, self.size_info.cols() - self.cursor.col);
|
let count = min(count, self.size_info.cols() - self.cursor.col);
|
||||||
|
|
||||||
let source = self.cursor.col;
|
let source = self.cursor.col;
|
||||||
let destination = self.cursor.col + count;
|
let destination = self.cursor.col + count;
|
||||||
|
@ -708,29 +713,28 @@ impl ansi::Handler for Term {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn move_up(&mut self, lines: Line) {
|
fn move_up(&mut self, lines: Line) {
|
||||||
debug_println!("move_up: {}", lines);
|
debug_println!("move_up: {}", lines);
|
||||||
let lines = cmp::min(self.cursor.line, lines);
|
let lines = min(self.cursor.line, lines);
|
||||||
self.cursor.line = cmp::min(self.cursor.line - lines, self.grid.num_lines() -1);
|
self.cursor.line = min(self.cursor.line - lines, self.grid.num_lines() -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn move_down(&mut self, lines: Line) {
|
fn move_down(&mut self, lines: Line) {
|
||||||
debug_println!("move_down: {}", lines);
|
debug_println!("move_down: {}", lines);
|
||||||
self.cursor.line = cmp::min(self.cursor.line + lines, self.grid.num_lines() - 1);
|
self.cursor.line = min(self.cursor.line + lines, self.grid.num_lines() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn move_forward(&mut self, cols: Column) {
|
fn move_forward(&mut self, cols: Column) {
|
||||||
debug_println!("move_forward: {}", cols);
|
debug_println!("move_forward: {}", cols);
|
||||||
self.cursor.col = cmp::min(self.cursor.col + cols, self.grid.num_cols() - 1);
|
self.cursor.col = min(self.cursor.col + cols, self.grid.num_cols() - 1);
|
||||||
self.wrap = false;
|
self.input_needs_wrap = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn move_backward(&mut self, cols: Column) {
|
fn move_backward(&mut self, cols: Column) {
|
||||||
debug_println!("move_backward: {}", cols);
|
debug_println!("move_backward: {}", cols);
|
||||||
let cols = cmp::min(self.cursor.col, cols);
|
self.cursor.col -= min(self.cursor.col, cols);
|
||||||
self.cursor.col = cmp::min(self.cursor.col - cols, self.grid.num_cols() - 1);
|
self.input_needs_wrap = false;
|
||||||
self.wrap = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -764,7 +768,7 @@ impl ansi::Handler for Term {
|
||||||
}
|
}
|
||||||
|
|
||||||
self.cursor.col = col;
|
self.cursor.col = col;
|
||||||
self.wrap = false;
|
self.input_needs_wrap = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Backspace `count` characters
|
/// Backspace `count` characters
|
||||||
|
@ -773,7 +777,7 @@ impl ansi::Handler for Term {
|
||||||
debug_println!("backspace");
|
debug_println!("backspace");
|
||||||
if self.cursor.col > Column(0) {
|
if self.cursor.col > Column(0) {
|
||||||
self.cursor.col -= 1;
|
self.cursor.col -= 1;
|
||||||
self.wrap = false;
|
self.input_needs_wrap = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -782,7 +786,7 @@ impl ansi::Handler for Term {
|
||||||
fn carriage_return(&mut self) {
|
fn carriage_return(&mut self) {
|
||||||
debug_println!("carriage_return");
|
debug_println!("carriage_return");
|
||||||
self.cursor.col = Column(0);
|
self.cursor.col = Column(0);
|
||||||
self.wrap = false;
|
self.input_needs_wrap = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Linefeed
|
/// Linefeed
|
||||||
|
@ -851,7 +855,7 @@ impl ansi::Handler for Term {
|
||||||
fn erase_chars(&mut self, count: Column) {
|
fn erase_chars(&mut self, count: Column) {
|
||||||
debug_println!("erase_chars: {}, {}", count, self.cursor.col);
|
debug_println!("erase_chars: {}, {}", count, self.cursor.col);
|
||||||
let start = self.cursor.col;
|
let start = self.cursor.col;
|
||||||
let end = cmp::min(start + count, self.grid.num_cols() - 1);
|
let end = min(start + count, self.grid.num_cols() - 1);
|
||||||
|
|
||||||
let row = &mut self.grid[self.cursor.line];
|
let row = &mut self.grid[self.cursor.line];
|
||||||
let template = self.empty_cell;
|
let template = self.empty_cell;
|
||||||
|
@ -863,10 +867,10 @@ impl ansi::Handler for Term {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn delete_chars(&mut self, count: Column) {
|
fn delete_chars(&mut self, count: Column) {
|
||||||
// Ensure deleting within terminal bounds
|
// Ensure deleting within terminal bounds
|
||||||
let count = cmp::min(count, self.size_info.cols());
|
let count = min(count, self.size_info.cols());
|
||||||
|
|
||||||
let start = self.cursor.col;
|
let start = self.cursor.col;
|
||||||
let end = cmp::min(start + count, self.grid.num_cols() - 1);
|
let end = min(start + count, self.grid.num_cols() - 1);
|
||||||
let n = (self.size_info.cols() - end).0;
|
let n = (self.size_info.cols() - end).0;
|
||||||
|
|
||||||
let line = self.cursor.line; // borrowck
|
let line = self.cursor.line; // borrowck
|
||||||
|
|
Loading…
Reference in New Issue