handle unavailable terminal colors (#797)

Co-authored-by: ardocrat <ardocrat@gri.mw>
This commit is contained in:
wiesche
2026-09-16 14:06:41 +03:00
committed by GitHub
co-authored by ardocrat
parent 0f7aa2f7fa
commit 99cca5b8bf
+43 -45
View File
@@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::core::core::amount_to_hr_string;
use crate::core::core::FeeFields;
use crate::core::core::{self, amount_to_hr_string};
use crate::core::global;
use crate::libwallet::{
address, AcctPathMapping, Error, OutputCommitMapping, OutputStatus, SlatepackAddress,
@@ -43,9 +43,9 @@ pub fn outputs(
return Ok(());
}
let mut t = term::stdout().unwrap();
t.fg(term::color::MAGENTA).unwrap();
writeln!(t, "{}", title).unwrap();
t.reset().unwrap();
let _ = t.fg(term::color::MAGENTA);
writeln!(t, "{}", title)?;
let _ = t.reset();
let mut table = table!();
@@ -78,7 +78,7 @@ pub fn outputs(
};
let num_confirmations = format!("{}", m.output.num_confirmations(cur_height));
let value = format!("{}", core::amount_to_hr_string(m.output.value, false));
let value = format!("{}", amount_to_hr_string(m.output.value, false));
let tx = match m.output.tx_log_entry {
None => "".to_owned(),
Some(t) => t.to_string(),
@@ -118,7 +118,7 @@ pub fn outputs(
if !validated {
println!(
"\nWARNING: Wallet failed to verify data. \
The above is from local cache and possibly invalid! \
The above is from local cache and possibly invalid! \
(is your `grin server` offline or broken?)"
);
}
@@ -144,9 +144,9 @@ pub fn txs(
return Ok(());
}
let mut t = term::stdout().unwrap();
t.fg(term::color::MAGENTA).unwrap();
writeln!(t, "{}", title).unwrap();
t.reset().unwrap();
let _ = t.fg(term::color::MAGENTA);
writeln!(t, "{}", title)?;
let _ = t.reset();
let mut table = table!();
@@ -193,18 +193,18 @@ pub fn txs(
let confirmed = format!("{}", t.confirmed);
let num_inputs = format!("{}", t.num_inputs);
let num_outputs = format!("{}", t.num_outputs);
let amount_debited_str = core::amount_to_hr_string(t.amount_debited, true);
let amount_credited_str = core::amount_to_hr_string(t.amount_credited, true);
let amount_debited_str = amount_to_hr_string(t.amount_debited, true);
let amount_credited_str = amount_to_hr_string(t.amount_credited, true);
let fee = match t.fee {
Some(f) => format!("{}", core::amount_to_hr_string(f.fee(), true)),
Some(f) => format!("{}", amount_to_hr_string(f.fee(), true)),
None => "None".to_owned(),
};
let net_diff = if t.amount_credited >= t.amount_debited {
core::amount_to_hr_string(t.amount_credited - t.amount_debited, true)
amount_to_hr_string(t.amount_credited - t.amount_debited, true)
} else {
format!(
"-{}",
core::amount_to_hr_string(t.amount_debited - t.amount_credited, true)
amount_to_hr_string(t.amount_debited - t.amount_credited, true)
)
};
let tx_data = match t.stored_tx {
@@ -290,7 +290,7 @@ pub fn txs(
if !validated && include_status {
println!(
"\nWARNING: Wallet failed to verify data. \
The above is from local cache and possibly invalid! \
The above is from local cache and possibly invalid! \
(is your `grin server` offline or broken?)"
);
}
@@ -334,9 +334,9 @@ pub fn view_wallet_output(
}
let mut t = term::stdout().unwrap();
t.fg(term::color::MAGENTA).unwrap();
writeln!(t, "{}", title).unwrap();
t.reset().unwrap();
let _ = t.fg(term::color::MAGENTA);
writeln!(t, "{}", title)?;
let _ = t.reset();
let mut table = table!();
@@ -357,7 +357,7 @@ pub fn view_wallet_output(
let lock_height = format!("{}", m.lock_height);
let is_coinbase = format!("{}", m.is_coinbase);
let num_confirmations = format!("{}", m.num_confirmations(cur_height));
let value = format!("{}", core::amount_to_hr_string(m.value, false));
let value = format!("{}", amount_to_hr_string(m.value, false));
if dark_background_color_scheme {
table.add_row(row![
@@ -409,11 +409,11 @@ pub fn info(
]);
if wallet_info.amount_reverted > 0 {
table.add_row(row![
Fr->format!("Reverted"),
Fr->"Reverted",
Fr->amount_to_hr_string(wallet_info.amount_reverted, false)
]);
}
// Only dispay "Immature Coinbase" if we have related outputs in the wallet.
// Only display "Immature Coinbase" if we have related outputs in the wallet.
// This row just introduces confusion if the wallet does not receive coinbase rewards.
if wallet_info.amount_immature > 0 {
table.add_row(row![
@@ -426,7 +426,7 @@ pub fn info(
FY->amount_to_hr_string(wallet_info.amount_awaiting_confirmation, false)
]);
table.add_row(row![
bFB->format!("Awaiting Finalization"),
bFB->"Awaiting Finalization",
FB->amount_to_hr_string(wallet_info.amount_awaiting_finalization, false)
]);
table.add_row(row![
@@ -448,11 +448,11 @@ pub fn info(
]);
if wallet_info.amount_reverted > 0 {
table.add_row(row![
Fr->format!("Reverted"),
Fr->"Reverted",
Fr->amount_to_hr_string(wallet_info.amount_reverted, false)
]);
}
// Only dispay "Immature Coinbase" if we have related outputs in the wallet.
// Only display "Immature Coinbase" if we have related outputs in the wallet.
// This row just introduces confusion if the wallet does not receive coinbase rewards.
if wallet_info.amount_immature > 0 {
table.add_row(row![
@@ -483,7 +483,7 @@ pub fn info(
if !validated {
println!(
"\nWARNING: Wallet failed to verify data against a live chain. \
The above is from local cache and only valid up to the given height! \
The above is from local cache and only valid up to the given height! \
(is your `grin server` offline or broken?)"
);
}
@@ -533,7 +533,7 @@ pub fn estimate(
/// Display list of wallet accounts in a pretty way
pub fn accounts(acct_mappings: Vec<AcctPathMapping>) {
println!("\n____ Wallet Accounts ____\n",);
println!("\n____ Wallet Accounts ____\n");
let mut table = table!();
table.set_titles(row![
@@ -567,28 +567,28 @@ pub fn accounts(acct_mappings: Vec<AcctPathMapping>) {
/// Display individual Payment Proof
pub fn payment_proof(tx: &TxLogEntry) -> Result<(), Error> {
let title = format!("Payment Proof - Transaction '{}'", tx.id,);
let title = format!("Payment Proof - Transaction '{}'", tx.id);
println!();
if term::stdout().is_none() {
println!("Could not open terminal");
return Ok(());
}
let mut t = term::stdout().unwrap();
t.fg(term::color::MAGENTA).unwrap();
writeln!(t, "{}", title).unwrap();
t.reset().unwrap();
let _ = t.fg(term::color::MAGENTA);
writeln!(t, "{}", title)?;
let _ = t.reset();
let pp = match &tx.payment_proof {
None => {
writeln!(t, "None").unwrap();
t.reset().unwrap();
writeln!(t, "None")?;
let _ = t.reset();
return Ok(());
}
Some(p) => p.clone(),
};
t.fg(term::color::WHITE).unwrap();
writeln!(t).unwrap();
let _ = t.fg(term::color::WHITE);
writeln!(t)?;
let receiver_signature = match pp.receiver_signature {
Some(s) => {
let sig_bytes = s.to_bytes();
@@ -602,11 +602,11 @@ pub fn payment_proof(tx: &TxLogEntry) -> Result<(), Error> {
None => 0,
};
let amount = if tx.amount_credited >= tx.amount_debited {
core::amount_to_hr_string(tx.amount_credited - tx.amount_debited, true)
amount_to_hr_string(tx.amount_credited - tx.amount_debited, true)
} else {
format!(
"{}",
core::amount_to_hr_string(tx.amount_debited - tx.amount_credited - fee, true)
amount_to_hr_string(tx.amount_debited - tx.amount_credited - fee, true)
)
};
@@ -630,20 +630,18 @@ pub fn payment_proof(tx: &TxLogEntry) -> Result<(), Error> {
t,
"Receiver Address: {}",
SlatepackAddress::new(&pp.receiver_address)
)
.unwrap();
writeln!(t, "Receiver Signature: {}", receiver_signature).unwrap();
writeln!(t, "Amount: {}", amount).unwrap();
writeln!(t, "Kernel Excess: {}", kernel_excess).unwrap();
)?;
writeln!(t, "Receiver Signature: {}", receiver_signature)?;
writeln!(t, "Amount: {}", amount)?;
writeln!(t, "Kernel Excess: {}", kernel_excess)?;
writeln!(
t,
"Sender Address: {}",
SlatepackAddress::new(&pp.sender_address)
)
.unwrap();
writeln!(t, "Sender Signature: {}", sender_signature).unwrap();
)?;
writeln!(t, "Sender Signature: {}", sender_signature)?;
t.reset().unwrap();
let _ = t.reset();
println!();