Skip to content
Snippets Groups Projects
Commit 95473f80 authored by Stephen D's avatar Stephen D
Browse files

Gracefully handle broken stdout

parent 524cd928
No related branches found
No related tags found
No related merge requests found
use colored::Color;
use regex::{Regex, RegexBuilder};
use std::f64::consts;
use std::io::{stdout, Write};
use std::process::exit;
fn rainbow(freq: f64, i: f64) -> (u8, u8, u8) {
let red = ((freq * i).sin() * 127.0) as i16 + 128;
......@@ -23,10 +25,26 @@ pub fn print_rainbow(line: &str, freq: f64, seed: f64, spread: f64, invert: bool
let color = Color::TrueColor { r, g, b };
if invert {
print!("{}\x1B[{}m{}\x1B[49m", &c[1], color.to_bg_str(), &c[2]);
if stdout()
.write_all(
format!("{}\x1B[{}m{}\x1B[49m", &c[1], color.to_bg_str(), &c[2]).as_bytes(),
)
.is_err()
{
exit(0);
}
} else {
print!("{}\x1B[{}m{}\x1B[39m", &c[1], color.to_fg_str(), &c[2]);
if stdout()
.write_all(
format!("{}\x1B[{}m{}\x1B[39m", &c[1], color.to_fg_str(), &c[2]).as_bytes(),
)
.is_err()
{
exit(0);
}
}
}
println!();
if stdout().write_all(&[b'\n']).is_err() {
exit(0);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment