Skip to content

Add Screen::all_contents_formatted #3

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

Open
wants to merge 1 commit into
base: main
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
28 changes: 26 additions & 2 deletions src/grid.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::term::BufWrite as _;
use crate::{row::Row, term::BufWrite as _};
use std::convert::TryInto as _;

#[derive(Clone, Debug)]
Expand All @@ -16,6 +16,11 @@ pub struct Grid {
scrollback_offset: usize,
}

pub enum RowFilter {
All,
Visible,
}

impl Grid {
pub fn new(size: Size, scrollback_len: usize) -> Self {
Self {
Expand Down Expand Up @@ -106,6 +111,10 @@ impl Grid {
self.origin_mode = self.saved_origin_mode;
}

pub fn all_rows(&self) -> impl Iterator<Item = &crate::row::Row> {
self.scrollback.iter().chain(self.rows.iter())
}

pub fn visible_rows(&self) -> impl Iterator<Item = &crate::row::Row> {
let scrollback_len = self.scrollback.len();
let rows_len = self.rows.len();
Expand Down Expand Up @@ -199,6 +208,7 @@ impl Grid {

pub fn write_contents_formatted(
&self,
rows: RowFilter,
contents: &mut Vec<u8>,
) -> crate::attrs::Attrs {
crate::term::ClearAttrs::default().write_buf(contents);
Expand All @@ -207,7 +217,8 @@ impl Grid {
let mut prev_attrs = crate::attrs::Attrs::default();
let mut prev_pos = Pos::default();
let mut wrapping = false;
for (i, row) in self.visible_rows().enumerate() {

let mut process_row = |i: usize, row: &Row| {
let i = i.try_into().unwrap();
let (new_pos, new_attrs) = row.write_contents_formatted(
contents,
Expand All @@ -221,6 +232,19 @@ impl Grid {
prev_pos = new_pos;
prev_attrs = new_attrs;
wrapping = row.wrapped();
};

match rows {
RowFilter::All => {
for (i, row) in self.all_rows().enumerate() {
process_row(i, row);
}
}
RowFilter::Visible => {
for (i, row) in self.visible_rows().enumerate() {
process_row(i, row);
}
}
}

// writing a character to the last column of a row doesn't wrap the
Expand Down
63 changes: 59 additions & 4 deletions src/screen.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use crate::term::BufWrite as _;
use crate::{
grid::RowFilter,
term::{
BufWrite as _, DisableAlternateScreen, EnableAlternateScreen, MoveTo,
},
};
use std::convert::TryInto as _;
use unicode_width::UnicodeWidthChar as _;

Expand Down Expand Up @@ -239,13 +244,63 @@ impl Screen {
#[must_use]
pub fn contents_formatted(&self) -> Vec<u8> {
let mut contents = vec![];
self.write_contents_formatted(&mut contents);
self.write_contents_formatted(
self.grid(),
RowFilter::Visible,
&mut contents,
);
contents
}

/// Returns all output stored in the terminal, including both buffers (alternate and primary), and all scrollback
///
/// Formatting information will be included inline as terminal escape
/// codes. The result will be suitable for feeding directly to a raw
/// terminal parser, and will result in the same visual output.
#[must_use]
pub fn all_contents_formatted(&self) -> Vec<u8> {
let mut contents = vec![];
if self.modes.contains(Mode::AlternateScreen) {
self.write_contents_formatted(
&self.grid,
RowFilter::All,
&mut contents,
);

EnableAlternateScreen::default().write_buf(&mut contents);
MoveTo::default().write_buf(&mut contents);
self.write_contents_formatted(
&self.alternate_grid,
RowFilter::All,
&mut contents,
);
} else {
EnableAlternateScreen::default().write_buf(&mut contents);
self.write_contents_formatted(
&self.alternate_grid,
RowFilter::All,
&mut contents,
);

DisableAlternateScreen::default().write_buf(&mut contents);
MoveTo::default().write_buf(&mut contents);
self.write_contents_formatted(
&self.grid,
RowFilter::All,
&mut contents,
);
}
contents
}

fn write_contents_formatted(&self, contents: &mut Vec<u8>) {
fn write_contents_formatted(
&self,
grid: &crate::grid::Grid,
rows: RowFilter,
contents: &mut Vec<u8>,
) {
crate::term::HideCursor::new(self.hide_cursor()).write_buf(contents);
let prev_attrs = self.grid().write_contents_formatted(contents);
let prev_attrs = grid.write_contents_formatted(rows, contents);
self.attrs.write_escape_code_diff(contents, &prev_attrs);
}

Expand Down
20 changes: 20 additions & 0 deletions src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,23 @@ impl BufWrite for MouseProtocolEncoding {
}
}
}

#[derive(Default, Debug)]
#[must_use = "this struct does nothing unless you call write_buf"]
pub struct DisableAlternateScreen;

impl BufWrite for DisableAlternateScreen {
fn write_buf(&self, buf: &mut Vec<u8>) {
buf.extend_from_slice(b"\x1b[?1049l");
}
}

#[derive(Default, Debug)]
#[must_use = "this struct does nothing unless you call write_buf"]
pub struct EnableAlternateScreen;

impl BufWrite for EnableAlternateScreen {
fn write_buf(&self, buf: &mut Vec<u8>) {
buf.extend_from_slice(b"\x1b[?1049h");
}
}