diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-07-25 17:44:27 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-07-25 17:44:27 +0200 |
| commit | 8b7c56df1940d2ac6e3ece3385464009e46382fa (patch) | |
| tree | fe8162896f7ca003fef26cd6ce0d2b49087ff679 /src/widgets | |
| parent | daa65dd89ec432522482729261d81e916e645ade (diff) | |
implement simple log view
Diffstat (limited to 'src/widgets')
| -rw-r--r-- | src/widgets/logview.rs | 48 | ||||
| -rw-r--r-- | src/widgets/mod.rs | 1 |
2 files changed, 49 insertions, 0 deletions
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; |