feat: add rustfmt, update action workflow to check for formatting (#45)

This commit is contained in:
Denis Maximov 2020-11-14 19:59:37 +02:00 committed by GitHub
parent b66dffe5cf
commit a2914066bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 2058 additions and 1097 deletions

View file

@ -20,3 +20,19 @@ jobs:
run: cargo build --verbose run: cargo build --verbose
- name: Run tests - name: Run tests
run: cargo test --verbose run: cargo test --verbose
fmt:
name: Rustfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- run: rustup component add rustfmt
- uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

0
rustfmt.toml Normal file
View file

View file

@ -1,9 +1,13 @@
use std::collections::HashMap; use std::collections::HashMap;
fn _debug_log_to_file (message: String) { fn _debug_log_to_file(message: String) {
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::prelude::*; use std::io::prelude::*;
let mut file = OpenOptions::new().append(true).create(true).open("/tmp/mosaic-log.txt").unwrap(); let mut file = OpenOptions::new()
.append(true)
.create(true)
.open("/tmp/mosaic-log.txt")
.unwrap();
file.write_all(message.as_bytes()).unwrap(); file.write_all(message.as_bytes()).unwrap();
file.write_all("\n".as_bytes()).unwrap(); file.write_all("\n".as_bytes()).unwrap();
} }
@ -28,81 +32,139 @@ fn combine_symbols(current_symbol: &str, next_symbol: &str) -> Option<&'static s
match (current_symbol, next_symbol) { match (current_symbol, next_symbol) {
(boundary_type::TOP_RIGHT, boundary_type::TOP_RIGHT) => Some(boundary_type::TOP_RIGHT), // (┐, ┐) => Some(┐) (boundary_type::TOP_RIGHT, boundary_type::TOP_RIGHT) => Some(boundary_type::TOP_RIGHT), // (┐, ┐) => Some(┐)
(boundary_type::TOP_RIGHT, boundary_type::VERTICAL) => Some(boundary_type::VERTICAL_LEFT), // (┐, │) => Some(┤) (boundary_type::TOP_RIGHT, boundary_type::VERTICAL) => Some(boundary_type::VERTICAL_LEFT), // (┐, │) => Some(┤)
(boundary_type::TOP_RIGHT, boundary_type::HORIZONTAL) => Some(boundary_type::HORIZONTAL_DOWN), // (┐, ─) => Some(┬) (boundary_type::TOP_RIGHT, boundary_type::HORIZONTAL) => {
Some(boundary_type::HORIZONTAL_DOWN)
} // (┐, ─) => Some(┬)
(boundary_type::TOP_RIGHT, boundary_type::TOP_LEFT) => Some(boundary_type::HORIZONTAL_DOWN), // (┐, ┌) => Some(┬) (boundary_type::TOP_RIGHT, boundary_type::TOP_LEFT) => Some(boundary_type::HORIZONTAL_DOWN), // (┐, ┌) => Some(┬)
(boundary_type::TOP_RIGHT, boundary_type::BOTTOM_RIGHT) => Some(boundary_type::VERTICAL_LEFT), // (┐, ┘) => Some(┤) (boundary_type::TOP_RIGHT, boundary_type::BOTTOM_RIGHT) => {
Some(boundary_type::VERTICAL_LEFT)
} // (┐, ┘) => Some(┤)
(boundary_type::TOP_RIGHT, boundary_type::BOTTOM_LEFT) => Some(boundary_type::CROSS), // (┐, └) => Some(┼) (boundary_type::TOP_RIGHT, boundary_type::BOTTOM_LEFT) => Some(boundary_type::CROSS), // (┐, └) => Some(┼)
(boundary_type::TOP_RIGHT, boundary_type::VERTICAL_LEFT) => Some(boundary_type::VERTICAL_LEFT), // (┐, ┤) => Some(┤) (boundary_type::TOP_RIGHT, boundary_type::VERTICAL_LEFT) => {
Some(boundary_type::VERTICAL_LEFT)
} // (┐, ┤) => Some(┤)
(boundary_type::TOP_RIGHT, boundary_type::VERTICAL_RIGHT) => Some(boundary_type::CROSS), // (┐, ├) => Some(┼) (boundary_type::TOP_RIGHT, boundary_type::VERTICAL_RIGHT) => Some(boundary_type::CROSS), // (┐, ├) => Some(┼)
(boundary_type::TOP_RIGHT, boundary_type::HORIZONTAL_DOWN) => Some(boundary_type::HORIZONTAL_DOWN), // (┐, ┬) => Some(┬) (boundary_type::TOP_RIGHT, boundary_type::HORIZONTAL_DOWN) => {
Some(boundary_type::HORIZONTAL_DOWN)
} // (┐, ┬) => Some(┬)
(boundary_type::TOP_RIGHT, boundary_type::HORIZONTAL_UP) => Some(boundary_type::CROSS), // (┐, ┴) => Some(┼) (boundary_type::TOP_RIGHT, boundary_type::HORIZONTAL_UP) => Some(boundary_type::CROSS), // (┐, ┴) => Some(┼)
(boundary_type::TOP_RIGHT, boundary_type::CROSS) => Some(boundary_type::CROSS), // (┐, ┼) => Some(┼) (boundary_type::TOP_RIGHT, boundary_type::CROSS) => Some(boundary_type::CROSS), // (┐, ┼) => Some(┼)
(boundary_type::HORIZONTAL, boundary_type::HORIZONTAL) => Some(boundary_type::HORIZONTAL), // (─, ─) => Some(─) (boundary_type::HORIZONTAL, boundary_type::HORIZONTAL) => Some(boundary_type::HORIZONTAL), // (─, ─) => Some(─)
(boundary_type::HORIZONTAL, boundary_type::VERTICAL) => Some(boundary_type::CROSS), // (─, │) => Some(┼) (boundary_type::HORIZONTAL, boundary_type::VERTICAL) => Some(boundary_type::CROSS), // (─, │) => Some(┼)
(boundary_type::HORIZONTAL, boundary_type::TOP_LEFT) => Some(boundary_type::HORIZONTAL_DOWN), // (─, ┌) => Some(┬) (boundary_type::HORIZONTAL, boundary_type::TOP_LEFT) => {
(boundary_type::HORIZONTAL, boundary_type::BOTTOM_RIGHT) => Some(boundary_type::HORIZONTAL_UP), // (─, ┘) => Some(┴) Some(boundary_type::HORIZONTAL_DOWN)
(boundary_type::HORIZONTAL, boundary_type::BOTTOM_LEFT) => Some(boundary_type::HORIZONTAL_UP), // (─, └) => Some(┴) } // (─, ┌) => Some(┬)
(boundary_type::HORIZONTAL, boundary_type::BOTTOM_RIGHT) => {
Some(boundary_type::HORIZONTAL_UP)
} // (─, ┘) => Some(┴)
(boundary_type::HORIZONTAL, boundary_type::BOTTOM_LEFT) => {
Some(boundary_type::HORIZONTAL_UP)
} // (─, └) => Some(┴)
(boundary_type::HORIZONTAL, boundary_type::VERTICAL_LEFT) => Some(boundary_type::CROSS), // (─, ┤) => Some(┼) (boundary_type::HORIZONTAL, boundary_type::VERTICAL_LEFT) => Some(boundary_type::CROSS), // (─, ┤) => Some(┼)
(boundary_type::HORIZONTAL, boundary_type::VERTICAL_RIGHT) => Some(boundary_type::CROSS), // (─, ├) => Some(┼) (boundary_type::HORIZONTAL, boundary_type::VERTICAL_RIGHT) => Some(boundary_type::CROSS), // (─, ├) => Some(┼)
(boundary_type::HORIZONTAL, boundary_type::HORIZONTAL_DOWN) => Some(boundary_type::HORIZONTAL_DOWN), // (─, ┬) => Some(┬) (boundary_type::HORIZONTAL, boundary_type::HORIZONTAL_DOWN) => {
(boundary_type::HORIZONTAL, boundary_type::HORIZONTAL_UP) => Some(boundary_type::HORIZONTAL_UP), // (─, ┴) => Some(┴) Some(boundary_type::HORIZONTAL_DOWN)
} // (─, ┬) => Some(┬)
(boundary_type::HORIZONTAL, boundary_type::HORIZONTAL_UP) => {
Some(boundary_type::HORIZONTAL_UP)
} // (─, ┴) => Some(┴)
(boundary_type::HORIZONTAL, boundary_type::CROSS) => Some(boundary_type::CROSS), // (─, ┼) => Some(┼) (boundary_type::HORIZONTAL, boundary_type::CROSS) => Some(boundary_type::CROSS), // (─, ┼) => Some(┼)
(boundary_type::VERTICAL, boundary_type::VERTICAL) => Some(boundary_type::VERTICAL), // (│, │) => Some(│) (boundary_type::VERTICAL, boundary_type::VERTICAL) => Some(boundary_type::VERTICAL), // (│, │) => Some(│)
(boundary_type::VERTICAL, boundary_type::TOP_LEFT) => Some(boundary_type::VERTICAL_RIGHT), // (│, ┌) => Some(├) (boundary_type::VERTICAL, boundary_type::TOP_LEFT) => Some(boundary_type::VERTICAL_RIGHT), // (│, ┌) => Some(├)
(boundary_type::VERTICAL, boundary_type::BOTTOM_RIGHT) => Some(boundary_type::VERTICAL_LEFT), // (│, ┘) => Some(┤) (boundary_type::VERTICAL, boundary_type::BOTTOM_RIGHT) => {
(boundary_type::VERTICAL, boundary_type::BOTTOM_LEFT) => Some(boundary_type::VERTICAL_RIGHT), // (│, └) => Some(├) Some(boundary_type::VERTICAL_LEFT)
(boundary_type::VERTICAL, boundary_type::VERTICAL_LEFT) => Some(boundary_type::VERTICAL_LEFT), // (│, ┤) => Some(┤) } // (│, ┘) => Some(┤)
(boundary_type::VERTICAL, boundary_type::VERTICAL_RIGHT) => Some(boundary_type::VERTICAL_RIGHT), // (│, ├) => Some(├) (boundary_type::VERTICAL, boundary_type::BOTTOM_LEFT) => {
Some(boundary_type::VERTICAL_RIGHT)
} // (│, └) => Some(├)
(boundary_type::VERTICAL, boundary_type::VERTICAL_LEFT) => {
Some(boundary_type::VERTICAL_LEFT)
} // (│, ┤) => Some(┤)
(boundary_type::VERTICAL, boundary_type::VERTICAL_RIGHT) => {
Some(boundary_type::VERTICAL_RIGHT)
} // (│, ├) => Some(├)
(boundary_type::VERTICAL, boundary_type::HORIZONTAL_DOWN) => Some(boundary_type::CROSS), // (│, ┬) => Some(┼) (boundary_type::VERTICAL, boundary_type::HORIZONTAL_DOWN) => Some(boundary_type::CROSS), // (│, ┬) => Some(┼)
(boundary_type::VERTICAL, boundary_type::HORIZONTAL_UP) => Some(boundary_type::CROSS), // (│, ┴) => Some(┼) (boundary_type::VERTICAL, boundary_type::HORIZONTAL_UP) => Some(boundary_type::CROSS), // (│, ┴) => Some(┼)
(boundary_type::VERTICAL, boundary_type::CROSS) => Some(boundary_type::CROSS), // (│, ┼) => Some(┼) (boundary_type::VERTICAL, boundary_type::CROSS) => Some(boundary_type::CROSS), // (│, ┼) => Some(┼)
(boundary_type::TOP_LEFT, boundary_type::TOP_LEFT) => Some(boundary_type::TOP_LEFT), // (┌, ┌) => Some(┌) (boundary_type::TOP_LEFT, boundary_type::TOP_LEFT) => Some(boundary_type::TOP_LEFT), // (┌, ┌) => Some(┌)
(boundary_type::TOP_LEFT, boundary_type::BOTTOM_RIGHT) => Some(boundary_type::CROSS), // (┌, ┘) => Some(┼) (boundary_type::TOP_LEFT, boundary_type::BOTTOM_RIGHT) => Some(boundary_type::CROSS), // (┌, ┘) => Some(┼)
(boundary_type::TOP_LEFT, boundary_type::BOTTOM_LEFT) => Some(boundary_type::VERTICAL_RIGHT), // (┌, └) => Some(├) (boundary_type::TOP_LEFT, boundary_type::BOTTOM_LEFT) => {
Some(boundary_type::VERTICAL_RIGHT)
} // (┌, └) => Some(├)
(boundary_type::TOP_LEFT, boundary_type::VERTICAL_LEFT) => Some(boundary_type::CROSS), // (┌, ┤) => Some(┼) (boundary_type::TOP_LEFT, boundary_type::VERTICAL_LEFT) => Some(boundary_type::CROSS), // (┌, ┤) => Some(┼)
(boundary_type::TOP_LEFT, boundary_type::VERTICAL_RIGHT) => Some(boundary_type::VERTICAL_RIGHT), // (┌, ├) => Some(├) (boundary_type::TOP_LEFT, boundary_type::VERTICAL_RIGHT) => {
(boundary_type::TOP_LEFT, boundary_type::HORIZONTAL_DOWN) => Some(boundary_type::HORIZONTAL_DOWN), // (┌, ┬) => Some(┬) Some(boundary_type::VERTICAL_RIGHT)
} // (┌, ├) => Some(├)
(boundary_type::TOP_LEFT, boundary_type::HORIZONTAL_DOWN) => {
Some(boundary_type::HORIZONTAL_DOWN)
} // (┌, ┬) => Some(┬)
(boundary_type::TOP_LEFT, boundary_type::HORIZONTAL_UP) => Some(boundary_type::CROSS), // (┌, ┴) => Some(┼) (boundary_type::TOP_LEFT, boundary_type::HORIZONTAL_UP) => Some(boundary_type::CROSS), // (┌, ┴) => Some(┼)
(boundary_type::TOP_LEFT, boundary_type::CROSS) => Some(boundary_type::CROSS), // (┌, ┼) => Some(┼) (boundary_type::TOP_LEFT, boundary_type::CROSS) => Some(boundary_type::CROSS), // (┌, ┼) => Some(┼)
(boundary_type::BOTTOM_RIGHT, boundary_type::BOTTOM_RIGHT) => Some(boundary_type::BOTTOM_RIGHT), // (┘, ┘) => Some(┘) (boundary_type::BOTTOM_RIGHT, boundary_type::BOTTOM_RIGHT) => {
(boundary_type::BOTTOM_RIGHT, boundary_type::BOTTOM_LEFT) => Some(boundary_type::HORIZONTAL_UP), // (┘, └) => Some(┴) Some(boundary_type::BOTTOM_RIGHT)
(boundary_type::BOTTOM_RIGHT, boundary_type::VERTICAL_LEFT) => Some(boundary_type::VERTICAL_LEFT), // (┘, ┤) => Some(┤) } // (┘, ┘) => Some(┘)
(boundary_type::BOTTOM_RIGHT, boundary_type::BOTTOM_LEFT) => {
Some(boundary_type::HORIZONTAL_UP)
} // (┘, └) => Some(┴)
(boundary_type::BOTTOM_RIGHT, boundary_type::VERTICAL_LEFT) => {
Some(boundary_type::VERTICAL_LEFT)
} // (┘, ┤) => Some(┤)
(boundary_type::BOTTOM_RIGHT, boundary_type::VERTICAL_RIGHT) => Some(boundary_type::CROSS), // (┘, ├) => Some(┼) (boundary_type::BOTTOM_RIGHT, boundary_type::VERTICAL_RIGHT) => Some(boundary_type::CROSS), // (┘, ├) => Some(┼)
(boundary_type::BOTTOM_RIGHT, boundary_type::HORIZONTAL_DOWN) => Some(boundary_type::CROSS), // (┘, ┬) => Some(┼) (boundary_type::BOTTOM_RIGHT, boundary_type::HORIZONTAL_DOWN) => Some(boundary_type::CROSS), // (┘, ┬) => Some(┼)
(boundary_type::BOTTOM_RIGHT, boundary_type::HORIZONTAL_UP) => Some(boundary_type::HORIZONTAL_UP), // (┘, ┴) => Some(┴) (boundary_type::BOTTOM_RIGHT, boundary_type::HORIZONTAL_UP) => {
Some(boundary_type::HORIZONTAL_UP)
} // (┘, ┴) => Some(┴)
(boundary_type::BOTTOM_RIGHT, boundary_type::CROSS) => Some(boundary_type::CROSS), // (┘, ┼) => Some(┼) (boundary_type::BOTTOM_RIGHT, boundary_type::CROSS) => Some(boundary_type::CROSS), // (┘, ┼) => Some(┼)
(boundary_type::BOTTOM_LEFT, boundary_type::BOTTOM_LEFT) => Some(boundary_type::BOTTOM_LEFT), // (└, └) => Some(└) (boundary_type::BOTTOM_LEFT, boundary_type::BOTTOM_LEFT) => {
Some(boundary_type::BOTTOM_LEFT)
} // (└, └) => Some(└)
(boundary_type::BOTTOM_LEFT, boundary_type::VERTICAL_LEFT) => Some(boundary_type::CROSS), // (└, ┤) => Some(┼) (boundary_type::BOTTOM_LEFT, boundary_type::VERTICAL_LEFT) => Some(boundary_type::CROSS), // (└, ┤) => Some(┼)
(boundary_type::BOTTOM_LEFT, boundary_type::VERTICAL_RIGHT) => Some(boundary_type::VERTICAL_RIGHT), // (└, ├) => Some(├) (boundary_type::BOTTOM_LEFT, boundary_type::VERTICAL_RIGHT) => {
Some(boundary_type::VERTICAL_RIGHT)
} // (└, ├) => Some(├)
(boundary_type::BOTTOM_LEFT, boundary_type::HORIZONTAL_DOWN) => Some(boundary_type::CROSS), // (└, ┬) => Some(┼) (boundary_type::BOTTOM_LEFT, boundary_type::HORIZONTAL_DOWN) => Some(boundary_type::CROSS), // (└, ┬) => Some(┼)
(boundary_type::BOTTOM_LEFT, boundary_type::HORIZONTAL_UP) => Some(boundary_type::HORIZONTAL_UP), // (└, ┴) => Some(┴) (boundary_type::BOTTOM_LEFT, boundary_type::HORIZONTAL_UP) => {
Some(boundary_type::HORIZONTAL_UP)
} // (└, ┴) => Some(┴)
(boundary_type::BOTTOM_LEFT, boundary_type::CROSS) => Some(boundary_type::CROSS), // (└, ┼) => Some(┼) (boundary_type::BOTTOM_LEFT, boundary_type::CROSS) => Some(boundary_type::CROSS), // (└, ┼) => Some(┼)
(boundary_type::VERTICAL_LEFT, boundary_type::VERTICAL_LEFT) => Some(boundary_type::VERTICAL_LEFT), // (┤, ┤) => Some(┤) (boundary_type::VERTICAL_LEFT, boundary_type::VERTICAL_LEFT) => {
Some(boundary_type::VERTICAL_LEFT)
} // (┤, ┤) => Some(┤)
(boundary_type::VERTICAL_LEFT, boundary_type::VERTICAL_RIGHT) => Some(boundary_type::CROSS), // (┤, ├) => Some(┼) (boundary_type::VERTICAL_LEFT, boundary_type::VERTICAL_RIGHT) => Some(boundary_type::CROSS), // (┤, ├) => Some(┼)
(boundary_type::VERTICAL_LEFT, boundary_type::HORIZONTAL_DOWN) => Some(boundary_type::CROSS), // (┤, ┬) => Some(┼) (boundary_type::VERTICAL_LEFT, boundary_type::HORIZONTAL_DOWN) => {
(boundary_type::VERTICAL_LEFT, boundary_type::HORIZONTAL_UP) => Some(boundary_type::HORIZONTAL_UP), // (┤, ┴) => Some(┼) Some(boundary_type::CROSS)
} // (┤, ┬) => Some(┼)
(boundary_type::VERTICAL_LEFT, boundary_type::HORIZONTAL_UP) => {
Some(boundary_type::HORIZONTAL_UP)
} // (┤, ┴) => Some(┼)
(boundary_type::VERTICAL_LEFT, boundary_type::CROSS) => Some(boundary_type::CROSS), // (┤, ┼) => Some(┼) (boundary_type::VERTICAL_LEFT, boundary_type::CROSS) => Some(boundary_type::CROSS), // (┤, ┼) => Some(┼)
(boundary_type::VERTICAL_RIGHT, boundary_type::VERTICAL_RIGHT) => { (boundary_type::VERTICAL_RIGHT, boundary_type::VERTICAL_RIGHT) => {
Some(boundary_type::VERTICAL_RIGHT) Some(boundary_type::VERTICAL_RIGHT)
} // (├, ├) => Some(├) } // (├, ├) => Some(├)
(boundary_type::VERTICAL_RIGHT, boundary_type::HORIZONTAL_DOWN) => Some(boundary_type::CROSS), // (├, ┬) => Some(┼) (boundary_type::VERTICAL_RIGHT, boundary_type::HORIZONTAL_DOWN) => {
Some(boundary_type::CROSS)
} // (├, ┬) => Some(┼)
(boundary_type::VERTICAL_RIGHT, boundary_type::HORIZONTAL_UP) => Some(boundary_type::CROSS), // (├, ┴) => Some(┼) (boundary_type::VERTICAL_RIGHT, boundary_type::HORIZONTAL_UP) => Some(boundary_type::CROSS), // (├, ┴) => Some(┼)
(boundary_type::VERTICAL_RIGHT, boundary_type::CROSS) => Some(boundary_type::CROSS), // (├, ┼) => Some(┼) (boundary_type::VERTICAL_RIGHT, boundary_type::CROSS) => Some(boundary_type::CROSS), // (├, ┼) => Some(┼)
(boundary_type::HORIZONTAL_DOWN, boundary_type::HORIZONTAL_DOWN) => { (boundary_type::HORIZONTAL_DOWN, boundary_type::HORIZONTAL_DOWN) => {
Some(boundary_type::HORIZONTAL_DOWN) Some(boundary_type::HORIZONTAL_DOWN)
} // (┬, ┬) => Some(┬) } // (┬, ┬) => Some(┬)
(boundary_type::HORIZONTAL_DOWN, boundary_type::HORIZONTAL_UP) => Some(boundary_type::CROSS), // (┬, ┴) => Some(┼) (boundary_type::HORIZONTAL_DOWN, boundary_type::HORIZONTAL_UP) => {
Some(boundary_type::CROSS)
} // (┬, ┴) => Some(┼)
(boundary_type::HORIZONTAL_DOWN, boundary_type::CROSS) => Some(boundary_type::CROSS), // (┬, ┼) => Some(┼) (boundary_type::HORIZONTAL_DOWN, boundary_type::CROSS) => Some(boundary_type::CROSS), // (┬, ┼) => Some(┼)
(boundary_type::HORIZONTAL_UP, boundary_type::HORIZONTAL_UP) => Some(boundary_type::HORIZONTAL_UP), // (┴, ┴) => Some(┴) (boundary_type::HORIZONTAL_UP, boundary_type::HORIZONTAL_UP) => {
Some(boundary_type::HORIZONTAL_UP)
} // (┴, ┴) => Some(┴)
(boundary_type::HORIZONTAL_UP, boundary_type::CROSS) => Some(boundary_type::CROSS), // (┴, ┼) => Some(┼) (boundary_type::HORIZONTAL_UP, boundary_type::CROSS) => Some(boundary_type::CROSS), // (┴, ┼) => Some(┼)
(boundary_type::CROSS, boundary_type::CROSS) => Some(boundary_type::CROSS), // (┼, ┼) => Some(┼) (boundary_type::CROSS, boundary_type::CROSS) => Some(boundary_type::CROSS), // (┼, ┼) => Some(┼)
@ -127,7 +189,7 @@ pub struct Coordinates {
impl Coordinates { impl Coordinates {
pub fn new(x: usize, y: usize) -> Self { pub fn new(x: usize, y: usize) -> Self {
Coordinates {x, y} Coordinates { x, y }
} }
} }
@ -136,10 +198,10 @@ pub trait Rect {
fn y(&self) -> usize; fn y(&self) -> usize;
fn rows(&self) -> usize; fn rows(&self) -> usize;
fn columns(&self) -> usize; fn columns(&self) -> usize;
fn right_boundary_x_coords (&self) -> usize { fn right_boundary_x_coords(&self) -> usize {
self.x() + self.columns() self.x() + self.columns()
} }
fn bottom_boundary_y_coords (&self) -> usize { fn bottom_boundary_y_coords(&self) -> usize {
self.y() + self.rows() self.y() + self.rows()
} }
} }
@ -175,7 +237,9 @@ impl Boundaries {
} else { } else {
boundary_type::VERTICAL boundary_type::VERTICAL
}; };
let next_symbol = self.boundary_characters.get(&coordinates) let next_symbol = self
.boundary_characters
.get(&coordinates)
.and_then(|current_symbol| find_next_symbol(current_symbol, symbol_to_add)) .and_then(|current_symbol| find_next_symbol(current_symbol, symbol_to_add))
.unwrap_or(symbol_to_add); .unwrap_or(symbol_to_add);
self.boundary_characters.insert(coordinates, next_symbol); self.boundary_characters.insert(coordinates, next_symbol);
@ -194,42 +258,65 @@ impl Boundaries {
} else { } else {
boundary_type::HORIZONTAL boundary_type::HORIZONTAL
}; };
let next_symbol = self.boundary_characters.get(&coordinates) let next_symbol = self
.boundary_characters
.get(&coordinates)
.and_then(|current_symbol| find_next_symbol(current_symbol, symbol_to_add)) .and_then(|current_symbol| find_next_symbol(current_symbol, symbol_to_add))
.unwrap_or(symbol_to_add); .unwrap_or(symbol_to_add);
self.boundary_characters.insert(coordinates, next_symbol); self.boundary_characters.insert(coordinates, next_symbol);
} }
} }
} }
pub fn vte_output (&self) -> String { pub fn vte_output(&self) -> String {
let mut vte_output = String::new(); let mut vte_output = String::new();
for (coordinates, boundary_character) in &self.boundary_characters { for (coordinates, boundary_character) in &self.boundary_characters {
vte_output.push_str(&format!("\u{1b}[{};{}H\u{1b}[m{}", coordinates.y + 1, coordinates.x + 1, boundary_character)); // goto row/col + boundary character vte_output.push_str(&format!(
"\u{1b}[{};{}H\u{1b}[m{}",
coordinates.y + 1,
coordinates.x + 1,
boundary_character
)); // goto row/col + boundary character
} }
vte_output vte_output
} }
fn rect_right_boundary_is_before_screen_edge <R: Rect>(&self, rect: &R) -> bool { fn rect_right_boundary_is_before_screen_edge<R: Rect>(&self, rect: &R) -> bool {
rect.x() + rect.columns() < self.columns rect.x() + rect.columns() < self.columns
} }
fn rect_bottom_boundary_is_before_screen_edge<R: Rect>(&self, rect: &R) -> bool { fn rect_bottom_boundary_is_before_screen_edge<R: Rect>(&self, rect: &R) -> bool {
rect.y() + rect.rows() < self.rows rect.y() + rect.rows() < self.rows
} }
fn rect_right_boundary_row_start<R: Rect>(&self, rect: &R) -> usize { fn rect_right_boundary_row_start<R: Rect>(&self, rect: &R) -> usize {
if rect.y() == 0 { 0 } else { rect.y() - 1 } if rect.y() == 0 {
0
} else {
rect.y() - 1
}
} }
fn rect_right_boundary_row_end<R: Rect>(&self, rect: &R) -> usize { fn rect_right_boundary_row_end<R: Rect>(&self, rect: &R) -> usize {
let rect_bottom_row = rect.y() + rect.rows(); let rect_bottom_row = rect.y() + rect.rows();
// we do this because unless we're on the screen edge, we'd like to go one extra row to // we do this because unless we're on the screen edge, we'd like to go one extra row to
// connect to whatever boundary is beneath us // connect to whatever boundary is beneath us
if rect_bottom_row == self.rows { rect_bottom_row } else { rect_bottom_row + 1 } if rect_bottom_row == self.rows {
rect_bottom_row
} else {
rect_bottom_row + 1
}
} }
fn rect_bottom_boundary_col_start<R: Rect>(&self, rect: &R) -> usize { fn rect_bottom_boundary_col_start<R: Rect>(&self, rect: &R) -> usize {
if rect.x() == 0 { 0 } else { rect.x() - 1 } if rect.x() == 0 {
0
} else {
rect.x() - 1
}
} }
fn rect_bottom_boundary_col_end<R: Rect>(&self, rect: &R) -> usize { fn rect_bottom_boundary_col_end<R: Rect>(&self, rect: &R) -> usize {
let rect_right_col = rect.x() + rect.columns(); let rect_right_col = rect.x() + rect.columns();
// we do this because unless we're on the screen edge, we'd like to go one extra column to // we do this because unless we're on the screen edge, we'd like to go one extra column to
// connect to whatever boundary is right of us // connect to whatever boundary is right of us
if rect_right_col == self.columns { rect_right_col } else { rect_right_col + 1 } if rect_right_col == self.columns {
rect_right_col
} else {
rect_right_col + 1
}
} }
} }

View file

@ -1,23 +1,23 @@
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
mod boundaries;
mod os_input_output; mod os_input_output;
mod terminal_pane;
mod pty_bus; mod pty_bus;
mod screen; mod screen;
mod boundaries; mod terminal_pane;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::thread;
use std::os::unix::net::UnixStream; use std::os::unix::net::UnixStream;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::mpsc::{channel, Sender, Receiver}; use std::sync::mpsc::{channel, Receiver, Sender};
use std::thread;
use serde::{Serialize, Deserialize}; use serde::{Deserialize, Serialize};
use structopt::StructOpt; use structopt::StructOpt;
use crate::os_input_output::{get_os_input, OsApi}; use crate::os_input_output::{get_os_input, OsApi};
use crate::pty_bus::{VteEvent, PtyBus, PtyInstruction}; use crate::pty_bus::{PtyBus, PtyInstruction, VteEvent};
use crate::screen::{Screen, ScreenInstruction}; use crate::screen::{Screen, ScreenInstruction};
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
@ -45,13 +45,17 @@ pub struct Opt {
max_panes: Option<usize>, max_panes: Option<usize>,
#[structopt(short, long)] #[structopt(short, long)]
debug: bool debug: bool,
} }
fn _debug_log_to_file (message: String) { fn _debug_log_to_file(message: String) {
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::prelude::*; use std::io::prelude::*;
let mut file = OpenOptions::new().append(true).create(true).open("/tmp/mosaic-log.txt").unwrap(); let mut file = OpenOptions::new()
.append(true)
.create(true)
.open("/tmp/mosaic-log.txt")
.unwrap();
file.write_all(message.as_bytes()).unwrap(); file.write_all(message.as_bytes()).unwrap();
file.write_all("\n".as_bytes()).unwrap(); file.write_all("\n".as_bytes()).unwrap();
} }
@ -70,12 +74,12 @@ pub fn main() {
let mut stream = UnixStream::connect("/tmp/mosaic").unwrap(); let mut stream = UnixStream::connect("/tmp/mosaic").unwrap();
let api_command = bincode::serialize(&ApiCommand::SplitHorizontally).unwrap(); let api_command = bincode::serialize(&ApiCommand::SplitHorizontally).unwrap();
stream.write_all(&api_command).unwrap(); stream.write_all(&api_command).unwrap();
}, }
Some('v') => { Some('v') => {
let mut stream = UnixStream::connect("/tmp/mosaic").unwrap(); let mut stream = UnixStream::connect("/tmp/mosaic").unwrap();
let api_command = bincode::serialize(&ApiCommand::SplitVertically).unwrap(); let api_command = bincode::serialize(&ApiCommand::SplitVertically).unwrap();
stream.write_all(&api_command).unwrap(); stream.write_all(&api_command).unwrap();
}, }
_ => {} _ => {}
}; };
} else if opts.move_focus { } else if opts.move_focus {
@ -94,7 +98,7 @@ pub fn main() {
} }
pub enum AppInstruction { pub enum AppInstruction {
Exit Exit,
} }
pub fn start(mut os_input: Box<dyn OsApi>, opts: Opt) { pub fn start(mut os_input: Box<dyn OsApi>, opts: Opt) {
@ -104,11 +108,32 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: Opt) {
let full_screen_ws = os_input.get_terminal_size_using_fd(0); let full_screen_ws = os_input.get_terminal_size_using_fd(0);
os_input.into_raw_mode(0); os_input.into_raw_mode(0);
let (send_screen_instructions, receive_screen_instructions): (Sender<ScreenInstruction>, Receiver<ScreenInstruction>) = channel(); let (send_screen_instructions, receive_screen_instructions): (
let (send_pty_instructions, receive_pty_instructions): (Sender<PtyInstruction>, Receiver<PtyInstruction>) = channel(); Sender<ScreenInstruction>,
let (send_app_instructions, receive_app_instructions): (Sender<AppInstruction>, Receiver<AppInstruction>) = channel(); Receiver<ScreenInstruction>,
let mut screen = Screen::new(receive_screen_instructions, send_pty_instructions.clone(), send_app_instructions.clone(), &full_screen_ws, os_input.clone(), opts.max_panes); ) = channel();
let mut pty_bus = PtyBus::new(receive_pty_instructions, send_screen_instructions.clone(), os_input.clone(), opts.debug); let (send_pty_instructions, receive_pty_instructions): (
Sender<PtyInstruction>,
Receiver<PtyInstruction>,
) = channel();
let (send_app_instructions, receive_app_instructions): (
Sender<AppInstruction>,
Receiver<AppInstruction>,
) = channel();
let mut screen = Screen::new(
receive_screen_instructions,
send_pty_instructions.clone(),
send_app_instructions.clone(),
&full_screen_ws,
os_input.clone(),
opts.max_panes,
);
let mut pty_bus = PtyBus::new(
receive_pty_instructions,
send_screen_instructions.clone(),
os_input.clone(),
opts.debug,
);
active_threads.push( active_threads.push(
thread::Builder::new() thread::Builder::new()
@ -117,7 +142,8 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: Opt) {
move || { move || {
pty_bus.spawn_terminal_vertically(None); pty_bus.spawn_terminal_vertically(None);
loop { loop {
let event = pty_bus.receive_pty_instructions let event = pty_bus
.receive_pty_instructions
.recv() .recv()
.expect("failed to receive event on channel"); .expect("failed to receive event on channel");
match event { match event {
@ -139,77 +165,78 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: Opt) {
} }
} }
} }
}).unwrap() })
.unwrap(),
); );
active_threads.push( active_threads.push(
thread::Builder::new() thread::Builder::new()
.name("screen".to_string()) .name("screen".to_string())
.spawn({ .spawn({
move || { move || loop {
loop { let event = screen
let event = screen.receiver .receiver
.recv() .recv()
.expect("failed to receive event on channel"); .expect("failed to receive event on channel");
match event { match event {
ScreenInstruction::Pty(pid, vte_event) => { ScreenInstruction::Pty(pid, vte_event) => {
screen.handle_pty_event(pid, vte_event); screen.handle_pty_event(pid, vte_event);
}, }
ScreenInstruction::Render => { ScreenInstruction::Render => {
screen.render(); screen.render();
}, }
ScreenInstruction::NewPane(pid) => { ScreenInstruction::NewPane(pid) => {
screen.new_pane(pid); screen.new_pane(pid);
} }
ScreenInstruction::HorizontalSplit(pid) => { ScreenInstruction::HorizontalSplit(pid) => {
screen.horizontal_split(pid); screen.horizontal_split(pid);
} }
ScreenInstruction::VerticalSplit(pid) => { ScreenInstruction::VerticalSplit(pid) => {
screen.vertical_split(pid); screen.vertical_split(pid);
} }
ScreenInstruction::WriteCharacter(bytes) => { ScreenInstruction::WriteCharacter(bytes) => {
screen.write_to_active_terminal(bytes); screen.write_to_active_terminal(bytes);
} }
ScreenInstruction::ResizeLeft => { ScreenInstruction::ResizeLeft => {
screen.resize_left(); screen.resize_left();
} }
ScreenInstruction::ResizeRight => { ScreenInstruction::ResizeRight => {
screen.resize_right(); screen.resize_right();
} }
ScreenInstruction::ResizeDown => { ScreenInstruction::ResizeDown => {
screen.resize_down(); screen.resize_down();
} }
ScreenInstruction::ResizeUp => { ScreenInstruction::ResizeUp => {
screen.resize_up(); screen.resize_up();
} }
ScreenInstruction::MoveFocus => { ScreenInstruction::MoveFocus => {
screen.move_focus(); screen.move_focus();
} }
ScreenInstruction::ScrollUp => { ScreenInstruction::ScrollUp => {
screen.scroll_active_terminal_up(); screen.scroll_active_terminal_up();
} }
ScreenInstruction::ScrollDown => { ScreenInstruction::ScrollDown => {
screen.scroll_active_terminal_down(); screen.scroll_active_terminal_down();
} }
ScreenInstruction::ClearScroll => { ScreenInstruction::ClearScroll => {
screen.clear_active_terminal_scroll(); screen.clear_active_terminal_scroll();
} }
ScreenInstruction::CloseFocusedPane => { ScreenInstruction::CloseFocusedPane => {
screen.close_focused_pane(); screen.close_focused_pane();
} }
ScreenInstruction::ClosePane(id) => { ScreenInstruction::ClosePane(id) => {
screen.close_pane(id); screen.close_pane(id);
} }
ScreenInstruction::ToggleActiveTerminalFullscreen => { ScreenInstruction::ToggleActiveTerminalFullscreen => {
screen.toggle_active_terminal_fullscreen(); screen.toggle_active_terminal_fullscreen();
} }
ScreenInstruction::Quit => { ScreenInstruction::Quit => {
break; break;
}
} }
} }
} }
}).unwrap() })
.unwrap(),
); );
// TODO: currently we don't push this into active_threads // TODO: currently we don't push this into active_threads
@ -223,27 +250,39 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: Opt) {
let send_screen_instructions = send_screen_instructions.clone(); let send_screen_instructions = send_screen_instructions.clone();
move || { move || {
::std::fs::remove_file("/tmp/mosaic").ok(); ::std::fs::remove_file("/tmp/mosaic").ok();
let listener = ::std::os::unix::net::UnixListener::bind("/tmp/mosaic").expect("could not listen on ipc socket"); let listener = ::std::os::unix::net::UnixListener::bind("/tmp/mosaic")
.expect("could not listen on ipc socket");
for stream in listener.incoming() { for stream in listener.incoming() {
match stream { match stream {
Ok(mut stream) => { Ok(mut stream) => {
let mut buffer = [0; 65535]; // TODO: more accurate let mut buffer = [0; 65535]; // TODO: more accurate
stream.read(&mut buffer).expect("failed to parse ipc message"); stream
let decoded: ApiCommand = bincode::deserialize(&buffer).expect("failed to deserialize ipc message"); .read(&mut buffer)
.expect("failed to parse ipc message");
let decoded: ApiCommand = bincode::deserialize(&buffer)
.expect("failed to deserialize ipc message");
match &decoded { match &decoded {
ApiCommand::OpenFile(file_name) => { ApiCommand::OpenFile(file_name) => {
let path = PathBuf::from(file_name); let path = PathBuf::from(file_name);
send_pty_instructions.send(PtyInstruction::SpawnTerminal(Some(path))).unwrap(); send_pty_instructions
.send(PtyInstruction::SpawnTerminal(Some(path)))
.unwrap();
} }
ApiCommand::SplitHorizontally => { ApiCommand::SplitHorizontally => {
send_pty_instructions.send(PtyInstruction::SpawnTerminalHorizontally(None)).unwrap(); send_pty_instructions
.send(PtyInstruction::SpawnTerminalHorizontally(None))
.unwrap();
} }
ApiCommand::SplitVertically => { ApiCommand::SplitVertically => {
send_pty_instructions.send(PtyInstruction::SpawnTerminalVertically(None)).unwrap(); send_pty_instructions
.send(PtyInstruction::SpawnTerminalVertically(None))
.unwrap();
} }
ApiCommand::MoveFocus => { ApiCommand::MoveFocus => {
send_screen_instructions.send(ScreenInstruction::MoveFocus).unwrap(); send_screen_instructions
.send(ScreenInstruction::MoveFocus)
.unwrap();
} }
} }
} }
@ -253,7 +292,8 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: Opt) {
} }
} }
} }
}).unwrap(); })
.unwrap();
let _stdin_thread = thread::Builder::new() let _stdin_thread = thread::Builder::new()
.name("ipc_server".to_string()) .name("ipc_server".to_string())
@ -270,66 +310,109 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: Opt) {
// uncomment this to print the entered character to a log file (/tmp/mosaic-log.txt) for debugging // uncomment this to print the entered character to a log file (/tmp/mosaic-log.txt) for debugging
// _debug_log_to_file(format!("buffer {:?}", buffer)); // _debug_log_to_file(format!("buffer {:?}", buffer));
match buffer { match buffer {
[10, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-j [10, 0, 0, 0, 0, 0, 0, 0, 0, 0] => {
send_screen_instructions.send(ScreenInstruction::ResizeDown).unwrap(); // ctrl-j
}, send_screen_instructions
[11, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-k .send(ScreenInstruction::ResizeDown)
send_screen_instructions.send(ScreenInstruction::ResizeUp).unwrap(); .unwrap();
}, }
[16, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-p [11, 0, 0, 0, 0, 0, 0, 0, 0, 0] => {
send_screen_instructions.send(ScreenInstruction::MoveFocus).unwrap(); // ctrl-k
}, send_screen_instructions
[8, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-h .send(ScreenInstruction::ResizeUp)
send_screen_instructions.send(ScreenInstruction::ResizeLeft).unwrap(); .unwrap();
}, }
[12, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-l [16, 0, 0, 0, 0, 0, 0, 0, 0, 0] => {
send_screen_instructions.send(ScreenInstruction::ResizeRight).unwrap(); // ctrl-p
}, send_screen_instructions
[26, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-z .send(ScreenInstruction::MoveFocus)
send_pty_instructions.send(PtyInstruction::SpawnTerminal(None)).unwrap(); .unwrap();
}, }
[14, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-n [8, 0, 0, 0, 0, 0, 0, 0, 0, 0] => {
send_pty_instructions.send(PtyInstruction::SpawnTerminalVertically(None)).unwrap(); // ctrl-h
}, send_screen_instructions
[2, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-b .send(ScreenInstruction::ResizeLeft)
send_pty_instructions.send(PtyInstruction::SpawnTerminalHorizontally(None)).unwrap(); .unwrap();
}, }
[17, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-q [12, 0, 0, 0, 0, 0, 0, 0, 0, 0] => {
// ctrl-l
send_screen_instructions
.send(ScreenInstruction::ResizeRight)
.unwrap();
}
[26, 0, 0, 0, 0, 0, 0, 0, 0, 0] => {
// ctrl-z
send_pty_instructions
.send(PtyInstruction::SpawnTerminal(None))
.unwrap();
}
[14, 0, 0, 0, 0, 0, 0, 0, 0, 0] => {
// ctrl-n
send_pty_instructions
.send(PtyInstruction::SpawnTerminalVertically(None))
.unwrap();
}
[2, 0, 0, 0, 0, 0, 0, 0, 0, 0] => {
// ctrl-b
send_pty_instructions
.send(PtyInstruction::SpawnTerminalHorizontally(None))
.unwrap();
}
[17, 0, 0, 0, 0, 0, 0, 0, 0, 0] => {
// ctrl-q
let _ = send_screen_instructions.send(ScreenInstruction::Quit); let _ = send_screen_instructions.send(ScreenInstruction::Quit);
let _ = send_pty_instructions.send(PtyInstruction::Quit); let _ = send_pty_instructions.send(PtyInstruction::Quit);
let _ = send_app_instructions.send(AppInstruction::Exit); let _ = send_app_instructions.send(AppInstruction::Exit);
break; break;
}, }
[27, 91, 53, 94, 0, 0, 0, 0, 0, 0] => { // ctrl-PgUp [27, 91, 53, 94, 0, 0, 0, 0, 0, 0] => {
send_screen_instructions.send(ScreenInstruction::ScrollUp).unwrap(); // ctrl-PgUp
}, send_screen_instructions
[27, 91, 54, 94, 0, 0, 0, 0, 0, 0] => { // ctrl-PgDown .send(ScreenInstruction::ScrollUp)
send_screen_instructions.send(ScreenInstruction::ScrollDown).unwrap(); .unwrap();
}, }
[24, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-x [27, 91, 54, 94, 0, 0, 0, 0, 0, 0] => {
send_screen_instructions.send(ScreenInstruction::CloseFocusedPane).unwrap(); // ctrl-PgDown
send_screen_instructions
.send(ScreenInstruction::ScrollDown)
.unwrap();
}
[24, 0, 0, 0, 0, 0, 0, 0, 0, 0] => {
// ctrl-x
send_screen_instructions
.send(ScreenInstruction::CloseFocusedPane)
.unwrap();
// ::std::thread::sleep(::std::time::Duration::from_millis(10)); // ::std::thread::sleep(::std::time::Duration::from_millis(10));
}, }
[5, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-e [5, 0, 0, 0, 0, 0, 0, 0, 0, 0] => {
send_screen_instructions.send(ScreenInstruction::ToggleActiveTerminalFullscreen).unwrap(); // ctrl-e
}, send_screen_instructions
.send(ScreenInstruction::ToggleActiveTerminalFullscreen)
.unwrap();
}
_ => { _ => {
send_screen_instructions.send(ScreenInstruction::ClearScroll).unwrap(); send_screen_instructions
send_screen_instructions.send(ScreenInstruction::WriteCharacter(buffer)).unwrap(); .send(ScreenInstruction::ClearScroll)
.unwrap();
send_screen_instructions
.send(ScreenInstruction::WriteCharacter(buffer))
.unwrap();
} }
} }
}; }
} }
}); });
loop { loop {
let app_instruction = receive_app_instructions.recv().expect("failed to receive app instruction on channel"); let app_instruction = receive_app_instructions
.recv()
.expect("failed to receive app instruction on channel");
match app_instruction { match app_instruction {
AppInstruction::Exit => { AppInstruction::Exit => {
let _ = send_screen_instructions.send(ScreenInstruction::Quit); let _ = send_screen_instructions.send(ScreenInstruction::Quit);
let _ = send_pty_instructions.send(PtyInstruction::Quit); let _ = send_pty_instructions.send(PtyInstruction::Quit);
break; break;
}, }
} }
} }
@ -339,9 +422,15 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: Opt) {
// cleanup(); // cleanup();
let reset_style = "\u{1b}[m"; let reset_style = "\u{1b}[m";
let goto_start_of_last_line = format!("\u{1b}[{};{}H", full_screen_ws.ws_row, 1); let goto_start_of_last_line = format!("\u{1b}[{};{}H", full_screen_ws.ws_row, 1);
let goodbye_message = format!("{}\n{}Bye from Mosaic!", goto_start_of_last_line, reset_style); let goodbye_message = format!(
"{}\n{}Bye from Mosaic!",
goto_start_of_last_line, reset_style
);
os_input.unset_raw_mode(0); os_input.unset_raw_mode(0);
os_input.get_stdout_writer().write(goodbye_message.as_bytes()).unwrap(); os_input
.get_stdout_writer()
.write(goodbye_message.as_bytes())
.unwrap();
os_input.get_stdout_writer().flush().unwrap(); os_input.get_stdout_writer().flush().unwrap();
} }

