2016-06-30 03:56:12 +00:00
|
|
|
// Copyright 2016 Joe Wilm, The Alacritty Project Contributors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
//! Exports the `Term` type which is a high-level API for the Grid
|
2017-01-09 03:18:39 +00:00
|
|
|
use std::mem;
|
2017-02-11 20:49:40 +00:00
|
|
|
use std::ops::{Range, Index, IndexMut};
|
2016-09-15 15:49:55 +00:00
|
|
|
use std::ptr;
|
2017-01-08 03:46:02 +00:00
|
|
|
use std::cmp::min;
|
2016-12-04 23:48:30 +00:00
|
|
|
use std::io;
|
2017-02-03 23:34:52 +00:00
|
|
|
use std::time::{Duration, Instant};
|
2016-05-31 03:44:37 +00:00
|
|
|
|
2017-01-09 20:07:23 +00:00
|
|
|
use ansi::{self, Color, NamedColor, Attr, Handler, CharsetIndex, StandardCharset};
|
2017-02-11 20:49:40 +00:00
|
|
|
use grid::{BidirectionalIterator, Grid, ClearRegion, ToRange, Indexed};
|
2017-01-15 01:53:48 +00:00
|
|
|
use index::{self, Point, Column, Line, Linear, IndexRange, Contains, RangeInclusive, Side};
|
2016-12-27 03:52:37 +00:00
|
|
|
use selection::{Span, Selection};
|
2017-02-03 23:34:52 +00:00
|
|
|
use config::{Config, VisualBellAnimation};
|
2017-02-11 20:49:40 +00:00
|
|
|
use Rgb;
|
2016-05-31 03:44:37 +00:00
|
|
|
|
2016-11-28 22:30:08 +00:00
|
|
|
pub mod cell;
|
2017-02-11 20:49:40 +00:00
|
|
|
pub mod color;
|
2016-11-28 22:30:08 +00:00
|
|
|
pub use self::cell::Cell;
|
2016-12-27 03:52:37 +00:00
|
|
|
use self::cell::LineLength;
|
2016-11-28 22:30:08 +00:00
|
|
|
|
2016-11-28 22:13:11 +00:00
|
|
|
/// Iterator that yields cells needing render
|
|
|
|
///
|
|
|
|
/// Yields cells that require work to be displayed (that is, not a an empty
|
|
|
|
/// background cell). Additionally, this manages some state of the grid only
|
|
|
|
/// relevant for rendering like temporarily changing the cell with the cursor.
|
2016-07-04 21:46:25 +00:00
|
|
|
///
|
|
|
|
/// This manages the cursor during a render. The cursor location is inverted to
|
|
|
|
/// draw it, and reverted after drawing to maintain state.
|
2016-11-28 22:13:11 +00:00
|
|
|
pub struct RenderableCellsIter<'a> {
|
|
|
|
grid: &'a mut Grid<Cell>,
|
2016-12-29 16:09:29 +00:00
|
|
|
cursor: &'a Point,
|
2016-07-04 21:46:25 +00:00
|
|
|
mode: TermMode,
|
2016-11-28 22:13:11 +00:00
|
|
|
line: Line,
|
|
|
|
column: Column,
|
2017-02-11 20:49:40 +00:00
|
|
|
config: &'a Config,
|
|
|
|
colors: &'a color::List,
|
2016-12-22 18:43:06 +00:00
|
|
|
selection: Option<RangeInclusive<index::Linear>>,
|
2017-02-11 20:49:40 +00:00
|
|
|
cursor_original: Option<Indexed<Cell>>
|
2016-07-04 21:46:25 +00:00
|
|
|
}
|
|
|
|
|
2016-11-28 22:13:11 +00:00
|
|
|
impl<'a> RenderableCellsIter<'a> {
|
|
|
|
/// Create the renderable cells iterator
|
|
|
|
///
|
|
|
|
/// The cursor and terminal mode are required for properly displaying the
|
|
|
|
/// cursor.
|
|
|
|
fn new<'b>(
|
|
|
|
grid: &'b mut Grid<Cell>,
|
2016-12-29 16:09:29 +00:00
|
|
|
cursor: &'b Point,
|
2017-02-11 20:49:40 +00:00
|
|
|
colors: &'b color::List,
|
2016-12-22 18:43:06 +00:00
|
|
|
mode: TermMode,
|
2017-02-11 20:49:40 +00:00
|
|
|
config: &'b Config,
|
2016-12-22 18:43:06 +00:00
|
|
|
selection: &Selection,
|
2016-11-28 22:13:11 +00:00
|
|
|
) -> RenderableCellsIter<'b> {
|
2016-12-22 18:43:06 +00:00
|
|
|
let selection = selection.span()
|
|
|
|
.map(|span| span.to_range(grid.num_cols()));
|
|
|
|
|
2016-11-28 22:13:11 +00:00
|
|
|
RenderableCellsIter {
|
|
|
|
grid: grid,
|
2016-07-04 21:46:25 +00:00
|
|
|
cursor: cursor,
|
|
|
|
mode: mode,
|
2016-11-28 22:13:11 +00:00
|
|
|
line: Line(0),
|
|
|
|
column: Column(0),
|
2016-12-22 18:43:06 +00:00
|
|
|
selection: selection,
|
2017-02-11 20:49:40 +00:00
|
|
|
config: config,
|
|
|
|
colors: colors,
|
2017-01-09 03:18:39 +00:00
|
|
|
cursor_original: None,
|
2017-02-11 20:49:40 +00:00
|
|
|
}.initialize()
|
2016-11-28 22:13:11 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 20:49:40 +00:00
|
|
|
fn initialize(mut self) -> Self {
|
2016-11-28 22:13:11 +00:00
|
|
|
if self.cursor_is_visible() {
|
2017-02-11 20:49:40 +00:00
|
|
|
self.cursor_original = Some(Indexed {
|
2017-01-09 03:18:39 +00:00
|
|
|
line: self.cursor.line,
|
|
|
|
column: self.cursor.col,
|
|
|
|
inner: self.grid[self.cursor]
|
|
|
|
});
|
2017-02-11 20:49:40 +00:00
|
|
|
|
|
|
|
if self.config.custom_cursor_colors() {
|
2017-01-09 03:18:39 +00:00
|
|
|
let cell = &mut self.grid[self.cursor];
|
|
|
|
cell.fg = Color::Named(NamedColor::CursorForeground);
|
|
|
|
cell.bg = Color::Named(NamedColor::CursorBackground);
|
2016-11-28 22:13:11 +00:00
|
|
|
|
2017-01-09 03:18:39 +00:00
|
|
|
} else {
|
|
|
|
let cell = &mut self.grid[self.cursor];
|
|
|
|
mem::swap(&mut cell.fg, &mut cell.bg);
|
|
|
|
}
|
|
|
|
}
|
2016-11-28 22:13:11 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if the cursor should be rendered.
|
|
|
|
#[inline]
|
|
|
|
fn cursor_is_visible(&self) -> bool {
|
|
|
|
self.mode.contains(mode::SHOW_CURSOR) && self.grid.contains(self.cursor)
|
2016-07-04 21:46:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-28 22:13:11 +00:00
|
|
|
impl<'a> Drop for RenderableCellsIter<'a> {
|
|
|
|
/// Resets temporary render state on the grid
|
2016-07-04 21:46:25 +00:00
|
|
|
fn drop(&mut self) {
|
2016-11-28 22:13:11 +00:00
|
|
|
if self.cursor_is_visible() {
|
2017-01-09 03:18:39 +00:00
|
|
|
if let Some(ref original) = self.cursor_original {
|
|
|
|
self.grid[self.cursor] = original.inner;
|
|
|
|
}
|
2016-07-04 21:46:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 20:49:40 +00:00
|
|
|
pub struct RenderableCell {
|
2016-11-28 22:13:11 +00:00
|
|
|
pub line: Line,
|
|
|
|
pub column: Column,
|
2017-02-11 20:49:40 +00:00
|
|
|
pub c: char,
|
|
|
|
pub fg: Rgb,
|
|
|
|
pub bg: Rgb,
|
|
|
|
pub flags: cell::Flags,
|
2016-11-28 22:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for RenderableCellsIter<'a> {
|
2017-02-11 20:49:40 +00:00
|
|
|
type Item = RenderableCell;
|
2016-11-28 22:13:11 +00:00
|
|
|
|
|
|
|
/// Gets the next renderable cell
|
|
|
|
///
|
|
|
|
/// Skips empty (background) cells and applies any flags to the cell state
|
|
|
|
/// (eg. invert fg and bg colors).
|
2016-12-17 06:13:51 +00:00
|
|
|
#[inline]
|
2016-11-28 22:13:11 +00:00
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
while self.line < self.grid.num_lines() {
|
|
|
|
while self.column < self.grid.num_cols() {
|
|
|
|
// Grab current state for this iteration
|
|
|
|
let line = self.line;
|
|
|
|
let column = self.column;
|
|
|
|
let cell = &self.grid[line][column];
|
|
|
|
|
2016-12-22 18:43:06 +00:00
|
|
|
let index = Linear(line.0 * self.grid.num_cols().0 + column.0);
|
|
|
|
|
2016-11-28 22:13:11 +00:00
|
|
|
// Update state for next iteration
|
|
|
|
self.column += 1;
|
|
|
|
|
2016-12-22 18:43:06 +00:00
|
|
|
let selected = self.selection.as_ref()
|
2017-01-06 23:48:23 +00:00
|
|
|
.map(|range| range.contains_(index))
|
2016-12-22 18:43:06 +00:00
|
|
|
.unwrap_or(false);
|
|
|
|
|
2016-11-28 22:13:11 +00:00
|
|
|
// Skip empty cells
|
2016-12-22 18:43:06 +00:00
|
|
|
if cell.is_empty() && !selected {
|
2016-11-28 22:13:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fg, bg are dependent on INVERSE flag
|
2016-12-22 18:43:06 +00:00
|
|
|
let invert = cell.flags.contains(cell::INVERSE) || selected;
|
|
|
|
let (fg, bg) = if invert {
|
2016-11-28 22:13:11 +00:00
|
|
|
(&cell.bg, &cell.fg)
|
|
|
|
} else {
|
|
|
|
(&cell.fg, &cell.bg)
|
|
|
|
};
|
|
|
|
|
2017-02-11 20:49:40 +00:00
|
|
|
// Get Rgb value for foreground
|
|
|
|
let fg = match *fg {
|
|
|
|
Color::Spec(rgb) => rgb,
|
|
|
|
Color::Named(ansi) => {
|
|
|
|
if self.config.draw_bold_text_with_bright_colors() && cell.bold() {
|
|
|
|
self.colors[ansi.to_bright()]
|
|
|
|
} else {
|
|
|
|
self.colors[ansi]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Color::Indexed(idx) => {
|
|
|
|
let idx = if self.config.draw_bold_text_with_bright_colors()
|
|
|
|
&& cell.bold()
|
|
|
|
&& idx < 8
|
|
|
|
{
|
|
|
|
idx + 8
|
|
|
|
} else {
|
|
|
|
idx
|
|
|
|
};
|
|
|
|
|
|
|
|
self.colors[idx]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Get Rgb value for background
|
|
|
|
let bg = match *bg {
|
|
|
|
Color::Spec(rgb) => rgb,
|
|
|
|
Color::Named(ansi) => self.colors[ansi],
|
|
|
|
Color::Indexed(idx) => self.colors[idx],
|
|
|
|
};
|
|
|
|
|
|
|
|
return Some(RenderableCell {
|
2016-11-28 22:13:11 +00:00
|
|
|
line: line,
|
|
|
|
column: column,
|
2017-02-11 20:49:40 +00:00
|
|
|
flags: cell.flags,
|
|
|
|
c: cell.c,
|
|
|
|
fg: fg,
|
|
|
|
bg: bg,
|
2016-11-28 22:13:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
self.column = Column(0);
|
|
|
|
self.line += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
2016-07-04 21:46:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-29 17:21:02 +00:00
|
|
|
/// coerce val to be between min and max
|
2016-11-25 20:03:11 +00:00
|
|
|
#[inline]
|
2017-01-08 03:46:02 +00:00
|
|
|
fn limit<T: PartialOrd + Ord>(val: T, min_limit: T, max_limit: T) -> T {
|
|
|
|
use std::cmp::max;
|
|
|
|
min(max(min_limit, val), max_limit)
|
2016-06-29 17:21:02 +00:00
|
|
|
}
|
|
|
|
|
2016-06-08 04:17:48 +00:00
|
|
|
pub mod mode {
|
|
|
|
bitflags! {
|
2016-06-23 16:42:00 +00:00
|
|
|
pub flags TermMode: u8 {
|
2016-11-24 04:25:37 +00:00
|
|
|
const SHOW_CURSOR = 0b00000001,
|
|
|
|
const APP_CURSOR = 0b00000010,
|
|
|
|
const APP_KEYPAD = 0b00000100,
|
|
|
|
const MOUSE_REPORT_CLICK = 0b00001000,
|
2016-11-28 22:39:37 +00:00
|
|
|
const BRACKETED_PASTE = 0b00010000,
|
2017-01-02 02:28:49 +00:00
|
|
|
const SGR_MOUSE = 0b00100000,
|
|
|
|
const MOUSE_MOTION = 0b01000000,
|
2017-01-12 03:00:29 +00:00
|
|
|
const LINE_WRAP = 0b10000000,
|
2016-11-24 04:25:37 +00:00
|
|
|
const ANY = 0b11111111,
|
|
|
|
const NONE = 0b00000000,
|
2016-06-23 16:42:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for TermMode {
|
|
|
|
fn default() -> TermMode {
|
2017-01-12 03:00:29 +00:00
|
|
|
SHOW_CURSOR | LINE_WRAP
|
2016-06-08 04:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub use self::mode::TermMode;
|
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
pub const TAB_SPACES: usize = 8;
|
|
|
|
|
2017-01-09 20:07:23 +00:00
|
|
|
trait CharsetMapping {
|
|
|
|
fn map(&self, c: char) -> char {
|
|
|
|
c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CharsetMapping for StandardCharset {
|
|
|
|
/// Switch/Map character to the active charset. Ascii is the common case and
|
|
|
|
/// for that we want to do as little as possible.
|
|
|
|
#[inline]
|
|
|
|
fn map(&self, c: char) -> char {
|
|
|
|
match *self {
|
|
|
|
StandardCharset::Ascii => c,
|
|
|
|
StandardCharset::SpecialCharacterAndLineDrawing =>
|
|
|
|
match c {
|
|
|
|
'`' => '◆',
|
|
|
|
'a' => '▒',
|
|
|
|
'b' => '\t',
|
|
|
|
'c' => '\u{000c}',
|
|
|
|
'd' => '\r',
|
|
|
|
'e' => '\n',
|
|
|
|
'f' => '°',
|
|
|
|
'g' => '±',
|
|
|
|
'h' => '\u{2424}',
|
|
|
|
'i' => '\u{000b}',
|
|
|
|
'j' => '┘',
|
|
|
|
'k' => '┐',
|
|
|
|
'l' => '┌',
|
|
|
|
'm' => '└',
|
|
|
|
'n' => '┼',
|
|
|
|
'o' => '⎺',
|
|
|
|
'p' => '⎻',
|
|
|
|
'q' => '─',
|
|
|
|
'r' => '⎼',
|
|
|
|
's' => '⎽',
|
|
|
|
't' => '├',
|
|
|
|
'u' => '┤',
|
|
|
|
'v' => '┴',
|
|
|
|
'w' => '┬',
|
|
|
|
'x' => '│',
|
|
|
|
'y' => '≤',
|
|
|
|
'z' => '≥',
|
|
|
|
'{' => 'π',
|
|
|
|
'|' => '≠',
|
|
|
|
'}' => '£',
|
|
|
|
'~' => '·',
|
|
|
|
_ => c
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 17:55:23 +00:00
|
|
|
#[derive(Default, Copy, Clone)]
|
2017-01-09 20:07:23 +00:00
|
|
|
struct Charsets([StandardCharset; 4]);
|
|
|
|
|
|
|
|
impl Index<CharsetIndex> for Charsets {
|
|
|
|
type Output = StandardCharset;
|
|
|
|
fn index(&self, index: CharsetIndex) -> &StandardCharset {
|
|
|
|
&self.0[index as usize]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IndexMut<CharsetIndex> for Charsets {
|
|
|
|
fn index_mut(&mut self, index: CharsetIndex) -> &mut StandardCharset {
|
|
|
|
&mut self.0[index as usize]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 17:55:23 +00:00
|
|
|
#[derive(Default, Copy, Clone)]
|
|
|
|
pub struct Cursor {
|
|
|
|
/// The location of this cursor
|
|
|
|
point: Point,
|
|
|
|
|
|
|
|
/// Template cell when using this cursor
|
|
|
|
template: Cell,
|
|
|
|
|
|
|
|
/// Currently configured graphic character sets
|
|
|
|
charsets: Charsets,
|
|
|
|
}
|
|
|
|
|
2017-02-03 23:34:52 +00:00
|
|
|
pub struct VisualBell {
|
|
|
|
/// Visual bell animation
|
|
|
|
animation: VisualBellAnimation,
|
|
|
|
|
|
|
|
/// Visual bell duration
|
|
|
|
duration: Duration,
|
|
|
|
|
|
|
|
/// The last time the visual bell rang, if at all
|
|
|
|
start_time: Option<Instant>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cubic_bezier(p0: f64, p1: f64, p2: f64, p3: f64, x: f64) -> f64 {
|
|
|
|
(1.0 - x).powi(3) * p0 +
|
|
|
|
3.0 * (1.0 - x).powi(2) * x * p1 +
|
|
|
|
3.0 * (1.0 - x) * x.powi(2) * p2 +
|
|
|
|
x.powi(3) * p3
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VisualBell {
|
|
|
|
pub fn new(config: &Config) -> VisualBell {
|
|
|
|
let visual_bell_config = config.visual_bell();
|
|
|
|
VisualBell {
|
|
|
|
animation: visual_bell_config.animation(),
|
|
|
|
duration: visual_bell_config.duration(),
|
|
|
|
start_time: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Ring the visual bell, and return its intensity.
|
|
|
|
pub fn ring(&mut self) -> f64 {
|
|
|
|
let now = Instant::now();
|
|
|
|
self.start_time = Some(now);
|
|
|
|
self.intensity_at_instant(now)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the currenty intensity of the visual bell. The bell's intensity
|
|
|
|
/// ramps down from 1.0 to 0.0 at a rate determined by the bell's duration.
|
|
|
|
pub fn intensity(&self) -> f64 {
|
|
|
|
self.intensity_at_instant(Instant::now())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check whether or not the visual bell has completed "ringing".
|
2017-02-20 20:22:26 +00:00
|
|
|
pub fn completed(&mut self) -> bool {
|
2017-02-03 23:34:52 +00:00
|
|
|
match self.start_time {
|
2017-02-20 20:22:26 +00:00
|
|
|
Some(earlier) => {
|
|
|
|
if Instant::now().duration_since(earlier) >= self.duration {
|
|
|
|
self.start_time = None;
|
|
|
|
}
|
|
|
|
false
|
|
|
|
},
|
2017-02-03 23:34:52 +00:00
|
|
|
None => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the intensity of the visual bell at a particular instant. The bell's
|
|
|
|
/// intensity ramps down from 1.0 to 0.0 at a rate determined by the bell's
|
|
|
|
/// duration.
|
|
|
|
pub fn intensity_at_instant(&self, instant: Instant) -> f64 {
|
|
|
|
// If `duration` is zero, then the VisualBell is disabled; therefore,
|
|
|
|
// its `intensity` is zero.
|
|
|
|
if self.duration == Duration::from_secs(0) {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.start_time {
|
|
|
|
// Similarly, if `start_time` is `None`, then the VisualBell has not
|
|
|
|
// been "rung"; therefore, its `intensity` is zero.
|
|
|
|
None => 0.0,
|
|
|
|
|
|
|
|
Some(earlier) => {
|
|
|
|
// Finally, if the `instant` at which we wish to compute the
|
|
|
|
// VisualBell's `intensity` occurred before the VisualBell was
|
|
|
|
// "rung", then its `intensity` is also zero.
|
|
|
|
if instant < earlier {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
let elapsed = instant.duration_since(earlier);
|
|
|
|
let elapsed_f = elapsed.as_secs() as f64 +
|
|
|
|
elapsed.subsec_nanos() as f64 / 1e9f64;
|
|
|
|
let duration_f = self.duration.as_secs() as f64 +
|
|
|
|
self.duration.subsec_nanos() as f64 / 1e9f64;
|
|
|
|
|
|
|
|
// Otherwise, we compute a value `time` from 0.0 to 1.0
|
|
|
|
// inclusive that represents the ratio of `elapsed` time to the
|
|
|
|
// `duration` of the VisualBell.
|
|
|
|
let time = (elapsed_f / duration_f).min(1.0);
|
|
|
|
|
|
|
|
// We use this to compute the inverse `intensity` of the
|
|
|
|
// VisualBell. When `time` is 0.0, `inverse_intensity` is 0.0,
|
|
|
|
// and when `time` is 1.0, `inverse_intensity` is 1.0.
|
|
|
|
let inverse_intensity = match self.animation {
|
|
|
|
VisualBellAnimation::Ease => cubic_bezier(0.25, 0.1, 0.25, 1.0, time),
|
|
|
|
VisualBellAnimation::EaseOut => cubic_bezier(0.25, 0.1, 0.25, 1.0, time),
|
|
|
|
VisualBellAnimation::EaseOutSine => cubic_bezier(0.39, 0.575, 0.565, 1.0, time),
|
|
|
|
VisualBellAnimation::EaseOutQuad => cubic_bezier(0.25, 0.46, 0.45, 0.94, time),
|
|
|
|
VisualBellAnimation::EaseOutCubic => cubic_bezier(0.215, 0.61, 0.355, 1.0, time),
|
|
|
|
VisualBellAnimation::EaseOutQuart => cubic_bezier(0.165, 0.84, 0.44, 1.0, time),
|
|
|
|
VisualBellAnimation::EaseOutQuint => cubic_bezier(0.23, 1.0, 0.32, 1.0, time),
|
|
|
|
VisualBellAnimation::EaseOutExpo => cubic_bezier(0.19, 1.0, 0.22, 1.0, time),
|
|
|
|
VisualBellAnimation::EaseOutCirc => cubic_bezier(0.075, 0.82, 0.165, 1.0, time),
|
|
|
|
VisualBellAnimation::Linear => time,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Since we want the `intensity` of the VisualBell to decay over
|
|
|
|
// `time`, we subtract the `inverse_intensity` from 1.0.
|
|
|
|
1.0 - inverse_intensity
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_config(&mut self, config: &Config) {
|
|
|
|
let visual_bell_config = config.visual_bell();
|
|
|
|
self.animation = visual_bell_config.animation();
|
|
|
|
self.duration = visual_bell_config.duration();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
pub struct Term {
|
|
|
|
/// The grid
|
2016-07-04 00:00:00 +00:00
|
|
|
grid: Grid<Cell>,
|
2016-05-31 03:44:37 +00:00
|
|
|
|
2017-01-12 03:00:29 +00:00
|
|
|
/// Tracks if the next call to input will need to first handle wrapping.
|
2017-01-08 03:46:02 +00:00
|
|
|
/// 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
|
2017-01-12 03:00:29 +00:00
|
|
|
/// arrays. Without it we wold have to sanitize cursor.col every time we used it.
|
|
|
|
input_needs_wrap: bool,
|
2017-01-07 23:13:42 +00:00
|
|
|
|
2017-01-11 05:21:19 +00:00
|
|
|
/// Got a request to set title; it's buffered here until next draw.
|
|
|
|
///
|
|
|
|
/// Would be nice to avoid the allocation...
|
|
|
|
next_title: Option<String>,
|
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
/// Alternate grid
|
2016-07-04 00:00:00 +00:00
|
|
|
alt_grid: Grid<Cell>,
|
2016-05-31 03:44:37 +00:00
|
|
|
|
|
|
|
/// Alt is active
|
|
|
|
alt: bool,
|
|
|
|
|
|
|
|
/// The cursor
|
2017-02-01 17:55:23 +00:00
|
|
|
cursor: Cursor,
|
2017-01-09 20:07:23 +00:00
|
|
|
|
|
|
|
/// The graphic character set, out of `charsets`, which ASCII is currently
|
|
|
|
/// being mapped to
|
|
|
|
active_charset: CharsetIndex,
|
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
/// Tabstops
|
2016-06-06 23:54:15 +00:00
|
|
|
tabs: Vec<bool>,
|
|
|
|
|
2016-06-08 04:17:48 +00:00
|
|
|
/// Mode flags
|
|
|
|
mode: TermMode,
|
2016-06-08 17:39:49 +00:00
|
|
|
|
|
|
|
/// Scroll region
|
2016-07-04 00:00:00 +00:00
|
|
|
scroll_region: Range<Line>,
|
2016-06-28 16:18:54 +00:00
|
|
|
|
|
|
|
/// Size
|
|
|
|
size_info: SizeInfo,
|
2016-07-30 03:16:13 +00:00
|
|
|
|
2016-10-15 22:56:27 +00:00
|
|
|
/// Empty cell
|
|
|
|
empty_cell: Cell,
|
|
|
|
|
2016-09-01 17:24:20 +00:00
|
|
|
pub dirty: bool,
|
2017-01-09 03:18:39 +00:00
|
|
|
|
2017-02-03 23:34:52 +00:00
|
|
|
pub visual_bell: VisualBell,
|
|
|
|
|
2017-02-01 17:55:23 +00:00
|
|
|
/// Saved cursor from main grid
|
|
|
|
cursor_save: Cursor,
|
|
|
|
|
|
|
|
/// Saved cursor from alt grid
|
|
|
|
cursor_save_alt: Cursor,
|
2017-01-15 01:53:48 +00:00
|
|
|
|
|
|
|
semantic_escape_chars: String,
|
2017-02-11 20:49:40 +00:00
|
|
|
|
|
|
|
/// Colors used for rendering
|
|
|
|
colors: color::List,
|
2016-06-28 16:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Terminal size info
|
2016-11-20 00:16:20 +00:00
|
|
|
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
|
2016-06-28 16:18:54 +00:00
|
|
|
pub struct SizeInfo {
|
|
|
|
/// Terminal window width
|
|
|
|
pub width: f32,
|
|
|
|
|
|
|
|
/// Terminal window height
|
|
|
|
pub height: f32,
|
|
|
|
|
|
|
|
/// Width of individual cell
|
|
|
|
pub cell_width: f32,
|
|
|
|
|
|
|
|
/// Height of individual cell
|
|
|
|
pub cell_height: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SizeInfo {
|
|
|
|
#[inline]
|
2016-07-04 00:00:00 +00:00
|
|
|
pub fn lines(&self) -> Line {
|
|
|
|
Line((self.height / self.cell_height) as usize)
|
2016-06-28 16:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2016-07-04 00:00:00 +00:00
|
|
|
pub fn cols(&self) -> Column {
|
|
|
|
Column((self.width / self.cell_width) as usize)
|
2016-06-28 16:18:54 +00:00
|
|
|
}
|
2016-12-12 06:02:03 +00:00
|
|
|
|
2016-12-30 01:39:30 +00:00
|
|
|
pub fn pixels_to_coords(&self, x: usize, y: usize) -> Option<Point> {
|
2016-12-12 06:02:03 +00:00
|
|
|
if x > self.width as usize || y > self.height as usize {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let col = Column(x / (self.cell_width as usize));
|
|
|
|
let line = Line(y / (self.cell_height as usize));
|
|
|
|
|
2016-12-30 01:39:30 +00:00
|
|
|
Some(Point {
|
2017-01-08 03:46:02 +00:00
|
|
|
line: min(line, self.lines() - 1),
|
|
|
|
col: min(col, self.cols() - 1)
|
2016-12-30 01:39:30 +00:00
|
|
|
})
|
2016-12-12 06:02:03 +00:00
|
|
|
}
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 20:49:40 +00:00
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
impl Term {
|
2017-01-11 05:21:19 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn get_next_title(&mut self) -> Option<String> {
|
|
|
|
self.next_title.take()
|
|
|
|
}
|
|
|
|
|
2017-01-09 03:18:39 +00:00
|
|
|
pub fn new(config : &Config, size: SizeInfo) -> Term {
|
2016-12-30 02:43:55 +00:00
|
|
|
let template = Cell::default();
|
2016-07-30 03:16:13 +00:00
|
|
|
|
2016-06-28 16:18:54 +00:00
|
|
|
let num_cols = size.cols();
|
2016-07-04 00:00:00 +00:00
|
|
|
let num_lines = size.lines();
|
2016-06-28 16:18:54 +00:00
|
|
|
|
2016-10-23 22:37:06 +00:00
|
|
|
let grid = Grid::new(num_lines, num_cols, &template);
|
2016-06-28 16:18:54 +00:00
|
|
|
|
2017-01-06 23:25:04 +00:00
|
|
|
let mut tabs = IndexRange::from(Column(0)..grid.num_cols())
|
2016-07-30 03:16:13 +00:00
|
|
|
.map(|i| (*i as usize) % TAB_SPACES == 0)
|
|
|
|
.collect::<Vec<bool>>();
|
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
tabs[0] = false;
|
|
|
|
|
|
|
|
let alt = grid.clone();
|
2016-07-04 00:00:00 +00:00
|
|
|
let scroll_region = Line(0)..grid.num_lines();
|
2016-05-31 03:44:37 +00:00
|
|
|
|
|
|
|
Term {
|
2017-01-11 05:21:19 +00:00
|
|
|
next_title: None,
|
2016-12-30 02:38:22 +00:00
|
|
|
dirty: false,
|
2017-02-03 23:34:52 +00:00
|
|
|
visual_bell: VisualBell::new(config),
|
2017-01-12 03:00:29 +00:00
|
|
|
input_needs_wrap: false,
|
2016-05-31 03:44:37 +00:00
|
|
|
grid: grid,
|
|
|
|
alt_grid: alt,
|
|
|
|
alt: false,
|
2017-02-01 17:55:23 +00:00
|
|
|
active_charset: Default::default(),
|
|
|
|
cursor: Default::default(),
|
|
|
|
cursor_save: Default::default(),
|
|
|
|
cursor_save_alt: Default::default(),
|
2016-05-31 03:44:37 +00:00
|
|
|
tabs: tabs,
|
2016-06-23 16:42:00 +00:00
|
|
|
mode: Default::default(),
|
2016-06-08 17:39:49 +00:00
|
|
|
scroll_region: scroll_region,
|
2016-07-30 03:16:13 +00:00
|
|
|
size_info: size,
|
2016-10-15 22:56:27 +00:00
|
|
|
empty_cell: template,
|
2017-02-11 20:49:40 +00:00
|
|
|
colors: color::List::from(config.colors()),
|
2017-01-15 01:53:48 +00:00
|
|
|
semantic_escape_chars: config.selection().semantic_escape_chars.clone(),
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 03:18:39 +00:00
|
|
|
pub fn update_config(&mut self, config: &Config) {
|
2017-01-15 01:53:48 +00:00
|
|
|
self.semantic_escape_chars = config.selection().semantic_escape_chars.clone();
|
2017-02-11 20:49:40 +00:00
|
|
|
self.colors.fill_named(config.colors());
|
2017-02-03 23:34:52 +00:00
|
|
|
self.visual_bell.update_config(config);
|
2017-01-09 03:18:39 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 02:38:22 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn needs_draw(&self) -> bool {
|
|
|
|
self.dirty
|
|
|
|
}
|
|
|
|
|
2017-01-15 01:53:48 +00:00
|
|
|
pub fn line_selection(&self, selection: &mut Selection, point: Point) {
|
|
|
|
selection.clear();
|
|
|
|
selection.update(Point {
|
|
|
|
line: point.line,
|
|
|
|
col: Column(0),
|
|
|
|
}, Side::Left);
|
|
|
|
selection.update(Point {
|
|
|
|
line: point.line,
|
|
|
|
col: self.grid.num_cols() - Column(1),
|
|
|
|
}, Side::Right);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn semantic_selection(&self, selection: &mut Selection, point: Point) {
|
|
|
|
let mut side_left = Point {
|
|
|
|
line: point.line,
|
|
|
|
col: point.col
|
|
|
|
};
|
|
|
|
let mut side_right = Point {
|
|
|
|
line: point.line,
|
|
|
|
col: point.col
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut left_iter = self.grid.iter_from(point);
|
|
|
|
let mut right_iter = self.grid.iter_from(point);
|
|
|
|
|
|
|
|
let last_col = self.grid.num_cols() - Column(1);
|
|
|
|
|
|
|
|
while let Some(cell) = left_iter.prev() {
|
|
|
|
if self.semantic_escape_chars.contains(cell.c) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if left_iter.cur.col == last_col && !cell.flags.contains(cell::WRAPLINE) {
|
|
|
|
break; // cut off if on new line or hit escape char
|
|
|
|
}
|
|
|
|
|
|
|
|
side_left.col = left_iter.cur.col;
|
|
|
|
side_left.line = left_iter.cur.line;
|
|
|
|
}
|
|
|
|
|
|
|
|
while let Some(cell) = right_iter.next() {
|
|
|
|
if self.semantic_escape_chars.contains(cell.c) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
side_right.col = right_iter.cur.col;
|
|
|
|
side_right.line = right_iter.cur.line;
|
|
|
|
|
|
|
|
if right_iter.cur.col == last_col && !cell.flags.contains(cell::WRAPLINE) {
|
|
|
|
break; // cut off if on new line or hit escape char
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
selection.clear();
|
|
|
|
selection.update(side_left, Side::Left);
|
|
|
|
selection.update(side_right, Side::Right);
|
|
|
|
}
|
|
|
|
|
2016-12-27 03:52:37 +00:00
|
|
|
pub fn string_from_selection(&self, span: &Span) -> String {
|
2016-12-29 15:43:58 +00:00
|
|
|
/// Need a generic push() for the Append trait
|
|
|
|
trait PushChar {
|
|
|
|
fn push_char(&mut self, c: char);
|
|
|
|
fn maybe_newline(&mut self, grid: &Grid<Cell>, line: Line, ending: Column) {
|
|
|
|
if ending != Column(0) && !grid[line][ending - 1].flags.contains(cell::WRAPLINE) {
|
|
|
|
self.push_char('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PushChar for String {
|
|
|
|
#[inline]
|
|
|
|
fn push_char(&mut self, c: char) {
|
|
|
|
self.push(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
trait Append<T> : PushChar {
|
2016-12-30 01:39:30 +00:00
|
|
|
fn append(&mut self, grid: &Grid<Cell>, line: Line, cols: T) -> Option<Range<Column>>;
|
2016-12-27 03:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
use std::ops::{Range, RangeTo, RangeFrom, RangeFull};
|
|
|
|
|
|
|
|
impl Append<Range<Column>> for String {
|
2016-12-30 01:39:30 +00:00
|
|
|
fn append(
|
|
|
|
&mut self,
|
|
|
|
grid: &Grid<Cell>,
|
|
|
|
line: Line,
|
|
|
|
cols: Range<Column>
|
|
|
|
) -> Option<Range<Column>> {
|
2016-12-27 03:52:37 +00:00
|
|
|
let line = &grid[line];
|
|
|
|
let line_length = line.line_length();
|
2017-01-08 03:46:02 +00:00
|
|
|
let line_end = min(line_length, cols.end + 1);
|
2016-12-29 15:43:58 +00:00
|
|
|
|
2016-12-30 01:39:30 +00:00
|
|
|
if cols.start >= line_end {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
for cell in &line[cols.start..line_end] {
|
|
|
|
self.push(cell.c);
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(cols.start..line_end)
|
|
|
|
}
|
2016-12-27 03:52:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Append<RangeTo<Column>> for String {
|
|
|
|
#[inline]
|
2016-12-30 01:39:30 +00:00
|
|
|
fn append(&mut self, grid: &Grid<Cell>, line: Line, cols: RangeTo<Column>) -> Option<Range<Column>> {
|
2016-12-29 15:43:58 +00:00
|
|
|
self.append(grid, line, Column(0)..cols.end)
|
2016-12-27 03:52:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Append<RangeFrom<Column>> for String {
|
|
|
|
#[inline]
|
2016-12-30 01:39:30 +00:00
|
|
|
fn append(
|
|
|
|
&mut self,
|
|
|
|
grid: &Grid<Cell>,
|
|
|
|
line: Line,
|
|
|
|
cols: RangeFrom<Column>
|
|
|
|
) -> Option<Range<Column>> {
|
2016-12-29 15:43:58 +00:00
|
|
|
let range = self.append(grid, line, cols.start..Column(usize::max_value() - 1));
|
2016-12-30 01:39:30 +00:00
|
|
|
range.as_ref()
|
|
|
|
.map(|range| self.maybe_newline(grid, line, range.end));
|
2016-12-29 15:43:58 +00:00
|
|
|
range
|
2016-12-27 03:52:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Append<RangeFull> for String {
|
|
|
|
#[inline]
|
2016-12-30 01:39:30 +00:00
|
|
|
fn append(
|
|
|
|
&mut self,
|
|
|
|
grid: &Grid<Cell>,
|
|
|
|
line: Line,
|
|
|
|
_: RangeFull
|
|
|
|
) -> Option<Range<Column>> {
|
2016-12-29 15:43:58 +00:00
|
|
|
let range = self.append(grid, line, Column(0)..Column(usize::max_value() - 1));
|
2016-12-30 01:39:30 +00:00
|
|
|
range.as_ref()
|
|
|
|
.map(|range| self.maybe_newline(grid, line, range.end));
|
2016-12-29 15:43:58 +00:00
|
|
|
range
|
2016-12-27 03:52:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut res = String::new();
|
|
|
|
|
|
|
|
let (start, end) = span.to_locations(self.grid.num_cols());
|
|
|
|
let line_count = end.line - start.line;
|
|
|
|
|
|
|
|
match line_count {
|
|
|
|
// Selection within single line
|
|
|
|
Line(0) => {
|
|
|
|
res.append(&self.grid, start.line, start.col..end.col);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Selection ends on line following start
|
|
|
|
Line(1) => {
|
|
|
|
// Starting line
|
|
|
|
res.append(&self.grid, start.line, start.col..);
|
|
|
|
|
|
|
|
// Ending line
|
|
|
|
res.append(&self.grid, end.line, ..end.col);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Multi line selection
|
|
|
|
_ => {
|
|
|
|
// Starting line
|
|
|
|
res.append(&self.grid, start.line, start.col..);
|
|
|
|
|
2017-01-06 23:25:04 +00:00
|
|
|
let middle_range = IndexRange::from((start.line + 1)..(end.line));
|
2016-12-27 03:52:37 +00:00
|
|
|
for line in middle_range {
|
|
|
|
res.append(&self.grid, line, ..);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ending line
|
|
|
|
res.append(&self.grid, end.line, ..end.col);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2016-11-24 04:25:37 +00:00
|
|
|
/// Convert the given pixel values to a grid coordinate
|
|
|
|
///
|
|
|
|
/// The mouse coordinates are expected to be relative to the top left. The
|
|
|
|
/// line and column returned are also relative to the top left.
|
|
|
|
///
|
|
|
|
/// Returns None if the coordinates are outside the screen
|
2016-12-30 01:39:30 +00:00
|
|
|
pub fn pixels_to_coords(&self, x: usize, y: usize) -> Option<Point> {
|
2016-12-12 06:02:03 +00:00
|
|
|
self.size_info().pixels_to_coords(x, y)
|
2016-11-24 04:25:37 +00:00
|
|
|
}
|
|
|
|
|
2016-11-28 22:27:52 +00:00
|
|
|
/// Access to the raw grid data structure
|
|
|
|
///
|
|
|
|
/// This is a bit of a hack; when the window is closed, the event processor
|
|
|
|
/// serializes the grid state to a file.
|
2016-11-20 00:16:20 +00:00
|
|
|
pub fn grid(&self) -> &Grid<Cell> {
|
|
|
|
&self.grid
|
|
|
|
}
|
|
|
|
|
2016-11-28 22:27:52 +00:00
|
|
|
/// Iterate over the *renderable* cells in the terminal
|
|
|
|
///
|
|
|
|
/// A renderable cell is any cell which has content other than the default
|
|
|
|
/// background color. Cells with an alternate background color are
|
|
|
|
/// considered renderable as are cells with any text content.
|
2017-02-11 20:49:40 +00:00
|
|
|
pub fn renderable_cells<'b>(
|
|
|
|
&'b mut self,
|
|
|
|
config: &'b Config,
|
|
|
|
selection: &'b Selection
|
|
|
|
) -> RenderableCellsIter {
|
2017-02-01 17:55:23 +00:00
|
|
|
RenderableCellsIter::new(
|
|
|
|
&mut self.grid,
|
|
|
|
&self.cursor.point,
|
2017-02-11 20:49:40 +00:00
|
|
|
&self.colors,
|
2017-02-01 17:55:23 +00:00
|
|
|
self.mode,
|
2017-02-11 20:49:40 +00:00
|
|
|
config,
|
2017-02-01 17:55:23 +00:00
|
|
|
selection,
|
|
|
|
)
|
2016-07-04 21:46:25 +00:00
|
|
|
}
|
|
|
|
|
2016-06-29 17:21:02 +00:00
|
|
|
/// Resize terminal to new dimensions
|
|
|
|
pub fn resize(&mut self, width: f32, height: f32) {
|
|
|
|
let size = SizeInfo {
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
cell_width: self.size_info.cell_width,
|
|
|
|
cell_height: self.size_info.cell_height,
|
|
|
|
};
|
|
|
|
|
|
|
|
let old_cols = self.size_info.cols();
|
2016-07-04 00:00:00 +00:00
|
|
|
let old_lines = self.size_info.lines();
|
2017-01-08 00:07:03 +00:00
|
|
|
let mut num_cols = size.cols();
|
|
|
|
let mut num_lines = size.lines();
|
2016-06-29 17:21:02 +00:00
|
|
|
|
|
|
|
self.size_info = size;
|
|
|
|
|
2016-07-04 00:00:00 +00:00
|
|
|
if old_cols == num_cols && old_lines == num_lines {
|
2016-06-29 17:21:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-12 03:00:29 +00:00
|
|
|
// Should not allow less than 1 col, causes all sorts of checks to be required.
|
2017-01-08 00:07:03 +00:00
|
|
|
if num_cols <= Column(1) {
|
2017-01-12 03:00:29 +00:00
|
|
|
num_cols = Column(2);
|
2017-01-08 00:07:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 03:00:29 +00:00
|
|
|
// Should not allow less than 1 line, causes all sorts of checks to be required.
|
2017-01-08 00:07:03 +00:00
|
|
|
if num_lines <= Line(1) {
|
2017-01-12 03:00:29 +00:00
|
|
|
num_lines = Line(2);
|
2017-01-08 00:07:03 +00:00
|
|
|
}
|
|
|
|
|
2016-12-17 06:48:04 +00:00
|
|
|
// Scroll up to keep cursor and as much context as possible in grid.
|
|
|
|
// This only runs when the lines decreases.
|
2016-07-04 00:00:00 +00:00
|
|
|
self.scroll_region = Line(0)..self.grid.num_lines();
|
|
|
|
|
|
|
|
// Scroll up to keep cursor in terminal
|
2017-02-01 17:55:23 +00:00
|
|
|
if self.cursor.point.line >= num_lines {
|
|
|
|
let lines = self.cursor.point.line - num_lines + 1;
|
2016-08-20 03:35:13 +00:00
|
|
|
self.scroll_up(lines);
|
2017-02-01 17:55:23 +00:00
|
|
|
self.cursor.point.line -= lines;
|
2016-06-29 17:21:02 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 00:00:00 +00:00
|
|
|
println!("num_cols, num_lines = {}, {}", num_cols, num_lines);
|
2016-06-29 17:21:02 +00:00
|
|
|
|
|
|
|
// Resize grids to new size
|
2017-02-17 06:17:10 +00:00
|
|
|
let template = self.empty_cell;
|
2016-10-23 22:37:06 +00:00
|
|
|
self.grid.resize(num_lines, num_cols, &template);
|
|
|
|
self.alt_grid.resize(num_lines, num_cols, &template);
|
2016-06-29 17:21:02 +00:00
|
|
|
|
|
|
|
// Ensure cursor is in-bounds
|
2017-02-01 17:55:23 +00:00
|
|
|
self.cursor.point.line = limit(self.cursor.point.line, Line(0), num_lines - 1);
|
|
|
|
self.cursor.point.col = limit(self.cursor.point.col, Column(0), num_cols - 1);
|
2016-06-29 17:21:02 +00:00
|
|
|
|
|
|
|
// Recreate tabs list
|
2017-01-06 23:25:04 +00:00
|
|
|
self.tabs = IndexRange::from(Column(0)..self.grid.num_cols())
|
2016-12-17 06:48:04 +00:00
|
|
|
.map(|i| (*i as usize) % TAB_SPACES == 0)
|
|
|
|
.collect::<Vec<bool>>();
|
2016-06-29 17:21:02 +00:00
|
|
|
|
2016-07-30 03:16:13 +00:00
|
|
|
self.tabs[0] = false;
|
|
|
|
|
2017-01-08 00:07:03 +00:00
|
|
|
if num_lines > old_lines {
|
|
|
|
// Make sure bottom of terminal is clear
|
|
|
|
let template = self.empty_cell;
|
2017-02-01 17:55:23 +00:00
|
|
|
self.grid.clear_region((self.cursor.point.line + 1).., |c| c.reset(&template));
|
|
|
|
self.alt_grid.clear_region((self.cursor.point.line + 1).., |c| c.reset(&template));
|
2017-01-08 00:07:03 +00:00
|
|
|
}
|
2016-06-29 17:21:02 +00:00
|
|
|
|
|
|
|
// Reset scrolling region to new size
|
2016-07-04 00:00:00 +00:00
|
|
|
self.scroll_region = Line(0)..self.grid.num_lines();
|
2016-06-28 16:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn size_info(&self) -> &SizeInfo {
|
|
|
|
&self.size_info
|
|
|
|
}
|
|
|
|
|
2016-06-08 04:17:48 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn mode(&self) -> &TermMode {
|
|
|
|
&self.mode
|
|
|
|
}
|
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
pub fn swap_alt(&mut self) {
|
|
|
|
if self.alt {
|
2017-01-07 04:44:51 +00:00
|
|
|
let template = self.empty_cell;
|
2016-07-30 03:16:13 +00:00
|
|
|
self.grid.clear(|c| c.reset(&template));
|
2016-06-08 17:39:49 +00:00
|
|
|
}
|
2017-02-03 04:51:14 +00:00
|
|
|
|
|
|
|
self.alt = !self.alt;
|
|
|
|
::std::mem::swap(&mut self.grid, &mut self.alt_grid);
|
2016-06-08 17:39:49 +00:00
|
|
|
}
|
2016-08-22 15:37:50 +00:00
|
|
|
|
|
|
|
/// Scroll screen down
|
|
|
|
///
|
|
|
|
/// Text moves down; clear at bottom
|
2017-01-10 06:05:26 +00:00
|
|
|
/// Expects origin to be in scroll range.
|
2016-08-22 15:37:50 +00:00
|
|
|
#[inline]
|
|
|
|
fn scroll_down_relative(&mut self, origin: Line, lines: Line) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("scroll_down: {}", lines);
|
2016-08-22 15:37:50 +00:00
|
|
|
|
|
|
|
// Copy of cell template; can't have it borrowed when calling clear/scroll
|
2017-01-07 04:44:51 +00:00
|
|
|
let template = self.empty_cell;
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2017-01-10 06:05:26 +00:00
|
|
|
// Clear the entire region if lines is going to be greater than the region.
|
|
|
|
// This also ensures all the math below this if statement is sane.
|
|
|
|
if lines > self.scroll_region.end - origin {
|
|
|
|
self.grid.clear_region(origin..self.scroll_region.end, |c| c.reset(&template));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-22 15:37:50 +00:00
|
|
|
// Clear `lines` lines at bottom of area
|
|
|
|
{
|
|
|
|
let end = self.scroll_region.end;
|
2017-02-11 21:53:32 +00:00
|
|
|
let start = Line(end.0.saturating_sub(lines.0));
|
2016-08-22 15:37:50 +00:00
|
|
|
self.grid.clear_region(start..end, |c| c.reset(&template));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scroll between origin and bottom
|
|
|
|
{
|
|
|
|
let end = self.scroll_region.end;
|
2017-01-10 06:05:26 +00:00
|
|
|
let start = origin + lines;
|
2016-08-22 15:37:50 +00:00
|
|
|
self.grid.scroll_down(start..end, lines);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Scroll screen up
|
|
|
|
///
|
|
|
|
/// Text moves up; clear at top
|
2017-01-10 06:05:26 +00:00
|
|
|
/// Expects origin to be in scroll range.
|
2016-08-22 15:37:50 +00:00
|
|
|
#[inline]
|
|
|
|
fn scroll_up_relative(&mut self, origin: Line, lines: Line) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("scroll_up: {}", lines);
|
2016-08-22 15:37:50 +00:00
|
|
|
|
|
|
|
// Copy of cell template; can't have it borrowed when calling clear/scroll
|
2017-01-07 04:44:51 +00:00
|
|
|
let template = self.empty_cell;
|
2016-08-22 15:37:50 +00:00
|
|
|
|
2017-01-10 06:05:26 +00:00
|
|
|
// Clear the entire region if lines is going to be greater than the region.
|
|
|
|
// This also ensures all the math below this if statement is sane.
|
|
|
|
if lines > self.scroll_region.end - origin {
|
|
|
|
self.grid.clear_region(origin..self.scroll_region.end, |c| c.reset(&template));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-22 15:37:50 +00:00
|
|
|
// Clear `lines` lines starting from origin to origin + lines
|
2017-01-12 03:00:29 +00:00
|
|
|
{
|
2017-02-11 21:53:32 +00:00
|
|
|
let end = min(origin + lines, self.grid.num_lines());
|
2017-01-10 06:05:26 +00:00
|
|
|
self.grid.clear_region(origin..end, |c| c.reset(&template));
|
2016-08-22 15:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scroll from origin to bottom less number of lines
|
|
|
|
{
|
2017-01-12 03:00:29 +00:00
|
|
|
let end = self.scroll_region.end - lines;
|
2017-01-10 06:05:26 +00:00
|
|
|
self.grid.scroll_up(origin..end, lines);
|
2016-08-22 15:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-08 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ansi::TermInfo for Term {
|
|
|
|
#[inline]
|
2016-07-04 04:12:43 +00:00
|
|
|
fn lines(&self) -> Line {
|
|
|
|
self.grid.num_lines()
|
2016-06-08 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2016-07-04 04:12:43 +00:00
|
|
|
fn cols(&self) -> Column {
|
|
|
|
self.grid.num_cols()
|
2016-06-08 17:39:49 +00:00
|
|
|
}
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ansi::Handler for Term {
|
2017-01-11 05:21:19 +00:00
|
|
|
/// Set the window title
|
|
|
|
#[inline]
|
|
|
|
fn set_title(&mut self, title: &str) {
|
|
|
|
self.next_title = Some(title.to_owned());
|
|
|
|
}
|
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
/// A character to be displayed
|
|
|
|
#[inline]
|
|
|
|
fn input(&mut self, c: char) {
|
2017-01-08 03:46:02 +00:00
|
|
|
if self.input_needs_wrap {
|
2017-01-12 03:00:29 +00:00
|
|
|
|
|
|
|
if !self.mode.contains(mode::LINE_WRAP) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("wrapping");
|
2017-01-12 03:00:29 +00:00
|
|
|
|
2016-12-29 15:43:58 +00:00
|
|
|
{
|
2016-12-29 16:09:29 +00:00
|
|
|
let location = Point {
|
2017-02-01 17:55:23 +00:00
|
|
|
line: self.cursor.point.line,
|
|
|
|
col: self.cursor.point.col
|
2016-12-29 15:43:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let cell = &mut self.grid[&location];
|
|
|
|
cell.flags.insert(cell::WRAPLINE);
|
|
|
|
}
|
2017-01-12 03:00:29 +00:00
|
|
|
|
2017-02-01 17:55:23 +00:00
|
|
|
if (self.cursor.point.line + 1) >= self.scroll_region.end {
|
2016-08-20 00:55:44 +00:00
|
|
|
self.linefeed();
|
|
|
|
} else {
|
2017-02-01 17:55:23 +00:00
|
|
|
self.cursor.point.line += 1;
|
2016-08-20 00:55:44 +00:00
|
|
|
}
|
2017-01-12 03:00:29 +00:00
|
|
|
|
2017-02-01 17:55:23 +00:00
|
|
|
self.cursor.point.col = Column(0);
|
2017-01-08 03:46:02 +00:00
|
|
|
self.input_needs_wrap = false;
|
2016-07-30 03:16:13 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 21:47:05 +00:00
|
|
|
{
|
2017-02-01 17:55:23 +00:00
|
|
|
let cell = &mut self.grid[&self.cursor.point];
|
|
|
|
*cell = self.cursor.template;
|
|
|
|
cell.c = self.cursor.charsets[self.active_charset].map(c);
|
2017-01-12 03:00:29 +00:00
|
|
|
}
|
|
|
|
|
2017-02-01 17:55:23 +00:00
|
|
|
if (self.cursor.point.col + 1) < self.grid.num_cols() {
|
|
|
|
self.cursor.point.col += 1;
|
2017-01-07 23:13:42 +00:00
|
|
|
} else {
|
2017-01-08 03:46:02 +00:00
|
|
|
self.input_needs_wrap = true;
|
2016-07-30 03:16:13 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 00:00:00 +00:00
|
|
|
#[inline]
|
2016-07-04 04:58:35 +00:00
|
|
|
fn goto(&mut self, line: Line, col: Column) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("goto: line={}, col={}", line, col);
|
2017-02-01 17:55:23 +00:00
|
|
|
self.cursor.point.line = min(line, self.grid.num_lines() - 1);
|
|
|
|
self.cursor.point.col = min(col, self.grid.num_cols() - 1);
|
2017-01-08 03:46:02 +00:00
|
|
|
self.input_needs_wrap = false;
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
2016-07-04 00:00:00 +00:00
|
|
|
|
2016-07-04 00:07:23 +00:00
|
|
|
#[inline]
|
2016-07-04 04:58:35 +00:00
|
|
|
fn goto_line(&mut self, line: Line) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("goto_line: {}", line);
|
2017-02-01 17:55:23 +00:00
|
|
|
self.cursor.point.line = min(line, self.grid.num_lines() - 1);
|
2017-01-08 03:46:02 +00:00
|
|
|
self.input_needs_wrap = false;
|
2016-06-06 22:13:45 +00:00
|
|
|
}
|
2016-07-04 00:07:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
2016-07-04 04:58:35 +00:00
|
|
|
fn goto_col(&mut self, col: Column) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("goto_col: {}", col);
|
2017-02-01 17:55:23 +00:00
|
|
|
self.cursor.point.col = min(col, self.grid.num_cols() - 1);
|
2017-01-08 03:46:02 +00:00
|
|
|
self.input_needs_wrap = false;
|
2016-06-06 22:13:45 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 00:07:23 +00:00
|
|
|
#[inline]
|
2016-10-10 15:29:51 +00:00
|
|
|
fn insert_blank(&mut self, count: Column) {
|
|
|
|
// Ensure inserting within terminal bounds
|
2017-01-12 03:00:29 +00:00
|
|
|
|
2017-02-01 17:55:23 +00:00
|
|
|
let count = min(count, self.size_info.cols() - self.cursor.point.col);
|
2016-10-10 15:29:51 +00:00
|
|
|
|
2017-02-01 17:55:23 +00:00
|
|
|
let source = self.cursor.point.col;
|
|
|
|
let destination = self.cursor.point.col + count;
|
2016-10-10 15:29:51 +00:00
|
|
|
let num_cells = (self.size_info.cols() - destination).0;
|
|
|
|
|
2017-02-01 17:55:23 +00:00
|
|
|
let line = self.cursor.point.line; // borrowck
|
2016-10-10 15:29:51 +00:00
|
|
|
let line = &mut self.grid[line];
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let src = line[source..].as_ptr();
|
|
|
|
let dst = line[destination..].as_mut_ptr();
|
|
|
|
|
|
|
|
ptr::copy(src, dst, num_cells);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cells were just moved out towards the end of the line; fill in
|
|
|
|
// between source and dest with blanks.
|
2017-01-07 04:44:51 +00:00
|
|
|
let template = self.empty_cell;
|
2016-10-10 15:29:51 +00:00
|
|
|
for c in &mut line[source..destination] {
|
|
|
|
c.reset(&template);
|
|
|
|
}
|
2016-07-04 20:37:08 +00:00
|
|
|
}
|
2016-05-31 03:44:37 +00:00
|
|
|
|
2016-07-04 00:07:23 +00:00
|
|
|
#[inline]
|
2016-07-04 04:58:35 +00:00
|
|
|
fn move_up(&mut self, lines: Line) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("move_up: {}", lines);
|
2017-02-01 17:55:23 +00:00
|
|
|
let lines = min(self.cursor.point.line, lines);
|
|
|
|
self.cursor.point.line = min(self.cursor.point.line - lines, self.grid.num_lines() -1);
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 00:07:23 +00:00
|
|
|
#[inline]
|
2016-07-04 04:58:35 +00:00
|
|
|
fn move_down(&mut self, lines: Line) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("move_down: {}", lines);
|
2017-02-01 17:55:23 +00:00
|
|
|
self.cursor.point.line = min(self.cursor.point.line + lines, self.grid.num_lines() - 1);
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 00:07:23 +00:00
|
|
|
#[inline]
|
2016-07-04 04:58:35 +00:00
|
|
|
fn move_forward(&mut self, cols: Column) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("move_forward: {}", cols);
|
2017-02-01 17:55:23 +00:00
|
|
|
self.cursor.point.col = min(self.cursor.point.col + cols, self.grid.num_cols() - 1);
|
2017-01-08 03:46:02 +00:00
|
|
|
self.input_needs_wrap = false;
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 00:07:23 +00:00
|
|
|
#[inline]
|
2016-07-04 04:58:35 +00:00
|
|
|
fn move_backward(&mut self, cols: Column) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("move_backward: {}", cols);
|
2017-02-01 17:55:23 +00:00
|
|
|
self.cursor.point.col -= min(self.cursor.point.col, cols);
|
2017-01-08 03:46:02 +00:00
|
|
|
self.input_needs_wrap = false;
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 00:07:23 +00:00
|
|
|
#[inline]
|
2016-12-04 23:48:30 +00:00
|
|
|
fn identify_terminal<W: io::Write>(&mut self, writer: &mut W) {
|
2016-12-17 06:13:51 +00:00
|
|
|
let _ = writer.write_all(b"\x1b[?6c");
|
2016-07-04 20:37:08 +00:00
|
|
|
}
|
2016-07-04 00:07:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
2016-07-04 20:37:08 +00:00
|
|
|
fn move_down_and_cr(&mut self, lines: Line) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("[unimplemented] move_down_and_cr: {}", lines);
|
2016-07-04 20:37:08 +00:00
|
|
|
}
|
2016-07-04 00:07:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
2016-07-04 20:37:08 +00:00
|
|
|
fn move_up_and_cr(&mut self, lines: Line) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("[unimplemented] move_up_and_cr: {}", lines);
|
2016-07-04 20:37:08 +00:00
|
|
|
}
|
2016-07-04 00:00:00 +00:00
|
|
|
|
2016-07-04 00:07:23 +00:00
|
|
|
#[inline]
|
2016-05-31 03:44:37 +00:00
|
|
|
fn put_tab(&mut self, mut count: i64) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("put_tab: {}", count);
|
2016-05-31 03:44:37 +00:00
|
|
|
|
2017-02-01 17:55:23 +00:00
|
|
|
let mut col = self.cursor.point.col;
|
2016-07-04 00:00:00 +00:00
|
|
|
while col < self.grid.num_cols() && count != 0 {
|
2016-05-31 03:44:37 +00:00
|
|
|
count -= 1;
|
|
|
|
loop {
|
2017-01-07 05:08:52 +00:00
|
|
|
if (col + 1) == self.grid.num_cols() || self.tabs[*col as usize] {
|
2016-05-31 03:44:37 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-07-04 00:00:00 +00:00
|
|
|
col += 1;
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 17:55:23 +00:00
|
|
|
self.cursor.point.col = col;
|
2017-01-08 03:46:02 +00:00
|
|
|
self.input_needs_wrap = false;
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Backspace `count` characters
|
|
|
|
#[inline]
|
2016-06-09 15:37:59 +00:00
|
|
|
fn backspace(&mut self) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("backspace");
|
2017-02-01 17:55:23 +00:00
|
|
|
if self.cursor.point.col > Column(0) {
|
|
|
|
self.cursor.point.col -= 1;
|
2017-01-08 03:46:02 +00:00
|
|
|
self.input_needs_wrap = false;
|
2016-12-10 18:14:45 +00:00
|
|
|
}
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Carriage return
|
|
|
|
#[inline]
|
|
|
|
fn carriage_return(&mut self) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("carriage_return");
|
2017-02-01 17:55:23 +00:00
|
|
|
self.cursor.point.col = Column(0);
|
2017-01-08 03:46:02 +00:00
|
|
|
self.input_needs_wrap = false;
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Linefeed
|
|
|
|
#[inline]
|
|
|
|
fn linefeed(&mut self) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("linefeed");
|
2017-02-01 17:55:23 +00:00
|
|
|
if (self.cursor.point.line + 1) >= self.scroll_region.end {
|
2016-08-20 03:35:13 +00:00
|
|
|
self.scroll_up(Line(1));
|
2016-05-31 03:44:37 +00:00
|
|
|
} else {
|
2017-02-01 17:55:23 +00:00
|
|
|
self.cursor.point.line += 1;
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set current position as a tabstop
|
2016-07-04 00:07:23 +00:00
|
|
|
#[inline]
|
2016-07-04 15:42:18 +00:00
|
|
|
fn bell(&mut self) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("bell");
|
2017-02-03 23:34:52 +00:00
|
|
|
self.visual_bell.ring();
|
2016-07-04 15:42:18 +00:00
|
|
|
}
|
2016-07-04 00:07:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
2016-07-04 15:42:18 +00:00
|
|
|
fn substitute(&mut self) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("[unimplemented] substitute");
|
2016-07-04 15:42:18 +00:00
|
|
|
}
|
2016-07-04 00:07:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
2016-07-04 15:42:18 +00:00
|
|
|
fn newline(&mut self) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("[unimplemented] newline");
|
2016-07-04 15:42:18 +00:00
|
|
|
}
|
2016-07-04 00:07:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
2016-07-04 15:42:18 +00:00
|
|
|
fn set_horizontal_tabstop(&mut self) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("[unimplemented] set_horizontal_tabstop");
|
2016-07-04 15:42:18 +00:00
|
|
|
}
|
2016-07-04 00:07:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
2016-08-22 15:37:50 +00:00
|
|
|
fn scroll_up(&mut self, lines: Line) {
|
|
|
|
let origin = self.scroll_region.start;
|
|
|
|
self.scroll_up_relative(origin, lines);
|
2016-06-08 17:39:49 +00:00
|
|
|
}
|
2016-07-04 00:07:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
2016-08-22 15:37:50 +00:00
|
|
|
fn scroll_down(&mut self, lines: Line) {
|
|
|
|
let origin = self.scroll_region.start;
|
|
|
|
self.scroll_down_relative(origin, lines);
|
2016-06-08 17:39:49 +00:00
|
|
|
}
|
2016-07-04 00:07:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
2016-07-04 04:58:35 +00:00
|
|
|
fn insert_blank_lines(&mut self, lines: Line) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("insert_blank_lines: {}", lines);
|
2017-02-01 17:55:23 +00:00
|
|
|
if self.scroll_region.contains_(self.cursor.point.line) {
|
|
|
|
let origin = self.cursor.point.line;
|
2016-08-22 15:37:50 +00:00
|
|
|
self.scroll_down_relative(origin, lines);
|
2016-06-08 17:39:49 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-04 00:07:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
2016-07-04 04:58:35 +00:00
|
|
|
fn delete_lines(&mut self, lines: Line) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("delete_lines: {}", lines);
|
2017-02-01 17:55:23 +00:00
|
|
|
if self.scroll_region.contains_(self.cursor.point.line) {
|
|
|
|
let origin = self.cursor.point.line;
|
2016-08-22 15:37:50 +00:00
|
|
|
self.scroll_up_relative(origin, lines);
|
2016-06-08 17:39:49 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-04 00:07:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
2016-07-04 04:58:35 +00:00
|
|
|
fn erase_chars(&mut self, count: Column) {
|
2017-02-01 17:55:23 +00:00
|
|
|
trace!("erase_chars: {}, {}", count, self.cursor.point.col);
|
|
|
|
let start = self.cursor.point.col;
|
2017-01-08 03:46:02 +00:00
|
|
|
let end = min(start + count, self.grid.num_cols() - 1);
|
2016-07-02 04:13:09 +00:00
|
|
|
|
2017-02-01 17:55:23 +00:00
|
|
|
let row = &mut self.grid[self.cursor.point.line];
|
2017-01-07 04:44:51 +00:00
|
|
|
let template = self.empty_cell;
|
2016-07-04 04:58:35 +00:00
|
|
|
for c in &mut row[start..end] {
|
2016-07-30 03:16:13 +00:00
|
|
|
c.reset(&template);
|
2016-07-02 04:13:09 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-04 00:07:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
2016-07-04 15:42:18 +00:00
|
|
|
fn delete_chars(&mut self, count: Column) {
|
2016-09-15 15:49:55 +00:00
|
|
|
// Ensure deleting within terminal bounds
|
2017-01-08 03:46:02 +00:00
|
|
|
let count = min(count, self.size_info.cols());
|
2017-01-12 03:00:29 +00:00
|
|
|
|
2017-02-01 17:55:23 +00:00
|
|
|
let start = self.cursor.point.col;
|
2017-01-08 03:46:02 +00:00
|
|
|
let end = min(start + count, self.grid.num_cols() - 1);
|
2016-09-15 15:49:55 +00:00
|
|
|
let n = (self.size_info.cols() - end).0;
|
|
|
|
|
2017-02-01 17:55:23 +00:00
|
|
|
let line = self.cursor.point.line; // borrowck
|
2016-09-15 15:49:55 +00:00
|
|
|
let line = &mut self.grid[line];
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let src = line[end..].as_ptr();
|
|
|
|
let dst = line[start..].as_mut_ptr();
|
|
|
|
|
|
|
|
ptr::copy(src, dst, n);
|
|
|
|
}
|
|
|
|
|
2016-12-17 06:48:04 +00:00
|
|
|
// Clear last `count` cells in line. If deleting 1 char, need to delete
|
|
|
|
// 1 cell.
|
2017-01-07 04:44:51 +00:00
|
|
|
let template = self.empty_cell;
|
2016-09-15 15:49:55 +00:00
|
|
|
let end = self.size_info.cols() - count;
|
|
|
|
for c in &mut line[end..] {
|
|
|
|
c.reset(&template);
|
|
|
|
}
|
2016-07-04 15:42:18 +00:00
|
|
|
}
|
2016-07-04 00:07:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
2016-07-04 15:42:18 +00:00
|
|
|
fn move_backward_tabs(&mut self, count: i64) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("[unimplemented] move_backward_tabs: {}", count);
|
2016-07-04 15:42:18 +00:00
|
|
|
}
|
2016-07-04 00:07:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
2016-07-04 15:42:18 +00:00
|
|
|
fn move_forward_tabs(&mut self, count: i64) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("[unimplemented] move_forward_tabs: {}", count);
|
2016-07-04 15:42:18 +00:00
|
|
|
}
|
2016-07-04 00:07:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
2016-07-04 15:42:18 +00:00
|
|
|
fn save_cursor_position(&mut self) {
|
2017-02-01 17:55:23 +00:00
|
|
|
trace!("CursorSave");
|
|
|
|
let mut holder = if self.alt {
|
|
|
|
&mut self.cursor_save_alt
|
|
|
|
} else {
|
|
|
|
&mut self.cursor_save
|
|
|
|
};
|
|
|
|
|
|
|
|
*holder = self.cursor;
|
2016-07-04 15:42:18 +00:00
|
|
|
}
|
2016-07-04 00:07:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
2016-07-04 15:42:18 +00:00
|
|
|
fn restore_cursor_position(&mut self) {
|
2017-02-01 17:55:23 +00:00
|
|
|
trace!("CursorRestore");
|
|
|
|
let holder = if self.alt {
|
|
|
|
&self.cursor_save_alt
|
|
|
|
} else {
|
|
|
|
&self.cursor_save
|
|
|
|
};
|
|
|
|
|
|
|
|
self.cursor = *holder;
|
2017-02-04 03:42:25 +00:00
|
|
|
self.cursor.point.line = min(self.cursor.point.line, self.grid.num_lines() - 1);
|
|
|
|
self.cursor.point.col = min(self.cursor.point.col, self.grid.num_cols() - 1);
|
2016-07-04 15:42:18 +00:00
|
|
|
}
|
2016-07-04 00:07:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
2016-05-31 03:44:37 +00:00
|
|
|
fn clear_line(&mut self, mode: ansi::LineClearMode) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("clear_line: {:?}", mode);
|
2017-01-07 04:44:51 +00:00
|
|
|
let template = self.empty_cell;
|
2017-02-01 17:55:23 +00:00
|
|
|
let col = self.cursor.point.col;
|
2017-01-12 03:00:29 +00:00
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
match mode {
|
|
|
|
ansi::LineClearMode::Right => {
|
2017-02-01 17:55:23 +00:00
|
|
|
let row = &mut self.grid[self.cursor.point.line];
|
2017-01-07 05:08:52 +00:00
|
|
|
for cell in &mut row[col..] {
|
2016-07-30 03:16:13 +00:00
|
|
|
cell.reset(&template);
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
},
|
2016-08-22 15:11:34 +00:00
|
|
|
ansi::LineClearMode::Left => {
|
2017-02-01 17:55:23 +00:00
|
|
|
let row = &mut self.grid[self.cursor.point.line];
|
2017-01-07 05:08:52 +00:00
|
|
|
for cell in &mut row[..(col + 1)] {
|
2016-08-22 15:11:34 +00:00
|
|
|
cell.reset(&template);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ansi::LineClearMode::All => {
|
2017-02-01 17:55:23 +00:00
|
|
|
let row = &mut self.grid[self.cursor.point.line];
|
2016-08-22 15:11:34 +00:00
|
|
|
for cell in &mut row[..] {
|
|
|
|
cell.reset(&template);
|
|
|
|
}
|
|
|
|
},
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-04 00:07:23 +00:00
|
|
|
|
2017-02-06 21:28:33 +00:00
|
|
|
/// Set the indexed color value
|
|
|
|
///
|
|
|
|
/// TODO needs access to `Config`, and `Config` should not overwrite values
|
|
|
|
/// when reloading
|
|
|
|
#[inline]
|
|
|
|
fn set_color(&mut self, index: usize, color: Rgb) {
|
|
|
|
trace!("set_color[{}] = {:?}", index, color);
|
|
|
|
self.colors[index] = color;
|
|
|
|
}
|
|
|
|
|
2016-07-04 00:07:23 +00:00
|
|
|
#[inline]
|
2016-05-31 03:44:37 +00:00
|
|
|
fn clear_screen(&mut self, mode: ansi::ClearMode) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("clear_screen: {:?}", mode);
|
2017-01-07 04:44:51 +00:00
|
|
|
let template = self.empty_cell;
|
2016-05-31 03:44:37 +00:00
|
|
|
match mode {
|
|
|
|
ansi::ClearMode::Below => {
|
2017-02-06 20:11:35 +00:00
|
|
|
for cell in &mut self.grid[self.cursor.point.line][self.cursor.point.col..] {
|
|
|
|
cell.reset(&template);
|
|
|
|
}
|
|
|
|
if self.cursor.point.line < self.grid.num_lines() - 1 {
|
|
|
|
for row in &mut self.grid[(self.cursor.point.line + 1)..] {
|
|
|
|
for cell in row {
|
|
|
|
cell.reset(&template);
|
|
|
|
}
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ansi::ClearMode::All => {
|
2016-07-30 03:16:13 +00:00
|
|
|
self.grid.clear(|c| c.reset(&template));
|
2016-05-31 03:44:37 +00:00
|
|
|
},
|
2017-02-06 21:13:25 +00:00
|
|
|
ansi::ClearMode::Above => {
|
|
|
|
// If clearing more than one line
|
|
|
|
if self.cursor.point.line > Line(1) {
|
|
|
|
// Fully clear all lines before the current line
|
|
|
|
for row in &mut self.grid[..self.cursor.point.line] {
|
|
|
|
for cell in row {
|
|
|
|
cell.reset(&template);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Clear up to the current column in the current line
|
|
|
|
for cell in &mut self.grid[self.cursor.point.line][..self.cursor.point.col] {
|
|
|
|
cell.reset(&template);
|
|
|
|
}
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-04 00:07:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
2016-07-04 15:42:18 +00:00
|
|
|
fn clear_tabs(&mut self, mode: ansi::TabulationClearMode) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("[unimplemented] clear_tabs: {:?}", mode);
|
2016-07-04 15:42:18 +00:00
|
|
|
}
|
2016-07-04 00:07:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
2016-07-04 15:42:18 +00:00
|
|
|
fn reset_state(&mut self) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("[unimplemented] reset_state");
|
2016-07-04 15:42:18 +00:00
|
|
|
}
|
2016-07-04 00:07:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
2016-05-31 03:44:37 +00:00
|
|
|
fn reverse_index(&mut self) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("reverse_index");
|
2016-05-31 03:44:37 +00:00
|
|
|
// if cursor is at the top
|
2017-02-01 17:55:23 +00:00
|
|
|
if self.cursor.point.line == self.scroll_region.start {
|
2016-08-20 03:35:13 +00:00
|
|
|
self.scroll_down(Line(1));
|
2016-05-31 03:44:37 +00:00
|
|
|
} else {
|
2017-02-01 17:55:23 +00:00
|
|
|
self.cursor.point.line -= min(self.cursor.point.line, Line(1));
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// set a terminal attribute
|
2016-07-04 00:07:23 +00:00
|
|
|
#[inline]
|
2016-05-31 03:44:37 +00:00
|
|
|
fn terminal_attribute(&mut self, attr: Attr) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("Set Attribute: {:?}", attr);
|
2016-05-31 03:44:37 +00:00
|
|
|
match attr {
|
2017-02-01 17:55:23 +00:00
|
|
|
Attr::Foreground(color) => self.cursor.template.fg = color,
|
|
|
|
Attr::Background(color) => self.cursor.template.bg = color,
|
2016-05-31 03:44:37 +00:00
|
|
|
Attr::Reset => {
|
2017-02-01 17:55:23 +00:00
|
|
|
self.cursor.template.fg = Color::Named(NamedColor::Foreground);
|
|
|
|
self.cursor.template.bg = Color::Named(NamedColor::Background);
|
|
|
|
self.cursor.template.flags = cell::Flags::empty();
|
2016-06-06 23:54:15 +00:00
|
|
|
},
|
2017-02-01 17:55:23 +00:00
|
|
|
Attr::Reverse => self.cursor.template.flags.insert(cell::INVERSE),
|
|
|
|
Attr::CancelReverse => self.cursor.template.flags.remove(cell::INVERSE),
|
|
|
|
Attr::Bold => self.cursor.template.flags.insert(cell::BOLD),
|
|
|
|
Attr::CancelBoldDim => self.cursor.template.flags.remove(cell::BOLD),
|
|
|
|
Attr::Italic => self.cursor.template.flags.insert(cell::ITALIC),
|
|
|
|
Attr::CancelItalic => self.cursor.template.flags.remove(cell::ITALIC),
|
|
|
|
Attr::Underscore => self.cursor.template.flags.insert(cell::UNDERLINE),
|
|
|
|
Attr::CancelUnderline => self.cursor.template.flags.remove(cell::UNDERLINE),
|
2016-05-31 03:44:37 +00:00
|
|
|
_ => {
|
2017-01-13 07:15:06 +00:00
|
|
|
debug!("Term got unhandled attr: {:?}", attr);
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-04 00:07:23 +00:00
|
|
|
#[inline]
|
2016-05-31 03:44:37 +00:00
|
|
|
fn set_mode(&mut self, mode: ansi::Mode) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("set_mode: {:?}", mode);
|
2016-05-31 03:44:37 +00:00
|
|
|
match mode {
|
2017-02-01 17:55:23 +00:00
|
|
|
ansi::Mode::SwapScreenAndSetRestoreCursor => {
|
|
|
|
self.save_cursor_position();
|
|
|
|
self.swap_alt();
|
2017-02-03 04:51:14 +00:00
|
|
|
self.save_cursor_position();
|
2017-02-01 17:55:23 +00:00
|
|
|
},
|
2016-06-23 16:42:00 +00:00
|
|
|
ansi::Mode::ShowCursor => self.mode.insert(mode::SHOW_CURSOR),
|
2016-06-23 16:48:31 +00:00
|
|
|
ansi::Mode::CursorKeys => self.mode.insert(mode::APP_CURSOR),
|
2016-11-24 04:25:37 +00:00
|
|
|
ansi::Mode::ReportMouseClicks => self.mode.insert(mode::MOUSE_REPORT_CLICK),
|
2017-01-02 02:28:49 +00:00
|
|
|
ansi::Mode::ReportMouseMotion => self.mode.insert(mode::MOUSE_MOTION),
|
2016-11-28 22:39:37 +00:00
|
|
|
ansi::Mode::BracketedPaste => self.mode.insert(mode::BRACKETED_PASTE),
|
2017-01-02 02:28:49 +00:00
|
|
|
ansi::Mode::SgrMouse => self.mode.insert(mode::SGR_MOUSE),
|
2017-01-12 03:00:29 +00:00
|
|
|
ansi::Mode::LineWrap => self.mode.insert(mode::LINE_WRAP),
|
2016-06-08 04:17:48 +00:00
|
|
|
_ => {
|
2017-01-13 07:15:06 +00:00
|
|
|
debug!(".. ignoring set_mode");
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-04 00:07:23 +00:00
|
|
|
#[inline]
|
2016-06-08 04:17:48 +00:00
|
|
|
fn unset_mode(&mut self,mode: ansi::Mode) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("unset_mode: {:?}", mode);
|
2016-05-31 03:44:37 +00:00
|
|
|
match mode {
|
2017-02-01 17:55:23 +00:00
|
|
|
ansi::Mode::SwapScreenAndSetRestoreCursor => {
|
|
|
|
self.restore_cursor_position();
|
|
|
|
self.swap_alt();
|
2017-02-03 04:51:14 +00:00
|
|
|
self.restore_cursor_position();
|
2017-02-01 17:55:23 +00:00
|
|
|
},
|
2016-06-23 16:42:00 +00:00
|
|
|
ansi::Mode::ShowCursor => self.mode.remove(mode::SHOW_CURSOR),
|
2016-06-23 16:48:31 +00:00
|
|
|
ansi::Mode::CursorKeys => self.mode.remove(mode::APP_CURSOR),
|
2016-11-24 04:25:37 +00:00
|
|
|
ansi::Mode::ReportMouseClicks => self.mode.remove(mode::MOUSE_REPORT_CLICK),
|
2017-01-02 02:28:49 +00:00
|
|
|
ansi::Mode::ReportMouseMotion => self.mode.remove(mode::MOUSE_MOTION),
|
2016-11-28 22:39:37 +00:00
|
|
|
ansi::Mode::BracketedPaste => self.mode.remove(mode::BRACKETED_PASTE),
|
2017-01-02 02:28:49 +00:00
|
|
|
ansi::Mode::SgrMouse => self.mode.remove(mode::SGR_MOUSE),
|
2017-01-12 03:00:29 +00:00
|
|
|
ansi::Mode::LineWrap => self.mode.remove(mode::LINE_WRAP),
|
2016-06-08 04:17:48 +00:00
|
|
|
_ => {
|
2017-01-13 07:15:06 +00:00
|
|
|
debug!(".. ignoring unset_mode");
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-08 17:39:49 +00:00
|
|
|
|
2016-07-04 00:07:23 +00:00
|
|
|
#[inline]
|
2016-07-04 04:58:35 +00:00
|
|
|
fn set_scrolling_region(&mut self, region: Range<Line>) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("set scroll region: {:?}", region);
|
2017-01-10 06:05:26 +00:00
|
|
|
self.scroll_region.start = min(region.start, self.grid.num_lines());
|
|
|
|
self.scroll_region.end = min(region.end, self.grid.num_lines());
|
2016-08-22 15:37:50 +00:00
|
|
|
self.goto(Line(0), Column(0));
|
2016-06-08 17:39:49 +00:00
|
|
|
}
|
2016-07-04 16:19:48 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn set_keypad_application_mode(&mut self) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("set mode::APP_KEYPAD");
|
2016-07-04 16:19:48 +00:00
|
|
|
self.mode.insert(mode::APP_KEYPAD);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn unset_keypad_application_mode(&mut self) {
|
2017-01-13 07:15:06 +00:00
|
|
|
trace!("unset mode::APP_KEYPAD");
|
2016-07-04 16:19:48 +00:00
|
|
|
self.mode.remove(mode::APP_KEYPAD);
|
|
|
|
}
|
2017-01-09 20:07:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn configure_charset(&mut self, index: CharsetIndex, charset: StandardCharset) {
|
2017-01-23 16:50:24 +00:00
|
|
|
trace!("designate {:?} character set as {:?}", index, charset);
|
2017-02-01 17:55:23 +00:00
|
|
|
self.cursor.charsets[index] = charset;
|
2017-01-09 20:07:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn set_active_charset(&mut self, index: CharsetIndex) {
|
2017-01-23 16:50:24 +00:00
|
|
|
trace!("Activate {:?} character set", index);
|
2017-01-09 20:07:23 +00:00
|
|
|
self.active_charset = index;
|
|
|
|
}
|
2016-05-31 03:44:37 +00:00
|
|
|
}
|
2016-11-20 00:16:20 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
extern crate serde_json;
|
2016-11-25 20:03:11 +00:00
|
|
|
|
2017-01-15 01:53:48 +00:00
|
|
|
use super::{Cell, Term, limit, SizeInfo};
|
|
|
|
use term::cell;
|
2016-11-20 00:16:20 +00:00
|
|
|
|
|
|
|
use grid::Grid;
|
2017-01-09 20:07:23 +00:00
|
|
|
use index::{Point, Line, Column};
|
|
|
|
use ansi::{Handler, CharsetIndex, StandardCharset};
|
2017-01-15 01:53:48 +00:00
|
|
|
use selection::Selection;
|
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn semantic_selection_works() {
|
|
|
|
let size = SizeInfo {
|
|
|
|
width: 21.0,
|
|
|
|
height: 51.0,
|
|
|
|
cell_width: 3.0,
|
|
|
|
cell_height: 3.0,
|
|
|
|
};
|
|
|
|
let mut term = Term::new(&Default::default(), size);
|
|
|
|
let mut grid: Grid<Cell> = Grid::new(Line(3), Column(5), &Cell::default());
|
|
|
|
for i in 0..5 {
|
|
|
|
for j in 0..2 {
|
|
|
|
grid[Line(j)][Column(i)].c = 'a';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
grid[Line(0)][Column(0)].c = '"';
|
|
|
|
grid[Line(0)][Column(3)].c = '"';
|
|
|
|
grid[Line(1)][Column(2)].c = '"';
|
|
|
|
grid[Line(0)][Column(4)].flags.insert(cell::WRAPLINE);
|
|
|
|
|
|
|
|
let mut escape_chars = String::from("\"");
|
|
|
|
|
|
|
|
mem::swap(&mut term.grid, &mut grid);
|
|
|
|
mem::swap(&mut term.semantic_escape_chars, &mut escape_chars);
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut selection = Selection::new();
|
|
|
|
term.semantic_selection(&mut selection, Point { line: Line(0), col: Column(1) });
|
|
|
|
assert_eq!(term.string_from_selection(&selection.span().unwrap()), "aa");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut selection = Selection::new();
|
|
|
|
term.semantic_selection(&mut selection, Point { line: Line(0), col: Column(4) });
|
|
|
|
assert_eq!(term.string_from_selection(&selection.span().unwrap()), "aaa");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut selection = Selection::new();
|
|
|
|
term.semantic_selection(&mut selection, Point { line: Line(1), col: Column(1) });
|
|
|
|
assert_eq!(term.string_from_selection(&selection.span().unwrap()), "aaa");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn line_selection_works() {
|
|
|
|
let size = SizeInfo {
|
|
|
|
width: 21.0,
|
|
|
|
height: 51.0,
|
|
|
|
cell_width: 3.0,
|
|
|
|
cell_height: 3.0,
|
|
|
|
};
|
|
|
|
let mut term = Term::new(&Default::default(), size);
|
|
|
|
let mut grid: Grid<Cell> = Grid::new(Line(1), Column(5), &Cell::default());
|
|
|
|
for i in 0..5 {
|
|
|
|
grid[Line(0)][Column(i)].c = 'a';
|
|
|
|
}
|
|
|
|
grid[Line(0)][Column(0)].c = '"';
|
|
|
|
grid[Line(0)][Column(3)].c = '"';
|
|
|
|
|
|
|
|
|
|
|
|
mem::swap(&mut term.grid, &mut grid);
|
|
|
|
|
|
|
|
let mut selection = Selection::new();
|
|
|
|
term.line_selection(&mut selection, Point { line: Line(0), col: Column(3) });
|
|
|
|
match selection.span() {
|
|
|
|
Some(span) => assert_eq!(term.string_from_selection(&span), "\"aa\"a"),
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
2016-11-20 00:16:20 +00:00
|
|
|
|
|
|
|
/// Check that the grid can be serialized back and forth losslessly
|
|
|
|
///
|
|
|
|
/// This test is in the term module as opposed to the grid since we want to
|
|
|
|
/// test this property with a T=Cell.
|
|
|
|
#[test]
|
|
|
|
fn grid_serde() {
|
2016-12-30 02:43:55 +00:00
|
|
|
let template = Cell::default();
|
2016-11-20 00:16:20 +00:00
|
|
|
|
|
|
|
let grid = Grid::new(Line(24), Column(80), &template);
|
|
|
|
let serialized = serde_json::to_string(&grid).expect("ser");
|
|
|
|
let deserialized = serde_json::from_str::<Grid<Cell>>(&serialized)
|
|
|
|
.expect("de");
|
|
|
|
|
|
|
|
assert_eq!(deserialized, grid);
|
|
|
|
}
|
2016-11-25 20:03:11 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn limit_works() {
|
|
|
|
assert_eq!(limit(5, 1, 10), 5);
|
|
|
|
assert_eq!(limit(5, 6, 10), 6);
|
|
|
|
assert_eq!(limit(5, 1, 4), 4);
|
|
|
|
}
|
2017-01-09 20:07:23 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn input_line_drawing_character() {
|
|
|
|
let size = SizeInfo {
|
|
|
|
width: 21.0,
|
|
|
|
height: 51.0,
|
|
|
|
cell_width: 3.0,
|
|
|
|
cell_height: 3.0,
|
|
|
|
};
|
2017-01-09 03:18:39 +00:00
|
|
|
let mut term = Term::new(&Default::default(), size);
|
2017-01-09 20:07:23 +00:00
|
|
|
let cursor = Point::new(Line(0), Column(0));
|
|
|
|
term.configure_charset(CharsetIndex::G0,
|
|
|
|
StandardCharset::SpecialCharacterAndLineDrawing);
|
|
|
|
term.input('a');
|
|
|
|
|
|
|
|
assert_eq!(term.grid()[&cursor].c, '▒');
|
|
|
|
}
|
2016-11-20 00:16:20 +00:00
|
|
|
}
|
2016-11-28 22:13:11 +00:00
|
|
|
|
2017-02-03 04:50:48 +00:00
|
|
|
#[cfg(all(test, feature = "bench"))]
|
2016-12-30 02:51:17 +00:00
|
|
|
mod benches {
|
2016-11-28 22:13:11 +00:00
|
|
|
extern crate test;
|
|
|
|
extern crate serde_json as json;
|
|
|
|
|
|
|
|
use std::io::Read;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::mem;
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
use grid::Grid;
|
2016-12-22 18:43:06 +00:00
|
|
|
use selection::Selection;
|
2016-11-28 22:13:11 +00:00
|
|
|
|
|
|
|
use super::{SizeInfo, Term};
|
|
|
|
use super::cell::Cell;
|
|
|
|
|
|
|
|
fn read_string<P>(path: P) -> String
|
|
|
|
where P: AsRef<Path>
|
|
|
|
{
|
|
|
|
let mut res = String::new();
|
|
|
|
File::open(path.as_ref()).unwrap()
|
|
|
|
.read_to_string(&mut res).unwrap();
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Benchmark for the renderable cells iterator
|
|
|
|
///
|
2016-12-17 06:48:04 +00:00
|
|
|
/// The renderable cells iterator yields cells that require work to be
|
|
|
|
/// displayed (that is, not a an empty background cell). This benchmark
|
|
|
|
/// measures how long it takes to process the whole iterator.
|
2016-11-28 22:13:11 +00:00
|
|
|
///
|
2016-12-17 06:48:04 +00:00
|
|
|
/// When this benchmark was first added, it averaged ~78usec on my macbook
|
|
|
|
/// pro. The total render time for this grid is anywhere between ~1500 and
|
|
|
|
/// ~2000usec (measured imprecisely with the visual meter).
|
2016-11-28 22:13:11 +00:00
|
|
|
#[bench]
|
|
|
|
fn render_iter(b: &mut test::Bencher) {
|
|
|
|
// Need some realistic grid state; using one of the ref files.
|
|
|
|
let serialized_grid = read_string(
|
|
|
|
concat!(env!("CARGO_MANIFEST_DIR"), "/tests/ref/vim_large_window_scroll/grid.json")
|
|
|
|
);
|
|
|
|
let serialized_size = read_string(
|
|
|
|
concat!(env!("CARGO_MANIFEST_DIR"), "/tests/ref/vim_large_window_scroll/size.json")
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut grid: Grid<Cell> = json::from_str(&serialized_grid).unwrap();
|
|
|
|
let size: SizeInfo = json::from_str(&serialized_size).unwrap();
|
|
|
|
|
2017-01-09 03:18:39 +00:00
|
|
|
let mut terminal = Term::new(&Default::default(), size);
|
2016-11-28 22:13:11 +00:00
|
|
|
mem::swap(&mut terminal.grid, &mut grid);
|
|
|
|
|
|
|
|
b.iter(|| {
|
2016-12-22 18:43:06 +00:00
|
|
|
let iter = terminal.renderable_cells(&Selection::Empty);
|
2016-11-28 22:13:11 +00:00
|
|
|
for cell in iter {
|
|
|
|
test::black_box(cell);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|