Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tombstone/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ edition = "2024"
ghast = { path = "../ghast" }
clap = { version = "4.5.18", features = ["derive"] }
crossterm = "0.29.0"
ratatui = { version = "0.30.0", features = ["all-widgets"] }
ratatui = { version = "=0.29.0", features = ["all-widgets"] }
spirit = { path = "../spirit" }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
tokio = { version = "1.40.0", features = ["full"] }
tokio = { version = "1.47.1", features = ["full"] }
tokio-stream = { version = "0.1.17", features = ["sync"] }
iced = "0.13.1"
tokio-stream = { version = "0.1.16", features = ["sync"] }
indexmap = "2.6.0"
heapless = "0.9.1"
serde = { version = "1.0.228", features = ["derive"] }
Expand Down
42 changes: 0 additions & 42 deletions tombstone/src/cli/history.rs

This file was deleted.

35 changes: 35 additions & 0 deletions tombstone/src/gui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use iced::Element;
use iced::widget::Image;
use iced::widget::image::Handle;

pub struct GuiState {
image: Handle,
}

#[derive(Debug)]
pub enum GuiMessage {
Render(Handle),
}

impl GuiState {
pub fn new() -> Self {
Self {
image: Handle::from_bytes(Vec::new()),
}
}
pub fn update(&mut self, msg: GuiMessage) {
match msg {
GuiMessage::Render(handle) => self.image = handle,
}
}

pub fn view(&self) -> Element<'_, GuiMessage> {
Image::new(self.image.clone()).into()
}
}

impl Default for GuiState {
fn default() -> Self {
Self::new()
}
}
3 changes: 2 additions & 1 deletion tombstone/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ use crossterm::terminal::enable_raw_mode;
use ratatui::Terminal;
use ratatui::backend::CrosstermBackend;

pub mod cli;
pub mod command;
pub mod config;
pub mod display_windows;
pub mod gui;
pub mod pc_state;
pub mod repl;
pub mod state;

use command::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use ratatui::layout::Position;
use ratatui::prelude::Backend;

use crate::Command;
use crate::cli::PROMPT;
use crate::command::ReplCommand;
use crate::repl::PROMPT;

/// Create a thread to poll for user inputs and forward them to the main thread.
/// Originally based off of [bottom's implementation](https://github.com/ClementTsang/bottom/blob/master/src/main.rs).
Expand Down
132 changes: 132 additions & 0 deletions tombstone/src/repl/history.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
use std::borrow::Cow;

use ratatui::layout::Rect;
use ratatui::style;
use ratatui::text::Line;
use ratatui::text::Span;
use ratatui::text::Text;
use ratatui::widgets::Paragraph;

use super::PROMPT;

#[derive(Debug, Default)]
pub struct CliHistory {
// The history that is displayed (contains dups)
visual_history: Vec<Line<'static>>,
// The "up arrow" history (removes dups and is ordered by last use)
pub minimized_history: Vec<String>,
}

impl CliHistory {
pub fn push_command_output(&mut self, value: String) {
self.push_visual_history(value.lines().map(ToOwned::to_owned).map(Line::from));
}

pub fn push_command(&mut self, value: String) {
self.visual_history.push(format!("{PROMPT}{value}").into());
self.minimized_history.retain(|val| val != &value);
self.minimized_history.push(value);
}

pub fn push_visual_history(&mut self, lines: impl Iterator<Item = Line<'static>>) {
self.visual_history.extend(lines);
}

pub fn render(&self, rect: Rect) -> (u16, Paragraph<'_>) {
if self.visual_history.is_empty() {
return (0, Paragraph::default());
}
// Calcuate the number of lines needed for the visual history, before wrapping.
let index = self
.visual_history
.len()
.saturating_sub(rect.height as usize);
let entries = &self.visual_history[index..];

// Collect the wrapped lines into in a vec to be trimmed
let width = rect.width as usize;
let lines = Vec::new();
for line in entries {
let Line {
style,
alignment,
spans,
} = line;
let mut curr_width = 0;
let mut buffer = Vec::new();
for span in spans {
let (start, end) = split_span(width, span);
loop {}
}
}

// Use only the lines that will fit into the box
let index = lines.len().saturating_sub(rect.height as usize - 1);
let lines = lines[index..].to_vec();

(lines.len() as u16, Paragraph::new(Text::from(lines)))
}
}

fn wrap_lines(width: usize, line: &Line<'static>) -> impl Iterator<Item = Line<'static>> {
let style = line.style;
let alignment = line.alignment;
let mut spans = line.spans.clone().into_iter();
let mut curr_width = 0;
let mut buffer = Vec::new();
std::iter::from_fn(move || {
loop {
// After the first trim, the remaining span could be longer than width
if curr_width > width {
let span = buffer.pop().unwrap();
let (start, end) = split_span(width, span);
buffer.push(start);
let line = Line {
style,
alignment,
spans: std::mem::take(&mut buffer),
};
curr_width = end.width();
buffer.push(end);
return Some(line);
}
let Some(span) = spans.next() else {
return buffer
.is_empty()
.then(|| std::mem::take(&mut buffer))
.map(|spans| Line {
style,
alignment,
spans,
});
};
if curr_width + span.width() > width {
let (start, end) = split_span(width - curr_width, span);
buffer.push(start);
let line = Line {
style,
alignment,
spans: std::mem::take(&mut buffer),
};
curr_width = end.width();
buffer.push(end);
return Some(line);
}
curr_width += span.width();
buffer.push(span);
}
})
}

fn split_span(width: usize, span: Span) -> (Span, Span) {
let mut chars = span.content.chars();
let start = Span {
style: span.style,
content: chars.by_ref().take(width).collect(),
};
let end = Span {
style: span.style,
content: chars.collect(),
};
(start, end)
}
16 changes: 9 additions & 7 deletions tombstone/src/cli/mod.rs → tombstone/src/repl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ mod commands;
mod history;

use crate::Command;
use crate::cli::commands::CommandProcessor;
use crate::cli::history::CliHistory;
use crate::repl::commands::CommandProcessor;
use crate::repl::history::CliHistory;

/// The text displayed at the start of the shell command input.
static PROMPT: &str = "> ";

#[derive(Debug, Default)]
pub struct Cli {
pub struct Repl {
processor: CommandProcessor,
history: CliHistory,
/// Tracks where in the CLI history the cursor is at. Used for events like Up and Down.
Expand All @@ -25,7 +25,7 @@ pub struct Cli {
history_index: usize,
}

impl Cli {
impl Repl {
pub fn new() -> Self {
Self {
processor: CommandProcessor::new(),
Expand All @@ -35,7 +35,8 @@ impl Cli {
}

pub fn display(&mut self, data: String) {
self.history.push_visual_history(data);
self.history
.push_visual_history(data.lines().map(ToOwned::to_owned).map(Into::into));
}

pub fn push_to_history(&mut self, data: String) {
Expand Down Expand Up @@ -91,7 +92,8 @@ impl Cli {
if !s.is_empty() {
self.history.push_command(s);
} else {
self.history.push_visual_history(PROMPT.into());
self.history
.push_visual_history(std::iter::once(PROMPT.into()));
}
self.history_index = 0;
match command {
Expand All @@ -107,7 +109,7 @@ impl Cli {

pub fn render(&self, frame: &mut Frame, rect: Rect) {
let block = Block::bordered()
.title(" CLI ")
.title(" REPL ")
.title_alignment(ratatui::layout::Alignment::Center);
let inner_rect = block.inner(rect);
let y = inner_rect.y;
Expand Down
Loading
Loading