~exitb/retrofit

bb493ee89c8c6d28c69a553449d33f9637835f98 — exitb 1 year, 10 months ago 166432e
Position is its own object now
1 files changed, 45 insertions(+), 42 deletions(-)

M src/adapter.rs
M src/adapter.rs => src/adapter.rs +45 -42
@@ 266,11 266,21 @@ enum AdapterState {
    Finished,
}

pub struct Adapter<I> {
#[derive(Copy, Clone)]
struct Position {
    column: u16,
    line: usize,
    saved_column: u16,
    saved_line: usize,
}

impl Position {
    fn new() -> Position {
        Position { column: 0, line: 0 }
    }
}

pub struct Adapter<I> {
    position: Position,
    saved_position: Position,
    last_line: usize,
    brush: Style,
    output_buffer: VecDeque<Adapted>,


@@ 386,20 396,20 @@ where
        match code {
            printable @ AnsiCode::Printable(_) => {
                self.push_code(printable);
                self.column += 1;
                self.position.column += 1;
            }
            AnsiCode::ControlCharacter(character) => match character {
                '\n' => {
                    self.new_line();
                }
                '\r' => {
                    self.column = 0;
                    self.position.column = 0;
                    self.push_code(AnsiCode::ControlCharacter('\r'))
                }
                '\x16' | '\x1A' | '\x1F' => (),
                '\x00' => {
                    self.push_code(AnsiCode::Printable(' '));
                    self.column += 1;
                    self.position.column += 1;
                }
                character => Err(Error::UnknownControlCharacter(character))?,
            },


@@ 418,7 428,7 @@ where
                            ))?,
                        };

                        self.column += count;
                        self.position.column += count;

                        true
                    }


@@ 432,7 442,7 @@ where
                            ))?,
                        };

                        self.line = self.line.saturating_sub(count as usize);
                        self.position.line = self.position.line.saturating_sub(count as usize);

                        true
                    }


@@ 446,7 456,7 @@ where
                            ))?,
                        };

                        self.column = self.column.saturating_sub(count);
                        self.position.column = self.position.column.saturating_sub(count);

                        true
                    }


@@ 499,13 509,13 @@ where
                        }

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

                        self.push_code(AnsiCode::ControlSequence {


@@ 513,42 523,41 @@ where
                            parameters: vec![difference as u16],
                        });

                        self.line -= difference;
                        self.position.line -= difference;
                        self.push_code(AnsiCode::ControlCharacter('\r'));
                        self.column = 0;
                        self.position.column = 0;

                        true
                    }
                    ControlSequenceIntroducer::CursorPosition => {
                        self.line = match parameters.get(0) {
                        self.position.line = match parameters.get(0) {
                            Some(0) | None => 0,
                            Some(n) => *n as usize - 1,
                        };

                        self.column = match parameters.get(1) {
                        self.position.column = match parameters.get(1) {
                            Some(0) | None => 0,
                            Some(n) => n - 1,
                        };

                        self.push_code(AnsiCode::ControlSequence {
                            introducer: ControlSequenceIntroducer::CursorPosition,
                            parameters: vec![self.line as u16 + 1, self.column + 1],
                            parameters: vec![
                                self.position.line as u16 + 1,
                                self.position.column + 1,
                            ],
                        });

                        false
                    }
                    ControlSequenceIntroducer::SaveCursorPosition => {
                        self.saved_line = self.line;
                        self.saved_column = self.column;

                        self.saved_position = self.position;
                        self.push_code(AnsiCode::DecSaveCursorPosition);

                        false
                    }
                    ControlSequenceIntroducer::RestoreCursorPosition => {
                        self.line = self.saved_line;
                        self.column = self.saved_column;

                        self.position = self.saved_position;
                        self.push_code(AnsiCode::DecRestoreCursorPosition);

                        false


@@ 566,15 575,11 @@ where
                }
            }
            AnsiCode::DecSaveCursorPosition => {
                self.saved_line = self.line;
                self.saved_column = self.column;

                self.saved_position = self.position;
                self.push_code(code);
            }
            AnsiCode::DecRestoreCursorPosition => {
                self.line = self.saved_line;
                self.column = self.saved_column;

                self.position = self.saved_position;
                self.push_code(code);
            }
        }


@@ 601,17 606,17 @@ where

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

        self.line += 1;
        self.position.line += 1;

        if self.column > 0 {
        if self.position.column > 0 {
            self.push_code(AnsiCode::ControlSequence {
                introducer: ControlSequenceIntroducer::CursorForward,
                parameters: vec![self.column],
                parameters: vec![self.position.column],
            });
        }

        if self.line > self.last_line {
            self.last_line = self.line;
        if self.position.line > self.last_line {
            self.last_line = self.position.line;
            self.fill_line()
        }



@@ 624,11 629,11 @@ where
    }

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

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

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


@@ 677,10 682,8 @@ where
{
    fn adapt(self, running_flag: Arc<AtomicBool>, parameters: AdaptationParameters) -> Adapter<I> {
        Adapter {
            column: 0,
            line: 0,
            saved_column: 0,
            saved_line: 0,
            position: Position::new(),
            saved_position: Position::new(),
            last_line: 0,
            brush: Style::new(),
            output_buffer: VecDeque::new(),