~exitb/retrofit

166432e918e658f4a1276669e81e4eb2a71edff9 — exitb 1 year, 10 months ago 5b98e45
Group Adapter running parameters into a single object
2 files changed, 49 insertions(+), 66 deletions(-)

M src/adapter.rs
M src/main.rs
M src/adapter.rs => src/adapter.rs +36 -48
@@ 246,6 246,14 @@ impl Style {
    }
}

pub struct AdaptationParameters {
    pub truecolor: bool,
    pub hide_cursor: bool,
    pub width: u16,
    pub height: Option<u16>,
    pub ice_colors: bool,
}

#[derive(Debug)]
pub enum Adapted {
    Code(AnsiCode),


@@ 265,15 273,11 @@ pub struct Adapter<I> {
    saved_line: usize,
    last_line: usize,
    brush: Style,
    width: u16,
    height: Option<u16>,
    truecolor: bool,
    hide_cursor: bool,
    ice_colors: bool,
    output_buffer: VecDeque<Adapted>,
    codes: I,
    state: AdapterState,
    running_flag: Arc<AtomicBool>,
    parameters: AdaptationParameters,
}

impl<I> Iterator for Adapter<I>


@@ 330,14 334,14 @@ where
    fn prelude(&mut self) {
        self.reset_terminal();

        if self.hide_cursor {
        if self.parameters.hide_cursor {
            self.push_code(AnsiCode::ControlSequence {
                introducer: ControlSequenceIntroducer::ResetScreenMode,
                parameters: vec![25],
            });
        }

        if let Some(height) = self.height {
        if let Some(height) = self.parameters.height {
            self.push_code(AnsiCode::ControlSequence {
                introducer: ControlSequenceIntroducer::EraseInDisplay,
                parameters: vec![2],


@@ 354,7 358,7 @@ where
    fn finish(&mut self) {
        self.reset_terminal();

        if let Some(height) = self.height {
        if let Some(height) = self.parameters.height {
            self.push_code(AnsiCode::ControlSequence {
                introducer: ControlSequenceIntroducer::SetMargins,
                parameters: vec![],


@@ 368,7 372,7 @@ where

        self.push_code(AnsiCode::ControlCharacter('\n'));

        if self.hide_cursor {
        if self.parameters.hide_cursor {
            self.push_code(AnsiCode::ControlSequence {
                introducer: ControlSequenceIntroducer::SetScreenMode,
                parameters: vec![25],


@@ 464,16 468,17 @@ where
                    }
                    ControlSequenceIntroducer::SelectGraphicRendition
                    | ControlSequenceIntroducer::PabloDraw24BitColor => {
                        let new_brush =
                            self.brush
                                .clone()
                                .parse(&parameters, &introducer, self.truecolor)?;
                        let new_brush = self.brush.clone().parse(
                            &parameters,
                            &introducer,
                            self.parameters.truecolor,
                        )?;

                        if self.brush != new_brush {
                            let difference_params = new_brush.render_difference(
                                &self.brush,
                                self.truecolor,
                                self.ice_colors,
                                self.parameters.truecolor,
                                self.parameters.ice_colors,
                            );

                            self.brush = new_brush;


@@ 493,7 498,7 @@ where
                            return Err(Error::EraseInDisplayUnsupportedParameter(parameters));
                        }

                        let difference = if let Some(height) = self.height {
                        let difference = if let Some(height) = self.parameters.height {
                            if self.line > height as usize {
                                height as usize
                            } else {


@@ 612,29 617,32 @@ where

        self.push_code(AnsiCode::ControlSequence {
            introducer: ControlSequenceIntroducer::SelectGraphicRendition,
            parameters: self.brush.render(self.truecolor, self.ice_colors),
            parameters: self
                .brush
                .render(self.parameters.truecolor, self.parameters.ice_colors),
        });
    }

    fn handle_line_end(&mut self) {
        if self.column >= self.width {
            let lines = self.column / self.width;
            let characters = self.column % self.width;
        if self.column >= self.parameters.width {
            let lines = self.column / self.parameters.width;
            let characters = self.column % self.parameters.width;

            self.column %= self.width;
            self.column %= self.parameters.width;

            for _ in 0..lines {
                self.new_line();
            }

            for _ in 0..characters {
                // is this ever used?
                self.push_code(AnsiCode::Printable(' '));
            }

            self.push_code(AnsiCode::ControlSequence {
                introducer: ControlSequenceIntroducer::SelectGraphicRendition,
                parameters: self.brush.render(self.truecolor, self.ice_colors),
                parameters: self
                    .brush
                    .render(self.parameters.truecolor, self.parameters.ice_colors),
            });
        }
    }


@@ 644,14 652,14 @@ where

        self.push_code(AnsiCode::ControlSequence {
            introducer: ControlSequenceIntroducer::SelectGraphicRendition,
            parameters: if self.truecolor {
            parameters: if self.parameters.truecolor {
                vec![0, 48, 2, 0, 0, 0]
            } else {
                vec![0, 48, 5, 16]
            },
        });

        for _ in 0..self.width {
        for _ in 0..self.parameters.width {
            self.push_code(AnsiCode::Printable(' '));
        }



@@ 660,30 668,14 @@ where
}

pub trait Adapt<I> {
    fn adapt(
        self,
        width: u16,
        height: Option<u16>,
        truecolor: bool,
        hide_cursor: bool,
        ice_colors: bool,
        running_flag: Arc<AtomicBool>,
    ) -> Adapter<I>;
    fn adapt(self, running_flag: Arc<AtomicBool>, parameters: AdaptationParameters) -> Adapter<I>;
}

impl<I> Adapt<I> for I
where
    I: Iterator<Item = AnsiCode>,
{
    fn adapt(
        self,
        width: u16,
        height: Option<u16>,
        truecolor: bool,
        hide_cursor: bool,
        ice_colors: bool,
        running_flag: Arc<AtomicBool>,
    ) -> Adapter<I> {
    fn adapt(self, running_flag: Arc<AtomicBool>, parameters: AdaptationParameters) -> Adapter<I> {
        Adapter {
            column: 0,
            line: 0,


@@ 691,15 683,11 @@ where
            saved_line: 0,
            last_line: 0,
            brush: Style::new(),
            width,
            height,
            truecolor,
            hide_cursor,
            ice_colors,
            output_buffer: VecDeque::new(),
            codes: self,
            state: AdapterState::New,
            running_flag,
            parameters,
        }
    }
}

M src/main.rs => src/main.rs +13 -18
@@ 3,7 3,7 @@ extern crate matches;
#[macro_use]
extern crate clap;

use adapter::{Adapt, Adapted};
use adapter::{Adapt, AdaptationParameters, Adapted};
use ansi::{AnsiCode, Escape};
use clap::{App, Arg};
use cp437::Cp437;


@@ 113,15 113,8 @@ fn retrofit() -> Result<()> {
        )
        .get_matches();

    let width = value_t_or_exit!(matches, "width", u16);
    let height = value_t!(matches, "height", u16).ok();
    let baud = value_t!(matches, "baud", u64).ok();

    let truecolor = match matches.value_of("color-mode") {
        Some("24bit") => true,
        Some("8bit") | _ => false,
    };

    let source = match matches.value_of("FILE") {
        Some(filename) => {
            let file = File::open(filename).map_err(Error::InputFileOpening)?;


@@ 132,8 125,6 @@ fn retrofit() -> Result<()> {

    let flush_needed = baud.is_some();

    let hide_cursor = baud.is_some() || matches.occurrences_of("hide-cursor") > 0;
    let ice_colors = matches.occurrences_of("ice-colors") > 0;
    let verbose = matches.occurrences_of("verbose") > 0;

    let target = match matches.value_of("output") {


@@ 144,6 135,17 @@ fn retrofit() -> Result<()> {
        None => Box::new(stdout()) as Box<dyn Write>,
    };

    let parameters = AdaptationParameters {
        truecolor: match matches.value_of("color-mode") {
            Some("24bit") => true,
            Some("8bit") | _ => false,
        },
        hide_cursor: baud.is_some() || matches.occurrences_of("hide-cursor") > 0,
        width: value_t_or_exit!(matches, "width", u16),
        height: value_t!(matches, "height", u16).ok(),
        ice_colors: matches.occurrences_of("ice-colors") > 0,
    };

    let mut output = BufWriter::new(target);

    BufReader::new(source)


@@ 163,14 165,7 @@ fn retrofit() -> Result<()> {
                                eprintln!("{:?}", code);
                            }
                        })
                        .adapt(
                            width,
                            height,
                            truecolor,
                            hide_cursor,
                            ice_colors,
                            running_flag,
                        )
                        .adapt(running_flag, parameters)
                        .map_err(Error::Adapter)
                        .and_then_result(|mut adapted| {
                            adapted