Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion crates/shared/cli-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ mod version;
pub use version::Version;

mod runtime;
pub use runtime::{build_runtime, run_until_ctrl_c, run_until_ctrl_c_fallible};
pub use runtime::RuntimeManager;

mod styles;
pub use styles::CliStyles;
70 changes: 38 additions & 32 deletions crates/shared/cli-utils/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,48 @@

use std::future::Future;

/// Builds a multi-threaded Tokio runtime with all features enabled.
pub fn build_runtime() -> eyre::Result<tokio::runtime::Runtime> {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.map_err(|e| eyre::eyre!("Failed to build tokio runtime: {}", e))
}
/// A runtime manager.
#[derive(Debug, Clone, Copy)]
pub struct RuntimeManager;

impl RuntimeManager {
/// Builds a multi-threaded Tokio runtime with all features enabled.
pub fn build_runtime() -> eyre::Result<tokio::runtime::Runtime> {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.map_err(|e| eyre::eyre!("Failed to build tokio runtime: {}", e))
}

/// Runs a future to completion, returning early on Ctrl+C.
pub async fn run_until_ctrl_c<F>(fut: F) -> eyre::Result<()>
where
F: Future<Output = ()>,
{
let ctrl_c = async {
tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler");
};
/// Runs a future to completion, returning early on Ctrl+C.
pub async fn run_until_ctrl_c<F>(fut: F) -> eyre::Result<()>
where
F: Future<Output = ()>,
{
let ctrl_c = async {
tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler");
};

tokio::select! {
biased;
() = ctrl_c => Ok(()),
() = fut => Ok(()),
tokio::select! {
biased;
() = ctrl_c => Ok(()),
() = fut => Ok(()),
}
}
}

/// Runs a fallible future to completion, returning early on Ctrl+C.
pub async fn run_until_ctrl_c_fallible<F>(fut: F) -> eyre::Result<()>
where
F: Future<Output = eyre::Result<()>>,
{
let ctrl_c = async {
tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler");
};
/// Runs a fallible future to completion, returning early on Ctrl+C.
pub async fn run_until_ctrl_c_fallible<F>(fut: F) -> eyre::Result<()>
where
F: Future<Output = eyre::Result<()>>,
{
let ctrl_c = async {
tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler");
};

tokio::select! {
biased;
() = ctrl_c => Ok(()),
result = fut => result,
tokio::select! {
biased;
() = ctrl_c => Ok(()),
result = fut => result,
}
}
}
24 changes: 24 additions & 0 deletions crates/shared/cli-utils/src/styles.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//! Cli styles for [clap].

use clap::builder::{
Styles,
styling::{AnsiColor, Color, Style},
};

/// A wrapper type for CLI styles.
#[derive(Debug, Clone, Copy)]
pub struct CliStyles;

impl CliStyles {
/// Initialize the CLI styles, returning a [`Styles`] instance.
pub const fn init() -> Styles {
clap::builder::Styles::styled()
.usage(Style::new().bold().underline().fg_color(Some(Color::Ansi(AnsiColor::Yellow))))
.header(Style::new().bold().underline().fg_color(Some(Color::Ansi(AnsiColor::Yellow))))
.literal(Style::new().fg_color(Some(Color::Ansi(AnsiColor::Green))))
.invalid(Style::new().bold().fg_color(Some(Color::Ansi(AnsiColor::Red))))
.error(Style::new().bold().fg_color(Some(Color::Ansi(AnsiColor::Red))))
.valid(Style::new().bold().underline().fg_color(Some(Color::Ansi(AnsiColor::Green))))
.placeholder(Style::new().fg_color(Some(Color::Ansi(AnsiColor::Blue))))
}
}
Loading