diff --git a/src/app/app.rs b/src/app/app.rs index 916eea2..a5f092b 100644 --- a/src/app/app.rs +++ b/src/app/app.rs @@ -8,11 +8,11 @@ use std::{ use ratatui::{ DefaultTerminal, Frame, - crossterm::event, - layout::{self, Constraint, Layout, Rect}, + crossterm::event::{self, KeyCode}, + layout::{self, Constraint, Layout, Margin, Rect}, prelude, style::{Color, Modifier, Style}, - widgets::{Paragraph, Widget}, + widgets::{Block, Borders, Paragraph, Widget}, }; use crate::app::{ @@ -20,6 +20,7 @@ use crate::app::{ error_msg::StatusMessage, logic::{calc::Grid, cell::CellType}, mode::Mode, + plot::Plot, screen::ScreenSpace, }; @@ -35,6 +36,7 @@ pub struct App { // this could probably be a normal array pub marks: HashMap, pub clipboard: Clipboard, + pub plot_popup: Option, } impl Widget for &App { @@ -42,7 +44,7 @@ impl Widget for &App { let (x_max, y_max) = self.screen.how_many_cells_fit_in(&area, &self.vars); let is_selected = |x: usize, y: usize| -> bool { - if let Mode::Visual((mut x1, mut y1)) | Mode::VisualCmd((mut x1, mut y1), _)= self.mode { + if let Mode::Visual((mut x1, mut y1)) | Mode::VisualCmd((mut x1, mut y1), _) = self.mode { let (mut x2, mut y2) = self.grid.cursor(); x1 += 1; y1 += 1; @@ -217,6 +219,7 @@ impl App { marks: HashMap::new(), clipboard: Clipboard::new(), file_modified_date: SystemTime::now(), + plot_popup: None, } } @@ -315,9 +318,46 @@ impl App { )), cmd_line_debug, ); + + // popups + if let Some(plot) = &self.plot_popup { + let block = Block::default() + .title("Plot Editor") + .title_alignment(layout::Alignment::Center) + .border_type(ratatui::widgets::BorderType::Rounded) + .borders(Borders::all()) + .style(Style::default().fg(Color::White)); + let popup_y = 20; + let popup_x = 40; + + let a = frame.area(); + let xpos = (a.width / 2) - (popup_x / 2); + let ypos = (a.height / 2) - (popup_y / 2); + let area = Rect::new(xpos, ypos, popup_x, popup_y); + + frame.render_widget(ratatui::widgets::Clear, area); + frame.render_widget(block, area); + + let area = area.inner(Margin::new(1, 1)); + frame.render_widget(plot, area); + } } fn handle_events(&mut self) -> io::Result<()> { + if let Some(plot) = &mut self.plot_popup { + if let event::Event::Key(key) = event::read()? { + match key.code { + KeyCode::Esc => self.plot_popup = None, + KeyCode::PrintScreen | KeyCode::Enter => unimplemented!("Generate plot data"), + KeyCode::Delete => plot.del_column(), + KeyCode::Insert => plot.add_column(), + KeyCode::Char(c) => plot.process_key(c), + _ => {} + } + } + return Ok(()); + } + match &mut self.mode { Mode::VisualCmd(pos, chord) => match event::read()? { event::Event::Key(key) => match key.code { diff --git a/src/app/mod.rs b/src/app/mod.rs index e18abed..64185c9 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -3,4 +3,5 @@ mod mode; mod error_msg; mod screen; mod logic; -mod clipboard; \ No newline at end of file +mod clipboard; +mod plot; \ No newline at end of file diff --git a/src/app/mode.rs b/src/app/mode.rs index 42ed1fe..d8f6369 100644 --- a/src/app/mode.rs +++ b/src/app/mode.rs @@ -17,7 +17,7 @@ use crate::app::{ logic::{ calc::{CSV_EXT, CUSTOM_EXT, Grid, LEN}, cell::CellType, - }, + }, plot::Plot, }; pub enum Mode { @@ -198,6 +198,8 @@ impl Mode { app.mode = Mode::Normal } "plot" => { + app.plot_popup = Some(Plot::new(0, 1)); + return; // Use gnuplot to plot the selected data. // * Temp data will be stored in /tmp/ // * Output will either be plot.png or a name that you pass in diff --git a/src/app/plot.rs b/src/app/plot.rs new file mode 100644 index 0000000..75f6b6f --- /dev/null +++ b/src/app/plot.rs @@ -0,0 +1,45 @@ +use ratatui::{layout::{Constraint, Direction, Layout}, widgets::{Paragraph, Widget}}; + +pub struct Plot { + x: usize, + y: Vec, +} +impl Plot { + pub fn new(x: usize, y: usize) -> Self { + Self { + x, + y: vec![y], + } + } + + pub fn add_column(&mut self) { + self.y.push(1) + } + pub fn del_column(&mut self) { + self.y.pop(); + } + + pub fn process_key(&mut self, c: char) { + + } +} + +impl Widget for &Plot { + fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) { + // plus 1 for x + let columns = self.y.len() + 1; + + let mut constraints = Vec::new(); + for _ in 0..=columns { + constraints.push(Constraint::Min(1)); + } + + Paragraph::new("Foobar").render(area, buf); + let layout = Layout::default() + .direction(Direction::Vertical) + .constraints(constraints) + .split(area); + + let x_space = layout[0]; + } +} \ No newline at end of file