* cli-output: Minor refactor of `build_balance_message()` (cherry picked from commit1c6f31241a
) * cli: Improve `stake-history` output readability (cherry picked from commitc8d83ae019
) Co-authored-by: Trent Nelson <trent@solana.com>
This commit is contained in:
@ -1,5 +1,8 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
display::{build_balance_message, format_labeled_address, writeln_name_value},
|
display::{
|
||||||
|
build_balance_message, build_balance_message_with_config, format_labeled_address,
|
||||||
|
writeln_name_value, BuildBalanceMessageConfig,
|
||||||
|
},
|
||||||
QuietDisplay, VerboseDisplay,
|
QuietDisplay, VerboseDisplay,
|
||||||
};
|
};
|
||||||
use chrono::{DateTime, NaiveDateTime, SecondsFormat, Utc};
|
use chrono::{DateTime, NaiveDateTime, SecondsFormat, Utc};
|
||||||
@ -866,14 +869,19 @@ impl fmt::Display for CliStakeHistory {
|
|||||||
))
|
))
|
||||||
.bold()
|
.bold()
|
||||||
)?;
|
)?;
|
||||||
|
let config = BuildBalanceMessageConfig {
|
||||||
|
use_lamports_unit: self.use_lamports_unit,
|
||||||
|
show_unit: false,
|
||||||
|
trim_trailing_zeros: false,
|
||||||
|
};
|
||||||
for entry in &self.entries {
|
for entry in &self.entries {
|
||||||
writeln!(
|
writeln!(
|
||||||
f,
|
f,
|
||||||
" {:>5} {:>20} {:>20} {:>20} {}",
|
" {:>5} {:>20} {:>20} {:>20} {}",
|
||||||
entry.epoch,
|
entry.epoch,
|
||||||
build_balance_message(entry.effective_stake, self.use_lamports_unit, false),
|
build_balance_message_with_config(entry.effective_stake, &config),
|
||||||
build_balance_message(entry.activating_stake, self.use_lamports_unit, false),
|
build_balance_message_with_config(entry.activating_stake, &config),
|
||||||
build_balance_message(entry.deactivating_stake, self.use_lamports_unit, false),
|
build_balance_message_with_config(entry.deactivating_stake, &config),
|
||||||
if self.use_lamports_unit {
|
if self.use_lamports_unit {
|
||||||
"lamports"
|
"lamports"
|
||||||
} else {
|
} else {
|
||||||
|
@ -7,22 +7,63 @@ use solana_sdk::{
|
|||||||
use solana_transaction_status::UiTransactionStatusMeta;
|
use solana_transaction_status::UiTransactionStatusMeta;
|
||||||
use std::{collections::HashMap, fmt, io};
|
use std::{collections::HashMap, fmt, io};
|
||||||
|
|
||||||
pub fn build_balance_message(lamports: u64, use_lamports_unit: bool, show_unit: bool) -> String {
|
#[derive(Clone, Debug)]
|
||||||
if use_lamports_unit {
|
pub struct BuildBalanceMessageConfig {
|
||||||
let ess = if lamports == 1 { "" } else { "s" };
|
pub use_lamports_unit: bool,
|
||||||
let unit = if show_unit {
|
pub show_unit: bool,
|
||||||
format!(" lamport{}", ess)
|
pub trim_trailing_zeros: bool,
|
||||||
} else {
|
}
|
||||||
"".to_string()
|
|
||||||
};
|
impl Default for BuildBalanceMessageConfig {
|
||||||
format!("{:?}{}", lamports, unit)
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
use_lamports_unit: false,
|
||||||
|
show_unit: true,
|
||||||
|
trim_trailing_zeros: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build_balance_message_with_config(
|
||||||
|
lamports: u64,
|
||||||
|
config: &BuildBalanceMessageConfig,
|
||||||
|
) -> String {
|
||||||
|
let value = if config.use_lamports_unit {
|
||||||
|
lamports.to_string()
|
||||||
} else {
|
} else {
|
||||||
let sol = lamports_to_sol(lamports);
|
let sol = lamports_to_sol(lamports);
|
||||||
let sol_str = format!("{:.9}", sol);
|
let sol_str = format!("{:.9}", sol);
|
||||||
let pretty_sol = sol_str.trim_end_matches('0').trim_end_matches('.');
|
if config.trim_trailing_zeros {
|
||||||
let unit = if show_unit { " SOL" } else { "" };
|
sol_str
|
||||||
format!("{}{}", pretty_sol, unit)
|
.trim_end_matches('0')
|
||||||
}
|
.trim_end_matches('.')
|
||||||
|
.to_string()
|
||||||
|
} else {
|
||||||
|
sol_str
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let unit = if config.show_unit {
|
||||||
|
if config.use_lamports_unit {
|
||||||
|
let ess = if lamports == 1 { "" } else { "s" };
|
||||||
|
format!(" lamport{}", ess)
|
||||||
|
} else {
|
||||||
|
" SOL".to_string()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
"".to_string()
|
||||||
|
};
|
||||||
|
format!("{}{}", value, unit)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build_balance_message(lamports: u64, use_lamports_unit: bool, show_unit: bool) -> String {
|
||||||
|
build_balance_message_with_config(
|
||||||
|
lamports,
|
||||||
|
&BuildBalanceMessageConfig {
|
||||||
|
use_lamports_unit,
|
||||||
|
show_unit,
|
||||||
|
..BuildBalanceMessageConfig::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pretty print a "name value"
|
// Pretty print a "name value"
|
||||||
|
Reference in New Issue
Block a user