aboutsummaryrefslogtreecommitdiff
path: root/src/index.rs
diff options
context:
space:
mode:
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)));
+ }
+}