2024-05-10 12:03:38 +02:00
|
|
|
use hyperlog_core::log::GraphItem;
|
2024-05-02 23:47:47 +02:00
|
|
|
use ratatui::{
|
|
|
|
prelude::*,
|
|
|
|
widgets::{Block, Borders, Padding, Paragraph},
|
|
|
|
};
|
|
|
|
|
2024-05-09 14:10:43 +02:00
|
|
|
use crate::{
|
2024-05-12 14:29:14 +02:00
|
|
|
command_parser::CommandParser,
|
|
|
|
commander,
|
|
|
|
commands::{batch::BatchCommand, IntoCommand},
|
|
|
|
components::graph_explorer::GraphExplorer,
|
|
|
|
state::SharedState,
|
|
|
|
Msg,
|
2024-05-09 14:10:43 +02:00
|
|
|
};
|
2024-05-07 23:21:13 +02:00
|
|
|
|
2024-05-09 00:15:42 +02:00
|
|
|
use self::{
|
|
|
|
command_bar::{CommandBar, CommandBarState},
|
2024-05-10 12:03:38 +02:00
|
|
|
dialog::{
|
|
|
|
create_item::{CreateItem, CreateItemState},
|
|
|
|
edit_item::{EditItem, EditItemState},
|
|
|
|
},
|
2024-05-09 00:15:42 +02:00
|
|
|
};
|
2024-05-07 23:21:13 +02:00
|
|
|
|
2024-05-09 00:15:42 +02:00
|
|
|
mod command_bar;
|
2024-05-07 23:21:13 +02:00
|
|
|
pub mod dialog;
|
|
|
|
|
|
|
|
pub enum Dialog {
|
|
|
|
CreateItem { state: CreateItemState },
|
2024-05-10 12:03:38 +02:00
|
|
|
EditItem { state: EditItemState },
|
2024-05-07 23:21:13 +02:00
|
|
|
}
|
|
|
|
|
2024-05-09 14:10:43 +02:00
|
|
|
impl Dialog {
|
2024-05-11 23:23:00 +02:00
|
|
|
pub fn get_command(&self) -> Option<commander::Command> {
|
2024-05-09 14:10:43 +02:00
|
|
|
match self {
|
|
|
|
Dialog::CreateItem { state } => state.get_command(),
|
2024-05-10 12:03:38 +02:00
|
|
|
Dialog::EditItem { state } => state.get_command(),
|
2024-05-09 14:10:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-07 23:21:13 +02:00
|
|
|
pub enum Mode {
|
|
|
|
View,
|
|
|
|
Insert,
|
2024-05-09 00:15:42 +02:00
|
|
|
Command,
|
2024-05-07 23:21:13 +02:00
|
|
|
}
|
2024-05-02 23:47:47 +02:00
|
|
|
|
2024-05-09 15:26:58 +02:00
|
|
|
pub enum AppFocus {
|
|
|
|
Dialog,
|
|
|
|
Graph,
|
|
|
|
}
|
|
|
|
|
2024-05-02 23:47:47 +02:00
|
|
|
pub struct App<'a> {
|
2024-05-09 14:10:43 +02:00
|
|
|
root: String,
|
|
|
|
|
2024-05-02 23:47:47 +02:00
|
|
|
state: SharedState,
|
|
|
|
|
2024-05-07 23:21:13 +02:00
|
|
|
pub mode: Mode,
|
|
|
|
dialog: Option<Dialog>,
|
2024-05-09 00:15:42 +02:00
|
|
|
command: Option<CommandBarState>,
|
2024-05-07 23:21:13 +02:00
|
|
|
|
2024-05-02 23:47:47 +02:00
|
|
|
graph_explorer: GraphExplorer<'a>,
|
2024-05-09 15:26:58 +02:00
|
|
|
|
|
|
|
focus: AppFocus,
|
2024-05-02 23:47:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> App<'a> {
|
2024-05-09 14:10:43 +02:00
|
|
|
pub fn new(
|
|
|
|
root: impl Into<String>,
|
|
|
|
state: SharedState,
|
|
|
|
graph_explorer: GraphExplorer<'a>,
|
|
|
|
) -> Self {
|
2024-05-02 23:47:47 +02:00
|
|
|
Self {
|
2024-05-09 17:02:06 +02:00
|
|
|
state,
|
2024-05-09 14:10:43 +02:00
|
|
|
root: root.into(),
|
2024-05-09 17:02:06 +02:00
|
|
|
|
2024-05-07 23:21:13 +02:00
|
|
|
dialog: None,
|
2024-05-09 00:15:42 +02:00
|
|
|
command: None,
|
2024-05-09 17:02:06 +02:00
|
|
|
|
2024-05-02 23:47:47 +02:00
|
|
|
graph_explorer,
|
2024-05-09 17:02:06 +02:00
|
|
|
|
|
|
|
mode: Mode::View,
|
2024-05-09 15:26:58 +02:00
|
|
|
focus: AppFocus::Graph,
|
2024-05-02 23:47:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-09 00:15:42 +02:00
|
|
|
pub fn update(&mut self, msg: Msg) -> anyhow::Result<impl IntoCommand> {
|
2024-05-02 23:47:47 +02:00
|
|
|
tracing::trace!("handling msg: {:?}", msg);
|
|
|
|
|
2024-05-12 14:29:14 +02:00
|
|
|
let mut batch = BatchCommand::default();
|
|
|
|
|
2024-05-10 12:03:38 +02:00
|
|
|
match &msg {
|
2024-05-07 23:21:13 +02:00
|
|
|
Msg::MoveRight => self.graph_explorer.move_right()?,
|
|
|
|
Msg::MoveLeft => self.graph_explorer.move_left()?,
|
|
|
|
Msg::MoveDown => self.graph_explorer.move_down()?,
|
|
|
|
Msg::MoveUp => self.graph_explorer.move_up()?,
|
|
|
|
Msg::OpenCreateItemDialog => self.open_dialog(),
|
2024-05-10 12:03:38 +02:00
|
|
|
Msg::OpenEditItemDialog { item } => self.open_edit_item_dialog(item),
|
2024-05-07 23:21:13 +02:00
|
|
|
Msg::EnterInsertMode => self.mode = Mode::Insert,
|
2024-05-09 00:15:42 +02:00
|
|
|
Msg::EnterViewMode => self.mode = Mode::View,
|
|
|
|
Msg::EnterCommandMode => {
|
|
|
|
self.command = Some(CommandBarState::default());
|
|
|
|
self.mode = Mode::Command
|
|
|
|
}
|
2024-05-09 15:26:58 +02:00
|
|
|
Msg::Interact => match self.focus {
|
|
|
|
AppFocus::Dialog => {}
|
2024-05-12 14:29:14 +02:00
|
|
|
AppFocus::Graph => {
|
|
|
|
let cmd = self.graph_explorer.interact()?;
|
|
|
|
batch.with(cmd);
|
|
|
|
}
|
2024-05-09 15:26:58 +02:00
|
|
|
},
|
2024-05-09 12:07:23 +02:00
|
|
|
Msg::SubmitCommand { command } => {
|
2024-05-09 00:15:42 +02:00
|
|
|
tracing::info!("submitting command");
|
|
|
|
|
2024-05-10 12:03:38 +02:00
|
|
|
if let Some(command) = CommandParser::parse(command) {
|
2024-05-09 15:26:58 +02:00
|
|
|
match self.focus {
|
|
|
|
AppFocus::Dialog => {
|
|
|
|
if command.is_write() {
|
|
|
|
if let Some(dialog) = &self.dialog {
|
|
|
|
if let Some(output) = dialog.get_command() {
|
|
|
|
self.state.commander.execute(output)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-12 14:29:14 +02:00
|
|
|
batch.with(self.graph_explorer.new_update_graph());
|
2024-05-09 14:10:43 +02:00
|
|
|
}
|
|
|
|
|
2024-05-09 15:26:58 +02:00
|
|
|
if command.is_quit() {
|
2024-05-09 17:02:06 +02:00
|
|
|
self.focus = AppFocus::Graph;
|
2024-05-09 15:26:58 +02:00
|
|
|
self.dialog = None;
|
|
|
|
}
|
|
|
|
}
|
2024-05-10 12:03:38 +02:00
|
|
|
AppFocus::Graph => {
|
2024-05-12 14:29:14 +02:00
|
|
|
if let Some(cmd) = self.graph_explorer.execute_command(&command)? {
|
2024-05-10 12:03:38 +02:00
|
|
|
self.command = None;
|
2024-05-12 14:29:14 +02:00
|
|
|
batch.with(cmd);
|
2024-05-10 12:03:38 +02:00
|
|
|
}
|
2024-05-10 12:20:43 +02:00
|
|
|
|
|
|
|
if command.is_quit() {
|
2024-05-12 14:29:14 +02:00
|
|
|
batch.with(Msg::QuitApp.into_command());
|
2024-05-10 12:20:43 +02:00
|
|
|
}
|
2024-05-10 12:03:38 +02:00
|
|
|
}
|
2024-05-09 14:10:43 +02:00
|
|
|
}
|
|
|
|
}
|
2024-05-09 00:15:42 +02:00
|
|
|
self.command = None;
|
2024-05-12 14:29:14 +02:00
|
|
|
batch.with(Msg::EnterViewMode.into_command());
|
2024-05-09 00:15:42 +02:00
|
|
|
}
|
2024-05-07 23:21:13 +02:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2024-05-12 14:29:14 +02:00
|
|
|
let cmd = self.graph_explorer.inner.update(&msg);
|
|
|
|
if let Some(cmd) = cmd {
|
|
|
|
batch.with(cmd);
|
|
|
|
}
|
|
|
|
|
2024-05-09 00:15:42 +02:00
|
|
|
if let Some(command) = &mut self.command {
|
|
|
|
let cmd = command.update(&msg)?;
|
2024-05-12 14:29:14 +02:00
|
|
|
batch.with(cmd);
|
2024-05-09 00:15:42 +02:00
|
|
|
} else if let Some(dialog) = &mut self.dialog {
|
2024-05-07 23:21:13 +02:00
|
|
|
match dialog {
|
|
|
|
Dialog::CreateItem { state } => state.update(&msg)?,
|
2024-05-10 12:03:38 +02:00
|
|
|
Dialog::EditItem { state } => state.update(&msg)?,
|
2024-05-07 23:21:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-12 14:29:14 +02:00
|
|
|
Ok(batch.into_command())
|
2024-05-07 23:21:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn open_dialog(&mut self) {
|
|
|
|
if self.dialog.is_none() {
|
2024-05-09 14:10:43 +02:00
|
|
|
let root = self.root.clone();
|
|
|
|
let path = self.graph_explorer.get_current_path();
|
|
|
|
|
2024-05-09 17:02:06 +02:00
|
|
|
self.focus = AppFocus::Dialog;
|
2024-05-07 23:21:13 +02:00
|
|
|
self.dialog = Some(Dialog::CreateItem {
|
2024-05-09 14:10:43 +02:00
|
|
|
state: CreateItemState::new(root, path),
|
2024-05-07 23:21:13 +02:00
|
|
|
});
|
2024-05-02 23:47:47 +02:00
|
|
|
}
|
|
|
|
}
|
2024-05-10 12:03:38 +02:00
|
|
|
|
|
|
|
fn open_edit_item_dialog(&mut self, item: &GraphItem) {
|
|
|
|
if self.dialog.is_none() {
|
|
|
|
let root = self.root.clone();
|
|
|
|
let path = self.graph_explorer.get_current_path();
|
|
|
|
|
|
|
|
self.dialog = Some(Dialog::EditItem {
|
|
|
|
state: EditItemState::new(root, path, item),
|
|
|
|
});
|
|
|
|
self.command = None;
|
|
|
|
self.focus = AppFocus::Dialog;
|
|
|
|
self.mode = Mode::Insert;
|
|
|
|
}
|
|
|
|
}
|
2024-05-02 23:47:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Widget for &mut App<'a> {
|
|
|
|
fn render(self, area: Rect, buf: &mut Buffer)
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
StatefulWidget::render(
|
2024-05-09 17:02:06 +02:00
|
|
|
GraphExplorer::new(self.root.clone(), self.state.clone()),
|
2024-05-02 23:47:47 +02:00
|
|
|
area,
|
|
|
|
buf,
|
|
|
|
&mut self.graph_explorer.inner,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn render_app(frame: &mut Frame, state: &mut App) {
|
2024-05-07 23:21:13 +02:00
|
|
|
let chunks = Layout::vertical(vec![
|
|
|
|
Constraint::Length(2),
|
|
|
|
Constraint::Min(0),
|
|
|
|
Constraint::Length(1),
|
|
|
|
])
|
|
|
|
.split(frame.size());
|
|
|
|
|
|
|
|
let mut heading_parts = vec![Span::styled("hyperlog", Style::default()).fg(Color::Green)];
|
|
|
|
|
|
|
|
if let Some(dialog) = &state.dialog {
|
|
|
|
heading_parts.push(Span::raw(" ~ "));
|
|
|
|
|
|
|
|
match dialog {
|
|
|
|
Dialog::CreateItem { .. } => heading_parts.push(Span::raw("create item")),
|
2024-05-10 12:03:38 +02:00
|
|
|
Dialog::EditItem { .. } => heading_parts.push(Span::raw("edit item")),
|
2024-05-07 23:21:13 +02:00
|
|
|
}
|
|
|
|
}
|
2024-05-02 23:47:47 +02:00
|
|
|
|
2024-05-07 23:21:13 +02:00
|
|
|
let heading = Paragraph::new(text::Line::from(heading_parts));
|
2024-05-02 23:47:47 +02:00
|
|
|
let block_heading = Block::default().borders(Borders::BOTTOM);
|
|
|
|
|
|
|
|
frame.render_widget(heading.block(block_heading), chunks[0]);
|
|
|
|
|
2024-05-09 00:15:42 +02:00
|
|
|
match &state.mode {
|
|
|
|
Mode::View => {
|
|
|
|
let line = Line::raw("-- VIEW --");
|
|
|
|
|
|
|
|
let powerbar_block = Block::default()
|
|
|
|
.borders(Borders::empty())
|
|
|
|
.padding(Padding::new(1, 1, 0, 0));
|
|
|
|
frame.render_widget(Paragraph::new(vec![line]).block(powerbar_block), chunks[2]);
|
|
|
|
}
|
|
|
|
Mode::Insert => {
|
|
|
|
let line = Line::raw("-- EDIT --");
|
|
|
|
|
|
|
|
let powerbar_block = Block::default()
|
|
|
|
.borders(Borders::empty())
|
|
|
|
.padding(Padding::new(1, 1, 0, 0));
|
|
|
|
frame.render_widget(Paragraph::new(vec![line]).block(powerbar_block), chunks[2]);
|
|
|
|
}
|
|
|
|
Mode::Command => {
|
|
|
|
if let Some(command) = &mut state.command {
|
|
|
|
frame.render_stateful_widget(CommandBar::default(), chunks[2], command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-05-07 23:21:13 +02:00
|
|
|
|
2024-05-02 23:47:47 +02:00
|
|
|
let Rect { width, height, .. } = chunks[1];
|
|
|
|
|
|
|
|
let height = height as usize;
|
|
|
|
let width = width as usize;
|
|
|
|
|
|
|
|
let mut lines = Vec::new();
|
|
|
|
for y in 0..height {
|
|
|
|
if !y % 2 == 0 {
|
|
|
|
lines.push(text::Line::default());
|
|
|
|
} else {
|
|
|
|
lines.push(text::Line::raw(" ~ ".repeat(width / 3)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let _background = Paragraph::new(lines);
|
|
|
|
|
|
|
|
let _bg_block = Block::default()
|
|
|
|
.fg(Color::DarkGray)
|
|
|
|
.bold()
|
|
|
|
.padding(Padding {
|
|
|
|
left: 4,
|
|
|
|
right: 4,
|
|
|
|
top: 2,
|
|
|
|
bottom: 2,
|
|
|
|
});
|
|
|
|
|
2024-05-07 23:21:13 +02:00
|
|
|
if let Some(dialog) = state.dialog.as_mut() {
|
|
|
|
match dialog {
|
|
|
|
Dialog::CreateItem { state } => {
|
|
|
|
frame.render_stateful_widget(&mut CreateItem::default(), chunks[1], state)
|
|
|
|
}
|
2024-05-10 12:03:38 +02:00
|
|
|
Dialog::EditItem { state } => {
|
|
|
|
frame.render_stateful_widget(&mut EditItem::default(), chunks[1], state)
|
|
|
|
}
|
2024-05-07 23:21:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
frame.render_widget(state, chunks[1]);
|
2024-05-02 23:47:47 +02:00
|
|
|
}
|