Skip to content

Commit

Permalink
[refactor]: Extract style module
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandr Petrosyan <a-p-petrosyan@yandex.ru>
  • Loading branch information
appetrosyan committed Apr 19, 2023
1 parent 2813707 commit cf3c803
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 79 deletions.
83 changes: 5 additions & 78 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use torii::Torii;
mod event;
pub mod samples;
mod stream;
pub mod style;
pub mod torii;

/// Arguments for Iroha2. Configuration for arguments is parsed from
Expand Down Expand Up @@ -451,6 +452,10 @@ fn genesis_domain(configuration: &Configuration) -> Domain {
domain
}

/// Combine configuration proxies from several locations, preferring `ENV` vars over config file
///
/// # Errors
/// - if config fails to build
pub fn combine_configs(args: &Arguments) -> color_eyre::eyre::Result<Configuration> {
args.config_path
.first_existing_path()
Expand All @@ -466,84 +471,6 @@ pub fn combine_configs(args: &Arguments) -> color_eyre::eyre::Result<Configurati
.map_err(Into::into)
}

pub mod style {
//! Style and colouration of Iroha CLI outputs.
use owo_colors::{OwoColorize, Style};

/// Styling information set at run-time for pretty-printing with colour
#[derive(Clone, Copy, Debug)]
pub struct Styling {
/// Positive highlight
pub positive: Style,
/// Negative highlight. Usually error message.
pub negative: Style,
/// Neutral highlight
pub highlight: Style,
/// Minor message
pub minor: Style,
}

impl Default for Styling {
fn default() -> Self {
Self {
positive: Style::new().green().bold(),
negative: Style::new().red().bold(),
highlight: Style::new().bold(),
minor: Style::new().green(),
}
}
}

/// Determine if message colourisation is to be enabled
pub fn should_disable_color() -> bool {
supports_color::on(supports_color::Stream::Stdout).is_none()
|| std::env::var("TERMINAL_COLORS")
.map(|s| !s.as_str().parse().unwrap_or(true))
.unwrap_or(false)
}

impl Styling {
#[must_use]
/// Constructor
pub fn new() -> Self {
if should_disable_color() {
Self::no_color()
} else {
Self::default()
}
}

fn no_color() -> Self {
Self {
positive: Style::new(),
negative: Style::new(),
highlight: Style::new(),
minor: Style::new(),
}
}

/// Produce documentation for argument group
pub fn or(&self, arg_group: &[&str; 2]) -> String {
format!(
"`{}` (short `{}`)",
arg_group[0].style(self.positive),
arg_group[1].style(self.minor)
)
}

/// Convenience method for ".json or .json5" pattern
pub fn with_json_file_ext(&self, name: &str) -> String {
let json = format!("{name}.json");
let json5 = format!("{name}.json5");
format!(
"`{}` or `{}`",
json.style(self.highlight),
json5.style(self.highlight)
)
}
}
}

#[cfg(not(feature = "test-network"))]
#[cfg(test)]
mod tests {
Expand Down
75 changes: 75 additions & 0 deletions cli/src/style.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//! Style and colouration of Iroha CLI outputs.
use owo_colors::{OwoColorize, Style};

/// Styling information set at run-time for pretty-printing with colour
#[derive(Clone, Copy, Debug)]
pub struct Styling {
/// Positive highlight
pub positive: Style,
/// Negative highlight. Usually error message.
pub negative: Style,
/// Neutral highlight
pub highlight: Style,
/// Minor message
pub minor: Style,
}

impl Default for Styling {
fn default() -> Self {
Self {
positive: Style::new().green().bold(),
negative: Style::new().red().bold(),
highlight: Style::new().bold(),
minor: Style::new().green(),
}
}
}

/// Determine if message colourisation is to be enabled
pub fn should_disable_color() -> bool {
supports_color::on(supports_color::Stream::Stdout).is_none()
|| std::env::var("TERMINAL_COLORS")
.map(|s| !s.as_str().parse().unwrap_or(true))
.unwrap_or(false)
}

impl Styling {
#[must_use]
/// Constructor
pub fn new() -> Self {
if should_disable_color() {
Self::no_color()
} else {
Self::default()
}
}

fn no_color() -> Self {
Self {
positive: Style::new(),
negative: Style::new(),
highlight: Style::new(),
minor: Style::new(),
}
}

/// Produce documentation for argument group
pub fn or(&self, arg_group: &[&str; 2]) -> String {
format!(
"`{}` (short `{}`)",
arg_group[0].style(self.positive),
arg_group[1].style(self.minor)
)
}

/// Convenience method for ".json or .json5" pattern
pub fn with_json_file_ext(&self, name: &str) -> String {
let json = format!("{name}.json");
let json5 = format!("{name}.json5");
format!(
"`{}` or `{}`",
json.style(self.highlight),
json5.style(self.highlight)
)
}
}
2 changes: 1 addition & 1 deletion data_model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use std::borrow::Cow;
use block::VersionedCommittedBlock;
#[cfg(not(target_arch = "aarch64"))]
use derive_more::Into;
use derive_more::{AsRef, Deref, Display, From, FromStr, DebugCustom};
use derive_more::{AsRef, DebugCustom, Deref, Display, From, FromStr};
use events::FilterBox;
use getset::Getters;
use iroha_crypto::{Hash, PublicKey};
Expand Down

0 comments on commit cf3c803

Please # to comment.