Compare commits
1 Commits
master
...
plot-popup
| Author | SHA1 | Date | |
|---|---|---|---|
| dee88c916e |
@@ -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<char, (usize, usize)>,
|
||||
pub clipboard: Clipboard,
|
||||
pub plot_popup: Option<Plot>,
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
@@ -3,4 +3,5 @@ mod mode;
|
||||
mod error_msg;
|
||||
mod screen;
|
||||
mod logic;
|
||||
mod clipboard;
|
||||
mod clipboard;
|
||||
mod plot;
|
||||
@@ -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
|
||||
|
||||
45
src/app/plot.rs
Normal file
45
src/app/plot.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use ratatui::{layout::{Constraint, Direction, Layout}, widgets::{Paragraph, Widget}};
|
||||
|
||||
pub struct Plot {
|
||||
x: usize,
|
||||
y: Vec<usize>,
|
||||
}
|
||||
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];
|
||||
}
|
||||
}
|
||||
@@ -14,5 +14,4 @@ set grid
|
||||
set term png size 1280, 720
|
||||
set output '$OUTPUT'
|
||||
plot datafile using 1:2 with linespoints linestyle 1
|
||||
# plot datafile using 1:2 with linespoints linestyle 1, datafile using 1:3, etc...
|
||||
replot
|
||||
|
||||
Reference in New Issue
Block a user