From cfe815fe2e83cf829835f54d780df11fa0d7a77b Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Thu, 18 Jul 2024 15:19:12 +0300 Subject: [PATCH] feat: use dots for navigation --- src/main.rs | 81 +++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9bd91c7..7d54475 100644 --- a/src/main.rs +++ b/src/main.rs @@ -98,10 +98,10 @@ fn make_event(text: &str, tags: &[Tag]) -> Event { .unwrap() } +type TaskMap = HashMap; async fn repl() { - let mut tasks: HashMap = HashMap::new(); - let add_task = - |tasks: &mut HashMap, event: Event| tasks.insert(event.id, Task::new(event)); + let mut tasks: TaskMap = HashMap::new(); + let add_task = |tasks: &mut TaskMap, event: Event| tasks.insert(event.id, Task::new(event)); for argument in args().skip(1) { add_task( &mut tasks, @@ -109,8 +109,8 @@ async fn repl() { ); } - let mut position: Option = None; let mut properties: Vec = vec!["id".into(), "name".into(), "state".into()]; + let mut position: Option = None; loop { let mut prompt = String::with_capacity(64); let mut pos = position; @@ -127,11 +127,36 @@ async fn repl() { if input.trim() == "exit" { break; } - if input.starts_with(".") { - match input[1..2].parse::() { + let dots = input.chars().take_while(|c| c == &'.').count(); + match dots { + 0 => { + let mut tags: Vec = Vec::new(); + position.inspect(|p| tags.push(Tag::event(*p))); + let event = match input.split_once(": ") { + None => make_event(&input, &tags), + Some(s) => { + tags.append( + &mut s.1.split(" ") + .map(|t| Tag::Hashtag(t.to_string())) + .collect()); + make_event(s.0, &tags) + } + }; + for tag in event.tags.iter() { + match tag { + Tag::Event { event_id, .. } => { + tasks + .get_mut(event_id) + .map(|t| t.children.push(event.clone())); + } + _ => {} + } + } + add_task(&mut tasks, event); + } + 1 => match input[1..2].parse::() { Ok(index) => { properties.insert(index, input[2..].to_string()); - continue; } Err(_) => { let prop = &input[1..]; @@ -145,37 +170,14 @@ async fn repl() { } } } - } - } else { - match input.split_once(": ") { - None => { - position = EventId::parse(input).ok().or(position - .and_then(|p| tasks.get(&p)) - .and_then(|t| t.parent_id())); - } - Some(s) => { - let mut tags: Vec = - s.1.split(" ") - .map(|t| Tag::Hashtag(t.to_string())) - .collect(); - if let Some(pos) = position { - tags.push(Tag::event(pos)); - } - let event = make_event(s.0, &tags); - for tag in event.tags.iter() { - match tag { - Tag::Event { event_id, .. } => { - tasks - .get_mut(event_id) - .unwrap() - .children - .push(event.clone()); - } - _ => {} - } - } - add_task(&mut tasks, event); + }, + _ => { + for _ in 1..dots { + position = position + .and_then(|id| tasks.get(&id)) + .and_then(|t| t.parent_id()); } + let _ = EventId::parse(&input[dots..]).map(|p| position = Some(p)); } }; let events: Vec<&Event> = @@ -192,13 +194,12 @@ async fn repl() { _ => {} } } - CLIENT + let _ = CLIENT .batch_event( tasks.into_values().map(|t| t.event).collect(), RelaySendOptions::new().skip_send_confirmation(true), ) - .await - .unwrap(); + .await; } struct Task {