Skip to content

Commit

Permalink
println_with_color & print_with_color should expect Result (FuelLabs#…
Browse files Browse the repository at this point in the history
  • Loading branch information
emilyaherbert authored Mar 30, 2022
1 parent 8816dfa commit 8eb59ac
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion forc-pkg/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Manifest {
let toml_de = &mut toml::de::Deserializer::new(&manifest_str);
let manifest: Self = serde_ignored::deserialize(toml_de, |path| {
let warning = format!(" WARNING! unused manifest key: {}", path);
println_yellow_err(&warning).unwrap();
println_yellow_err(&warning);
})
.map_err(|e| anyhow!("failed to parse manifest: {}.", e))?;
manifest.validate(path)?;
Expand Down
3 changes: 1 addition & 2 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,7 @@ impl BuildPlan {
" WARNING! Dependency \"{}\" specifies the unused `version` field: \
consider using `branch` or `tag` instead",
dep_name
))
.unwrap();
));
}
}

Expand Down
67 changes: 34 additions & 33 deletions forc-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use annotate_snippets::{
};
use anyhow::{bail, Result};
use std::ffi::OsStr;
use std::io::{self, Write};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::str;
use sway_core::{error::LineCol, CompileError, CompileWarning, TreeType};
Expand Down Expand Up @@ -197,68 +197,69 @@ pub fn print_on_failure(silent_mode: bool, warnings: &[CompileWarning], errors:
" Aborting due to {} {}.",
e_len,
if e_len > 1 { "errors" } else { "error" }
))
.unwrap();
));
}

pub fn println_red(txt: &str) -> io::Result<()> {
println_std_out(txt, TermColor::Red)
pub fn println_red(txt: &str) {
println_std_out(txt, TermColor::Red);
}

pub fn println_green(txt: &str) -> io::Result<()> {
println_std_out(txt, TermColor::Green)
pub fn println_green(txt: &str) {
println_std_out(txt, TermColor::Green);
}

pub fn print_blue_err(txt: &str) -> io::Result<()> {
print_std_err(txt, TermColor::Blue)
pub fn print_blue_err(txt: &str) {
print_std_err(txt, TermColor::Blue);
}

pub fn println_yellow_err(txt: &str) -> io::Result<()> {
println_std_err(txt, TermColor::Yellow)
pub fn println_yellow_err(txt: &str) {
println_std_err(txt, TermColor::Yellow);
}

pub fn println_red_err(txt: &str) -> io::Result<()> {
println_std_err(txt, TermColor::Red)
pub fn println_red_err(txt: &str) {
println_std_err(txt, TermColor::Red);
}

pub fn println_green_err(txt: &str) -> io::Result<()> {
println_std_err(txt, TermColor::Green)
pub fn println_green_err(txt: &str) {
println_std_err(txt, TermColor::Green);
}

pub fn print_std_out(txt: &str, color: TermColor) -> io::Result<()> {
pub fn print_std_out(txt: &str, color: TermColor) {
let stdout = StandardStream::stdout(ColorChoice::Always);
print_with_color(txt, color, stdout)
print_with_color(txt, color, stdout);
}

fn println_std_out(txt: &str, color: TermColor) -> io::Result<()> {
fn println_std_out(txt: &str, color: TermColor) {
let stdout = StandardStream::stdout(ColorChoice::Always);
println_with_color(txt, color, stdout)
println_with_color(txt, color, stdout);
}

fn print_std_err(txt: &str, color: TermColor) -> io::Result<()> {
fn print_std_err(txt: &str, color: TermColor) {
let stdout = StandardStream::stderr(ColorChoice::Always);
print_with_color(txt, color, stdout)
print_with_color(txt, color, stdout);
}

fn println_std_err(txt: &str, color: TermColor) -> io::Result<()> {
fn println_std_err(txt: &str, color: TermColor) {
let stdout = StandardStream::stderr(ColorChoice::Always);
println_with_color(txt, color, stdout)
println_with_color(txt, color, stdout);
}

fn print_with_color(txt: &str, color: TermColor, stream: StandardStream) -> io::Result<()> {
fn print_with_color(txt: &str, color: TermColor, stream: StandardStream) {
let mut stream = stream;
stream.set_color(ColorSpec::new().set_fg(Some(color)))?;
write!(&mut stream, "{}", txt)?;
stream.reset()?;
Ok(())
stream
.set_color(ColorSpec::new().set_fg(Some(color)))
.expect("internal printing error");
write!(&mut stream, "{}", txt).expect("internal printing error");
stream.reset().expect("internal printing error");
}

fn println_with_color(txt: &str, color: TermColor, stream: StandardStream) -> io::Result<()> {
fn println_with_color(txt: &str, color: TermColor, stream: StandardStream) {
let mut stream = stream;
stream.set_color(ColorSpec::new().set_fg(Some(color)))?;
writeln!(&mut stream, "{}", txt)?;
stream.reset()?;
Ok(())
stream
.set_color(ColorSpec::new().set_fg(Some(color)))
.expect("internal printing error");
writeln!(&mut stream, "{}", txt).expect("internal printing error");
stream.reset().expect("internal printing error");
}

fn format_err(err: &sway_core::CompileError) {
Expand Down
8 changes: 4 additions & 4 deletions forc/src/ops/forc_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,22 @@ fn display_file_diff(file_content: &str, formatted_content: &str) -> Result<(),
DiffOp::Insert(new) => {
count_of_updates += 1;
for n in new {
println_green(&format!("+{}", n))?;
println_green(&format!("+{}", n));
}
}
DiffOp::Remove(old) => {
count_of_updates += 1;
for o in old {
println_red(&format!("-{}", o))?;
println_red(&format!("-{}", o));
}
}
DiffOp::Replace(old, new) => {
count_of_updates += 1;
for o in old {
println_red(&format!("-{}", o))?;
println_red(&format!("-{}", o));
}
for n in new {
println_green(&format!("+{}", n))?;
println_green(&format!("+{}", n));
}
}
}
Expand Down

0 comments on commit 8eb59ac

Please sign in to comment.