Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: loading spinner #93

Merged
merged 4 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ extern crate log;

mod cli;
mod error;
mod help_strings;
mod modules;
mod register;
mod utils;
mod help_strings;

use colored::*;
use log::error;
use register::register;
use colored::*;

#[tokio::main]
async fn main() {
Expand Down
3 changes: 1 addition & 2 deletions cli/src/modules/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ pub enum ConnectionSubcommands {
},
}

// TODO: we should implement `from` so we can use todo and have a cleaner api
pub async fn parse_connection_args(
options: &ConnectionOptions,
agent: impl ConnectionModule,
Expand All @@ -47,7 +46,7 @@ pub async fn parse_connection_args(
return agent
.get_connection_by_id(id.to_string())
.await
.map(|connection| pretty_print_obj(connection));
.map(pretty_print_obj);
}
if options.all {
return agent
Expand Down
6 changes: 4 additions & 2 deletions cli/src/modules/credential_definition.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use agent_controller::modules::credential_definition::CredentialDefinitionModule;
use clap::{Args, Subcommand};
use serde_json::json;
use log::{debug, info};
use serde_json::json;

use crate::{
error::{Error, Result},
utils::logger::{pretty_stringify_obj},
utils::loader::{start_loader, Loader},
utils::logger::pretty_stringify_obj,
};

#[derive(Args)]
Expand All @@ -32,6 +33,7 @@ pub async fn parse_credential_definition_args(
options: &CredentialDefinitionOptions,
agent: impl CredentialDefinitionModule,
) -> Result<()> {
start_loader(Loader::Spinner);
if let Some(id) = &options.id {
return agent.get_by_id(id.to_string()).await.map(|cred_def| {
let loggable = json!({
Expand Down
6 changes: 5 additions & 1 deletion cli/src/modules/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use clap::{Args, Subcommand};
use log::{debug, info};

use crate::error::{Error, Result};
use crate::utils::logger::pretty_stringify_obj;
use crate::{
utils::loader::{start_loader, Loader},
utils::logger::pretty_stringify_obj,
};
use colored::*;

#[derive(Args)]
Expand Down Expand Up @@ -34,6 +37,7 @@ pub async fn parse_credentials_args(
commands: &CredentialSubcommands,
agent: impl CredentialsModule,
) -> Result<()> {
start_loader(Loader::Spinner);
match commands {
CredentialSubcommands::Offer {
connection_id,
Expand Down
2 changes: 2 additions & 0 deletions cli/src/modules/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ use clap::Args;
use log::{debug, info};

use crate::error::Result;
use crate::utils::loader::{start_loader, Loader};
use crate::utils::logger::pretty_stringify_obj;

#[derive(Args)]
pub struct FeaturesOptions {}

pub async fn parse_features_args(agent: impl FeaturesModule) -> Result<()> {
start_loader(Loader::Spinner);
agent.discover_features().await.map(|features| {
debug!("{}", pretty_stringify_obj(&features));
features
Expand Down
2 changes: 2 additions & 0 deletions cli/src/modules/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use colored::*;
use log::info;

use crate::error::Result;
use crate::utils::loader::{start_loader, Loader};

#[derive(Args)]
pub struct MessageOptions {
Expand All @@ -14,6 +15,7 @@ pub struct MessageOptions {
}

pub async fn parse_message_args(options: &MessageOptions, agent: impl MessageModule) -> Result<()> {
start_loader(Loader::Spinner);
let send_options = SendMessageOptions {
id: options.id.to_owned(),
message: options.message.to_owned(),
Expand Down
2 changes: 2 additions & 0 deletions cli/src/modules/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use log::info;

use crate::{
error::{Error, Result},
utils::loader::{start_loader, Loader},
utils::logger::pretty_print_obj,
};

Expand Down Expand Up @@ -32,6 +33,7 @@ pub enum SchemaSubcommands {
}

pub async fn parse_schema_args(options: &SchemaOptions, agent: impl SchemaModule) -> Result<()> {
start_loader(Loader::Spinner);
if let Some(id) = &options.id {
return agent
.get_by_id(id.to_string())
Expand Down
41 changes: 41 additions & 0 deletions cli/src/utils/loader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use core::time;
use std::io::{self, Write};

/// All the types of loaders
pub enum Loader {
Spinner,
}

/// prints a char to stdout with a delay and whether it should be inplace or
/// not
pub fn print_char(c: impl Into<String>, timeout: u64, inplace: bool) {
if inplace {
print!("{}\r", c.into());
} else {
print!("{}", c.into());
}
io::stdout().flush().unwrap();
std::thread::sleep(time::Duration::from_millis(timeout));
}

/// Start the loader
pub fn start_loader(loader: Loader) {
match loader {
Loader::Spinner => spinner_loader(),
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does the spinner know when to stop? 😅


I have an alternate suggestion for the spinner API:

two functions: start_spinner() and stop_spinner(). Do you think something like this is possible with a reasonable amount of effort 😅 ?

Sample code:

start_spinner();
do_something_async().await;
stop_spinner();

let me know what you think!


/// Spinning loader. Does inplace replacement.
fn spinner_loader() {
let time_between = 50;

std::thread::spawn(move || loop {
print_char('|', time_between, true);
print_char('/', time_between * 2, true);
print_char('-', time_between * 3, true);
print_char('\\', time_between * 4, true);
print_char('/', time_between * 5, true);
print_char('-', time_between * 6, true);
print_char('\\', time_between * 7, true);
});
}
1 change: 1 addition & 0 deletions cli/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
pub mod logger;
pub mod config;
pub mod qr;
pub mod loader;