summaryrefslogtreecommitdiff
path: root/src/index.rs
diff options
context:
space:
mode:
authorJoe Wilm <joe@jwilm.com>2016-12-22 13:43:06 -0500
committerJoe Wilm <joe@jwilm.com>2016-12-22 13:44:13 -0500
commit6e708d2119ce0c839a89858a42a6b124a5cf48f4 (patch)
treea4ea2078153d136536587e04922f4ec841860298 /src/index.rs
parentfd11660c0a714852a3f477a6730d49b9694e1345 (diff)
downloadalacritty-6e708d2119ce0c839a89858a42a6b124a5cf48f4.tar.gz
alacritty-6e708d2119ce0c839a89858a42a6b124a5cf48f4.zip
Implement visual component of mouse selections
This adds the ability to click and drag with the mouse and have the effect of visually selecting text. The ability to copy the selection into a clipboard buffer is not yet implemented.
Diffstat (limited to 'src/index.rs')
-rw-r--r--src/index.rs62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/index.rs b/src/index.rs
index dc98be1e..61b59405 100644
--- a/src/index.rs
+++ b/src/index.rs
@@ -15,11 +15,19 @@
//! Line and Column newtypes for strongly typed tty/grid/terminal APIs
/// Indexing types and implementations for Grid and Line
+use std::cmp::{Ord, Ordering};
use std::fmt;
use std::iter::Step;
use std::mem;
use std::ops::{self, Deref, Add};
+/// The side of a cell
+#[derive(Debug, Copy, Clone, Eq, PartialEq)]
+pub enum Side {
+ Left,
+ Right
+}
+
/// Index in the grid using row, column notation
#[derive(Debug, Clone, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct Cursor {
@@ -27,6 +35,32 @@ pub struct Cursor {
pub col: Column,
}
+/// Location
+#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Serialize, Deserialize, PartialOrd)]
+pub struct Location {
+ pub line: Line,
+ pub col: Column,
+}
+
+impl Location {
+ pub fn new(line: Line, col: Column) -> Location {
+ Location { line: line, col: col }
+ }
+}
+
+impl Ord for Location {
+ fn cmp(&self, other: &Location) -> Ordering {
+ use std::cmp::Ordering::*;
+ match (self.line.cmp(&other.line), self.col.cmp(&other.col)) {
+ (Equal, Equal) => Equal,
+ (Equal, ord) => ord,
+ (ord, Equal) => ord,
+ (Less, _) => Less,
+ (Greater, _) => Greater,
+ }
+ }
+}
+
/// A line
///
/// Newtype to avoid passing values incorrectly
@@ -51,6 +85,18 @@ impl fmt::Display for Column {
}
}
+/// A linear index
+///
+/// Newtype to avoid passing values incorrectly
+#[derive(Debug, Copy, Clone, Eq, PartialEq, Default, Ord, PartialOrd, Serialize, Deserialize)]
+pub struct Linear(pub usize);
+
+impl fmt::Display for Linear {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "Linear({})", self.0)
+ }
+}
+
/// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
/// file at the top-level directory of this distribution and at
/// http://rust-lang.org/COPYRIGHT.
@@ -281,3 +327,19 @@ macro_rules! ops {
ops!(Line, Line);
ops!(Column, Column);
+ops!(Linear, Linear);
+
+#[cfg(test)]
+mod tests {
+ use super::{Line, Column, Location};
+
+ #[test]
+ fn location_ordering() {
+ assert!(Location::new(Line(0), Column(0)) == Location::new(Line(0), Column(0)));
+ assert!(Location::new(Line(1), Column(0)) > Location::new(Line(0), Column(0)));
+ assert!(Location::new(Line(0), Column(1)) > Location::new(Line(0), Column(0)));
+ assert!(Location::new(Line(1), Column(1)) > Location::new(Line(0), Column(0)));
+ assert!(Location::new(Line(1), Column(1)) > Location::new(Line(0), Column(1)));
+ assert!(Location::new(Line(1), Column(1)) > Location::new(Line(1), Column(0)));
+ }
+}