Commit d2b876ea authored by Christopher Silva's avatar Christopher Silva

add command/info area and add delete cell shortcut

parent 9f3b9973
......@@ -5,52 +5,34 @@ use termion::{clear, color, cursor};
use termion::event::Key;
use termion::input::TermRead;
use termion::raw::{IntoRawMode, RawTerminal};
use termion::terminal_size;
use spreadsheet_core::workbook::Workbook;
use spreadsheet_core::sheet::Sheet;
fn main() -> Result<()> {
let stdin = stdin();
print!("{}", clear::All);
let mut workbook = Workbook::new();
let sheet = workbook.add_sheet("Test".into());
for i in 1..=200 {
for j in 1..=200 {
sheet.write_text(i, j, format!("tx {} {}", i, j)).unwrap();
sheet.write_text(i, j, format!("text {} {}", i, j)).unwrap();
}
}
sheet.process_changes()?;
let cells = sheet.render_range(1, 1, 40, 20);
let mut ui = UI::new();
ui.cells = cells;
ui.print_all();
for c in stdin.keys() {
match c.unwrap() {
Key::Char('q') => break,
Key::Up => ui.move_cursor_up(),
Key::Down => ui.move_cursor_down(),
Key::Left => ui.move_cursor_left(),
Key::Right => ui.move_cursor_right(),
_ => {},
}
ui.print_changes();
}
print!("{}{}{}", color::Fg(color::Reset), color::Bg(color::Reset), clear::All);
let mut ui = UI::new(sheet);
ui.run()?;
Ok(())
}
struct UI {
struct UI<'a> {
stdout: RawTerminal<Stdout>,
size: (usize, usize),
cursor: (usize, usize),
previous: (usize, usize),
sheet: &'a mut Sheet,
cells: Vec<Vec<String>>,
width: usize,
fg: color::Fg<color::Blue>,
......@@ -59,14 +41,22 @@ struct UI {
ibg: color::Bg<color::Blue>,
}
impl UI {
fn new() -> Self {
#[allow(dead_code)]
impl<'a> UI<'a> {
fn new(sheet: &'a mut Sheet) -> Self {
let width = 10;
let (w, h) = terminal_size().unwrap();
let cell_w = w / width as u16 - 1;
let cell_h = h - 2;
let cells = sheet.render_range(1, 1, cell_h as usize, cell_w as usize);
UI {
stdout: stdout().into_raw_mode().unwrap(),
size: (w as usize, h as usize),
cursor: (1, 1),
previous: (1, 1),
cells: Vec::new(),
width: 7,
sheet: sheet,
cells: cells,
width: width,
fg: color::Fg(color::Blue),
bg: color::Bg(color::White),
ifg: color::Fg(color::White),
......@@ -74,6 +64,36 @@ impl UI {
}
}
fn run(&mut self) -> Result<()> {
self.print_all();
let stdin = stdin();
for c in stdin.keys() {
match c.unwrap() {
Key::Char('q') => break,
Key::Up => self.move_cursor_up(),
Key::Down => self.move_cursor_down(),
Key::Left => self.move_cursor_left(),
Key::Right => self.move_cursor_right(),
Key::Char('d') => {
self.sheet.delete_cell(self.cursor.0, self.cursor.1);
self.sheet.process_changes()?;
let cell_w = self.size.0 / self.width - 1;
let cell_h = self.size.1 - 2;
self.cells = self.sheet.render_range(1, 1, cell_h, cell_w);
},
_ => {},
}
self.print_changes();
}
print!("{}{}{}", color::Fg(color::Reset), color::Bg(color::Reset), clear::All);
Ok(())
}
fn move_cursor_up(&mut self) {
if self.cursor.0 > 1 {
self.previous = self.cursor;
......@@ -108,18 +128,11 @@ impl UI {
}
fn print_all(&mut self) {
print!("{}", clear::All);
print!("{}{}{}", self.fg, self.bg, 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.print_command_area();
self.stdout.flush().unwrap();
}
......@@ -137,6 +150,7 @@ impl UI {
}
fn print_row_numbers(&self) {
print!("{}{}", self.fg, self.bg);
for i in 1..=self.cells.len() {
if i == self.cursor.0 {
print!("{}{}", self.ifg, self.ibg);
......@@ -149,6 +163,7 @@ impl UI {
}
fn print_cells(&self) {
print!("{}{}", self.bg, self.fg);
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() {
......@@ -167,6 +182,10 @@ impl UI {
}
}
fn print_command_area(&self) {
print!("{}{}{}{}{}", cursor::Goto(1, 1), self.fg, self.bg, clear::CurrentLine, self.cells[self.cursor.0-1][self.cursor.1-1]);
}
fn print_changes(&mut self) {
// reprint current/previous column numbers
print!(
......@@ -219,7 +238,9 @@ impl UI {
print!("{:#<width$}", "", width=self.width);
}
print!("{}", cursor::Goto(1, 1));
// reprint command area
self.print_command_area();
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