View file

@ -1,20 +1,13 @@
use nix::unistd::{read, write, ForkResult, Pid};
use nix::fcntl::{fcntl, FcntlArg, OFlag}; use nix::fcntl::{fcntl, FcntlArg, OFlag};
use nix::sys::termios::{
Termios,
tcgetattr,
cfmakeraw,
tcsetattr,
SetArg,
tcdrain,
};
use nix::sys::wait::waitpid;
use nix::sys::signal::{kill, Signal};
use nix::pty::{forkpty, Winsize}; use nix::pty::{forkpty, Winsize};
use std::os::unix::io::RawFd; use nix::sys::signal::{kill, Signal};
use std::process::{Command, Child}; use nix::sys::termios::{cfmakeraw, tcdrain, tcgetattr, tcsetattr, SetArg, Termios};
use nix::sys::wait::waitpid;
use nix::unistd::{read, write, ForkResult, Pid};
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::os::unix::io::RawFd;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::{Child, Command};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::env; use std::env;
@ -23,15 +16,15 @@ fn into_raw_mode(pid: RawFd) {
let mut tio = tcgetattr(pid).expect("could not get terminal attribute"); let mut tio = tcgetattr(pid).expect("could not get terminal attribute");
cfmakeraw(&mut tio); cfmakeraw(&mut tio);
match tcsetattr(pid, SetArg::TCSANOW, &mut tio) { match tcsetattr(pid, SetArg::TCSANOW, &mut tio) {
Ok(_) => {}, Ok(_) => {}
Err(e) => panic!("error {:?}", e) Err(e) => panic!("error {:?}", e),
}; };
} }
fn unset_raw_mode(pid: RawFd, mut orig_termios: Termios) { fn unset_raw_mode(pid: RawFd, mut orig_termios: Termios) {
match tcsetattr(pid, SetArg::TCSANOW, &mut orig_termios) { match tcsetattr(pid, SetArg::TCSANOW, &mut orig_termios) {
Ok(_) => {}, Ok(_) => {}
Err(e) => panic!("error {:?}", e) Err(e) => panic!("error {:?}", e),
}; };
} }
@ -65,10 +58,14 @@ pub fn set_terminal_size_using_fd(fd: RawFd, columns: u16, rows: u16) {
unsafe { ioctl(fd, TIOCSWINSZ.into(), &winsize) }; unsafe { ioctl(fd, TIOCSWINSZ.into(), &winsize) };
} }
fn _debug_log_to_file (message: String) { fn _debug_log_to_file(message: String) {
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::prelude::*; use std::io::prelude::*;
let mut file = OpenOptions::new().append(true).create(true).open("/tmp/mosaic-log.txt").unwrap(); let mut file = OpenOptions::new()
.append(true)
.create(true)
.open("/tmp/mosaic-log.txt")
.unwrap();
file.write_all(message.as_bytes()).unwrap(); file.write_all(message.as_bytes()).unwrap();
file.write_all("\n".as_bytes()).unwrap(); file.write_all("\n".as_bytes()).unwrap();
} }
@ -96,11 +93,11 @@ fn handle_command_exit(mut child: Child) {
} }
_ => {} _ => {}
} }
}; }
} }
} }
fn spawn_terminal (file_to_open: Option<PathBuf>, orig_termios: Termios) -> (RawFd, RawFd) { fn spawn_terminal(file_to_open: Option<PathBuf>, orig_termios: Termios) -> (RawFd, RawFd) {
let (pid_primary, pid_secondary): (RawFd, RawFd) = { let (pid_primary, pid_secondary): (RawFd, RawFd) = {
match forkpty(None, Some(&orig_termios)) { match forkpty(None, Some(&orig_termios)) {
Ok(fork_pty_res) => { Ok(fork_pty_res) => {
@ -108,26 +105,31 @@ fn spawn_terminal (file_to_open: Option<PathBuf>, orig_termios: Termios) -> (Raw
let pid_secondary = match fork_pty_res.fork_result { let pid_secondary = match fork_pty_res.fork_result {
ForkResult::Parent { child } => { ForkResult::Parent { child } => {
// fcntl(pid_primary, FcntlArg::F_SETFL(OFlag::empty())).expect("could not fcntl"); // fcntl(pid_primary, FcntlArg::F_SETFL(OFlag::empty())).expect("could not fcntl");
fcntl(pid_primary, FcntlArg::F_SETFL(OFlag::O_NONBLOCK)).expect("could not fcntl"); fcntl(pid_primary, FcntlArg::F_SETFL(OFlag::O_NONBLOCK))
.expect("could not fcntl");
child child
}, }
ForkResult::Child => { ForkResult::Child => match file_to_open {
match file_to_open { Some(file_to_open) => {
Some(file_to_open) => { if env::var("EDITOR").is_err() && env::var("VISUAL").is_err() {
if env::var("EDITOR").is_err() && env::var("VISUAL").is_err() { panic!("Can't edit files if an editor is not defined. To fix: define the EDITOR or VISUAL environment variables with the path to your editor (eg. /usr/bin/vim)");
panic!("Can't edit files if an editor is not defined. To fix: define the EDITOR or VISUAL environment variables with the path to your editor (eg. /usr/bin/vim)");
}
let editor = env::var("EDITOR").unwrap_or_else(|_| env::var("VISUAL").unwrap());
let child = Command::new(editor).args(&[file_to_open]).spawn().expect("failed to spawn");
handle_command_exit(child);
::std::process::exit(0);
},
None => {
let child = Command::new(env::var("SHELL").unwrap()).spawn().expect("failed to spawn");
handle_command_exit(child);
::std::process::exit(0);
} }
let editor =
env::var("EDITOR").unwrap_or_else(|_| env::var("VISUAL").unwrap());
let child = Command::new(editor)
.args(&[file_to_open])
.spawn()
.expect("failed to spawn");
handle_command_exit(child);
::std::process::exit(0);
}
None => {
let child = Command::new(env::var("SHELL").unwrap())
.spawn()
.expect("failed to spawn");
handle_command_exit(child);
::std::process::exit(0);
} }
}, },
}; };
@ -209,17 +211,14 @@ impl OsApi for OsInputOutput {
} }
} }
impl Clone for Box<dyn OsApi> impl Clone for Box<dyn OsApi> {
{
fn clone(&self) -> Box<dyn OsApi> { fn clone(&self) -> Box<dyn OsApi> {
self.box_clone() self.box_clone()
} }
} }
pub fn get_os_input () -> OsInputOutput { pub fn get_os_input() -> OsInputOutput {
let current_termios = tcgetattr(0).unwrap(); let current_termios = tcgetattr(0).unwrap();
let orig_termios = Arc::new(Mutex::new(current_termios)); let orig_termios = Arc::new(Mutex::new(current_termios));
OsInputOutput { OsInputOutput { orig_termios }
orig_termios
}
} }

View file

@ -1,13 +1,13 @@
use ::std::os::unix::io::RawFd;
use ::async_std::stream::*; use ::async_std::stream::*;
use ::async_std::task; use ::async_std::task;
use ::async_std::task::*; use ::async_std::task::*;
use ::std::pin::*;
use ::std::sync::mpsc::{Sender, Receiver};
use ::std::time::{Instant, Duration};
use ::std::collections::HashMap; use ::std::collections::HashMap;
use std::path::PathBuf; use ::std::os::unix::io::RawFd;
use ::std::pin::*;
use ::std::sync::mpsc::{Receiver, Sender};
use ::std::time::{Duration, Instant};
use ::vte; use ::vte;
use std::path::PathBuf;
use crate::os_input_output::OsApi; use crate::os_input_output::OsApi;
use crate::ScreenInstruction; use crate::ScreenInstruction;
@ -26,10 +26,14 @@ impl ReadFromPid {
} }
} }
fn _debug_log_to_file (message: String) { fn _debug_log_to_file(message: String) {
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::prelude::*; use std::io::prelude::*;
let mut file = OpenOptions::new().append(true).create(true).open("/tmp/mosaic-log.txt").unwrap(); let mut file = OpenOptions::new()
.append(true)
.create(true)
.open("/tmp/mosaic-log.txt")
.unwrap();
file.write_all(message.as_bytes()).unwrap(); file.write_all(message.as_bytes()).unwrap();
file.write_all("\n".as_bytes()).unwrap(); file.write_all("\n".as_bytes()).unwrap();
} }
@ -69,17 +73,17 @@ impl Stream for ReadFromPid {
let res = Some(read_buffer[..=*res].to_vec()); let res = Some(read_buffer[..=*res].to_vec());
return Poll::Ready(res); return Poll::Ready(res);
} }
}, }
Err(e) => { Err(e) => {
match e { match e {
nix::Error::Sys(errno) => { nix::Error::Sys(errno) => {
if *errno == nix::errno::Errno::EAGAIN { if *errno == nix::errno::Errno::EAGAIN {
return Poll::Ready(Some(vec![])) // TODO: better with timeout waker somehow return Poll::Ready(Some(vec![])); // TODO: better with timeout waker somehow
} else { } else {
Poll::Ready(None) Poll::Ready(None)
} }
}, }
_ => Poll::Ready(None) _ => Poll::Ready(None),
} }
} }
} }
@ -87,15 +91,16 @@ impl Stream for ReadFromPid {
} }
#[derive(Debug)] #[derive(Debug)]
pub enum VteEvent { // TODO: try not to allocate Vecs pub enum VteEvent {
// TODO: try not to allocate Vecs
Print(char), Print(char),
Execute(u8), // byte Execute(u8), // byte
Hook(Vec<i64>, Vec<u8>, bool, char), // params, intermediates, ignore, char Hook(Vec<i64>, Vec<u8>, bool, char), // params, intermediates, ignore, char
Put(u8), // byte Put(u8), // byte
Unhook, Unhook,
OscDispatch(Vec<Vec<u8>>, bool), // params, bell_terminated OscDispatch(Vec<Vec<u8>>, bool), // params, bell_terminated
CsiDispatch(Vec<i64>, Vec<u8>, bool, char), // params, intermediates, ignore, char CsiDispatch(Vec<i64>, Vec<u8>, bool, char), // params, intermediates, ignore, char
EscDispatch(Vec<u8>, bool, u8), // intermediates, ignore, byte EscDispatch(Vec<u8>, bool, u8), // intermediates, ignore, byte
} }
struct VteEventSender { struct VteEventSender {
@ -104,52 +109,64 @@ struct VteEventSender {
} }
impl VteEventSender { impl VteEventSender {
pub fn new (id: RawFd, sender: Sender<ScreenInstruction>) -> Self { pub fn new(id: RawFd, sender: Sender<ScreenInstruction>) -> Self {
VteEventSender { id, sender } VteEventSender { id, sender }
} }
} }
impl vte::Perform for VteEventSender { impl vte::Perform for VteEventSender {
fn print(&mut self, c: char) { fn print(&mut self, c: char) {
self.sender.send( self.sender
ScreenInstruction::Pty(self.id, VteEvent::Print(c)) .send(ScreenInstruction::Pty(self.id, VteEvent::Print(c)))
).unwrap(); .unwrap();
} }
fn execute(&mut self, byte: u8) { fn execute(&mut self, byte: u8) {
self.sender.send(ScreenInstruction::Pty(self.id, VteEvent::Execute(byte))).unwrap(); self.sender
.send(ScreenInstruction::Pty(self.id, VteEvent::Execute(byte)))
.unwrap();
} }
fn hook(&mut self, params: &[i64], intermediates: &[u8], ignore: bool, c: char) { fn hook(&mut self, params: &[i64], intermediates: &[u8], ignore: bool, c: char) {
let params = params.iter().copied().collect(); let params = params.iter().copied().collect();
let intermediates = intermediates.iter().copied().collect(); let intermediates = intermediates.iter().copied().collect();
let instruction = ScreenInstruction::Pty(self.id, VteEvent::Hook(params, intermediates, ignore, c)); let instruction =
ScreenInstruction::Pty(self.id, VteEvent::Hook(params, intermediates, ignore, c));
self.sender.send(instruction).unwrap(); self.sender.send(instruction).unwrap();
} }
fn put(&mut self, byte: u8) { fn put(&mut self, byte: u8) {
self.sender.send(ScreenInstruction::Pty(self.id, VteEvent::Put(byte))).unwrap(); self.sender
.send(ScreenInstruction::Pty(self.id, VteEvent::Put(byte)))
.unwrap();
} }
fn unhook(&mut self) { fn unhook(&mut self) {
self.sender.send(ScreenInstruction::Pty(self.id, VteEvent::Unhook)).unwrap(); self.sender
.send(ScreenInstruction::Pty(self.id, VteEvent::Unhook))
.unwrap();
} }
fn osc_dispatch(&mut self, params: &[&[u8]], bell_terminated: bool) { fn osc_dispatch(&mut self, params: &[&[u8]], bell_terminated: bool) {
let params = params.iter().map(|p| p.to_vec()).collect(); let params = params.iter().map(|p| p.to_vec()).collect();
let instruction = ScreenInstruction::Pty(self.id, VteEvent::OscDispatch(params, bell_terminated)); let instruction =
ScreenInstruction::Pty(self.id, VteEvent::OscDispatch(params, bell_terminated));
self.sender.send(instruction).unwrap(); self.sender.send(instruction).unwrap();
} }
fn csi_dispatch(&mut self, params: &[i64], intermediates: &[u8], ignore: bool, c: char) { fn csi_dispatch(&mut self, params: &[i64], intermediates: &[u8], ignore: bool, c: char) {
let params = params.iter().copied().collect(); let params = params.iter().copied().collect();
let intermediates = intermediates.iter().copied().collect(); let intermediates = intermediates.iter().copied().collect();
let instruction = ScreenInstruction::Pty(self.id, VteEvent::CsiDispatch(params, intermediates, ignore, c)); let instruction = ScreenInstruction::Pty(
self.id,
VteEvent::CsiDispatch(params, intermediates, ignore, c),
);
self.sender.send(instruction).unwrap(); self.sender.send(instruction).unwrap();
} }
fn esc_dispatch(&mut self, intermediates: &[u8], ignore: bool, byte: u8) { fn esc_dispatch(&mut self, intermediates: &[u8], ignore: bool, byte: u8) {
let intermediates = intermediates.iter().copied().collect(); let intermediates = intermediates.iter().copied().collect();
let instruction = ScreenInstruction::Pty(self.id, VteEvent::EscDispatch(intermediates, ignore, byte)); let instruction =
ScreenInstruction::Pty(self.id, VteEvent::EscDispatch(intermediates, ignore, byte));
self.sender.send(instruction).unwrap(); self.sender.send(instruction).unwrap();
} }
} }
@ -159,7 +176,7 @@ pub enum PtyInstruction {
SpawnTerminalVertically(Option<PathBuf>), SpawnTerminalVertically(Option<PathBuf>),
SpawnTerminalHorizontally(Option<PathBuf>), SpawnTerminalHorizontally(Option<PathBuf>),
ClosePane(RawFd), ClosePane(RawFd),
Quit Quit,
} }
pub struct PtyBus { pub struct PtyBus {
@ -167,10 +184,15 @@ pub struct PtyBus {
pub receive_pty_instructions: Receiver<PtyInstruction>, pub receive_pty_instructions: Receiver<PtyInstruction>,
pub id_to_child_pid: HashMap<RawFd, RawFd>, pub id_to_child_pid: HashMap<RawFd, RawFd>,
os_input: Box<dyn OsApi>, os_input: Box<dyn OsApi>,
debug_to_file: bool debug_to_file: bool,
} }
fn stream_terminal_bytes(pid: RawFd, send_screen_instructions: Sender<ScreenInstruction>, os_input: Box<dyn OsApi>, debug: bool) { fn stream_terminal_bytes(
pid: RawFd,
send_screen_instructions: Sender<ScreenInstruction>,
os_input: Box<dyn OsApi>,
debug: bool,
) {
task::spawn({ task::spawn({
async move { async move {
let mut vte_parser = vte::Parser::new(); let mut vte_parser = vte::Parser::new();
@ -200,64 +222,100 @@ fn stream_terminal_bytes(pid: RawFd, send_screen_instructions: Sender<ScreenInst
Some(receive_time) => { Some(receive_time) => {
if receive_time.elapsed() > max_render_pause { if receive_time.elapsed() > max_render_pause {
pending_render = false; pending_render = false;
send_screen_instructions.send(ScreenInstruction::Render).unwrap(); send_screen_instructions
.send(ScreenInstruction::Render)
.unwrap();
last_byte_receive_time = Some(Instant::now()); last_byte_receive_time = Some(Instant::now());
} else { } else {
pending_render = true; pending_render = true;
} }
}, }
None => { None => {
last_byte_receive_time = Some(Instant::now()); last_byte_receive_time = Some(Instant::now());
pending_render = true; pending_render = true;
} }
}; };
} else { } else {
if pending_render { if pending_render {
pending_render = false; pending_render = false;
send_screen_instructions.send(ScreenInstruction::Render).unwrap(); send_screen_instructions
.send(ScreenInstruction::Render)
.unwrap();
} }
last_byte_receive_time = None; last_byte_receive_time = None;
task::sleep(::std::time::Duration::from_millis(10)).await; task::sleep(::std::time::Duration::from_millis(10)).await;
} }
} }
send_screen_instructions.send(ScreenInstruction::Render).unwrap(); send_screen_instructions
.send(ScreenInstruction::Render)
.unwrap();
#[cfg(not(test))] #[cfg(not(test))]
// this is a little hacky, and is because the tests end the file as soon as // this is a little hacky, and is because the tests end the file as soon as
// we read everything, rather than hanging until there is new data // we read everything, rather than hanging until there is new data
// a better solution would be to fix the test fakes, but this will do for now // a better solution would be to fix the test fakes, but this will do for now
send_screen_instructions.send(ScreenInstruction::ClosePane(pid)).unwrap(); send_screen_instructions
.send(ScreenInstruction::ClosePane(pid))
.unwrap();
} }
}); });
} }
impl PtyBus { impl PtyBus {
pub fn new (receive_pty_instructions: Receiver<PtyInstruction>, send_screen_instructions: Sender<ScreenInstruction>, os_input: Box<dyn OsApi>, debug_to_file: bool) -> Self { pub fn new(
receive_pty_instructions: Receiver<PtyInstruction>,
send_screen_instructions: Sender<ScreenInstruction>,
os_input: Box<dyn OsApi>,
debug_to_file: bool,
) -> Self {
PtyBus { PtyBus {
send_screen_instructions, send_screen_instructions,
receive_pty_instructions, receive_pty_instructions,
os_input, os_input,
id_to_child_pid: HashMap::new(), id_to_child_pid: HashMap::new(),
debug_to_file debug_to_file,
} }
} }
pub fn spawn_terminal(&mut self, file_to_open: Option<PathBuf>) { pub fn spawn_terminal(&mut self, file_to_open: Option<PathBuf>) {
let (pid_primary, pid_secondary): (RawFd, RawFd) = self.os_input.spawn_terminal(file_to_open); let (pid_primary, pid_secondary): (RawFd, RawFd) =
stream_terminal_bytes(pid_primary, self.send_screen_instructions.clone(), self.os_input.clone(), self.debug_to_file); self.os_input.spawn_terminal(file_to_open);
stream_terminal_bytes(
pid_primary,
self.send_screen_instructions.clone(),
self.os_input.clone(),
self.debug_to_file,
);
self.id_to_child_pid.insert(pid_primary, pid_secondary); self.id_to_child_pid.insert(pid_primary, pid_secondary);
self.send_screen_instructions.send(ScreenInstruction::NewPane(pid_primary)).unwrap(); self.send_screen_instructions
.send(ScreenInstruction::NewPane(pid_primary))
.unwrap();
} }
pub fn spawn_terminal_vertically(&mut self, file_to_open: Option<PathBuf>) { pub fn spawn_terminal_vertically(&mut self, file_to_open: Option<PathBuf>) {
let (pid_primary, pid_secondary): (RawFd, RawFd) = self.os_input.spawn_terminal(file_to_open); let (pid_primary, pid_secondary): (RawFd, RawFd) =
stream_terminal_bytes(pid_primary, self.send_screen_instructions.clone(), self.os_input.clone(), self.debug_to_file); self.os_input.spawn_terminal(file_to_open);
stream_terminal_bytes(
pid_primary,
self.send_screen_instructions.clone(),
self.os_input.clone(),
self.debug_to_file,
);
self.id_to_child_pid.insert(pid_primary, pid_secondary); self.id_to_child_pid.insert(pid_primary, pid_secondary);
self.send_screen_instructions.send(ScreenInstruction::VerticalSplit(pid_primary)).unwrap(); self.send_screen_instructions
.send(ScreenInstruction::VerticalSplit(pid_primary))
.unwrap();
} }
pub fn spawn_terminal_horizontally(&mut self, file_to_open: Option<PathBuf>) { pub fn spawn_terminal_horizontally(&mut self, file_to_open: Option<PathBuf>) {
let (pid_primary, pid_secondary): (RawFd, RawFd) = self.os_input.spawn_terminal(file_to_open); let (pid_primary, pid_secondary): (RawFd, RawFd) =
stream_terminal_bytes(pid_primary, self.send_screen_instructions.clone(), self.os_input.clone(), self.debug_to_file); self.os_input.spawn_terminal(file_to_open);
stream_terminal_bytes(
pid_primary,
self.send_screen_instructions.clone(),
self.os_input.clone(),
self.debug_to_file,
);
self.id_to_child_pid.insert(pid_primary, pid_secondary); self.id_to_child_pid.insert(pid_primary, pid_secondary);
self.send_screen_instructions.send(ScreenInstruction::HorizontalSplit(pid_primary)).unwrap(); self.send_screen_instructions
.send(ScreenInstruction::HorizontalSplit(pid_primary))
.unwrap();
} }
pub fn close_pane(&mut self, id: RawFd) { pub fn close_pane(&mut self, id: RawFd) {
let child_pid = self.id_to_child_pid.get(&id).unwrap(); let child_pid = self.id_to_child_pid.get(&id).unwrap();

File diff suppressed because it is too large Load diff

View file

@ -1,7 +1,7 @@
mod terminal_pane;
mod terminal_character;
mod scroll; mod scroll;
mod terminal_character;
mod terminal_pane;
pub use terminal_pane::*;
pub use terminal_character::*;
pub use scroll::*; pub use scroll::*;
pub use terminal_character::*;
pub use terminal_pane::*;

View file

@ -1,5 +1,5 @@
use ::std::fmt::{self, Debug, Formatter};
use ::std::collections::VecDeque; use ::std::collections::VecDeque;
use ::std::fmt::{self, Debug, Formatter};
use crate::terminal_pane::terminal_character::{TerminalCharacter, EMPTY_TERMINAL_CHARACTER}; use crate::terminal_pane::terminal_character::{TerminalCharacter, EMPTY_TERMINAL_CHARACTER};
@ -23,13 +23,17 @@ use crate::terminal_pane::terminal_character::{TerminalCharacter, EMPTY_TERMINAL
#[derive(Clone)] #[derive(Clone)]
pub struct CanonicalLine { pub struct CanonicalLine {
pub wrapped_fragments: Vec<WrappedFragment> pub wrapped_fragments: Vec<WrappedFragment>,
} }
fn debug_log_to_file (message: String) { fn debug_log_to_file(message: String) {
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::prelude::*; use std::io::prelude::*;
let mut file = OpenOptions::new().append(true).create(true).open("/tmp/mosaic-log.txt").unwrap(); let mut file = OpenOptions::new()
.append(true)
.create(true)
.open("/tmp/mosaic-log.txt")
.unwrap();
file.write_all(message.as_bytes()).unwrap(); file.write_all(message.as_bytes()).unwrap();
file.write_all("\n".as_bytes()).unwrap(); file.write_all("\n".as_bytes()).unwrap();
} }
@ -46,12 +50,16 @@ impl CanonicalLine {
self.wrapped_fragments.push(new_fragment); self.wrapped_fragments.push(new_fragment);
} }
pub fn change_width(&mut self, new_width: usize) { pub fn change_width(&mut self, new_width: usize) {
let mut characters: Vec<TerminalCharacter> = self.wrapped_fragments let mut characters: Vec<TerminalCharacter> = self
.wrapped_fragments
.iter() .iter()
.fold(Vec::with_capacity(self.wrapped_fragments.len()), |mut characters, wrapped_fragment| { .fold(
characters.push(wrapped_fragment.characters.iter().copied()); Vec::with_capacity(self.wrapped_fragments.len()),
characters |mut characters, wrapped_fragment| {
}) characters.push(wrapped_fragment.characters.iter().copied());
characters
},
)
.into_iter() .into_iter()
.flatten() .flatten()
.collect(); .collect();
@ -59,7 +67,9 @@ impl CanonicalLine {
while characters.len() > 0 { while characters.len() > 0 {
if characters.len() > new_width { if characters.len() > new_width {
wrapped_fragments.push(WrappedFragment::from_vec(characters.drain(..new_width).collect())); wrapped_fragments.push(WrappedFragment::from_vec(
characters.drain(..new_width).collect(),
));
} else { } else {
wrapped_fragments.push(WrappedFragment::from_vec(characters.drain(..).collect())); wrapped_fragments.push(WrappedFragment::from_vec(characters.drain(..).collect()));
} }
@ -70,7 +80,10 @@ impl CanonicalLine {
self.wrapped_fragments = wrapped_fragments; self.wrapped_fragments = wrapped_fragments;
} }
pub fn clear_after(&mut self, fragment_index: usize, column_index: usize) { pub fn clear_after(&mut self, fragment_index: usize, column_index: usize) {
let fragment_to_clear = self.wrapped_fragments.get_mut(fragment_index).expect("fragment out of bounds"); let fragment_to_clear = self
.wrapped_fragments
.get_mut(fragment_index)
.expect("fragment out of bounds");
fragment_to_clear.clear_after_and_including(column_index); fragment_to_clear.clear_after_and_including(column_index);
self.wrapped_fragments.truncate(fragment_index + 1); self.wrapped_fragments.truncate(fragment_index + 1);
} }
@ -87,16 +100,18 @@ impl Debug for CanonicalLine {
#[derive(Clone)] #[derive(Clone)]
pub struct WrappedFragment { pub struct WrappedFragment {
pub characters: Vec<TerminalCharacter> pub characters: Vec<TerminalCharacter>,
} }
impl WrappedFragment { impl WrappedFragment {
pub fn new() -> Self { pub fn new() -> Self {
WrappedFragment { WrappedFragment { characters: vec![] }
characters: vec![]
}
} }
pub fn add_character(&mut self, terminal_character: TerminalCharacter, position_in_line: usize) { pub fn add_character(
&mut self,
terminal_character: TerminalCharacter,
position_in_line: usize,
) {
if position_in_line == self.characters.len() { if position_in_line == self.characters.len() {
self.characters.push(terminal_character); self.characters.push(terminal_character);
} else { } else {
@ -106,9 +121,7 @@ impl WrappedFragment {
} }
} }
pub fn from_vec(characters: Vec<TerminalCharacter>) -> Self { pub fn from_vec(characters: Vec<TerminalCharacter>) -> Self {
WrappedFragment { WrappedFragment { characters }
characters
}
} }
pub fn clear_after_and_including(&mut self, character_index: usize) { pub fn clear_after_and_including(&mut self, character_index: usize) {
self.characters.truncate(character_index); self.characters.truncate(character_index);
@ -127,17 +140,17 @@ impl Debug for WrappedFragment {
#[derive(Debug)] #[derive(Debug)]
pub struct CursorPosition { pub struct CursorPosition {
line_index: (usize, usize), // (canonical line index, fragment index in line) line_index: (usize, usize), // (canonical line index, fragment index in line)
column_index: usize // 0 is the first character from the pane edge column_index: usize, // 0 is the first character from the pane edge
} }
impl CursorPosition { impl CursorPosition {
pub fn new() -> Self { pub fn new() -> Self {
CursorPosition { CursorPosition {
line_index: (0, 0), line_index: (0, 0),
column_index: 0 column_index: 0,
} }
} }
pub fn move_forward (&mut self, count: usize) { pub fn move_forward(&mut self, count: usize) {
// TODO: panic if out of bounds? // TODO: panic if out of bounds?
self.column_index += count; self.column_index += count;
} }
@ -150,7 +163,7 @@ impl CursorPosition {
pub fn move_to_next_canonical_line(&mut self) { pub fn move_to_next_canonical_line(&mut self) {
self.line_index.0 += 1; self.line_index.0 += 1;
} }
pub fn move_to_beginning_of_linewrap (&mut self) { pub fn move_to_beginning_of_linewrap(&mut self) {
self.column_index = 0; self.column_index = 0;
} }
pub fn move_to_beginning_of_canonical_line(&mut self) { pub fn move_to_beginning_of_canonical_line(&mut self) {
@ -188,7 +201,7 @@ pub struct Scroll {
} }
impl Scroll { impl Scroll {
pub fn new (total_columns: usize, lines_in_view: usize) -> Self { pub fn new(total_columns: usize, lines_in_view: usize) -> Self {
let mut canonical_lines = vec![]; let mut canonical_lines = vec![];
canonical_lines.push(CanonicalLine::new()); canonical_lines.push(CanonicalLine::new());
let cursor_position = CursorPosition::new(); let cursor_position = CursorPosition::new();
@ -210,7 +223,8 @@ impl Scroll {
match canonical_lines.next() { match canonical_lines.next() {
Some(current_canonical_line) => { Some(current_canonical_line) => {
for wrapped_fragment in current_canonical_line.wrapped_fragments.iter().rev() { for wrapped_fragment in current_canonical_line.wrapped_fragments.iter().rev() {
let mut line: Vec<TerminalCharacter> = wrapped_fragment.characters.iter().copied().collect(); let mut line: Vec<TerminalCharacter> =
wrapped_fragment.characters.iter().copied().collect();
if lines_to_skip > 0 { if lines_to_skip > 0 {
lines_to_skip -= 1; lines_to_skip -= 1;
} else { } else {
@ -224,7 +238,7 @@ impl Scroll {
break 'gather_lines; break 'gather_lines;
} }
} }
}, }
None => break, // no more lines None => break, // no more lines
} }
} }
@ -242,10 +256,17 @@ impl Scroll {
Vec::from(lines) Vec::from(lines)
} }
pub fn add_character(&mut self, terminal_character: TerminalCharacter) { pub fn add_character(&mut self, terminal_character: TerminalCharacter) {
let (canonical_line_position, wrapped_fragment_index_in_line) = self.cursor_position.line_index; let (canonical_line_position, wrapped_fragment_index_in_line) =
self.cursor_position.line_index;
let cursor_position_in_line = self.cursor_position.column_index; let cursor_position_in_line = self.cursor_position.column_index;
let current_line = self.canonical_lines.get_mut(canonical_line_position).expect("cursor out of bounds"); let current_line = self
let current_wrapped_fragment = current_line.wrapped_fragments.get_mut(wrapped_fragment_index_in_line).expect("cursor out of bounds"); .canonical_lines
.get_mut(canonical_line_position)
.expect("cursor out of bounds");
let current_wrapped_fragment = current_line
.wrapped_fragments
.get_mut(wrapped_fragment_index_in_line)
.expect("cursor out of bounds");
if cursor_position_in_line <= self.total_columns { if cursor_position_in_line <= self.total_columns {
current_wrapped_fragment.add_character(terminal_character, cursor_position_in_line); current_wrapped_fragment.add_character(terminal_character, cursor_position_in_line);
@ -257,27 +278,30 @@ impl Scroll {
self.cursor_position.move_forward(1); self.cursor_position.move_forward(1);
} }
} }
pub fn show_cursor (&mut self) { pub fn show_cursor(&mut self) {
self.show_cursor = true; self.show_cursor = true;
} }
pub fn hide_cursor (&mut self) { pub fn hide_cursor(&mut self) {
self.show_cursor = false; self.show_cursor = false;
} }
pub fn add_canonical_line(&mut self) { pub fn add_canonical_line(&mut self) {
let current_canonical_line_index = self.cursor_position.line_index.0; let current_canonical_line_index = self.cursor_position.line_index.0;
if let Some((scroll_region_top, scroll_region_bottom)) = self.scroll_region { if let Some((scroll_region_top, scroll_region_bottom)) = self.scroll_region {
// the scroll region indices start at 1, so we need to adjust them // the scroll region indices start at 1, so we need to adjust them
if self.show_cursor { // scroll region should be ignored if the cursor is hidden if self.show_cursor {
// scroll region should be ignored if the cursor is hidden
let scroll_region_top_index = scroll_region_top - 1; let scroll_region_top_index = scroll_region_top - 1;
let scroll_region_bottom_index = scroll_region_bottom - 1; let scroll_region_bottom_index = scroll_region_bottom - 1;
if current_canonical_line_index == scroll_region_bottom_index { // end of scroll region if current_canonical_line_index == scroll_region_bottom_index {
// end of scroll region
// when we have a scroll region set and we're at its bottom // when we have a scroll region set and we're at its bottom
// we need to delete its first line, thus shifting all lines in it upwards // we need to delete its first line, thus shifting all lines in it upwards
// then we add an empty line at its end which will be filled by the application // then we add an empty line at its end which will be filled by the application
// controlling the scroll region (presumably filled by whatever comes next in the // controlling the scroll region (presumably filled by whatever comes next in the
// scroll buffer, but that's not something we control) // scroll buffer, but that's not something we control)
self.canonical_lines.remove(scroll_region_top_index); self.canonical_lines.remove(scroll_region_top_index);
self.canonical_lines.insert(scroll_region_bottom_index, CanonicalLine::new()); self.canonical_lines
.insert(scroll_region_bottom_index, CanonicalLine::new());
return; return;
} }
} }
@ -294,11 +318,13 @@ impl Scroll {
panic!("cursor out of bounds, cannot add_canonical_line"); panic!("cursor out of bounds, cannot add_canonical_line");
} }
} }
pub fn cursor_coordinates_on_screen(&self) -> Option<(usize, usize)> { // (x, y) pub fn cursor_coordinates_on_screen(&self) -> Option<(usize, usize)> {
// (x, y)
if !self.show_cursor { if !self.show_cursor {
return None return None;
} }
let (canonical_line_cursor_position, line_wrap_cursor_position) = self.cursor_position.line_index; let (canonical_line_cursor_position, line_wrap_cursor_position) =
self.cursor_position.line_index;
let x = self.cursor_position.column_index; let x = self.cursor_position.column_index;
let mut y = 0; let mut y = 0;
let mut indices_and_canonical_lines = self.canonical_lines.iter().enumerate().rev(); let mut indices_and_canonical_lines = self.canonical_lines.iter().enumerate().rev();
@ -311,12 +337,16 @@ impl Scroll {
} else { } else {
y += current_line.wrapped_fragments.len(); y += current_line.wrapped_fragments.len();
} }
}
},
None => break, None => break,
} }
} }
let total_lines = self.canonical_lines.iter().fold(0, |total_lines, current_line| total_lines + current_line.wrapped_fragments.len()); // TODO: is this performant enough? should it be cached or kept track of? let total_lines = self
.canonical_lines
.iter()
.fold(0, |total_lines, current_line| {
total_lines + current_line.wrapped_fragments.len()
}); // TODO: is this performant enough? should it be cached or kept track of?
let y = if total_lines < self.lines_in_view { let y = if total_lines < self.lines_in_view {
total_lines - y total_lines - y
} else if y > self.lines_in_view { } else if y > self.lines_in_view {
@ -327,10 +357,17 @@ impl Scroll {
Some((x, y)) Some((x, y))
} }
pub fn move_cursor_forward(&mut self, count: usize) { pub fn move_cursor_forward(&mut self, count: usize) {
let (current_canonical_line_index, current_line_wrap_position) = self.cursor_position.line_index; let (current_canonical_line_index, current_line_wrap_position) =
self.cursor_position.line_index;
let current_cursor_column_position = self.cursor_position.column_index; let current_cursor_column_position = self.cursor_position.column_index;
let current_canonical_line = self.canonical_lines.get_mut(current_canonical_line_index).expect("cursor out of bounds"); let current_canonical_line = self
let current_fragment = current_canonical_line.wrapped_fragments.get_mut(current_line_wrap_position).expect("cursor out of bounds"); .canonical_lines
.get_mut(current_canonical_line_index)
.expect("cursor out of bounds");
let current_fragment = current_canonical_line
.wrapped_fragments
.get_mut(current_line_wrap_position)
.expect("cursor out of bounds");
let move_count = if current_cursor_column_position + count > self.total_columns { let move_count = if current_cursor_column_position + count > self.total_columns {
// move to last column in the current line wrap // move to last column in the current line wrap
self.total_columns - current_cursor_column_position self.total_columns - current_cursor_column_position
@ -367,7 +404,10 @@ impl Scroll {
for canonical_line in self.canonical_lines.iter_mut() { for canonical_line in self.canonical_lines.iter_mut() {
canonical_line.change_width(columns); canonical_line.change_width(columns);
} }
let cursor_line = self.canonical_lines.get(self.cursor_position.line_index.0).expect("cursor out of bounds"); let cursor_line = self
.canonical_lines
.get(self.cursor_position.line_index.0)
.expect("cursor out of bounds");
if cursor_line.wrapped_fragments.len() <= self.cursor_position.line_index.1 { if cursor_line.wrapped_fragments.len() <= self.cursor_position.line_index.1 {
self.cursor_position.line_index.1 = cursor_line.wrapped_fragments.len() - 1; self.cursor_position.line_index.1 = cursor_line.wrapped_fragments.len() - 1;
} }
@ -376,17 +416,28 @@ impl Scroll {
self.total_columns = columns; self.total_columns = columns;
} }
pub fn clear_canonical_line_right_of_cursor(&mut self) { pub fn clear_canonical_line_right_of_cursor(&mut self) {
let (current_canonical_line_index, current_line_wrap_position) = self.cursor_position.line_index; let (current_canonical_line_index, current_line_wrap_position) =
self.cursor_position.line_index;
let current_cursor_column_position = self.cursor_position.column_index; let current_cursor_column_position = self.cursor_position.column_index;
let current_canonical_line = self.canonical_lines.get_mut(current_canonical_line_index).expect("cursor out of bounds"); let current_canonical_line = self
current_canonical_line.clear_after(current_line_wrap_position, current_cursor_column_position); .canonical_lines
.get_mut(current_canonical_line_index)
.expect("cursor out of bounds");
current_canonical_line
.clear_after(current_line_wrap_position, current_cursor_column_position);
} }
pub fn clear_all_after_cursor(&mut self) { pub fn clear_all_after_cursor(&mut self) {
let (current_canonical_line_index, current_line_wrap_position) = self.cursor_position.line_index; let (current_canonical_line_index, current_line_wrap_position) =
self.cursor_position.line_index;
let current_cursor_column_position = self.cursor_position.column_index; let current_cursor_column_position = self.cursor_position.column_index;
let current_canonical_line = self.canonical_lines.get_mut(current_canonical_line_index).expect("cursor out of bounds"); let current_canonical_line = self
current_canonical_line.clear_after(current_line_wrap_position, current_cursor_column_position); .canonical_lines
self.canonical_lines.truncate(current_canonical_line_index + 1); .get_mut(current_canonical_line_index)
.expect("cursor out of bounds");
current_canonical_line
.clear_after(current_line_wrap_position, current_cursor_column_position);
self.canonical_lines
.truncate(current_canonical_line_index + 1);
} }
pub fn clear_all(&mut self) { pub fn clear_all(&mut self) {
self.canonical_lines.clear(); self.canonical_lines.clear();
@ -402,9 +453,16 @@ impl Scroll {
} }
self.cursor_position.move_to_canonical_line(line); self.cursor_position.move_to_canonical_line(line);
} }
let (current_canonical_line_index, current_line_wrap_position) = self.cursor_position.line_index; let (current_canonical_line_index, current_line_wrap_position) =
let current_canonical_line = self.canonical_lines.get_mut(current_canonical_line_index).expect("cursor out of bounds"); self.cursor_position.line_index;
let current_fragment = current_canonical_line.wrapped_fragments.get_mut(current_line_wrap_position).expect("cursor out of bounds"); let current_canonical_line = self
.canonical_lines
.get_mut(current_canonical_line_index)
.expect("cursor out of bounds");
let current_fragment = current_canonical_line
.wrapped_fragments
.get_mut(current_line_wrap_position)
.expect("cursor out of bounds");
for _ in current_fragment.characters.len()..col { for _ in current_fragment.characters.len()..col {
current_fragment.characters.push(EMPTY_TERMINAL_CHARACTER); current_fragment.characters.push(EMPTY_TERMINAL_CHARACTER);
} }
@ -423,15 +481,17 @@ impl Scroll {
let scroll_region_top_index = scroll_region_top - 1; let scroll_region_top_index = scroll_region_top - 1;
let scroll_region_bottom_index = scroll_region_bottom - 1; let scroll_region_bottom_index = scroll_region_bottom - 1;
let current_canonical_line_index = self.cursor_position.line_index.0; let current_canonical_line_index = self.cursor_position.line_index.0;
if current_canonical_line_index >= scroll_region_top_index && if current_canonical_line_index >= scroll_region_top_index
current_canonical_line_index <= scroll_region_bottom_index { && current_canonical_line_index <= scroll_region_bottom_index
{
// when deleting lines inside the scroll region, we must make sure it stays the // when deleting lines inside the scroll region, we must make sure it stays the
// same size (and that other lines below it aren't shifted inside it) // same size (and that other lines below it aren't shifted inside it)
// so we delete the current line(s) and add an empty line at the end of the scroll // so we delete the current line(s) and add an empty line at the end of the scroll
// region // region
for _ in 0..count { for _ in 0..count {
self.canonical_lines.remove(current_canonical_line_index); self.canonical_lines.remove(current_canonical_line_index);
self.canonical_lines.insert(scroll_region_bottom_index, CanonicalLine::new()); self.canonical_lines
.insert(scroll_region_bottom_index, CanonicalLine::new());
} }
} }
} }
@ -442,28 +502,29 @@ impl Scroll {
let scroll_region_top_index = scroll_region_top - 1; let scroll_region_top_index = scroll_region_top - 1;
let scroll_region_bottom_index = scroll_region_bottom - 1; let scroll_region_bottom_index = scroll_region_bottom - 1;
let current_canonical_line_index = self.cursor_position.line_index.0; let current_canonical_line_index = self.cursor_position.line_index.0;
if current_canonical_line_index >= scroll_region_top_index && if current_canonical_line_index >= scroll_region_top_index
current_canonical_line_index <= scroll_region_bottom_index { && current_canonical_line_index <= scroll_region_bottom_index
{
// when adding empty lines inside the scroll region, we must make sure it stays the // when adding empty lines inside the scroll region, we must make sure it stays the
// same size and that lines don't "leak" outside of it // same size and that lines don't "leak" outside of it
// so we add an empty line where the cursor currently is, and delete the last line // so we add an empty line where the cursor currently is, and delete the last line
// of the scroll region // of the scroll region
for _ in 0..count { for _ in 0..count {
self.canonical_lines.remove(scroll_region_bottom_index); self.canonical_lines.remove(scroll_region_bottom_index);
self.canonical_lines.insert(current_canonical_line_index, CanonicalLine::new()); self.canonical_lines
.insert(current_canonical_line_index, CanonicalLine::new());
} }
} }
} }
} }
pub fn move_viewport_up (&mut self, count: usize) { pub fn move_viewport_up(&mut self, count: usize) {
if let Some(current_offset) = self.viewport_bottom_offset.as_mut() { if let Some(current_offset) = self.viewport_bottom_offset.as_mut() {
*current_offset += count; *current_offset += count;
} else { } else {
self.viewport_bottom_offset = Some(count); self.viewport_bottom_offset = Some(count);
} }
} }
pub fn move_viewport_down (&mut self, count: usize) { pub fn move_viewport_down(&mut self, count: usize) {
if let Some(current_offset) = self.viewport_bottom_offset.as_mut() { if let Some(current_offset) = self.viewport_bottom_offset.as_mut() {
if *current_offset > count { if *current_offset > count {
*current_offset -= count; *current_offset -= count;
@ -472,7 +533,7 @@ impl Scroll {
} }
} }
} }
pub fn reset_viewport (&mut self) { pub fn reset_viewport(&mut self) {
self.viewport_bottom_offset = None; self.viewport_bottom_offset = None;
} }
} }

View file

@ -1,4 +1,4 @@
use ::std::fmt::{self, Display, Debug, Formatter}; use ::std::fmt::{self, Debug, Display, Formatter};
pub const EMPTY_TERMINAL_CHARACTER: TerminalCharacter = TerminalCharacter { pub const EMPTY_TERMINAL_CHARACTER: TerminalCharacter = TerminalCharacter {
character: ' ', character: ' ',
@ -14,14 +14,14 @@ pub const EMPTY_TERMINAL_CHARACTER: TerminalCharacter = TerminalCharacter {
bold: Some(AnsiCode::Reset), bold: Some(AnsiCode::Reset),
dim: Some(AnsiCode::Reset), dim: Some(AnsiCode::Reset),
italic: Some(AnsiCode::Reset), italic: Some(AnsiCode::Reset),
} },
}; };
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AnsiCode { pub enum AnsiCode {
Reset, Reset,
NamedColor(NamedColor), NamedColor(NamedColor),
Code((Option<u16>, Option<u16>)) Code((Option<u16>, Option<u16>)),
} }
#[derive(Clone, Copy, Eq, PartialEq, Debug)] #[derive(Clone, Copy, Eq, PartialEq, Debug)]
@ -151,32 +151,36 @@ impl CharacterStyles {
self.dim = None; self.dim = None;
self.italic = None; self.italic = None;
} }
pub fn update_and_return_diff(&mut self, new_styles: &CharacterStyles) -> Option<CharacterStyles> { pub fn update_and_return_diff(
&mut self,
new_styles: &CharacterStyles,
) -> Option<CharacterStyles> {
let mut diff: Option<CharacterStyles> = None; let mut diff: Option<CharacterStyles> = None;
if new_styles.foreground == Some(AnsiCode::Reset) && if new_styles.foreground == Some(AnsiCode::Reset)
new_styles.background == Some(AnsiCode::Reset) && && new_styles.background == Some(AnsiCode::Reset)
new_styles.strike == Some(AnsiCode::Reset) && && new_styles.strike == Some(AnsiCode::Reset)
new_styles.hidden == Some(AnsiCode::Reset) && && new_styles.hidden == Some(AnsiCode::Reset)
new_styles.reverse == Some(AnsiCode::Reset) && && new_styles.reverse == Some(AnsiCode::Reset)
new_styles.fast_blink == Some(AnsiCode::Reset) && && new_styles.fast_blink == Some(AnsiCode::Reset)
new_styles.slow_blink == Some(AnsiCode::Reset) && && new_styles.slow_blink == Some(AnsiCode::Reset)
new_styles.underline == Some(AnsiCode::Reset) && && new_styles.underline == Some(AnsiCode::Reset)
new_styles.bold == Some(AnsiCode::Reset) && && new_styles.bold == Some(AnsiCode::Reset)
new_styles.dim == Some(AnsiCode::Reset) && && new_styles.dim == Some(AnsiCode::Reset)
new_styles.italic == Some(AnsiCode::Reset) { && new_styles.italic == Some(AnsiCode::Reset)
self.foreground = Some(AnsiCode::Reset); {
self.background = Some(AnsiCode::Reset); self.foreground = Some(AnsiCode::Reset);
self.strike = Some(AnsiCode::Reset); self.background = Some(AnsiCode::Reset);
self.hidden = Some(AnsiCode::Reset); self.strike = Some(AnsiCode::Reset);
self.reverse = Some(AnsiCode::Reset); self.hidden = Some(AnsiCode::Reset);
self.fast_blink = Some(AnsiCode::Reset); self.reverse = Some(AnsiCode::Reset);
self.slow_blink = Some(AnsiCode::Reset); self.fast_blink = Some(AnsiCode::Reset);
self.underline = Some(AnsiCode::Reset); self.slow_blink = Some(AnsiCode::Reset);
self.bold = Some(AnsiCode::Reset); self.underline = Some(AnsiCode::Reset);
self.dim = Some(AnsiCode::Reset); self.bold = Some(AnsiCode::Reset);
self.italic = Some(AnsiCode::Reset); self.dim = Some(AnsiCode::Reset);
return Some(*new_styles) self.italic = Some(AnsiCode::Reset);
return Some(*new_styles);
}; };
if self.foreground != new_styles.foreground { if self.foreground != new_styles.foreground {
@ -218,10 +222,10 @@ impl CharacterStyles {
if self.reverse != new_styles.reverse { if self.reverse != new_styles.reverse {
if let Some(new_diff) = diff.as_mut() { if let Some(new_diff) = diff.as_mut() {
diff = Some(new_diff.reverse(new_styles.reverse)); diff = Some(new_diff.reverse(new_styles.reverse));
self.reverse= new_styles.reverse; self.reverse = new_styles.reverse;
} else { } else {
diff = Some(CharacterStyles::new().reverse(new_styles.reverse)); diff = Some(CharacterStyles::new().reverse(new_styles.reverse));
self.reverse= new_styles.reverse; self.reverse = new_styles.reverse;
} }
} }
if self.slow_blink != new_styles.slow_blink { if self.slow_blink != new_styles.slow_blink {
@ -245,28 +249,28 @@ impl CharacterStyles {
if self.underline != new_styles.underline { if self.underline != new_styles.underline {
if let Some(new_diff) = diff.as_mut() { if let Some(new_diff) = diff.as_mut() {
diff = Some(new_diff.underline(new_styles.underline)); diff = Some(new_diff.underline(new_styles.underline));
self.underline= new_styles.underline; self.underline = new_styles.underline;
} else { } else {
diff = Some(CharacterStyles::new().underline(new_styles.underline)); diff = Some(CharacterStyles::new().underline(new_styles.underline));
self.underline= new_styles.underline; self.underline = new_styles.underline;
} }
} }
if self.bold != new_styles.bold { if self.bold != new_styles.bold {
if let Some(new_diff) = diff.as_mut() { if let Some(new_diff) = diff.as_mut() {
diff = Some(new_diff.bold(new_styles.bold)); diff = Some(new_diff.bold(new_styles.bold));
self.bold= new_styles.bold; self.bold = new_styles.bold;
} else { } else {
diff = Some(CharacterStyles::new().bold(new_styles.bold)); diff = Some(CharacterStyles::new().bold(new_styles.bold));
self.bold= new_styles.bold; self.bold = new_styles.bold;
} }
} }
if self.dim != new_styles.dim { if self.dim != new_styles.dim {
if let Some(new_diff) = diff.as_mut() { if let Some(new_diff) = diff.as_mut() {
diff = Some(new_diff.dim(new_styles.dim)); diff = Some(new_diff.dim(new_styles.dim));
self.dim= new_styles.dim; self.dim = new_styles.dim;
} else { } else {
diff = Some(CharacterStyles::new().dim(new_styles.dim)); diff = Some(CharacterStyles::new().dim(new_styles.dim));
self.dim= new_styles.dim; self.dim = new_styles.dim;
} }
} }
if self.italic != new_styles.italic { if self.italic != new_styles.italic {
@ -297,18 +301,18 @@ impl CharacterStyles {
impl Display for CharacterStyles { impl Display for CharacterStyles {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if self.foreground == Some(AnsiCode::Reset) && if self.foreground == Some(AnsiCode::Reset)
self.background == Some(AnsiCode::Reset) && && self.background == Some(AnsiCode::Reset)
self.strike == Some(AnsiCode::Reset) && && self.strike == Some(AnsiCode::Reset)
self.hidden == Some(AnsiCode::Reset) && && self.hidden == Some(AnsiCode::Reset)
self.reverse == Some(AnsiCode::Reset) && && self.reverse == Some(AnsiCode::Reset)
self.fast_blink == Some(AnsiCode::Reset) && && self.fast_blink == Some(AnsiCode::Reset)
self.slow_blink == Some(AnsiCode::Reset) && && self.slow_blink == Some(AnsiCode::Reset)
self.underline == Some(AnsiCode::Reset) && && self.underline == Some(AnsiCode::Reset)
self.bold == Some(AnsiCode::Reset) && && self.bold == Some(AnsiCode::Reset)
self.dim == Some(AnsiCode::Reset) && && self.dim == Some(AnsiCode::Reset)
self.italic == Some(AnsiCode::Reset) { && self.italic == Some(AnsiCode::Reset)
{
write!(f, "\u{1b}[m")?; // reset all write!(f, "\u{1b}[m")?; // reset all
return Ok(()); return Ok(());
} }
@ -318,18 +322,18 @@ impl Display for CharacterStyles {
match (param1, param2) { match (param1, param2) {
(Some(param1), Some(param2)) => { (Some(param1), Some(param2)) => {
write!(f, "\u{1b}[38;{};{}m", param1, param2)?; write!(f, "\u{1b}[38;{};{}m", param1, param2)?;
}, }
(Some(param1), None) => { (Some(param1), None) => {
write!(f, "\u{1b}[38;{}m", param1)?; write!(f, "\u{1b}[38;{}m", param1)?;
}, }
(_, _) => { (_, _) => {
// TODO: can this happen? // TODO: can this happen?
} }
} }
}, }
AnsiCode::Reset => { AnsiCode::Reset => {
write!(f, "\u{1b}[39m")?; write!(f, "\u{1b}[39m")?;
}, }
AnsiCode::NamedColor(named_color) => { AnsiCode::NamedColor(named_color) => {
write!(f, "\u{1b}[{}m", named_color.to_foreground_ansi_code())?; write!(f, "\u{1b}[{}m", named_color.to_foreground_ansi_code())?;
} }
@ -341,15 +345,15 @@ impl Display for CharacterStyles {
match (param1, param2) { match (param1, param2) {
(Some(param1), Some(param2)) => { (Some(param1), Some(param2)) => {
write!(f, "\u{1b}[48;{};{}m", param1, param2)?; write!(f, "\u{1b}[48;{};{}m", param1, param2)?;
}, }
(Some(param1), None) => { (Some(param1), None) => {
write!(f, "\u{1b}[48;{}m", param1)?; write!(f, "\u{1b}[48;{}m", param1)?;
}, }
(_, _) => { (_, _) => {
// TODO: can this happen? // TODO: can this happen?
} }
} }
}, }
AnsiCode::Reset => { AnsiCode::Reset => {
write!(f, "\u{1b}[49m")?; write!(f, "\u{1b}[49m")?;
} }
@ -360,122 +364,110 @@ impl Display for CharacterStyles {
} }
if let Some(ansi_code) = self.strike { if let Some(ansi_code) = self.strike {
match ansi_code { match ansi_code {
AnsiCode::Code((param1, param2)) => { AnsiCode::Code((param1, param2)) => match (param1, param2) {
match (param1, param2) { (Some(param1), Some(param2)) => {
(Some(param1), Some(param2)) => { write!(f, "\u{1b}[9;{};{}m", param1, param2)?;
write!(f, "\u{1b}[9;{};{}m", param1, param2)?; }
}, (Some(param1), None) => {
(Some(param1), None) => { write!(f, "\u{1b}[9;{}m", param1)?;
write!(f, "\u{1b}[9;{}m", param1)?; }
}, (_, _) => {
(_, _) => { write!(f, "\u{1b}[9m")?;
write!(f, "\u{1b}[9m")?;
}
} }
}, },
AnsiCode::Reset => { AnsiCode::Reset => {
write!(f, "\u{1b}[29m")?; write!(f, "\u{1b}[29m")?;
}, }
_ => {} _ => {}
} }
} }
if let Some(ansi_code) = self.hidden { if let Some(ansi_code) = self.hidden {
match ansi_code { match ansi_code {
AnsiCode::Code((param1, param2)) => { AnsiCode::Code((param1, param2)) => match (param1, param2) {
match (param1, param2) { (Some(param1), Some(param2)) => {
(Some(param1), Some(param2)) => { write!(f, "\u{1b}[8;{};{}m", param1, param2)?;
write!(f, "\u{1b}[8;{};{}m", param1, param2)?; }
}, (Some(param1), None) => {
(Some(param1), None) => { write!(f, "\u{1b}[8;{}m", param1)?;
write!(f, "\u{1b}[8;{}m", param1)?; }
}, (_, _) => {
(_, _) => { write!(f, "\u{1b}[8m")?;
write!(f, "\u{1b}[8m")?;
}
} }
}, },
AnsiCode::Reset => { AnsiCode::Reset => {
write!(f, "\u{1b}[28m")?; write!(f, "\u{1b}[28m")?;
}, }
_ => {} _ => {}
} }
} }
if let Some(ansi_code) = self.reverse { if let Some(ansi_code) = self.reverse {
match ansi_code { match ansi_code {
AnsiCode::Code((param1, param2)) => { AnsiCode::Code((param1, param2)) => match (param1, param2) {
match (param1, param2) { (Some(param1), Some(param2)) => {
(Some(param1), Some(param2)) => { write!(f, "\u{1b}[7;{};{}m", param1, param2)?;
write!(f, "\u{1b}[7;{};{}m", param1, param2)?; }
}, (Some(param1), None) => {
(Some(param1), None) => { write!(f, "\u{1b}[7;{}m", param1)?;
write!(f, "\u{1b}[7;{}m", param1)?; }
}, (_, _) => {
(_, _) => { write!(f, "\u{1b}[7m")?;
write!(f, "\u{1b}[7m")?;
}
} }
}, },
AnsiCode::Reset => { AnsiCode::Reset => {
write!(f, "\u{1b}[27m")?; write!(f, "\u{1b}[27m")?;
}, }
_ => {} _ => {}
} }
} }
if let Some(ansi_code) = self.fast_blink { if let Some(ansi_code) = self.fast_blink {
match ansi_code { match ansi_code {
AnsiCode::Code((param1, param2)) => { AnsiCode::Code((param1, param2)) => match (param1, param2) {
match (param1, param2) { (Some(param1), Some(param2)) => {
(Some(param1), Some(param2)) => { write!(f, "\u{1b}[6;{};{}m", param1, param2)?;
write!(f, "\u{1b}[6;{};{}m", param1, param2)?; }
}, (Some(param1), None) => {
(Some(param1), None) => { write!(f, "\u{1b}[6;{}m", param1)?;
write!(f, "\u{1b}[6;{}m", param1)?; }
}, (_, _) => {
(_, _) => { write!(f, "\u{1b}[6m")?;
write!(f, "\u{1b}[6m")?;
}
} }
}, },
AnsiCode::Reset => { AnsiCode::Reset => {
write!(f, "\u{1b}[25m")?; write!(f, "\u{1b}[25m")?;
}, }
_ => {} _ => {}
} }
} }
if let Some(ansi_code) = self.slow_blink { if let Some(ansi_code) = self.slow_blink {
match ansi_code { match ansi_code {
AnsiCode::Code((param1, param2)) => { AnsiCode::Code((param1, param2)) => match (param1, param2) {
match (param1, param2) { (Some(param1), Some(param2)) => {
(Some(param1), Some(param2)) => { write!(f, "\u{1b}[5;{};{}m", param1, param2)?;
write!(f, "\u{1b}[5;{};{}m", param1, param2)?; }
}, (Some(param1), None) => {
(Some(param1), None) => { write!(f, "\u{1b}[5;{}m", param1)?;
write!(f, "\u{1b}[5;{}m", param1)?; }
}, (_, _) => {
(_, _) => { write!(f, "\u{1b}[5m")?;
write!(f, "\u{1b}[5m")?;
}
} }
}, },
AnsiCode::Reset => { AnsiCode::Reset => {
write!(f, "\u{1b}[25m")?; write!(f, "\u{1b}[25m")?;
}, }
_ => {} _ => {}
} }
} }
if let Some(ansi_code) = self.bold { if let Some(ansi_code) = self.bold {
match ansi_code { match ansi_code {
AnsiCode::Code((param1, param2)) => { AnsiCode::Code((param1, param2)) => match (param1, param2) {
match (param1, param2) { (Some(param1), Some(param2)) => {
(Some(param1), Some(param2)) => { write!(f, "\u{1b}[1;{};{}m", param1, param2)?;
write!(f, "\u{1b}[1;{};{}m", param1, param2)?; }
}, (Some(param1), None) => {
(Some(param1), None) => { write!(f, "\u{1b}[1;{}m", param1)?;
write!(f, "\u{1b}[1;{}m", param1)?; }
}, (_, _) => {
(_, _) => { write!(f, "\u{1b}[1m")?;
write!(f, "\u{1b}[1m")?;
}
} }
}, },
AnsiCode::Reset => { AnsiCode::Reset => {
@ -483,7 +475,7 @@ impl Display for CharacterStyles {
// character_ansi_codes.push_str(&format!("\u{1b}[22m")); // character_ansi_codes.push_str(&format!("\u{1b}[22m"));
// TODO: this cancels bold + underline, if this behaviour is indeed correct, we // TODO: this cancels bold + underline, if this behaviour is indeed correct, we
// need to properly handle it in the struct methods etc like dim // need to properly handle it in the struct methods etc like dim
}, }
_ => {} _ => {}
} }
} }
@ -492,38 +484,34 @@ impl Display for CharacterStyles {
// otherwise // otherwise
if let Some(ansi_code) = self.underline { if let Some(ansi_code) = self.underline {
match ansi_code { match ansi_code {
AnsiCode::Code((param1, param2)) => { AnsiCode::Code((param1, param2)) => match (param1, param2) {
match (param1, param2) { (Some(param1), Some(param2)) => {
(Some(param1), Some(param2)) => { write!(f, "\u{1b}[4;{};{}m", param1, param2)?;
write!(f, "\u{1b}[4;{};{}m", param1, param2)?; }
}, (Some(param1), None) => {
(Some(param1), None) => { write!(f, "\u{1b}[4;{}m", param1)?;
write!(f, "\u{1b}[4;{}m", param1)?; }
}, (_, _) => {
(_, _) => { write!(f, "\u{1b}[4m")?;
write!(f, "\u{1b}[4m")?;
}
} }
}, },
AnsiCode::Reset => { AnsiCode::Reset => {
write!(f, "\u{1b}[24m")?; write!(f, "\u{1b}[24m")?;
}, }
_ => {} _ => {}
} }
} }
if let Some(ansi_code) = self.dim { if let Some(ansi_code) = self.dim {
match ansi_code { match ansi_code {
AnsiCode::Code((param1, param2)) => { AnsiCode::Code((param1, param2)) => match (param1, param2) {
match (param1, param2) { (Some(param1), Some(param2)) => {
(Some(param1), Some(param2)) => { write!(f, "\u{1b}[2;{};{}m", param1, param2)?;
write!(f, "\u{1b}[2;{};{}m", param1, param2)?; }
}, (Some(param1), None) => {
(Some(param1), None) => { write!(f, "\u{1b}[2;{}m", param1)?;
write!(f, "\u{1b}[2;{}m", param1)?; }
}, (_, _) => {
(_, _) => { write!(f, "\u{1b}[2m")?;
write!(f, "\u{1b}[2m")?;
}
} }
}, },
AnsiCode::Reset => { AnsiCode::Reset => {
@ -532,32 +520,30 @@ impl Display for CharacterStyles {
match bold { match bold {
AnsiCode::Reset => { AnsiCode::Reset => {
write!(f, "\u{1b}[22m")?; write!(f, "\u{1b}[22m")?;
}, }
_ => {} _ => {}
} }
} }
}, }
_ => {} _ => {}
} }
} }
if let Some(ansi_code) = self.italic { if let Some(ansi_code) = self.italic {
match ansi_code { match ansi_code {
AnsiCode::Code((param1, param2)) => { AnsiCode::Code((param1, param2)) => match (param1, param2) {
match (param1, param2) { (Some(param1), Some(param2)) => {
(Some(param1), Some(param2)) => { write!(f, "\u{1b}[3;{};{}m", param1, param2)?;
write!(f, "\u{1b}[3;{};{}m", param1, param2)?; }
}, (Some(param1), None) => {
(Some(param1), None) => { write!(f, "\u{1b}[3;{}m", param1)?;
write!(f, "\u{1b}[3;{}m", param1)?; }
}, (_, _) => {
(_, _) => { write!(f, "\u{1b}[3m")?;
write!(f, "\u{1b}[3m")?;
}
} }
}, },
AnsiCode::Reset => { AnsiCode::Reset => {
write!(f, "\u{1b}[23m")?; write!(f, "\u{1b}[23m")?;
}, }
_ => {} _ => {}
} }
}; };

View file

@ -1,16 +1,13 @@
use ::std::os::unix::io::RawFd;
use ::nix::pty::Winsize; use ::nix::pty::Winsize;
use ::std::os::unix::io::RawFd;
use ::vte::Perform; use ::vte::Perform;
use crate::VteEvent;
use crate::boundaries::Rect; use crate::boundaries::Rect;
use crate::terminal_pane::Scroll;
use crate::terminal_pane::terminal_character::{ use crate::terminal_pane::terminal_character::{
TerminalCharacter, AnsiCode, CharacterStyles, NamedColor, TerminalCharacter,
CharacterStyles,
AnsiCode,
NamedColor
}; };
use crate::terminal_pane::Scroll;
use crate::VteEvent;
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub struct PositionAndSize { pub struct PositionAndSize {
@ -45,7 +42,7 @@ impl Rect for &mut TerminalPane {
} }
impl TerminalPane { impl TerminalPane {
pub fn new (pid: RawFd, ws: Winsize, x: usize, y: usize) -> TerminalPane { pub fn new(pid: RawFd, ws: Winsize, x: usize, y: usize) -> TerminalPane {
let scroll = Scroll::new(ws.ws_col as usize, ws.ws_row as usize); let scroll = Scroll::new(ws.ws_col as usize, ws.ws_row as usize);
let pending_styles = CharacterStyles::new(); let pending_styles = CharacterStyles::new();
let position_and_size = PositionAndSize { let position_and_size = PositionAndSize {
@ -68,26 +65,26 @@ impl TerminalPane {
VteEvent::Print(c) => { VteEvent::Print(c) => {
self.print(c); self.print(c);
self.should_render = true; self.should_render = true;
}, }
VteEvent::Execute(byte) => { VteEvent::Execute(byte) => {
self.execute(byte); self.execute(byte);
}, }
VteEvent::Hook(params, intermediates, ignore, c) => { VteEvent::Hook(params, intermediates, ignore, c) => {
self.hook(&params, &intermediates, ignore, c); self.hook(&params, &intermediates, ignore, c);
}, }
VteEvent::Put(byte) => { VteEvent::Put(byte) => {
self.put(byte); self.put(byte);
}, }
VteEvent::Unhook => { VteEvent::Unhook => {
self.unhook(); self.unhook();
}, }
VteEvent::OscDispatch(params, bell_terminated) => { VteEvent::OscDispatch(params, bell_terminated) => {
let params: Vec<&[u8]> = params.iter().map(|p| &p[..]).collect(); let params: Vec<&[u8]> = params.iter().map(|p| &p[..]).collect();
self.osc_dispatch(&params[..], bell_terminated); self.osc_dispatch(&params[..], bell_terminated);
}, }
VteEvent::CsiDispatch(params, intermediates, ignore, c) => { VteEvent::CsiDispatch(params, intermediates, ignore, c) => {
self.csi_dispatch(&params, &intermediates, ignore, c); self.csi_dispatch(&params, &intermediates, ignore, c);
}, }
VteEvent::EscDispatch(intermediates, ignore, byte) => { VteEvent::EscDispatch(intermediates, ignore, byte) => {
self.esc_dispatch(&intermediates, ignore, byte); self.esc_dispatch(&intermediates, ignore, byte);
} }
@ -145,50 +142,35 @@ impl TerminalPane {
} }
pub fn get_x(&self) -> usize { pub fn get_x(&self) -> usize {
match self.position_and_size_override { match self.position_and_size_override {
Some(position_and_size_override) => { Some(position_and_size_override) => position_and_size_override.x,
position_and_size_override.x None => self.position_and_size.x as usize,
},
None => {
self.position_and_size.x as usize
}
} }
} }
pub fn get_y(&self) -> usize { pub fn get_y(&self) -> usize {
match self.position_and_size_override { match self.position_and_size_override {
Some(position_and_size_override) => { Some(position_and_size_override) => position_and_size_override.y,
position_and_size_override.y None => self.position_and_size.y as usize,
},
None => {
self.position_and_size.y as usize
}
} }
} }
pub fn get_columns(&self) -> usize { pub fn get_columns(&self) -> usize {
match &self.position_and_size_override.as_ref() { match &self.position_and_size_override.as_ref() {
Some(position_and_size_override) => { Some(position_and_size_override) => position_and_size_override.columns,
position_and_size_override.columns None => self.position_and_size.columns as usize,
},
None => {
self.position_and_size.columns as usize
}
} }
} }
pub fn get_rows(&self) -> usize { pub fn get_rows(&self) -> usize {
match &self.position_and_size_override.as_ref() { match &self.position_and_size_override.as_ref() {
Some(position_and_size_override) => { Some(position_and_size_override) => position_and_size_override.rows,
position_and_size_override.rows None => self.position_and_size.rows as usize,
},
None => {
self.position_and_size.rows as usize
}
} }
} }
fn reflow_lines (&mut self) { fn reflow_lines(&mut self) {
let rows = self.get_rows(); let rows = self.get_rows();
let columns = self.get_columns(); let columns = self.get_columns();
self.scroll.change_size(columns, rows); self.scroll.change_size(columns, rows);
} }
pub fn buffer_as_vte_output(&mut self) -> Option<String> { // TODO: rename to render pub fn buffer_as_vte_output(&mut self) -> Option<String> {
// TODO: rename to render
// if self.should_render { // if self.should_render {
if true { if true {
// while checking should_render rather than rendering each pane every time // while checking should_render rather than rendering each pane every time
@ -208,7 +190,9 @@ impl TerminalPane {
// in some cases (eg. while resizing) some characters will spill over // in some cases (eg. while resizing) some characters will spill over
// before they are corrected by the shell (for the prompt) or by reflowing // before they are corrected by the shell (for the prompt) or by reflowing
// lines // lines
if let Some(new_styles) = character_styles.update_and_return_diff(&t_character.styles) { if let Some(new_styles) =
character_styles.update_and_return_diff(&t_character.styles)
{
// the terminal keeps the previous styles as long as we're in the same // the terminal keeps the previous styles as long as we're in the same
// line, so we only want to update the new styles here (this also // line, so we only want to update the new styles here (this also
// includes resetting previous styles as needed) // includes resetting previous styles as needed)
@ -225,10 +209,11 @@ impl TerminalPane {
None None
} }
} }
pub fn read_buffer_as_lines (&self) -> Vec<Vec<TerminalCharacter>> { pub fn read_buffer_as_lines(&self) -> Vec<Vec<TerminalCharacter>> {
self.scroll.as_character_lines() self.scroll.as_character_lines()
} }
pub fn cursor_coordinates (&self) -> Option<(usize, usize)> { // (x, y) pub fn cursor_coordinates(&self) -> Option<(usize, usize)> {
// (x, y)
self.scroll.cursor_coordinates_on_screen() self.scroll.cursor_coordinates_on_screen()
} }
pub fn scroll_up(&mut self, count: usize) { pub fn scroll_up(&mut self, count: usize) {
@ -259,12 +244,12 @@ impl TerminalPane {
self.reflow_lines(); self.reflow_lines();
self.should_render = true; self.should_render = true;
} }
fn add_newline (&mut self) { fn add_newline(&mut self) {
self.scroll.add_canonical_line(); self.scroll.add_canonical_line();
// self.reset_all_ansi_codes(); // TODO: find out if we should be resetting here or not // self.reset_all_ansi_codes(); // TODO: find out if we should be resetting here or not
self.should_render = true; self.should_render = true;
} }
fn move_to_beginning_of_line (&mut self) { fn move_to_beginning_of_line(&mut self) {
self.scroll.move_cursor_to_beginning_of_linewrap(); self.scroll.move_cursor_to_beginning_of_linewrap();
} }
fn move_cursor_backwards(&mut self, count: usize) { fn move_cursor_backwards(&mut self, count: usize) {
@ -275,11 +260,15 @@ impl TerminalPane {
} }
} }
fn debug_log_to_file (message: String, pid: RawFd) { fn debug_log_to_file(message: String, pid: RawFd) {
if pid == 3 { if pid == 3 {
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::prelude::*; use std::io::prelude::*;
let mut file = OpenOptions::new().append(true).create(true).open("/tmp/mosaic-log.txt").unwrap(); let mut file = OpenOptions::new()
.append(true)
.create(true)
.open("/tmp/mosaic-log.txt")
.unwrap();
file.write_all(message.as_bytes()).unwrap(); file.write_all(message.as_bytes()).unwrap();
file.write_all("\n".as_bytes()).unwrap(); file.write_all("\n".as_bytes()).unwrap();
} }
@ -297,11 +286,14 @@ impl vte::Perform for TerminalPane {
} }
fn execute(&mut self, byte: u8) { fn execute(&mut self, byte: u8) {
if byte == 13 { // 0d, carriage return if byte == 13 {
// 0d, carriage return
self.move_to_beginning_of_line(); self.move_to_beginning_of_line();
} else if byte == 08 { // backspace } else if byte == 08 {
// backspace
self.move_cursor_backwards(1); self.move_cursor_backwards(1);
} else if byte == 10 { // 0a, newline } else if byte == 10 {
// 0a, newline
self.add_newline(); self.add_newline();
} }
} }
@ -360,219 +352,334 @@ impl vte::Perform for TerminalPane {
} else if params[0] == 38 { } else if params[0] == 38 {
match (params.get(1), params.get(2)) { match (params.get(1), params.get(2)) {
(Some(param1), Some(param2)) => { (Some(param1), Some(param2)) => {
self.pending_styles = self.pending_styles.foreground(Some(AnsiCode::Code((Some(*param1 as u16), Some(*param2 as u16))))); self.pending_styles = self.pending_styles.foreground(Some(AnsiCode::Code(
}, (Some(*param1 as u16), Some(*param2 as u16)),
)));
}
(Some(param1), None) => { (Some(param1), None) => {
self.pending_styles = self.pending_styles.foreground(Some(AnsiCode::Code((Some(*param1 as u16), None)))); self.pending_styles = self
.pending_styles
.foreground(Some(AnsiCode::Code((Some(*param1 as u16), None))));
} }
(_, _) => { (_, _) => {
self.pending_styles = self.pending_styles.foreground(Some(AnsiCode::Code((None, None)))); self.pending_styles = self
.pending_styles
.foreground(Some(AnsiCode::Code((None, None))));
} }
}; };
} else if params[0] == 48 { } else if params[0] == 48 {
match (params.get(1), params.get(2)) { match (params.get(1), params.get(2)) {
(Some(param1), Some(param2)) => { (Some(param1), Some(param2)) => {
self.pending_styles = self.pending_styles.background(Some(AnsiCode::Code((Some(*param1 as u16), Some(*param2 as u16))))); self.pending_styles = self.pending_styles.background(Some(AnsiCode::Code(
}, (Some(*param1 as u16), Some(*param2 as u16)),
)));
}
(Some(param1), None) => { (Some(param1), None) => {
self.pending_styles = self.pending_styles.background(Some(AnsiCode::Code((Some(*param1 as u16), None)))); self.pending_styles = self
.pending_styles
.background(Some(AnsiCode::Code((Some(*param1 as u16), None))));
} }
(_, _) => { (_, _) => {
self.pending_styles = self.pending_styles.background(Some(AnsiCode::Code((None, None)))); self.pending_styles = self
.pending_styles
.background(Some(AnsiCode::Code((None, None))));
} }
}; };
} else if params[0] == 1 { } else if params[0] == 1 {
// bold // bold
match (params.get(1), params.get(2)) { match (params.get(1), params.get(2)) {
(Some(param1), Some(param2)) => { (Some(param1), Some(param2)) => {
self.pending_styles = self.pending_styles.bold(Some(AnsiCode::Code((Some(*param1 as u16), Some(*param2 as u16))))); self.pending_styles = self.pending_styles.bold(Some(AnsiCode::Code((
}, Some(*param1 as u16),
Some(*param2 as u16),
))));
}
(Some(param1), None) => { (Some(param1), None) => {
self.pending_styles = self.pending_styles.bold(Some(AnsiCode::Code((Some(*param1 as u16), None)))); self.pending_styles = self
.pending_styles
.bold(Some(AnsiCode::Code((Some(*param1 as u16), None))));
} }
(_, _) => { (_, _) => {
self.pending_styles = self.pending_styles.bold(Some(AnsiCode::Code((None, None)))); self.pending_styles =
self.pending_styles.bold(Some(AnsiCode::Code((None, None))));
} }
}; };
} else if params[0] == 2 { } else if params[0] == 2 {
// dim // dim
match (params.get(1), params.get(2)) { match (params.get(1), params.get(2)) {
(Some(param1), Some(param2)) => { (Some(param1), Some(param2)) => {
self.pending_styles = self.pending_styles.dim(Some(AnsiCode::Code((Some(*param1 as u16), Some(*param2 as u16))))); self.pending_styles = self.pending_styles.dim(Some(AnsiCode::Code((
}, Some(*param1 as u16),
Some(*param2 as u16),
))));
}
(Some(param1), None) => { (Some(param1), None) => {
self.pending_styles = self.pending_styles.dim(Some(AnsiCode::Code((Some(*param1 as u16), None)))); self.pending_styles = self
.pending_styles
.dim(Some(AnsiCode::Code((Some(*param1 as u16), None))));
} }
(_, _) => { (_, _) => {
self.pending_styles = self.pending_styles.dim(Some(AnsiCode::Code((None, None)))); self.pending_styles =
self.pending_styles.dim(Some(AnsiCode::Code((None, None))));
} }
}; };
} else if params[0] == 3 { } else if params[0] == 3 {
// italic // italic
match (params.get(1), params.get(2)) { match (params.get(1), params.get(2)) {
(Some(param1), Some(param2)) => { (Some(param1), Some(param2)) => {
self.pending_styles = self.pending_styles.italic(Some(AnsiCode::Code((Some(*param1 as u16), Some(*param2 as u16))))); self.pending_styles = self.pending_styles.italic(Some(AnsiCode::Code((
}, Some(*param1 as u16),
Some(*param2 as u16),
))));
}
(Some(param1), None) => { (Some(param1), None) => {
self.pending_styles = self.pending_styles.italic(Some(AnsiCode::Code((Some(*param1 as u16), None)))); self.pending_styles = self
.pending_styles
.italic(Some(AnsiCode::Code((Some(*param1 as u16), None))));
} }
(_, _) => { (_, _) => {
self.pending_styles = self.pending_styles.italic(Some(AnsiCode::Code((None, None)))); self.pending_styles = self
.pending_styles
.italic(Some(AnsiCode::Code((None, None))));
} }
}; };
} else if params[0] == 4 { } else if params[0] == 4 {
// underline // underline
match (params.get(1), params.get(2)) { match (params.get(1), params.get(2)) {
(Some(param1), Some(param2)) => { (Some(param1), Some(param2)) => {
self.pending_styles = self.pending_styles.underline(Some(AnsiCode::Code((Some(*param1 as u16), Some(*param2 as u16))))); self.pending_styles = self.pending_styles.underline(Some(AnsiCode::Code(
}, (Some(*param1 as u16), Some(*param2 as u16)),
)));
}
(Some(param1), None) => { (Some(param1), None) => {
self.pending_styles = self.pending_styles.underline(Some(AnsiCode::Code((Some(*param1 as u16), None)))); self.pending_styles = self
.pending_styles
.underline(Some(AnsiCode::Code((Some(*param1 as u16), None))));
} }
(_, _) => { (_, _) => {
self.pending_styles = self.pending_styles.underline(Some(AnsiCode::Code((None, None)))); self.pending_styles = self
.pending_styles
.underline(Some(AnsiCode::Code((None, None))));
} }
}; };
} else if params[0] == 5 { } else if params[0] == 5 {
// blink slow // blink slow
match (params.get(1), params.get(2)) { match (params.get(1), params.get(2)) {
(Some(param1), Some(param2)) => { (Some(param1), Some(param2)) => {
self.pending_styles = self.pending_styles.blink_slow(Some(AnsiCode::Code((Some(*param1 as u16), Some(*param2 as u16))))); self.pending_styles = self.pending_styles.blink_slow(Some(AnsiCode::Code(
}, (Some(*param1 as u16), Some(*param2 as u16)),
)));
}
(Some(param1), None) => { (Some(param1), None) => {
self.pending_styles = self.pending_styles.blink_slow(Some(AnsiCode::Code((Some(*param1 as u16), None)))); self.pending_styles = self
.pending_styles
.blink_slow(Some(AnsiCode::Code((Some(*param1 as u16), None))));
} }
(_, _) => { (_, _) => {
self.pending_styles = self.pending_styles.blink_slow(Some(AnsiCode::Code((None, None)))); self.pending_styles = self
.pending_styles
.blink_slow(Some(AnsiCode::Code((None, None))));
} }
}; };
} else if params[0] == 6 { } else if params[0] == 6 {
// blink fast // blink fast
match (params.get(1), params.get(2)) { match (params.get(1), params.get(2)) {
(Some(param1), Some(param2)) => { (Some(param1), Some(param2)) => {
self.pending_styles = self.pending_styles.blink_fast(Some(AnsiCode::Code((Some(*param1 as u16), Some(*param2 as u16))))); self.pending_styles = self.pending_styles.blink_fast(Some(AnsiCode::Code(
}, (Some(*param1 as u16), Some(*param2 as u16)),
)));
}
(Some(param1), None) => { (Some(param1), None) => {
self.pending_styles = self.pending_styles.blink_fast(Some(AnsiCode::Code((Some(*param1 as u16), None)))); self.pending_styles = self
.pending_styles
.blink_fast(Some(AnsiCode::Code((Some(*param1 as u16), None))));
} }
(_, _) => { (_, _) => {
self.pending_styles = self.pending_styles.blink_fast(Some(AnsiCode::Code((None, None)))); self.pending_styles = self
.pending_styles
.blink_fast(Some(AnsiCode::Code((None, None))));
} }
}; };
} else if params[0] == 7 { } else if params[0] == 7 {
// reverse // reverse
match (params.get(1), params.get(2)) { match (params.get(1), params.get(2)) {
(Some(param1), Some(param2)) => { (Some(param1), Some(param2)) => {
self.pending_styles = self.pending_styles.reverse(Some(AnsiCode::Code((Some(*param1 as u16), Some(*param2 as u16))))); self.pending_styles = self.pending_styles.reverse(Some(AnsiCode::Code((
}, Some(*param1 as u16),
Some(*param2 as u16),
))));
}
(Some(param1), None) => { (Some(param1), None) => {
self.pending_styles = self.pending_styles.reverse(Some(AnsiCode::Code((Some(*param1 as u16), None)))); self.pending_styles = self
.pending_styles
.reverse(Some(AnsiCode::Code((Some(*param1 as u16), None))));
} }
(_, _) => { (_, _) => {
self.pending_styles = self.pending_styles.reverse(Some(AnsiCode::Code((None, None)))); self.pending_styles = self
.pending_styles
.reverse(Some(AnsiCode::Code((None, None))));
} }
}; };
} else if params[0] == 8 { } else if params[0] == 8 {
// hidden // hidden
match (params.get(1), params.get(2)) { match (params.get(1), params.get(2)) {
(Some(param1), Some(param2)) => { (Some(param1), Some(param2)) => {
self.pending_styles = self.pending_styles.hidden(Some(AnsiCode::Code((Some(*param1 as u16), Some(*param2 as u16))))); self.pending_styles = self.pending_styles.hidden(Some(AnsiCode::Code((
}, Some(*param1 as u16),
Some(*param2 as u16),
))));
}
(Some(param1), None) => { (Some(param1), None) => {
self.pending_styles = self.pending_styles.hidden(Some(AnsiCode::Code((Some(*param1 as u16), None)))); self.pending_styles = self
.pending_styles
.hidden(Some(AnsiCode::Code((Some(*param1 as u16), None))));
} }
(_, _) => { (_, _) => {
self.pending_styles = self.pending_styles.hidden(Some(AnsiCode::Code((None, None)))); self.pending_styles = self
.pending_styles
.hidden(Some(AnsiCode::Code((None, None))));
} }
}; };
} else if params[0] == 9 { } else if params[0] == 9 {
// strike // strike
match (params.get(1), params.get(2)) { match (params.get(1), params.get(2)) {
(Some(param1), Some(param2)) => { (Some(param1), Some(param2)) => {
self.pending_styles = self.pending_styles.strike(Some(AnsiCode::Code((Some(*param1 as u16), Some(*param2 as u16))))); self.pending_styles = self.pending_styles.strike(Some(AnsiCode::Code((
}, Some(*param1 as u16),
Some(*param2 as u16),
))));
}
(Some(param1), None) => { (Some(param1), None) => {
self.pending_styles = self.pending_styles.strike(Some(AnsiCode::Code((Some(*param1 as u16), None)))); self.pending_styles = self
.pending_styles
.strike(Some(AnsiCode::Code((Some(*param1 as u16), None))));
} }
(_, _) => { (_, _) => {
self.pending_styles = self.pending_styles.strike(Some(AnsiCode::Code((None, None)))); self.pending_styles = self
.pending_styles
.strike(Some(AnsiCode::Code((None, None))));
} }
}; };
} else if params[0] == 30 { } else if params[0] == 30 {
self.pending_styles = self.pending_styles.foreground(Some(AnsiCode::NamedColor(NamedColor::Black))); self.pending_styles = self
.pending_styles
.foreground(Some(AnsiCode::NamedColor(NamedColor::Black)));
} else if params[0] == 31 { } else if params[0] == 31 {
self.pending_styles = self.pending_styles.foreground(Some(AnsiCode::NamedColor(NamedColor::Red))); self.pending_styles = self
.pending_styles
.foreground(Some(AnsiCode::NamedColor(NamedColor::Red)));
} else if params[0] == 32 { } else if params[0] == 32 {
self.pending_styles = self.pending_styles.foreground(Some(AnsiCode::NamedColor(NamedColor::Green))); self.pending_styles = self
.pending_styles
.foreground(Some(AnsiCode::NamedColor(NamedColor::Green)));
} else if params[0] == 33 { } else if params[0] == 33 {
self.pending_styles = self.pending_styles.foreground(Some(AnsiCode::NamedColor(NamedColor::Yellow))); self.pending_styles = self
.pending_styles
.foreground(Some(AnsiCode::NamedColor(NamedColor::Yellow)));
} else if params[0] == 34 { } else if params[0] == 34 {
self.pending_styles = self.pending_styles.foreground(Some(AnsiCode::NamedColor(NamedColor::Blue))); self.pending_styles = self
.pending_styles
.foreground(Some(AnsiCode::NamedColor(NamedColor::Blue)));
} else if params[0] == 35 { } else if params[0] == 35 {
self.pending_styles = self.pending_styles.foreground(Some(AnsiCode::NamedColor(NamedColor::Magenta))); self.pending_styles = self
.pending_styles
.foreground(Some(AnsiCode::NamedColor(NamedColor::Magenta)));
} else if params[0] == 36 { } else if params[0] == 36 {
self.pending_styles = self.pending_styles.foreground(Some(AnsiCode::NamedColor(NamedColor::Cyan))); self.pending_styles = self
.pending_styles
.foreground(Some(AnsiCode::NamedColor(NamedColor::Cyan)));
} else if params[0] == 37 { } else if params[0] == 37 {
self.pending_styles = self.pending_styles.foreground(Some(AnsiCode::NamedColor(NamedColor::White))); self.pending_styles = self
.pending_styles
.foreground(Some(AnsiCode::NamedColor(NamedColor::White)));
} else if params[0] == 40 { } else if params[0] == 40 {
self.pending_styles = self.pending_styles.background(Some(AnsiCode::NamedColor(NamedColor::Black))); self.pending_styles = self
.pending_styles
.background(Some(AnsiCode::NamedColor(NamedColor::Black)));
} else if params[0] == 41 { } else if params[0] == 41 {
self.pending_styles = self.pending_styles.background(Some(AnsiCode::NamedColor(NamedColor::Red))); self.pending_styles = self
.pending_styles
.background(Some(AnsiCode::NamedColor(NamedColor::Red)));
} else if params[0] == 42 { } else if params[0] == 42 {
self.pending_styles = self.pending_styles.background(Some(AnsiCode::NamedColor(NamedColor::Green))); self.pending_styles = self
.pending_styles
.background(Some(AnsiCode::NamedColor(NamedColor::Green)));
} else if params[0] == 43 { } else if params[0] == 43 {
self.pending_styles = self.pending_styles.background(Some(AnsiCode::NamedColor(NamedColor::Yellow))); self.pending_styles = self
.pending_styles
.background(Some(AnsiCode::NamedColor(NamedColor::Yellow)));
} else if params[0] == 44 { } else if params[0] == 44 {
self.pending_styles = self.pending_styles.background(Some(AnsiCode::NamedColor(NamedColor::Blue))); self.pending_styles = self
.pending_styles
.background(Some(AnsiCode::NamedColor(NamedColor::Blue)));
} else if params[0] == 45 { } else if params[0] == 45 {
self.pending_styles = self.pending_styles.background(Some(AnsiCode::NamedColor(NamedColor::Magenta))); self.pending_styles = self
.pending_styles
.background(Some(AnsiCode::NamedColor(NamedColor::Magenta)));
} else if params[0] == 46 { } else if params[0] == 46 {
self.pending_styles = self.pending_styles.background(Some(AnsiCode::NamedColor(NamedColor::Cyan))); self.pending_styles = self
.pending_styles
.background(Some(AnsiCode::NamedColor(NamedColor::Cyan)));
} else if params[0] == 47 { } else if params[0] == 47 {
self.pending_styles = self.pending_styles.background(Some(AnsiCode::NamedColor(NamedColor::White))); self.pending_styles = self
.pending_styles
.background(Some(AnsiCode::NamedColor(NamedColor::White)));
} else { } else {
debug_log_to_file(format!("unhandled csi m code {:?}", params), self.pid); debug_log_to_file(format!("unhandled csi m code {:?}", params), self.pid);
} }
} else if c == 'C' { // move cursor forward } else if c == 'C' {
let move_by = if params[0] == 0 { 1 } else { params[0] as usize }; // move cursor forward
let move_by = if params[0] == 0 {
1
} else {
params[0] as usize
};
self.scroll.move_cursor_forward(move_by); self.scroll.move_cursor_forward(move_by);
} else if c == 'K' { // clear line (0 => right, 1 => left, 2 => all) } else if c == 'K' {
// clear line (0 => right, 1 => left, 2 => all)
if params[0] == 0 { if params[0] == 0 {
self.scroll.clear_canonical_line_right_of_cursor(); self.scroll.clear_canonical_line_right_of_cursor();
} }
// TODO: implement 1 and 2 // TODO: implement 1 and 2
} else if c == 'J' { // clear all (0 => below, 1 => above, 2 => all, 3 => saved) } else if c == 'J' {
// clear all (0 => below, 1 => above, 2 => all, 3 => saved)
if params[0] == 0 { if params[0] == 0 {
self.scroll.clear_all_after_cursor(); self.scroll.clear_all_after_cursor();
} else if params[0] == 2 { } else if params[0] == 2 {
self.scroll.clear_all(); self.scroll.clear_all();
} }
// TODO: implement 1 // TODO: implement 1
} else if c == 'H' { // goto row/col } else if c == 'H' {
// goto row/col
let (row, col) = if params.len() == 1 { let (row, col) = if params.len() == 1 {
(params[0] as usize, 0) // TODO: is this always correct ? (params[0] as usize, 0) // TODO: is this always correct ?
} else { } else {
(params[0] as usize - 1, params[1] as usize - 1) // we subtract 1 here because this csi is 1 indexed and we index from 0 (params[0] as usize - 1, params[1] as usize - 1) // we subtract 1 here because this csi is 1 indexed and we index from 0
}; };
self.scroll.move_cursor_to(row, col); self.scroll.move_cursor_to(row, col);
} else if c == 'A' { // move cursor up until edge of screen } else if c == 'A' {
// move cursor up until edge of screen
let move_up_count = if params[0] == 0 { 1 } else { params[0] }; let move_up_count = if params[0] == 0 { 1 } else { params[0] };
self.scroll.move_cursor_up(move_up_count as usize); self.scroll.move_cursor_up(move_up_count as usize);
} else if c == 'D' { } else if c == 'D' {
let move_back_count = if params[0] == 0 { 1 } else { params[0] as usize }; let move_back_count = if params[0] == 0 {
1
} else {
params[0] as usize
};
self.scroll.move_cursor_back(move_back_count); self.scroll.move_cursor_back(move_back_count);
} else if c == 'l' { } else if c == 'l' {
let first_intermediate_is_questionmark = match _intermediates.get(0) { let first_intermediate_is_questionmark = match _intermediates.get(0) {
Some(b'?') => true, Some(b'?') => true,
None => false, None => false,
_ => false _ => false,
}; };
if first_intermediate_is_questionmark { if first_intermediate_is_questionmark {
match params.get(0) { match params.get(0) {
Some(25) => { Some(25) => {
self.scroll.hide_cursor(); self.scroll.hide_cursor();
self.should_render = true; self.should_render = true;
}, }
_ => {} _ => {}
}; };
} }
@ -580,14 +687,14 @@ impl vte::Perform for TerminalPane {
let first_intermediate_is_questionmark = match _intermediates.get(0) { let first_intermediate_is_questionmark = match _intermediates.get(0) {
Some(b'?') => true, Some(b'?') => true,
None => false, None => false,
_ => false _ => false,
}; };
if first_intermediate_is_questionmark { if first_intermediate_is_questionmark {
match params.get(0) { match params.get(0) {
Some(25) => { Some(25) => {
self.scroll.show_cursor(); self.scroll.show_cursor();
self.should_render = true; self.should_render = true;
}, }
_ => {} _ => {}
}; };
} }
@ -595,7 +702,8 @@ impl vte::Perform for TerminalPane {
if params.len() > 1 { if params.len() > 1 {
let top_line_index = params[0] as usize; let top_line_index = params[0] as usize;
let bottom_line_index = params[1] as usize; let bottom_line_index = params[1] as usize;
self.scroll.set_scroll_region(top_line_index, bottom_line_index); self.scroll
.set_scroll_region(top_line_index, bottom_line_index);
self.scroll.show_cursor(); self.scroll.show_cursor();
} else { } else {
self.scroll.clear_scroll_region(); self.scroll.clear_scroll_region();
@ -608,12 +716,22 @@ impl vte::Perform for TerminalPane {
// TBD - identify terminal // TBD - identify terminal
} else if c == 'M' { } else if c == 'M' {
// delete lines if currently inside scroll region // delete lines if currently inside scroll region
let line_count_to_delete = if params[0] == 0 { 1 } else { params[0] as usize }; let line_count_to_delete = if params[0] == 0 {
self.scroll.delete_lines_in_scroll_region(line_count_to_delete); 1
} else {
params[0] as usize
};
self.scroll
.delete_lines_in_scroll_region(line_count_to_delete);
} else if c == 'L' { } else if c == 'L' {
// insert blank lines if inside scroll region // insert blank lines if inside scroll region
let line_count_to_add = if params[0] == 0 { 1 } else { params[0] as usize }; let line_count_to_add = if params[0] == 0 {
self.scroll.add_empty_lines_in_scroll_region(line_count_to_add); 1
} else {
params[0] as usize
};
self.scroll
.add_empty_lines_in_scroll_region(line_count_to_add);
} else if c == 'q' || c == 'd' || c == 'X' || c == 'G' { } else if c == 'q' || c == 'd' || c == 'X' || c == 'G' {
// ignore for now to run on mac // ignore for now to run on mac
} else { } else {

View file

@ -1,13 +1,13 @@
use ::std::time::Duration;
use ::nix::pty::Winsize; use ::nix::pty::Winsize;
use ::std::os::unix::io::RawFd;
use ::std::io::{Read, Write};
use ::std::collections::HashMap; use ::std::collections::HashMap;
use ::std::sync::{Arc, Mutex}; use ::std::io::{Read, Write};
use ::std::os::unix::io::RawFd;
use ::std::path::PathBuf; use ::std::path::PathBuf;
use ::std::sync::{Arc, Mutex};
use ::std::time::Duration;
use crate::os_input_output::OsApi; use crate::os_input_output::OsApi;
use crate::tests::possible_tty_inputs::{Bytes, get_possible_tty_inputs}; use crate::tests::possible_tty_inputs::{get_possible_tty_inputs, Bytes};
#[derive(Clone)] #[derive(Clone)]
pub enum IoEvent { pub enum IoEvent {
@ -87,7 +87,7 @@ pub struct FakeInputOutput {
impl FakeInputOutput { impl FakeInputOutput {
pub fn new(winsize: Winsize) -> Self { pub fn new(winsize: Winsize) -> Self {
let mut win_sizes = HashMap::new(); let mut win_sizes = HashMap::new();
win_sizes.insert(0 , winsize); // 0 is the current terminal win_sizes.insert(0, winsize); // 0 is the current terminal
FakeInputOutput { FakeInputOutput {
read_buffers: Arc::new(Mutex::new(HashMap::new())), read_buffers: Arc::new(Mutex::new(HashMap::new())),
stdin_writes: Arc::new(Mutex::new(HashMap::new())), stdin_writes: Arc::new(Mutex::new(HashMap::new())),
@ -117,15 +117,30 @@ impl OsApi for FakeInputOutput {
*winsize *winsize
} }
fn set_terminal_size_using_fd(&mut self, pid: RawFd, cols: u16, rows: u16) { fn set_terminal_size_using_fd(&mut self, pid: RawFd, cols: u16, rows: u16) {
let terminal_input = self.possible_tty_inputs.get(&cols).expect(&format!("could not find input for size {:?}", cols)); let terminal_input = self
self.read_buffers.lock().unwrap().insert(pid, terminal_input.clone()); .possible_tty_inputs
self.io_events.lock().unwrap().push(IoEvent::SetTerminalSizeUsingFd(pid, cols, rows)); .get(&cols)
.expect(&format!("could not find input for size {:?}", cols));
self.read_buffers
.lock()
.unwrap()
.insert(pid, terminal_input.clone());
self.io_events
.lock()
.unwrap()
.push(IoEvent::SetTerminalSizeUsingFd(pid, cols, rows));
} }
fn into_raw_mode(&mut self, pid: RawFd) { fn into_raw_mode(&mut self, pid: RawFd) {
self.io_events.lock().unwrap().push(IoEvent::IntoRawMode(pid)); self.io_events
.lock()
.unwrap()
.push(IoEvent::IntoRawMode(pid));
} }
fn unset_raw_mode(&mut self, pid: RawFd) { fn unset_raw_mode(&mut self, pid: RawFd) {
self.io_events.lock().unwrap().push(IoEvent::UnsetRawMode(pid)); self.io_events
.lock()
.unwrap()
.push(IoEvent::UnsetRawMode(pid));
} }
fn spawn_terminal(&mut self, _file_to_open: Option<PathBuf>) -> (RawFd, RawFd) { fn spawn_terminal(&mut self, _file_to_open: Option<PathBuf>) -> (RawFd, RawFd) {
let next_terminal_id = { self.read_buffers.lock().unwrap().keys().len() as RawFd + 1 }; let next_terminal_id = { self.read_buffers.lock().unwrap().keys().len() as RawFd + 1 };
@ -158,7 +173,7 @@ impl OsApi for FakeInputOutput {
bytes.set_read_position(bytes_read); bytes.set_read_position(bytes_read);
} }
return Ok(bytes_read); return Ok(bytes_read);
}, }
None => { None => {
attempts_left -= 1; attempts_left -= 1;
} }

View file

@ -1,29 +1,20 @@
use ::nix::pty::Winsize;
use ::insta::assert_snapshot; use ::insta::assert_snapshot;
use ::nix::pty::Winsize;
use crate::{start, Opt}; use crate::tests::fakes::FakeInputOutput;
use crate::tests::fakes::{FakeInputOutput};
use crate::tests::utils::get_output_frame_snapshots;
use crate::tests::utils::commands::{ use crate::tests::utils::commands::{
SPLIT_HORIZONTALLY, MOVE_FOCUS, QUIT, RESIZE_LEFT, RESIZE_RIGHT, RESIZE_UP, SCROLL_DOWN, SCROLL_UP, SPAWN_TERMINAL,
SPLIT_VERTICALLY, SPLIT_HORIZONTALLY, SPLIT_VERTICALLY, TOGGLE_ACTIVE_TERMINAL_FULLSCREEN,
RESIZE_UP,
MOVE_FOCUS,
RESIZE_LEFT,
RESIZE_RIGHT,
SPAWN_TERMINAL,
QUIT,
SCROLL_UP,
SCROLL_DOWN,
TOGGLE_ACTIVE_TERMINAL_FULLSCREEN,
}; };
use crate::tests::utils::get_output_frame_snapshots;
use crate::{start, Opt};
fn get_fake_os_input (fake_win_size: &Winsize) -> FakeInputOutput { fn get_fake_os_input(fake_win_size: &Winsize) -> FakeInputOutput {
FakeInputOutput::new(fake_win_size.clone()) FakeInputOutput::new(fake_win_size.clone())
} }
#[test] #[test]
pub fn starts_with_one_terminal () { pub fn starts_with_one_terminal() {
let fake_win_size = Winsize { let fake_win_size = Winsize {
ws_col: 121, ws_col: 121,
ws_row: 20, ws_row: 20,
@ -33,7 +24,11 @@ pub fn starts_with_one_terminal () {
let mut fake_input_output = get_fake_os_input(&fake_win_size); let mut fake_input_output = get_fake_os_input(&fake_win_size);
fake_input_output.add_terminal_input(&[QUIT]); fake_input_output.add_terminal_input(&[QUIT]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -49,12 +44,13 @@ pub fn split_terminals_vertically() {
ws_ypixel: 0, ws_ypixel: 0,
}; };
let mut fake_input_output = get_fake_os_input(&fake_win_size); let mut fake_input_output = get_fake_os_input(&fake_win_size);
fake_input_output.add_terminal_input(&[ fake_input_output.add_terminal_input(&[SPLIT_VERTICALLY, QUIT]);
SPLIT_VERTICALLY,
QUIT,
]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -70,12 +66,13 @@ pub fn split_terminals_horizontally() {
ws_ypixel: 0, ws_ypixel: 0,
}; };
let mut fake_input_output = get_fake_os_input(&fake_win_size); let mut fake_input_output = get_fake_os_input(&fake_win_size);
fake_input_output.add_terminal_input(&[ fake_input_output.add_terminal_input(&[SPLIT_HORIZONTALLY, QUIT]);
SPLIT_HORIZONTALLY,
QUIT,
]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -83,7 +80,7 @@ pub fn split_terminals_horizontally() {
} }
#[test] #[test]
pub fn split_largest_terminal () { pub fn split_largest_terminal() {
// this finds the largest pane and splits along its longest edge (vertically or horizontally) // this finds the largest pane and splits along its longest edge (vertically or horizontally)
let fake_win_size = Winsize { let fake_win_size = Winsize {
ws_col: 121, ws_col: 121,
@ -92,14 +89,13 @@ pub fn split_largest_terminal () {
ws_ypixel: 0, ws_ypixel: 0,
}; };
let mut fake_input_output = get_fake_os_input(&fake_win_size); let mut fake_input_output = get_fake_os_input(&fake_win_size);
fake_input_output.add_terminal_input(&[ fake_input_output.add_terminal_input(&[SPAWN_TERMINAL, SPAWN_TERMINAL, SPAWN_TERMINAL, QUIT]);
SPAWN_TERMINAL,
SPAWN_TERMINAL,
SPAWN_TERMINAL,
QUIT,
]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -144,7 +140,11 @@ pub fn resize_right_and_up_on_the_same_axis() {
QUIT, QUIT,
]); ]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -170,7 +170,11 @@ pub fn scrolling_inside_a_pane() {
QUIT, QUIT,
]); ]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -178,7 +182,7 @@ pub fn scrolling_inside_a_pane() {
} }
#[test] #[test]
pub fn max_panes () { pub fn max_panes() {
// with the --max-panes option, we only allow a certain amount of panes on screen // with the --max-panes option, we only allow a certain amount of panes on screen
// simultaneously, new panes beyond this limit will close older panes on screen // simultaneously, new panes beyond this limit will close older panes on screen
let fake_win_size = Winsize { let fake_win_size = Winsize {
@ -193,12 +197,16 @@ pub fn max_panes () {
SPAWN_TERMINAL, SPAWN_TERMINAL,
SPAWN_TERMINAL, SPAWN_TERMINAL,
SPAWN_TERMINAL, SPAWN_TERMINAL,
QUIT QUIT,
]); ]);
let mut opts = Opt::default(); let mut opts = Opt::default();
opts.max_panes = Some(4); opts.max_panes = Some(4);
start(Box::new(fake_input_output.clone()), opts); start(Box::new(fake_input_output.clone()), opts);
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -206,7 +214,7 @@ pub fn max_panes () {
} }
#[test] #[test]
pub fn toggle_focused_pane_fullscreen () { pub fn toggle_focused_pane_fullscreen() {
let fake_win_size = Winsize { let fake_win_size = Winsize {
ws_col: 121, ws_col: 121,
ws_row: 20, ws_row: 20,
@ -229,12 +237,16 @@ pub fn toggle_focused_pane_fullscreen () {
MOVE_FOCUS, MOVE_FOCUS,
TOGGLE_ACTIVE_TERMINAL_FULLSCREEN, TOGGLE_ACTIVE_TERMINAL_FULLSCREEN,
TOGGLE_ACTIVE_TERMINAL_FULLSCREEN, TOGGLE_ACTIVE_TERMINAL_FULLSCREEN,
QUIT QUIT,
]); ]);
let mut opts = Opt::default(); let mut opts = Opt::default();
opts.max_panes = Some(4); opts.max_panes = Some(4);
start(Box::new(fake_input_output.clone()), opts); start(Box::new(fake_input_output.clone()), opts);
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);

View file

@ -1,22 +1,16 @@
use ::nix::pty::Winsize;
use ::insta::assert_snapshot; use ::insta::assert_snapshot;
use ::nix::pty::Winsize;
use crate::{start, Opt}; use crate::tests::fakes::FakeInputOutput;
use crate::tests::fakes::{FakeInputOutput};
use crate::tests::utils::get_output_frame_snapshots; use crate::tests::utils::get_output_frame_snapshots;
use crate::{start, Opt};
use crate::tests::utils::commands::{ use crate::tests::utils::commands::{
SPLIT_HORIZONTALLY, CLOSE_FOCUSED_PANE, MOVE_FOCUS, QUIT, RESIZE_DOWN, RESIZE_LEFT, RESIZE_UP, SPLIT_HORIZONTALLY,
SPLIT_VERTICALLY, SPLIT_VERTICALLY,
RESIZE_DOWN,
RESIZE_UP,
MOVE_FOCUS,
RESIZE_LEFT,
QUIT,
CLOSE_FOCUSED_PANE,
}; };
fn get_fake_os_input (fake_win_size: &Winsize) -> FakeInputOutput { fn get_fake_os_input(fake_win_size: &Winsize) -> FakeInputOutput {
FakeInputOutput::new(fake_win_size.clone()) FakeInputOutput::new(fake_win_size.clone())
} }
@ -37,14 +31,14 @@ pub fn close_pane_with_another_pane_above_it() {
ws_ypixel: 0, ws_ypixel: 0,
}; };
let mut fake_input_output = get_fake_os_input(&fake_win_size); let mut fake_input_output = get_fake_os_input(&fake_win_size);
fake_input_output.add_terminal_input(&[ fake_input_output.add_terminal_input(&[SPLIT_HORIZONTALLY, CLOSE_FOCUSED_PANE, QUIT]);
SPLIT_HORIZONTALLY,
CLOSE_FOCUSED_PANE,
QUIT,
]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -76,7 +70,11 @@ pub fn close_pane_with_another_pane_below_it() {
]); ]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -98,14 +96,14 @@ pub fn close_pane_with_another_pane_to_the_left() {
ws_ypixel: 0, ws_ypixel: 0,
}; };
let mut fake_input_output = get_fake_os_input(&fake_win_size); let mut fake_input_output = get_fake_os_input(&fake_win_size);
fake_input_output.add_terminal_input(&[ fake_input_output.add_terminal_input(&[SPLIT_VERTICALLY, CLOSE_FOCUSED_PANE, QUIT]);
SPLIT_VERTICALLY,
CLOSE_FOCUSED_PANE,
QUIT,
]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -127,15 +125,14 @@ pub fn close_pane_with_another_pane_to_the_right() {
ws_ypixel: 0, ws_ypixel: 0,
}; };
let mut fake_input_output = get_fake_os_input(&fake_win_size); let mut fake_input_output = get_fake_os_input(&fake_win_size);
fake_input_output.add_terminal_input(&[ fake_input_output.add_terminal_input(&[SPLIT_VERTICALLY, MOVE_FOCUS, CLOSE_FOCUSED_PANE, QUIT]);
SPLIT_VERTICALLY,
MOVE_FOCUS,
CLOSE_FOCUSED_PANE,
QUIT,
]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -170,7 +167,11 @@ pub fn close_pane_with_multiple_panes_above_it() {
]); ]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -203,7 +204,11 @@ pub fn close_pane_with_multiple_panes_below_it() {
]); ]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -238,7 +243,11 @@ pub fn close_pane_with_multiple_panes_to_the_left() {
]); ]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -271,7 +280,11 @@ pub fn close_pane_with_multiple_panes_to_the_right() {
]); ]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -318,7 +331,11 @@ pub fn close_pane_with_multiple_panes_above_it_away_from_screen_edges() {
]); ]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -365,7 +382,11 @@ pub fn close_pane_with_multiple_panes_below_it_away_from_screen_edges() {
]); ]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -414,7 +435,11 @@ pub fn close_pane_with_multiple_panes_to_the_left_away_from_screen_edges() {
]); ]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -463,7 +488,11 @@ pub fn close_pane_with_multiple_panes_to_the_right_away_from_screen_edges() {
]); ]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -489,7 +518,11 @@ pub fn closing_last_pane_exits_app() {
]); ]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);

View file

@ -1,11 +1,11 @@
use ::nix::pty::Winsize;
use ::insta::assert_snapshot; use ::insta::assert_snapshot;
use ::nix::pty::Winsize;
use ::std::collections::HashMap; use ::std::collections::HashMap;
use crate::{start, Opt}; use crate::tests::fakes::FakeInputOutput;
use crate::tests::possible_tty_inputs::Bytes; use crate::tests::possible_tty_inputs::Bytes;
use crate::tests::fakes::{FakeInputOutput};
use crate::tests::utils::get_output_frame_snapshots; use crate::tests::utils::get_output_frame_snapshots;
use crate::{start, Opt};
use crate::tests::utils::commands::QUIT; use crate::tests::utils::commands::QUIT;
@ -22,7 +22,7 @@ use crate::tests::utils::commands::QUIT;
* *
*/ */
fn get_fake_os_input (fake_win_size: &Winsize, fixture_name: &str) -> FakeInputOutput { fn get_fake_os_input(fake_win_size: &Winsize, fixture_name: &str) -> FakeInputOutput {
let mut tty_inputs = HashMap::new(); let mut tty_inputs = HashMap::new();
let fixture_bytes = Bytes::from_file_in_fixtures(&fixture_name); let fixture_bytes = Bytes::from_file_in_fixtures(&fixture_name);
tty_inputs.insert(fake_win_size.ws_col, fixture_bytes); tty_inputs.insert(fake_win_size.ws_col, fixture_bytes);
@ -41,7 +41,11 @@ pub fn run_bandwhich_from_fish_shell() {
let mut fake_input_output = get_fake_os_input(&fake_win_size, fixture_name); let mut fake_input_output = get_fake_os_input(&fake_win_size, fixture_name);
fake_input_output.add_terminal_input(&[QUIT]); fake_input_output.add_terminal_input(&[QUIT]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -60,7 +64,11 @@ pub fn fish_tab_completion_options() {
let mut fake_input_output = get_fake_os_input(&fake_win_size, fixture_name); let mut fake_input_output = get_fake_os_input(&fake_win_size, fixture_name);
fake_input_output.add_terminal_input(&[QUIT]); fake_input_output.add_terminal_input(&[QUIT]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -84,7 +92,11 @@ pub fn fish_select_tab_completion_options() {
let mut fake_input_output = get_fake_os_input(&fake_win_size, fixture_name); let mut fake_input_output = get_fake_os_input(&fake_win_size, fixture_name);
fake_input_output.add_terminal_input(&[QUIT]); fake_input_output.add_terminal_input(&[QUIT]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -92,7 +104,7 @@ pub fn fish_select_tab_completion_options() {
} }
#[test] #[test]
pub fn vim_scroll_region_down () { pub fn vim_scroll_region_down() {
// here we test a case where vim defines the scroll region as lesser than the screen row count // here we test a case where vim defines the scroll region as lesser than the screen row count
// and then scrolls down // and then scrolls down
// the region is defined here by vim as 1-26 (there are 28 rows) // the region is defined here by vim as 1-26 (there are 28 rows)
@ -113,7 +125,11 @@ pub fn vim_scroll_region_down () {
// fake_input_output.add_terminal_input(&[17]); // quit (ctrl-q) // fake_input_output.add_terminal_input(&[17]); // quit (ctrl-q)
fake_input_output.add_terminal_input(&[QUIT]); // quit (ctrl-q) fake_input_output.add_terminal_input(&[QUIT]); // quit (ctrl-q)
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -138,7 +154,11 @@ pub fn vim_ctrl_d() {
let mut fake_input_output = get_fake_os_input(&fake_win_size, fixture_name); let mut fake_input_output = get_fake_os_input(&fake_win_size, fixture_name);
fake_input_output.add_terminal_input(&[QUIT]); fake_input_output.add_terminal_input(&[QUIT]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -162,7 +182,11 @@ pub fn vim_ctrl_u() {
let mut fake_input_output = get_fake_os_input(&fake_win_size, fixture_name); let mut fake_input_output = get_fake_os_input(&fake_win_size, fixture_name);
fake_input_output.add_terminal_input(&[QUIT]); fake_input_output.add_terminal_input(&[QUIT]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);

View file

@ -1,7 +1,7 @@
pub mod basic; pub mod basic;
pub mod compatibility;
pub mod resize_right;
pub mod resize_left;
pub mod resize_up;
pub mod resize_down;
pub mod close_pane; pub mod close_pane;
pub mod compatibility;
pub mod resize_down;
pub mod resize_left;
pub mod resize_right;
pub mod resize_up;

View file

@ -1,20 +1,15 @@
use ::nix::pty::Winsize;
use ::insta::assert_snapshot; use ::insta::assert_snapshot;
use ::nix::pty::Winsize;
use crate::{start, Opt}; use crate::tests::fakes::FakeInputOutput;
use crate::tests::fakes::{FakeInputOutput};
use crate::tests::utils::get_output_frame_snapshots; use crate::tests::utils::get_output_frame_snapshots;
use crate::{start, Opt};
use crate::tests::utils::commands::{ use crate::tests::utils::commands::{
SPLIT_HORIZONTALLY, MOVE_FOCUS, QUIT, RESIZE_DOWN, RESIZE_LEFT, SPLIT_HORIZONTALLY, SPLIT_VERTICALLY,
SPLIT_VERTICALLY,
RESIZE_DOWN,
MOVE_FOCUS,
RESIZE_LEFT,
QUIT,
}; };
fn get_fake_os_input (fake_win_size: &Winsize) -> FakeInputOutput { fn get_fake_os_input(fake_win_size: &Winsize) -> FakeInputOutput {
FakeInputOutput::new(fake_win_size.clone()) FakeInputOutput::new(fake_win_size.clone())
} }
@ -25,7 +20,7 @@ pub fn resize_down_with_pane_above() {
// │ │ │ │ // │ │ │ │
// ├───────────┤ ==resize=down==> │ │ // ├───────────┤ ==resize=down==> │ │
// │███████████│ ├───────────┤ // │███████████│ ├───────────┤
// │███████████│ │███████████│ // │███████████│ │███████████│
// │███████████│ │███████████│ // │███████████│ │███████████│
// └───────────┘ └───────────┘ // └───────────┘ └───────────┘
// █ == focused pane // █ == focused pane
@ -36,14 +31,14 @@ pub fn resize_down_with_pane_above() {
ws_ypixel: 0, ws_ypixel: 0,
}; };
let mut fake_input_output = get_fake_os_input(&fake_win_size); let mut fake_input_output = get_fake_os_input(&fake_win_size);
fake_input_output.add_terminal_input(&[ fake_input_output.add_terminal_input(&[SPLIT_HORIZONTALLY, RESIZE_DOWN, QUIT]);
SPLIT_HORIZONTALLY,
RESIZE_DOWN,
QUIT,
]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -67,14 +62,14 @@ pub fn resize_down_with_pane_below() {
ws_ypixel: 0, ws_ypixel: 0,
}; };
let mut fake_input_output = get_fake_os_input(&fake_win_size); let mut fake_input_output = get_fake_os_input(&fake_win_size);
fake_input_output.add_terminal_input(&[SPLIT_HORIZONTALLY, fake_input_output.add_terminal_input(&[SPLIT_HORIZONTALLY, MOVE_FOCUS, RESIZE_DOWN, QUIT]);
MOVE_FOCUS,
RESIZE_DOWN,
QUIT,
]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -111,7 +106,11 @@ pub fn resize_down_with_panes_above_and_below() {
]); ]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -148,7 +147,11 @@ pub fn resize_down_with_multiple_panes_above() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -187,7 +190,11 @@ pub fn resize_down_with_panes_above_aligned_left_with_current_pane() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -225,7 +232,11 @@ pub fn resize_down_with_panes_below_aligned_left_with_current_pane() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -261,7 +272,11 @@ pub fn resize_down_with_panes_above_aligned_right_with_current_pane() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -298,7 +313,11 @@ pub fn resize_down_with_panes_below_aligned_right_with_current_pane() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -338,7 +357,11 @@ pub fn resize_down_with_panes_above_aligned_left_and_right_with_current_pane() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -380,7 +403,11 @@ pub fn resize_down_with_panes_below_aligned_left_and_right_with_current_pane() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -437,7 +464,11 @@ pub fn resize_down_with_panes_above_aligned_left_and_right_with_panes_to_the_lef
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -496,7 +527,11 @@ pub fn resize_down_with_panes_below_aligned_left_and_right_with_to_the_left_and_
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);

View file

@ -1,20 +1,15 @@
use ::nix::pty::Winsize;
use ::insta::assert_snapshot; use ::insta::assert_snapshot;
use ::nix::pty::Winsize;
use crate::{start, Opt}; use crate::tests::fakes::FakeInputOutput;
use crate::tests::fakes::{FakeInputOutput};
use crate::tests::utils::get_output_frame_snapshots; use crate::tests::utils::get_output_frame_snapshots;
use crate::{start, Opt};
use crate::tests::utils::commands::{ use crate::tests::utils::commands::{
SPLIT_HORIZONTALLY, MOVE_FOCUS, QUIT, RESIZE_LEFT, RESIZE_UP, SPLIT_HORIZONTALLY, SPLIT_VERTICALLY,
SPLIT_VERTICALLY,
RESIZE_UP,
MOVE_FOCUS,
RESIZE_LEFT,
QUIT,
}; };
fn get_fake_os_input (fake_win_size: &Winsize) -> FakeInputOutput { fn get_fake_os_input(fake_win_size: &Winsize) -> FakeInputOutput {
FakeInputOutput::new(fake_win_size.clone()) FakeInputOutput::new(fake_win_size.clone())
} }
@ -33,14 +28,14 @@ pub fn resize_left_with_pane_to_the_left() {
ws_ypixel: 0, ws_ypixel: 0,
}; };
let mut fake_input_output = get_fake_os_input(&fake_win_size); let mut fake_input_output = get_fake_os_input(&fake_win_size);
fake_input_output.add_terminal_input(&[ fake_input_output.add_terminal_input(&[SPLIT_VERTICALLY, RESIZE_LEFT, QUIT]);
SPLIT_VERTICALLY,
RESIZE_LEFT,
QUIT,
]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -62,15 +57,14 @@ pub fn resize_left_with_pane_to_the_right() {
ws_ypixel: 0, ws_ypixel: 0,
}; };
let mut fake_input_output = get_fake_os_input(&fake_win_size); let mut fake_input_output = get_fake_os_input(&fake_win_size);
fake_input_output.add_terminal_input(&[ fake_input_output.add_terminal_input(&[SPLIT_VERTICALLY, MOVE_FOCUS, RESIZE_LEFT, QUIT]);
SPLIT_VERTICALLY,
MOVE_FOCUS,
RESIZE_LEFT,
QUIT,
]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -102,7 +96,11 @@ pub fn resize_left_with_panes_to_the_left_and_right() {
]); ]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -137,7 +135,11 @@ pub fn resize_left_with_multiple_panes_to_the_left() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -174,7 +176,11 @@ pub fn resize_left_with_panes_to_the_left_aligned_top_with_current_pane() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -208,7 +214,11 @@ pub fn resize_left_with_panes_to_the_right_aligned_top_with_current_pane() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -244,7 +254,11 @@ pub fn resize_left_with_panes_to_the_left_aligned_bottom_with_current_pane() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -279,7 +293,11 @@ pub fn resize_left_with_panes_to_the_right_aligned_bottom_with_current_pane() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -319,7 +337,11 @@ pub fn resize_left_with_panes_to_the_left_aligned_top_and_bottom_with_current_pa
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -361,7 +383,11 @@ pub fn resize_left_with_panes_to_the_right_aligned_top_and_bottom_with_current_p
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -418,7 +444,11 @@ pub fn resize_left_with_panes_to_the_left_aligned_top_and_bottom_with_panes_abov
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -435,7 +465,8 @@ pub fn resize_left_with_panes_to_the_right_aligned_top_and_bottom_with_panes_abo
// ├─────┼─────┤ ├───┴─┬─────┤ // ├─────┼─────┤ ├───┴─┬─────┤
// └─────┴─────┘ └─────┴─────┘ // └─────┴─────┘ └─────┴─────┘
// █ == focused pane // █ == focused pane
let fake_win_size = Winsize { // TODO: combine with above let fake_win_size = Winsize {
// TODO: combine with above
ws_col: 121, ws_col: 121,
ws_row: 40, ws_row: 40,
ws_xpixel: 0, ws_xpixel: 0,
@ -477,7 +508,11 @@ pub fn resize_left_with_panes_to_the_right_aligned_top_and_bottom_with_panes_abo
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);

View file

@ -1,20 +1,15 @@
use ::nix::pty::Winsize;
use ::insta::assert_snapshot; use ::insta::assert_snapshot;
use ::nix::pty::Winsize;
use crate::{start, Opt}; use crate::tests::fakes::FakeInputOutput;
use crate::tests::fakes::{FakeInputOutput};
use crate::tests::utils::get_output_frame_snapshots; use crate::tests::utils::get_output_frame_snapshots;
use crate::{start, Opt};
use crate::tests::utils::commands::{ use crate::tests::utils::commands::{
SPLIT_HORIZONTALLY, MOVE_FOCUS, QUIT, RESIZE_RIGHT, RESIZE_UP, SPLIT_HORIZONTALLY, SPLIT_VERTICALLY,
SPLIT_VERTICALLY,
RESIZE_UP,
MOVE_FOCUS,
RESIZE_RIGHT,
QUIT,
}; };
fn get_fake_os_input (fake_win_size: &Winsize) -> FakeInputOutput { fn get_fake_os_input(fake_win_size: &Winsize) -> FakeInputOutput {
FakeInputOutput::new(fake_win_size.clone()) FakeInputOutput::new(fake_win_size.clone())
} }
@ -33,14 +28,14 @@ pub fn resize_right_with_pane_to_the_left() {
ws_ypixel: 0, ws_ypixel: 0,
}; };
let mut fake_input_output = get_fake_os_input(&fake_win_size); let mut fake_input_output = get_fake_os_input(&fake_win_size);
fake_input_output.add_terminal_input(&[ fake_input_output.add_terminal_input(&[SPLIT_VERTICALLY, RESIZE_RIGHT, QUIT]);
SPLIT_VERTICALLY,
RESIZE_RIGHT,
QUIT,
]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -62,15 +57,14 @@ pub fn resize_right_with_pane_to_the_right() {
ws_ypixel: 0, ws_ypixel: 0,
}; };
let mut fake_input_output = get_fake_os_input(&fake_win_size); let mut fake_input_output = get_fake_os_input(&fake_win_size);
fake_input_output.add_terminal_input(&[ fake_input_output.add_terminal_input(&[SPLIT_VERTICALLY, MOVE_FOCUS, RESIZE_RIGHT, QUIT]);
SPLIT_VERTICALLY,
MOVE_FOCUS,
RESIZE_RIGHT,
QUIT,
]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -102,7 +96,11 @@ pub fn resize_right_with_panes_to_the_left_and_right() {
]); ]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -137,7 +135,11 @@ pub fn resize_right_with_multiple_panes_to_the_left() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -174,7 +176,11 @@ pub fn resize_right_with_panes_to_the_left_aligned_top_with_current_pane() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -208,7 +214,11 @@ pub fn resize_right_with_panes_to_the_right_aligned_top_with_current_pane() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -244,7 +254,11 @@ pub fn resize_right_with_panes_to_the_left_aligned_bottom_with_current_pane() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -279,7 +293,11 @@ pub fn resize_right_with_panes_to_the_right_aligned_bottom_with_current_pane() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -314,12 +332,16 @@ pub fn resize_right_with_panes_to_the_left_aligned_top_and_bottom_with_current_p
MOVE_FOCUS, MOVE_FOCUS,
SPLIT_VERTICALLY, SPLIT_VERTICALLY,
RESIZE_RIGHT, RESIZE_RIGHT,
QUIT QUIT,
]); ]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -361,7 +383,11 @@ pub fn resize_right_with_panes_to_the_right_aligned_top_and_bottom_with_current_
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -418,7 +444,11 @@ pub fn resize_right_with_panes_to_the_left_aligned_top_and_bottom_with_panes_abo
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -477,7 +507,11 @@ pub fn resize_right_with_panes_to_the_right_aligned_top_and_bottom_with_panes_ab
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);

View file

@ -1,20 +1,15 @@
use ::nix::pty::Winsize;
use ::insta::assert_snapshot; use ::insta::assert_snapshot;
use ::nix::pty::Winsize;
use crate::{start, Opt}; use crate::tests::fakes::FakeInputOutput;
use crate::tests::fakes::{FakeInputOutput};
use crate::tests::utils::get_output_frame_snapshots; use crate::tests::utils::get_output_frame_snapshots;
use crate::{start, Opt};
use crate::tests::utils::commands::{ use crate::tests::utils::commands::{
SPLIT_HORIZONTALLY, MOVE_FOCUS, QUIT, RESIZE_LEFT, RESIZE_UP, SPLIT_HORIZONTALLY, SPLIT_VERTICALLY,
SPLIT_VERTICALLY,
RESIZE_UP,
MOVE_FOCUS,
RESIZE_LEFT,
QUIT,
}; };
fn get_fake_os_input (fake_win_size: &Winsize) -> FakeInputOutput { fn get_fake_os_input(fake_win_size: &Winsize) -> FakeInputOutput {
FakeInputOutput::new(fake_win_size.clone()) FakeInputOutput::new(fake_win_size.clone())
} }
@ -35,14 +30,14 @@ pub fn resize_up_with_pane_above() {
ws_ypixel: 0, ws_ypixel: 0,
}; };
let mut fake_input_output = get_fake_os_input(&fake_win_size); let mut fake_input_output = get_fake_os_input(&fake_win_size);
fake_input_output.add_terminal_input(&[ fake_input_output.add_terminal_input(&[SPLIT_HORIZONTALLY, RESIZE_UP, QUIT]);
SPLIT_HORIZONTALLY,
RESIZE_UP,
QUIT
]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -66,15 +61,14 @@ pub fn resize_up_with_pane_below() {
ws_ypixel: 0, ws_ypixel: 0,
}; };
let mut fake_input_output = get_fake_os_input(&fake_win_size); let mut fake_input_output = get_fake_os_input(&fake_win_size);
fake_input_output.add_terminal_input(&[ fake_input_output.add_terminal_input(&[SPLIT_HORIZONTALLY, MOVE_FOCUS, RESIZE_UP, QUIT]);
SPLIT_HORIZONTALLY,
MOVE_FOCUS,
RESIZE_UP,
QUIT,
]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -104,13 +98,18 @@ pub fn resize_up_with_panes_above_and_below() {
fake_input_output.add_terminal_input(&[ fake_input_output.add_terminal_input(&[
SPLIT_HORIZONTALLY, SPLIT_HORIZONTALLY,
SPLIT_HORIZONTALLY, SPLIT_HORIZONTALLY,
MOVE_FOCUS, MOVE_FOCUS, MOVE_FOCUS,
MOVE_FOCUS,
RESIZE_UP, RESIZE_UP,
QUIT, QUIT,
]); ]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -146,7 +145,11 @@ pub fn resize_up_with_multiple_panes_above() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -183,7 +186,11 @@ pub fn resize_up_with_panes_above_aligned_left_with_current_pane() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -216,12 +223,16 @@ pub fn resize_up_with_panes_below_aligned_left_with_current_pane() {
MOVE_FOCUS, MOVE_FOCUS,
MOVE_FOCUS, MOVE_FOCUS,
RESIZE_UP, RESIZE_UP,
QUIT QUIT,
]); ]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -257,7 +268,11 @@ pub fn resize_up_with_panes_above_aligned_right_with_current_pane() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -294,7 +309,11 @@ pub fn resize_up_with_panes_below_aligned_right_with_current_pane() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -334,7 +353,11 @@ pub fn resize_up_with_panes_above_aligned_left_and_right_with_current_pane() {
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -371,12 +394,16 @@ pub fn resize_up_with_panes_below_aligned_left_and_right_with_current_pane() {
MOVE_FOCUS, MOVE_FOCUS,
MOVE_FOCUS, MOVE_FOCUS,
RESIZE_UP, RESIZE_UP,
QUIT QUIT,
]); ]);
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -433,7 +460,11 @@ pub fn resize_up_with_panes_above_aligned_left_and_right_with_panes_to_the_left_
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);
@ -492,7 +523,11 @@ pub fn resize_up_with_panes_below_aligned_left_and_right_with_to_the_left_and_ri
start(Box::new(fake_input_output.clone()), Opt::default()); start(Box::new(fake_input_output.clone()), Opt::default());
let output_frames = fake_input_output.stdout_writer.output_frames.lock().unwrap(); let output_frames = fake_input_output
.stdout_writer
.output_frames
.lock()
.unwrap();
let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size); let snapshots = get_output_frame_snapshots(&output_frames, &fake_win_size);
for snapshot in snapshots { for snapshot in snapshots {
assert_snapshot!(snapshot); assert_snapshot!(snapshot);

View file

@ -1,5 +1,5 @@
pub mod fakes;
pub mod integration; pub mod integration;
pub mod possible_tty_inputs; pub mod possible_tty_inputs;
pub mod tty_inputs; pub mod tty_inputs;
pub mod fakes;
pub mod utils; pub mod utils;

View file

@ -1,7 +1,10 @@
use crate::tests::tty_inputs::{
COL_10, COL_121, COL_14, COL_15, COL_19, COL_20, COL_24, COL_29, COL_30, COL_34, COL_39,
COL_40, COL_50, COL_60, COL_70, COL_90,
};
use std::collections::HashMap; use std::collections::HashMap;
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
use crate::tests::tty_inputs::{COL_10, COL_60, COL_14, COL_15, COL_19, COL_20, COL_24, COL_29, COL_30, COL_34, COL_39, COL_40, COL_50, COL_70, COL_90, COL_121};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Bytes { pub struct Bytes {
@ -13,7 +16,7 @@ impl Bytes {
pub fn new() -> Self { pub fn new() -> Self {
Bytes { Bytes {
content: vec![], content: vec![],
read_position: 0 read_position: 0,
} }
} }
pub fn from_file_in_fixtures(file_name: &str) -> Self { pub fn from_file_in_fixtures(file_name: &str) -> Self {
@ -22,10 +25,11 @@ impl Bytes {
path_to_file.push("tests"); path_to_file.push("tests");
path_to_file.push("fixtures"); path_to_file.push("fixtures");
path_to_file.push(file_name); path_to_file.push(file_name);
let content = fs::read(path_to_file).expect(&format!("could not read fixture {:?}", &file_name)); let content =
fs::read(path_to_file).expect(&format!("could not read fixture {:?}", &file_name));
Bytes { Bytes {
content, content,
read_position: 0 read_position: 0,
} }
} }
pub fn content_from_str(mut self, content: &[&'static str]) -> Self { pub fn content_from_str(mut self, content: &[&'static str]) -> Self {
@ -43,7 +47,8 @@ impl Bytes {
} }
} }
pub fn get_possible_tty_inputs () -> HashMap<u16, Bytes> { // the key is the column count for this terminal input pub fn get_possible_tty_inputs() -> HashMap<u16, Bytes> {
// the key is the column count for this terminal input
let mut possible_inputs = HashMap::new(); let mut possible_inputs = HashMap::new();
let col_10_bytes = Bytes::new().content_from_str(&COL_10); let col_10_bytes = Bytes::new().content_from_str(&COL_10);
let col_14_bytes = Bytes::new().content_from_str(&COL_14); let col_14_bytes = Bytes::new().content_from_str(&COL_14);

View file

@ -1,5 +1,5 @@
use ::nix::pty::Winsize;
use crate::terminal_pane::TerminalPane; use crate::terminal_pane::TerminalPane;
use ::nix::pty::Winsize;
pub fn get_output_frame_snapshots(output_frames: &[Vec<u8>], win_size: &Winsize) -> Vec<String> { pub fn get_output_frame_snapshots(output_frames: &[Vec<u8>], win_size: &Winsize) -> Vec<String> {
let mut vte_parser = vte::Parser::new(); let mut vte_parser = vte::Parser::new();
@ -48,5 +48,6 @@ pub mod commands {
pub const SCROLL_UP: [u8; 10] = [27, 91, 53, 94, 0, 0, 0, 0, 0, 0]; // ctrl-PgUp pub const SCROLL_UP: [u8; 10] = [27, 91, 53, 94, 0, 0, 0, 0, 0, 0]; // ctrl-PgUp
pub const SCROLL_DOWN: [u8; 10] = [27, 91, 54, 94, 0, 0, 0, 0, 0, 0]; // ctrl-PgDown pub const SCROLL_DOWN: [u8; 10] = [27, 91, 54, 94, 0, 0, 0, 0, 0, 0]; // ctrl-PgDown
pub const CLOSE_FOCUSED_PANE: [u8; 10] = [24, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // ctrl-x pub const CLOSE_FOCUSED_PANE: [u8; 10] = [24, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // ctrl-x
pub const TOGGLE_ACTIVE_TERMINAL_FULLSCREEN: [u8; 10] = [5, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // ctrl-e pub const TOGGLE_ACTIVE_TERMINAL_FULLSCREEN: [u8; 10] = [5, 0, 0, 0, 0, 0, 0, 0, 0, 0];
// ctrl-e
} }