From 0bc4834ae318502ac3575c0f7d073bbd3aedfe98 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 11 May 2020 18:52:14 -0400 Subject: add contents_between, for things like clipboard selections --- src/screen.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'src/screen.rs') diff --git a/src/screen.rs b/src/screen.rs index c50d077..a47bfa1 100644 --- a/src/screen.rs +++ b/src/screen.rs @@ -172,6 +172,65 @@ impl Screen { }) } + /// Returns the text contents of the terminal logically between two cells. + /// This will include the remainder of the starting row after `start_col`, + /// followed by the entire contents of the rows between `start_row` and + /// `end_row`, followed by the beginning of the `end_row` up until + /// `end_col`. This is useful for things like determining the contents of + /// a clipboard selection. + #[must_use] + pub fn contents_between( + &self, + start_row: u16, + start_col: u16, + end_row: u16, + end_col: u16, + ) -> String { + match start_row.cmp(&end_row) { + std::cmp::Ordering::Less => { + let (_, cols) = self.size(); + let mut contents = String::new(); + for (i, row) in self + .grid() + .visible_rows() + .enumerate() + .skip(start_row as usize) + .take(end_row as usize - start_row as usize + 1) + { + if i == start_row as usize { + row.write_contents( + &mut contents, + start_col, + cols - start_col, + false, + ); + if !row.wrapped() { + contents.push('\n'); + } + } else if i == end_row as usize { + row.write_contents(&mut contents, 0, end_col, false); + } else { + row.write_contents(&mut contents, 0, cols, false); + if !row.wrapped() { + contents.push('\n'); + } + } + } + contents + } + std::cmp::Ordering::Equal => { + if start_col < end_col { + self.rows(start_col, end_col - start_col) + .nth(start_row as usize) + .unwrap_or_else(String::new) + } else { + String::new() + } + } + std::cmp::Ordering::Greater => String::new(), + } + } + /// Returns the formatted visible contents of the terminal. /// /// Formatting information will be included inline as terminal escape -- cgit v1.2.3-54-g00ecf