Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write to any writer #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
extern crate ansi_term;
extern crate image;

use std::io::{Write, self};
use std::io::Write;

use ansi_term::Colour::Fixed;
use ansi_term::ANSIStrings;
use image::{imageops, FilterType, Pixel};

pub fn print_image(img: image::DynamicImage, true_colour: bool, width: u32, height: u32) {
pub fn print_image<W: Write>(img: image::DynamicImage, true_colour: bool, width: u32, height: u32, w: &mut W) {
let img = imageops::resize(&img, width, height, FilterType::Nearest);

if !true_colour {
Expand All @@ -27,7 +27,7 @@ pub fn print_image(img: image::DynamicImage, true_colour: bool, width: u32, heig
Fixed(bottom_colour).on(Fixed(top_colour)).paint("▄")
}).collect();

print!("{}\n", ANSIStrings(&row));
write!(w, "{}\n", ANSIStrings(&row)).ok();
}
} else {
let mut row = Vec::new();
Expand All @@ -48,7 +48,7 @@ pub fn print_image(img: image::DynamicImage, true_colour: bool, width: u32, heig
}

write!(row, "\x1b[m\n").unwrap();
io::stdout().write(&row).unwrap();
w.write(&row).unwrap();
row.clear();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern crate serde_derive;
extern crate terminal_size;
extern crate termpix;

use std::io::Write;
use std::io::{Write, stdout};

use docopt::Docopt;
use image::GenericImage;
Expand Down Expand Up @@ -54,7 +54,7 @@ fn main() {
let true_colour = args.flag_true_colour || args.flag_true_color;
let (width, height) = determine_size(args, orig_width, orig_height);

termpix::print_image(img, true_colour, width, height);
termpix::print_image(img, true_colour, width, height, &mut stdout());

}

Expand Down