Commit 9f3b9973 authored by Christopher Silva's avatar Christopher Silva

add struct to keep track of ui state and minimize writes

parent 64e6d247
......@@ -225,6 +225,11 @@ dependencies = [
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "numtoa"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "opaque-debug"
version = "0.2.3"
......@@ -321,6 +326,19 @@ dependencies = [
"num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "redox_syscall"
version = "0.1.56"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "redox_termios"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rust_decimal"
version = "1.0.3"
......@@ -379,6 +397,7 @@ version = "0.1.0"
dependencies = [
"anyhow 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)",
"spreadsheet_core 0.1.0",
"termion 1.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......@@ -403,6 +422,17 @@ dependencies = [
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "termion"
version = "1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
"numtoa 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "typenum"
version = "1.11.2"
......@@ -448,6 +478,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum num-rational 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f2885278d5fe2adc2f75ced642d52d879bffaceb5a2e0b1d4309ffdfb239b454"
"checksum num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c81ffc11c212fa327657cb19dd85eb7419e163b5b076bede2bdb5c974c07e4"
"checksum num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "76dac5ed2a876980778b8b85f75a71b6cbf0db0b1232ee12f826bccb00d09d72"
"checksum numtoa 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef"
"checksum opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c"
"checksum ordermap 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a86ed3f5f244b372d6b1a00b72ef7f8876d0bc6a78a4c9985c53614041512063"
"checksum pest 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7e4fb201c5c22a55d8b24fef95f78be52738e5e1361129be1b5e862ecdb6894a"
......@@ -459,6 +490,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
"checksum rayon 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "43739f8831493b276363637423d3622d4bd6394ab6f0a9c4a552e208aeb7fddd"
"checksum rayon-core 1.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f8bf17de6f23b05473c437eb958b9c850bfc8af0961fe17b4cc92d5a627b4791"
"checksum redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)" = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
"checksum rust_decimal 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f7a28ded8f10361cefb69a8d8e1d195acf59344150534c165c401d6611cf013d"
"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
"checksum scopeguard 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b42e15e59b18a828bbf5c58ea01debb36b9b096346de35d941dcb89009f24a0d"
......@@ -467,6 +500,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum serde 1.0.103 (registry+https://github.com/rust-lang/crates.io-index)" = "1217f97ab8e8904b57dd22eb61cde455fa7446a9c1cf43966066da047c1f3702"
"checksum sha-1 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "23962131a91661d643c98940b20fcaffe62d776a823247be80a48fcb8b6fce68"
"checksum syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)" = "dff0acdb207ae2fe6d5976617f887eb1e35a2ba52c13c7234c790960cdad9238"
"checksum termion 1.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "818ef3700c2a7b447dca1a1dd28341fe635e6ee103c806c636bb9c929991b2cd"
"checksum typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9"
"checksum ucd-trie 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8f00ed7be0c1ff1e24f46c3d2af4859f7e863672ba3a6e92e7cff702bf9f06c2"
"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
use std::io::{Write, stdin, stdout};
use std::io::{Write, stdin, Stdout, stdout};
use anyhow::Result;
use termion::{clear, color, cursor, style};
use termion::{clear, color, cursor};
use termion::event::Key;
use termion::input::TermRead;
use termion::raw::IntoRawMode;
use termion::raw::{IntoRawMode, RawTerminal};
use spreadsheet_core::workbook::Workbook;
fn main() -> Result<()> {
let stdin = stdin();
let mut stdout = stdout().into_raw_mode().unwrap();
print!("{}", clear::All);
let mut workbook = Workbook::new();
let sheet = workbook.add_sheet("Test".into());
for i in 0..200 {
for j in 0..200 {
for i in 1..=200 {
for j in 1..=200 {
sheet.write_text(i, j, format!("tx {} {}", i, j)).unwrap();
}
}
sheet.process_changes()?;
let cells = sheet.render_range(0, 0, 40, 20);
let mut x = 0;
let mut y = 0;
let cells = sheet.render_range(1, 1, 40, 20);
let mut ui = UI::new();
ui.cells = cells;
print!("{}", clear::All);
print_cells(&cells, (x, y));
stdout.flush().unwrap();
ui.print_all();
for c in stdin.keys() {
match c.unwrap() {
Key::Char('q') => break,
Key::Up => {
if y > 0 {
y-=1;
}
},
Key::Down => {
if y < 39 {
y+=1;
}
},
Key::Left => {
if x > 0 {
x-=1;
}
},
Key::Right => {
if x < 19 {
x+=1;
}
},
Key::Up => ui.move_cursor_up(),
Key::Down => ui.move_cursor_down(),
Key::Left => ui.move_cursor_left(),
Key::Right => ui.move_cursor_right(),
_ => {},
}
print_cells(&cells, (y, x));
stdout.flush().unwrap();
ui.print_changes();
}
print!("{}{}{}", clear::All, color::Fg(color::Reset), color::Bg(color::Reset));
print!("{}{}{}", color::Fg(color::Reset), color::Bg(color::Reset), clear::All);
Ok(())
}
fn print_cells(cells: &Vec<Vec<String>>, highlighted: (usize, usize)) {
print!("{}{}{}", color::Fg(color::Blue), color::Bg(color::White), cursor::Goto(1, 2));
let width = 7;
struct UI {
stdout: RawTerminal<Stdout>,
cursor: (usize, usize),
previous: (usize, usize),
cells: Vec<Vec<String>>,
width: usize,
fg: color::Fg<color::Blue>,
bg: color::Bg<color::White>,
ifg: color::Fg<color::White>,
ibg: color::Bg<color::Blue>,
}
//print empty cell in top left
print!("{:width$}", "", width=width);
//print column numbers
for i in 0..cells[0].len() {
if i == highlighted.1 {
print!("{}{}", color::Fg(color::White), color::Bg(color::Blue));
impl UI {
fn new() -> Self {
UI {
stdout: stdout().into_raw_mode().unwrap(),
cursor: (1, 1),
previous: (1, 1),
cells: Vec::new(),
width: 7,
fg: color::Fg(color::Blue),
bg: color::Bg(color::White),
ifg: color::Fg(color::White),
ibg: color::Bg(color::Blue),
}
print!("{:^width$}", i+1, width=width);
if i == highlighted.1 {
print!("{}{}", color::Fg(color::Blue), color::Bg(color::White));
}
fn move_cursor_up(&mut self) {
if self.cursor.0 > 1 {
self.previous = self.cursor;
self.cursor.0 -= 1;
}
}
print!("{}", cursor::Goto(1, 3));
for (r, row) in cells.iter().enumerate() {
//print row numbers
if r == highlighted.0 {
print!("{}{}", color::Fg(color::White), color::Bg(color::Blue));
fn move_cursor_down(&mut self) {
if self.cursor.0 < self.cells.len() {
self.previous = self.cursor;
self.cursor.0 += 1;
}
print!("{:^width$}", r+1, width=width);
if r == highlighted.0 {
print!("{}{}", color::Fg(color::Blue), color::Bg(color::White));
}
fn move_cursor_left(&mut self) {
if self.cursor.1 > 1 {
self.previous = self.cursor;
self.cursor.1 -= 1;
}
}
fn move_cursor_right(&mut self) {
if self.cursor.1 < self.cells[0].len() {
self.previous = self.cursor;
self.cursor.1 += 1;
}
}
fn set_cursor(&mut self, cursor: (usize, usize)) {
self.previous = self.cursor;
self.cursor = cursor;
}
fn print_all(&mut self) {
print!("{}", clear::All);
self.print_column_numbers();
self.print_row_numbers();
self.print_cells();
print!("{}", cursor::Goto(1, 1));
self.stdout.flush().unwrap();
}
fn print_numbers(&mut self) {
self.print_column_numbers();
self.print_row_numbers();
print!("{}", cursor::Goto(1, 1));
self.stdout.flush().unwrap();
}
fn print_column_numbers(&self) {
print!("{}{}{}{:width$}", self.fg, self.bg, cursor::Goto(1, 2), "", width=self.width);
for i in 1..=self.cells[0].len() {
if i == self.cursor.1 {
print!("{}{}", self.ifg, self.ibg);
}
print!("{:^width$}", i, width=self.width);
if i == self.cursor.1 {
print!("{}{}", self.fg, self.bg);
}
}
for (c, cell) in row.iter().enumerate() {
if (r, c) == highlighted {
print!("{}{}", color::Fg(color::White), color::Bg(color::Blue));
}
fn print_row_numbers(&self) {
for i in 1..=self.cells.len() {
if i == self.cursor.0 {
print!("{}{}", self.ifg, self.ibg);
}
if cell.len() <= width {
print!("{:<width$}", cell, width=width);
} else {
print!("{:#<width$}", "", width=width);
print!("{}{:^width$}", cursor::Goto(1, (i+2) as u16), i, width=self.width);
if i == self.cursor.0 {
print!("{}{}", self.fg, self.bg);
}
if (r, c) == highlighted {
print!("{}{}", color::Fg(color::Blue), color::Bg(color::White));
}
}
fn print_cells(&self) {
for (r, row) in self.cells.iter().enumerate() {
print!("{}", cursor::Goto((1+self.width) as u16, (r+3) as u16));
for (c, cell) in row.iter().enumerate() {
if (r+1, c+1) == self.cursor {
print!("{}{}", self.ifg, self.ibg);
}
if cell.len() <= self.width {
print!("{:<width$}", cell, width=self.width);
} else {
print!("{:#<width$}", "", width=self.width);
}
if (r+1, c+1) == self.cursor {
print!("{}{}", self.fg, self.bg);
}
}
}
print!("{}", cursor::Goto(1, (r+4) as u16));
}
print!("{}{}{}", cursor::Goto(1, 1), color::Fg(color::Reset), color::Bg(color::Reset));
fn print_changes(&mut self) {
// reprint current/previous column numbers
print!(
"{}{}{}{:^width$}",
cursor::Goto((self.previous.1 * self.width + 1) as u16, 2),
self.fg, self.bg,
self.previous.1, width=self.width
);
print!(
"{}{}{}{:^width$}",
cursor::Goto((self.cursor.1 * self.width + 1) as u16, 2),
self.ifg, self.ibg,
self.cursor.1, width=self.width
);
// reprint current/previous row numbers
print!(
"{}{}{}{:^width$}",
cursor::Goto(1, (self.previous.0 + 2) as u16),
self.fg, self.bg,
self.previous.0, width=self.width
);
print!(
"{}{}{}{:^width$}",
cursor::Goto(1, (self.cursor.0 + 2) as u16),
self.ifg, self.ibg,
self.cursor.0, width=self.width
);
// reprint current/previous cells
print!(
"{}{}{}",
cursor::Goto((self.previous.1 * self.width + 1) as u16, (self.previous.0 + 2) as u16),
self.fg, self.bg
);
if self.cells[self.previous.0-1][self.previous.1-1].len() <= self.width {
print!("{:<width$}", self.cells[self.previous.0-1][self.previous.1-1], width=self.width);
} else {
print!("{:#<width$}", "", width=self.width);
}
print!(
"{}{}{}",
cursor::Goto((self.cursor.1 * self.width + 1) as u16, (self.cursor.0 + 2) as u16),
self.ifg, self.ibg
);
if self.cells[self.cursor.0-1][self.cursor.1-1].len() <= self.width {
print!("{:<width$}", self.cells[self.cursor.0-1][self.cursor.1-1], width=self.width);
} else {
print!("{:#<width$}", "", width=self.width);
}
print!("{}", cursor::Goto(1, 1));
self.stdout.flush().unwrap();
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment