Skip to content

Commit

Permalink
Add progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
Barre committed Sep 16, 2024
1 parent 953deba commit 60388eb
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 10 deletions.
68 changes: 67 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "merklemap-cli"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
license = "MIT"
description = "A CLI client for the merklemap.com API"
Expand All @@ -16,3 +16,4 @@ serde_json = "1.0"
anyhow = "1.0"
futures = "0.3.30"
chrono = "0.4.38"
indicatif = "0.17"
25 changes: 17 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
//! MerkleMap CLI Library
//!
//! This library provides the core functionality for interacting with the [merklemap](https://www.merklemap.com/) API.
//! It includes functions for searching subdomains and tailing live subdomain discoveries.

use anyhow::Result;
use chrono::{DateTime, TimeZone, Utc};
use futures::StreamExt;
use indicatif::{ProgressBar, ProgressStyle};
use reqwest_eventsource::{Error as EventSourceError, Event, EventSource};
use serde::Deserialize;

Expand All @@ -21,6 +17,11 @@ struct TailEntry {
hostname: String,
}

#[derive(Debug, Deserialize)]
struct ProgressEvent {
progress_percentage: f64,
}

trait Printable {
fn print(&self);
}
Expand Down Expand Up @@ -59,19 +60,27 @@ where
T: for<'de> Deserialize<'de> + Printable,
{
let mut es = EventSource::get(url);
let pb = ProgressBar::new(100);
pb.set_style(ProgressStyle::default_bar()
.template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {percent}% ({eta})")?
.progress_chars("#>-"));

while let Some(event) = es.next().await {
match event {
Ok(Event::Message(message)) => {
let entry = serde_json::from_str::<T>(&message.data)?;
entry.print();
if let Ok(progress) = serde_json::from_str::<ProgressEvent>(&message.data) {
pb.set_position(progress.progress_percentage as u64);
} else if let Ok(entry) = serde_json::from_str::<T>(&message.data) {
entry.print();
}
}
Ok(Event::Open) => {}
Err(EventSourceError::StreamEnded) => break,
Err(e) => eprintln!("Error: {:?}", e),
}
}

pb.finish_with_message("Done");
Ok(())
}

Expand All @@ -80,7 +89,7 @@ fn format_timestamp(timestamp: DateTime<Utc>) -> String {
}

pub async fn search(query: &str) -> Result<()> {
let url = format!("https://api.merklemap.com/search?query={}&stream=true", query);
let url = format!("https://api.merklemap.com/search?query={}&stream=true&stream_progress=true", query);
process_event_stream::<Entry>(&url).await
}

Expand Down

0 comments on commit 60388eb

Please sign in to comment.