From 65c2466f97990731987d66deee33c3630ce6b11b Mon Sep 17 00:00:00 2001 From: kjuulh Date: Thu, 16 May 2024 10:10:11 +0200 Subject: [PATCH] feat: enable creating items on the same level Signed-off-by: kjuulh --- crates/hyperlog-tui/src/app.rs | 17 +++++++++++++++++ crates/hyperlog-tui/src/command_parser.rs | 4 ++++ .../src/components/graph_explorer.rs | 19 +++++++++++++++++++ crates/hyperlog-tui/src/lib.rs | 5 +++++ crates/hyperlog-tui/src/models.rs | 1 + 5 files changed, 46 insertions(+) diff --git a/crates/hyperlog-tui/src/app.rs b/crates/hyperlog-tui/src/app.rs index 6dde906..f194db6 100644 --- a/crates/hyperlog-tui/src/app.rs +++ b/crates/hyperlog-tui/src/app.rs @@ -100,6 +100,7 @@ impl<'a> App<'a> { Msg::MoveDown => self.graph_explorer.move_down()?, Msg::MoveUp => self.graph_explorer.move_up()?, Msg::OpenCreateItemDialog => self.open_dialog(), + Msg::OpenCreateItemDialogBelow => self.open_dialog_below(), Msg::OpenEditItemDialog { item } => self.open_edit_item_dialog(item), Msg::EnterInsertMode => self.mode = Mode::Insert, Msg::EnterViewMode => self.mode = Mode::View, @@ -181,6 +182,22 @@ impl<'a> App<'a> { } } + fn open_dialog_below(&mut self) { + if self.dialog.is_none() { + let root = self.root.clone(); + let path = self.graph_explorer.get_current_path(); + + if let Some((_, rest)) = path.split_last() { + let path = rest.to_vec(); + + self.focus = AppFocus::Dialog; + self.dialog = Some(Dialog::CreateItem { + state: CreateItemState::new(&self.state, root, path), + }); + } + } + } + fn open_edit_item_dialog(&mut self, item: &GraphItem) { if self.dialog.is_none() { let root = self.root.clone(); diff --git a/crates/hyperlog-tui/src/command_parser.rs b/crates/hyperlog-tui/src/command_parser.rs index db9fcdc..9c1d2a5 100644 --- a/crates/hyperlog-tui/src/command_parser.rs +++ b/crates/hyperlog-tui/src/command_parser.rs @@ -7,6 +7,7 @@ pub enum Commands { Archive, CreateSection { name: String }, CreateItem { name: String }, + CreateBelow { name: String }, Edit, ShowAll, @@ -44,6 +45,9 @@ impl CommandParser { "ci" | "create-item" => Some(Commands::CreateItem { name: rest.join(" ").to_string(), }), + "cb" | "create-below" => Some(Commands::CreateBelow { + name: rest.join(" ").to_string(), + }), "e" | "edit" => Some(Commands::Edit), "show-all" => Some(Commands::ShowAll), "hide-done" => Some(Commands::HideDone), diff --git a/crates/hyperlog-tui/src/components/graph_explorer.rs b/crates/hyperlog-tui/src/components/graph_explorer.rs index fffec01..ae76113 100644 --- a/crates/hyperlog-tui/src/components/graph_explorer.rs +++ b/crates/hyperlog-tui/src/components/graph_explorer.rs @@ -274,6 +274,25 @@ impl<'a> GraphExplorer<'a> { batch.with(cmd.into_command()); } } + Commands::CreateBelow { name } => { + if !name.is_empty() { + let path = self.get_current_path(); + if let Some((_, path)) = path.split_last() { + let mut path = path.to_vec(); + path.push(name.replace(".", " ")); + + let cmd = self.state.create_item_command().command( + &self.inner.root, + &path.iter().map(|i| i.as_str()).collect_vec(), + name, + "", + &hyperlog_core::log::ItemState::default(), + ); + + batch.with(cmd.into_command()); + } + } + } Commands::Edit => { if let Some(item) = self.get_current_item() { let path = self.get_current_path(); diff --git a/crates/hyperlog-tui/src/lib.rs b/crates/hyperlog-tui/src/lib.rs index c0bb1c9..d0d9dd9 100644 --- a/crates/hyperlog-tui/src/lib.rs +++ b/crates/hyperlog-tui/src/lib.rs @@ -125,6 +125,11 @@ async fn update<'a>( app.update(Msg::OpenCreateItemDialog)?; app.update(Msg::EnterInsertMode)? } + KeyCode::Char('o') => { + // TODO: batch commands + app.update(Msg::OpenCreateItemDialogBelow)?; + app.update(Msg::EnterInsertMode)? + } KeyCode::Char('i') => app.update(Msg::EnterInsertMode)?, KeyCode::Char(':') => app.update(Msg::EnterCommandMode)?, _ => return Ok(UpdateConclusion(false)), diff --git a/crates/hyperlog-tui/src/models.rs b/crates/hyperlog-tui/src/models.rs index cf2094b..d56bd18 100644 --- a/crates/hyperlog-tui/src/models.rs +++ b/crates/hyperlog-tui/src/models.rs @@ -10,6 +10,7 @@ pub enum Msg { MoveUp, QuitApp, OpenCreateItemDialog, + OpenCreateItemDialogBelow, OpenEditItemDialog { item: GraphItem }, Interact,