diff --git a/crates/hyperlog-core/src/engine.rs b/crates/hyperlog-core/src/engine.rs index a8a5f2f..ec3bf39 100644 --- a/crates/hyperlog-core/src/engine.rs +++ b/crates/hyperlog-core/src/engine.rs @@ -196,6 +196,15 @@ impl Engine { Ok(()) } + + pub fn get_roots(&self) -> Option> { + let items = self.graph.keys().cloned().collect::>(); + if items.is_empty() { + None + } else { + Some(items) + } + } } impl Display for Engine { diff --git a/crates/hyperlog-core/src/querier.rs b/crates/hyperlog-core/src/querier.rs index f121f79..e7bfe57 100644 --- a/crates/hyperlog-core/src/querier.rs +++ b/crates/hyperlog-core/src/querier.rs @@ -9,6 +9,10 @@ impl Querier { Self { engine } } + pub fn get_available_roots(&self) -> Option> { + self.engine.get_roots() + } + pub fn get( &self, root: &str, diff --git a/crates/hyperlog-core/src/shared_engine.rs b/crates/hyperlog-core/src/shared_engine.rs index aff0a6f..a1be248 100644 --- a/crates/hyperlog-core/src/shared_engine.rs +++ b/crates/hyperlog-core/src/shared_engine.rs @@ -63,4 +63,8 @@ impl SharedEngine { Ok(()) } + + pub(crate) fn get_roots(&self) -> Option> { + self.inner.read().unwrap().get_roots() + } } diff --git a/crates/hyperlog-tui/src/lib.rs b/crates/hyperlog-tui/src/lib.rs index 9e74cfd..5950f49 100644 --- a/crates/hyperlog-tui/src/lib.rs +++ b/crates/hyperlog-tui/src/lib.rs @@ -39,7 +39,14 @@ pub async fn execute(state: State) -> Result<()> { } fn run(terminal: &mut Terminal>, state: SharedState) -> Result<()> { - let root = "kjuulh".to_string(); + let root = match state.querier.get_available_roots() { + // TODO: maybe present choose root screen + Some(roots) => roots.first().cloned().unwrap(), + None => { + // TODO: present create root screen + anyhow::bail!("no valid root available\nPlease run:\n\n$ hyperlog create-root --name "); + } + }; let mut graph_explorer = GraphExplorer::new(root.clone(), state.clone()); graph_explorer.update_graph()?; diff --git a/crates/hyperlog/src/cli.rs b/crates/hyperlog/src/cli.rs index 3797a00..55d9369 100644 --- a/crates/hyperlog/src/cli.rs +++ b/crates/hyperlog/src/cli.rs @@ -28,6 +28,11 @@ enum Commands { }, Info {}, + CreateRoot { + #[arg(long)] + name: String, + }, + ClearLock {}, } @@ -103,6 +108,12 @@ pub async fn execute() -> anyhow::Result<()> { println!("{}", output); } }, + Some(Commands::CreateRoot { name }) => { + state + .commander + .execute(commander::Command::CreateRoot { root: name })?; + println!("Root was successfully created, now run:\n\n$ hyperlog"); + } Some(Commands::Info {}) => { println!("graph stored at: {}", state.storage.info()?) }