summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/app.rs20
-rw-r--r--src/widgets/logview.rs48
-rw-r--r--src/widgets/mod.rs1
3 files changed, 66 insertions, 3 deletions
diff --git a/src/app.rs b/src/app.rs
index 71e6a02..937c7d7 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -1,6 +1,6 @@
use std::{io, time::Duration};
-use crate::{sheet::register::Register, tui, widgets::sheetview::SheetView};
+use crate::{sheet::register::Register, tui, widgets::{logview::LogView, sheetview::SheetView}};
use ratatui::{
crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers},
@@ -11,6 +11,7 @@ use ratatui::{
pub struct App<'a> {
exit: bool,
view: SheetView<'a>,
+ logview: Option<LogView<'a>>,
}
impl App<'_> {
@@ -30,7 +31,7 @@ impl App<'_> {
let view = SheetView::new(sheet_id).block(Block::bordered().title("Sheet"));
- Self { exit: false, view }
+ Self { exit: false, view, logview: None }
}
pub fn run(&mut self, terminal: &mut tui::Tui) -> io::Result<()> {
@@ -61,6 +62,12 @@ impl App<'_> {
fn handle_key_event(&mut self, key_event: KeyEvent) {
match key_event.code {
KeyCode::Char('q') if key_event.modifiers == KeyModifiers::CONTROL => self.exit(),
+ KeyCode::Char('l') if key_event.modifiers == KeyModifiers::CONTROL => {
+ match self.logview {
+ Some(_) => self.logview = None,
+ None => self.logview = Some(LogView::new().block(Block::bordered().title(" Log "))),
+ }
+ }
_ => self.view.handle_key_event(key_event),
}
}
@@ -75,6 +82,13 @@ impl Widget for &mut App<'_> {
where
Self: Sized,
{
- self.view.render(area, buf);
+ match &mut self.logview {
+ Some(logview) => {
+ let layout = Layout::vertical([Constraint::Min(2), Constraint::Length(15)]).split(area);
+ self.view.render(layout[0], buf);
+ logview.render(layout[1], buf);
+ },
+ None => self.view.render(area, buf),
+ }
}
}
diff --git a/src/widgets/logview.rs b/src/widgets/logview.rs
new file mode 100644
index 0000000..4259e35
--- /dev/null
+++ b/src/widgets/logview.rs
@@ -0,0 +1,48 @@
+use ratatui::{text::ToSpan, widgets::{block::BlockExt, Block, Widget}};
+
+use crate::lua::iobuffer::iobuffer;
+
+
+pub struct LogView<'a> {
+ block: Option<Block<'a>>
+}
+
+impl<'a> LogView<'a> {
+ pub fn new() -> Self {
+ Self { block: None }
+ }
+
+ pub fn block(mut self, block: Block<'a>) -> Self {
+ self.block = Some(block);
+ self
+ }
+}
+
+impl Widget for &mut LogView<'_> {
+ fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer)
+ where
+ Self: Sized {
+ self.block.render(area, buf);
+ let inner_area = self.block.inner_if_some(area);
+
+ let lines = {
+ let buffer = iobuffer().read().unwrap();
+ buffer.get()
+ };
+
+ let mut line_area = inner_area.clone();
+ line_area.height = 1;
+ line_area.y = inner_area.y + inner_area.height - 1;
+
+ for line in lines.lines().rev() {
+ let line = line.to_span();
+ line.render(line_area, buf);
+
+ line_area.y -= 1;
+
+ if !inner_area.contains(line_area.into()) {
+ break;
+ }
+ }
+ }
+}
diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs
index 8a9b1a0..48e6854 100644
--- a/src/widgets/mod.rs
+++ b/src/widgets/mod.rs
@@ -1,2 +1,3 @@
pub mod sheetview;
pub mod luaeditor;
+pub mod logview;