summaryrefslogtreecommitdiff
path: root/src/util.rs
diff options
context:
space:
mode:
authorJoe Wilm <joe@jwilm.com>2016-06-08 10:39:49 -0700
committerJoe Wilm <joe@jwilm.com>2016-06-08 10:39:49 -0700
commit8126841ed37a9cc249f646b830b3d3d48aaf4ed7 (patch)
tree781dcedf8bd704071447119d9cecb216203b2c3e /src/util.rs
parent0e7bb8d76e45af6154b0fb76184ae55df7cf80e1 (diff)
downloadalacritty-8126841ed37a9cc249f646b830b3d3d48aaf4ed7.tar.gz
alacritty-8126841ed37a9cc249f646b830b3d3d48aaf4ed7.zip
Add support for scrolling regions
It's now possible to move around within Vim without the screen becoming corrupt! The ANSI parser now calls a (new) `set_scrolling_region` on the handler when the DECSTBM CSI is received. In order to provide a sensible default in case that the sequence doesn't include arguments, a TermInfo trait was added which currently has methods for inspecting number of rows and columns. This was added as an additional trait instead of being included on Handler since they have semantically different purposes. The tests had to be updated to account for the additional trait bounds. The utilities module now has a `Rotate` trait which is implemented for the built-in slice type. This means that slices and anything derefing to a slice can be rotated. Since VecDeque doesn't support slicing (it's a circular buffer), the grid rows are now held in a Vec to support rotation. For ergomomic access to the grid for scrolling and clearing regions, additional Index/IndexMut implementations were added to the grid::Row type. Finally, a `reset` method was added to `Cell` which properly resets the state to default (instead of just clearing the char). This supports region clearing and also fixed a bug where cell backgrounds would remain after being cleared.
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs
index 0a3de227..aa382560 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -1,3 +1,5 @@
+use std::iter::Iterator;
+
/// Threading utilities
pub mod thread {
/// Like `thread::spawn`, but with a `name` argument
@@ -10,3 +12,80 @@ pub mod thread {
::std::thread::Builder::new().name(name.into()).spawn(f).expect("thread spawn works")
}
}
+
+/// Types that can have their elements rotated
+pub trait Rotate {
+ fn rotate(&mut self, positions: isize);
+}
+
+impl<T> Rotate for [T] {
+ fn rotate(&mut self, positions: isize) {
+ // length is needed over and over
+ let len = self.len();
+
+ // Enforce positions in [0, len) and treat negative rotations as a
+ // posititive rotation of len - positions.
+ let positions = if positions > 0 {
+ positions as usize % len
+ } else {
+ len - (-positions as usize) % len
+ };
+
+ // If positions is 0 or the entire slice, it's a noop.
+ if positions == 0 || positions == len {
+ return;
+ }
+
+ self[..positions].reverse();
+ self[positions..].reverse();
+ self.reverse();
+ }
+}
+
+
+#[cfg(test)]
+mod tests {
+ use super::Rotate;
+
+ #[test]
+ fn rotate_forwards_works() {
+ let s = &mut [1, 2, 3, 4, 5];
+ s.rotate(1);
+ assert_eq!(&[2, 3, 4, 5, 1], s);
+ }
+
+ #[test]
+ fn rotate_backwards_works() {
+ let s = &mut [1, 2, 3, 4, 5];
+ s.rotate(-1);
+ assert_eq!(&[5, 1, 2, 3, 4], s);
+ }
+
+ #[test]
+ fn rotate_multiple_forwards() {
+ let s = &mut [1, 2, 3, 4, 5, 6, 7];
+ s.rotate(2);
+ assert_eq!(&[3, 4, 5, 6, 7, 1, 2], s);
+ }
+
+ #[test]
+ fn rotate_multiple_backwards() {
+ let s = &mut [1, 2, 3, 4, 5];
+ s.rotate(-3);
+ assert_eq!(&[3, 4, 5, 1, 2], s);
+ }
+
+ #[test]
+ fn rotate_forwards_overflow() {
+ let s = &mut [1, 2, 3, 4, 5];
+ s.rotate(6);
+ assert_eq!(&[2, 3, 4, 5, 1], s);
+ }
+
+ #[test]
+ fn rotate_backwards_overflow() {
+ let s = &mut [1, 2, 3, 4, 5];
+ s.rotate(-6);
+ assert_eq!(&[5, 1, 2, 3, 4], s);
+ }
+}