Skip to content

Commit 3e1b7e2

Browse files
authored
chore(shared): CLI Styles (#397)
* chore(shared): cli-utils styles * chore: use blue for placeholder style Addresses review feedback to include blue in CLI styling. * fix(shared): runtime
1 parent 01a91f0 commit 3e1b7e2

File tree

3 files changed

+66
-33
lines changed

3 files changed

+66
-33
lines changed

crates/shared/cli-utils/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ mod version;
1919
pub use version::Version;
2020

2121
mod runtime;
22-
pub use runtime::{build_runtime, run_until_ctrl_c, run_until_ctrl_c_fallible};
22+
pub use runtime::RuntimeManager;
23+
24+
mod styles;
25+
pub use styles::CliStyles;

crates/shared/cli-utils/src/runtime.rs

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,48 @@
22
33
use std::future::Future;
44

5-
/// Builds a multi-threaded Tokio runtime with all features enabled.
6-
pub fn build_runtime() -> eyre::Result<tokio::runtime::Runtime> {
7-
tokio::runtime::Builder::new_multi_thread()
8-
.enable_all()
9-
.build()
10-
.map_err(|e| eyre::eyre!("Failed to build tokio runtime: {}", e))
11-
}
5+
/// A runtime manager.
6+
#[derive(Debug, Clone, Copy)]
7+
pub struct RuntimeManager;
8+
9+
impl RuntimeManager {
10+
/// Builds a multi-threaded Tokio runtime with all features enabled.
11+
pub fn build_runtime() -> eyre::Result<tokio::runtime::Runtime> {
12+
tokio::runtime::Builder::new_multi_thread()
13+
.enable_all()
14+
.build()
15+
.map_err(|e| eyre::eyre!("Failed to build tokio runtime: {}", e))
16+
}
1217

13-
/// Runs a future to completion, returning early on Ctrl+C.
14-
pub async fn run_until_ctrl_c<F>(fut: F) -> eyre::Result<()>
15-
where
16-
F: Future<Output = ()>,
17-
{
18-
let ctrl_c = async {
19-
tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler");
20-
};
18+
/// Runs a future to completion, returning early on Ctrl+C.
19+
pub async fn run_until_ctrl_c<F>(fut: F) -> eyre::Result<()>
20+
where
21+
F: Future<Output = ()>,
22+
{
23+
let ctrl_c = async {
24+
tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler");
25+
};
2126

22-
tokio::select! {
23-
biased;
24-
() = ctrl_c => Ok(()),
25-
() = fut => Ok(()),
27+
tokio::select! {
28+
biased;
29+
() = ctrl_c => Ok(()),
30+
() = fut => Ok(()),
31+
}
2632
}
27-
}
2833

29-
/// Runs a fallible future to completion, returning early on Ctrl+C.
30-
pub async fn run_until_ctrl_c_fallible<F>(fut: F) -> eyre::Result<()>
31-
where
32-
F: Future<Output = eyre::Result<()>>,
33-
{
34-
let ctrl_c = async {
35-
tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler");
36-
};
34+
/// Runs a fallible future to completion, returning early on Ctrl+C.
35+
pub async fn run_until_ctrl_c_fallible<F>(fut: F) -> eyre::Result<()>
36+
where
37+
F: Future<Output = eyre::Result<()>>,
38+
{
39+
let ctrl_c = async {
40+
tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler");
41+
};
3742

38-
tokio::select! {
39-
biased;
40-
() = ctrl_c => Ok(()),
41-
result = fut => result,
43+
tokio::select! {
44+
biased;
45+
() = ctrl_c => Ok(()),
46+
result = fut => result,
47+
}
4248
}
4349
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//! Cli styles for [clap].
2+
3+
use clap::builder::{
4+
Styles,
5+
styling::{AnsiColor, Color, Style},
6+
};
7+
8+
/// A wrapper type for CLI styles.
9+
#[derive(Debug, Clone, Copy)]
10+
pub struct CliStyles;
11+
12+
impl CliStyles {
13+
/// Initialize the CLI styles, returning a [`Styles`] instance.
14+
pub const fn init() -> Styles {
15+
clap::builder::Styles::styled()
16+
.usage(Style::new().bold().underline().fg_color(Some(Color::Ansi(AnsiColor::Yellow))))
17+
.header(Style::new().bold().underline().fg_color(Some(Color::Ansi(AnsiColor::Yellow))))
18+
.literal(Style::new().fg_color(Some(Color::Ansi(AnsiColor::Green))))
19+
.invalid(Style::new().bold().fg_color(Some(Color::Ansi(AnsiColor::Red))))
20+
.error(Style::new().bold().fg_color(Some(Color::Ansi(AnsiColor::Red))))
21+
.valid(Style::new().bold().underline().fg_color(Some(Color::Ansi(AnsiColor::Green))))
22+
.placeholder(Style::new().fg_color(Some(Color::Ansi(AnsiColor::Blue))))
23+
}
24+
}

0 commit comments

Comments
 (0)