Commit 64e6d247 authored by Christopher Silva's avatar Christopher Silva

start working on ui

parent 6edbeb8c
......@@ -9,3 +9,4 @@ edition = "2018"
[dependencies]
spreadsheet_core = { path = "../spreadsheet_core" }
anyhow = "1.0.25"
termion = "1.5.4"
use std::io::{Write, stdin, stdout};
use anyhow::Result;
use termion::{clear, color, cursor, style};
use termion::event::Key;
use termion::input::TermRead;
use termion::raw::IntoRawMode;
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 {
sheet.write_text(i, j, format!("text {} {}", i, j)).unwrap();
sheet.write_text(i, j, format!("tx {} {}", i, j)).unwrap();
}
}
sheet.process_changes()?;
println!("range r0c0:r9c9: {:?}", sheet.render_range(0, 0, 10, 10));
let cells = sheet.render_range(0, 0, 40, 20);
let mut x = 0;
let mut y = 0;
print!("{}", clear::All);
print_cells(&cells, (x, y));
stdout.flush().unwrap();
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;
}
},
_ => {},
}
print_cells(&cells, (y, x));
stdout.flush().unwrap();
}
print!("{}{}{}", clear::All, color::Fg(color::Reset), color::Bg(color::Reset));
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;
//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));
}
print!("{:^width$}", i+1, width=width);
if i == highlighted.1 {
print!("{}{}", color::Fg(color::Blue), color::Bg(color::White));
}
}
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));
}
print!("{:^width$}", r+1, width=width);
if r == highlighted.0 {
print!("{}{}", color::Fg(color::Blue), color::Bg(color::White));
}
for (c, cell) in row.iter().enumerate() {
if (r, c) == highlighted {
print!("{}{}", color::Fg(color::White), color::Bg(color::Blue));
}
if cell.len() <= width {
print!("{:<width$}", cell, width=width);
} else {
print!("{:#<width$}", "", width=width);
}
if (r, c) == highlighted {
print!("{}{}", color::Fg(color::Blue), color::Bg(color::White));
}
}
print!("{}", cursor::Goto(1, (r+4) as u16));
}
print!("{}{}{}", cursor::Goto(1, 1), color::Fg(color::Reset), color::Bg(color::Reset));
}
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