diff --git a/src/grid.rs b/src/grid.rs index 37c9dee6..b47be7ca 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -28,235 +28,7 @@ use std::borrow::ToOwned; use util::Rotate; -/// Indexing types and implementations for Grid and Line -pub mod index { - use std::fmt; - use std::iter::Step; - use std::num::{One, Zero}; - use std::ops::{self, Deref, Add}; - - /// Index in the grid using row, column notation - #[derive(Debug, Clone, Default, Eq, PartialEq)] - pub struct Cursor { - pub line: Line, - pub col: Column, - } - - /// A line - /// - /// Newtype to avoid passing values incorrectly - #[derive(Debug, Copy, Clone, Eq, PartialEq, Default, Ord, PartialOrd)] - pub struct Line(pub usize); - - impl fmt::Display for Line { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Line({})", self.0) - } - } - - /// A column - /// - /// Newtype to avoid passing values incorrectly - #[derive(Debug, Copy, Clone, Eq, PartialEq, Default, Ord, PartialOrd)] - pub struct Column(pub usize); - - impl fmt::Display for Column { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Column({})", 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. - /// - /// Licensed under the Apache License, Version 2.0 or the MIT license - /// , at your - /// option. This file may not be copied, modified, or distributed - /// except according to those terms. - /// - /// implements binary operators "&T op U", "T op &U", "&T op &U" - /// based on "T op U" where T and U are expected to be `Copy`able - macro_rules! forward_ref_binop { - (impl $imp:ident, $method:ident for $t:ty, $u:ty) => { - impl<'a> $imp<$u> for &'a $t { - type Output = <$t as $imp<$u>>::Output; - - #[inline] - fn $method(self, other: $u) -> <$t as $imp<$u>>::Output { - $imp::$method(*self, other) - } - } - - impl<'a> $imp<&'a $u> for $t { - type Output = <$t as $imp<$u>>::Output; - - #[inline] - fn $method(self, other: &'a $u) -> <$t as $imp<$u>>::Output { - $imp::$method(self, *other) - } - } - - impl<'a, 'b> $imp<&'a $u> for &'b $t { - type Output = <$t as $imp<$u>>::Output; - - #[inline] - fn $method(self, other: &'a $u) -> <$t as $imp<$u>>::Output { - $imp::$method(*self, *other) - } - } - } - } - - /// Macro for deriving deref - macro_rules! deref { - ($ty:ty, $target:ty) => { - impl Deref for $ty { - type Target = $target; - - #[inline] - fn deref(&self) -> &$target { - &self.0 - } - } - } - } - - macro_rules! add { - ($ty:ty, $construct:expr) => { - impl ops::Add<$ty> for $ty { - type Output = $ty; - - #[inline] - fn add(self, rhs: $ty) -> $ty { - $construct(self.0 + rhs.0) - } - } - } - } - - macro_rules! sub { - ($ty:ty, $construct:expr) => { - impl ops::Sub<$ty> for $ty { - type Output = $ty; - - #[inline] - fn sub(self, rhs: $ty) -> $ty { - $construct(self.0 - rhs.0) - } - } - } - } - - macro_rules! zero_one { - ($ty:ty, $construct:expr) => { - impl One for $ty { - fn one() -> $ty { - $construct(1) - } - } - - impl Zero for $ty { - fn zero() -> $ty { - $construct(0) - } - } - } - } - - macro_rules! ops { - ($ty:ty, $construct:expr) => { - add!($ty, $construct); - sub!($ty, $construct); - zero_one!($ty, $construct); - deref!($ty, usize); - forward_ref_binop!(impl Add, add for $ty, $ty); - - impl Step for $ty { - fn step(&self, by: &$ty) -> Option<$ty> { - Some(*self + *by) - } - - #[inline] - #[allow(trivial_numeric_casts)] - fn steps_between(start: &$ty, end: &$ty, by: &$ty) -> Option { - if *by == $construct(0) { return None; } - if *start < *end { - // Note: We assume $t <= usize here - let diff = (*end - *start).0; - let by = by.0; - if diff % by > 0 { - Some(diff / by + 1) - } else { - Some(diff / by) - } - } else { - Some(0) - } - } - } - - impl ops::AddAssign<$ty> for $ty { - #[inline] - fn add_assign(&mut self, rhs: $ty) { - self.0 += rhs.0 - } - } - - impl ops::SubAssign<$ty> for $ty { - #[inline] - fn sub_assign(&mut self, rhs: $ty) { - self.0 -= rhs.0 - } - } - - impl ops::AddAssign for $ty { - #[inline] - fn add_assign(&mut self, rhs: usize) { - self.0 += rhs - } - } - - impl ops::SubAssign for $ty { - #[inline] - fn sub_assign(&mut self, rhs: usize) { - self.0 -= rhs - } - } - - impl From for $ty { - #[inline] - fn from(val: usize) -> $ty { - $construct(val) - } - } - - impl ops::Add for $ty { - type Output = $ty; - - #[inline] - fn add(self, rhs: usize) -> $ty { - $construct(self.0 + rhs) - } - } - - impl ops::Sub for $ty { - type Output = $ty; - - #[inline] - fn sub(self, rhs: usize) -> $ty { - $construct(self.0 - rhs) - } - } - } - } - - ops!(Line, Line); - ops!(Column, Column); -} - -use self::index::Cursor; +use index::{self, Cursor}; /// Represents the terminal display contents #[derive(Clone)] diff --git a/src/index.rs b/src/index.rs new file mode 100644 index 00000000..a4167e0d --- /dev/null +++ b/src/index.rs @@ -0,0 +1,241 @@ +// Copyright 2016 Joe Wilm, The Alacritty Project Contributors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Line and Column newtypes for strongly typed tty/grid/terminal APIs + +/// Indexing types and implementations for Grid and Line +use std::fmt; +use std::iter::Step; +use std::num::{One, Zero}; +use std::ops::{self, Deref, Add}; + +/// Index in the grid using row, column notation +#[derive(Debug, Clone, Default, Eq, PartialEq)] +pub struct Cursor { + pub line: Line, + pub col: Column, +} + +/// A line +/// +/// Newtype to avoid passing values incorrectly +#[derive(Debug, Copy, Clone, Eq, PartialEq, Default, Ord, PartialOrd)] +pub struct Line(pub usize); + +impl fmt::Display for Line { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Line({})", self.0) + } +} + +/// A column +/// +/// Newtype to avoid passing values incorrectly +#[derive(Debug, Copy, Clone, Eq, PartialEq, Default, Ord, PartialOrd)] +pub struct Column(pub usize); + +impl fmt::Display for Column { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Column({})", 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. +/// +/// Licensed under the Apache License, Version 2.0 or the MIT license +/// , at your +/// option. This file may not be copied, modified, or distributed +/// except according to those terms. +/// +/// implements binary operators "&T op U", "T op &U", "&T op &U" +/// based on "T op U" where T and U are expected to be `Copy`able +macro_rules! forward_ref_binop { + (impl $imp:ident, $method:ident for $t:ty, $u:ty) => { + impl<'a> $imp<$u> for &'a $t { + type Output = <$t as $imp<$u>>::Output; + + #[inline] + fn $method(self, other: $u) -> <$t as $imp<$u>>::Output { + $imp::$method(*self, other) + } + } + + impl<'a> $imp<&'a $u> for $t { + type Output = <$t as $imp<$u>>::Output; + + #[inline] + fn $method(self, other: &'a $u) -> <$t as $imp<$u>>::Output { + $imp::$method(self, *other) + } + } + + impl<'a, 'b> $imp<&'a $u> for &'b $t { + type Output = <$t as $imp<$u>>::Output; + + #[inline] + fn $method(self, other: &'a $u) -> <$t as $imp<$u>>::Output { + $imp::$method(*self, *other) + } + } + } +} + +/// Macro for deriving deref +macro_rules! deref { + ($ty:ty, $target:ty) => { + impl Deref for $ty { + type Target = $target; + + #[inline] + fn deref(&self) -> &$target { + &self.0 + } + } + } +} + +macro_rules! add { + ($ty:ty, $construct:expr) => { + impl ops::Add<$ty> for $ty { + type Output = $ty; + + #[inline] + fn add(self, rhs: $ty) -> $ty { + $construct(self.0 + rhs.0) + } + } + } +} + +macro_rules! sub { + ($ty:ty, $construct:expr) => { + impl ops::Sub<$ty> for $ty { + type Output = $ty; + + #[inline] + fn sub(self, rhs: $ty) -> $ty { + $construct(self.0 - rhs.0) + } + } + } +} + +macro_rules! zero_one { + ($ty:ty, $construct:expr) => { + impl One for $ty { + fn one() -> $ty { + $construct(1) + } + } + + impl Zero for $ty { + fn zero() -> $ty { + $construct(0) + } + } + } +} + +macro_rules! ops { + ($ty:ty, $construct:expr) => { + add!($ty, $construct); + sub!($ty, $construct); + zero_one!($ty, $construct); + deref!($ty, usize); + forward_ref_binop!(impl Add, add for $ty, $ty); + + impl Step for $ty { + fn step(&self, by: &$ty) -> Option<$ty> { + Some(*self + *by) + } + + #[inline] + #[allow(trivial_numeric_casts)] + fn steps_between(start: &$ty, end: &$ty, by: &$ty) -> Option { + if *by == $construct(0) { return None; } + if *start < *end { + // Note: We assume $t <= usize here + let diff = (*end - *start).0; + let by = by.0; + if diff % by > 0 { + Some(diff / by + 1) + } else { + Some(diff / by) + } + } else { + Some(0) + } + } + } + + impl ops::AddAssign<$ty> for $ty { + #[inline] + fn add_assign(&mut self, rhs: $ty) { + self.0 += rhs.0 + } + } + + impl ops::SubAssign<$ty> for $ty { + #[inline] + fn sub_assign(&mut self, rhs: $ty) { + self.0 -= rhs.0 + } + } + + impl ops::AddAssign for $ty { + #[inline] + fn add_assign(&mut self, rhs: usize) { + self.0 += rhs + } + } + + impl ops::SubAssign for $ty { + #[inline] + fn sub_assign(&mut self, rhs: usize) { + self.0 -= rhs + } + } + + impl From for $ty { + #[inline] + fn from(val: usize) -> $ty { + $construct(val) + } + } + + impl ops::Add for $ty { + type Output = $ty; + + #[inline] + fn add(self, rhs: usize) -> $ty { + $construct(self.0 + rhs) + } + } + + impl ops::Sub for $ty { + type Output = $ty; + + #[inline] + fn sub(self, rhs: usize) -> $ty { + $construct(self.0 - rhs) + } + } + } +} + +ops!(Line, Line); +ops!(Column, Column); diff --git a/src/main.rs b/src/main.rs index e73ec15c..8d2a93cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,6 +44,7 @@ pub mod grid; mod meter; pub mod config; mod input; +mod index; mod tty; pub mod ansi; mod term; diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index c967fa05..4cd26771 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -24,11 +24,12 @@ use std::sync::atomic::{Ordering, AtomicBool}; use cgmath; use gl::types::*; use gl; +use grid::Grid; +use index; use notify::{Watcher as WatcherApi, RecommendedWatcher as Watcher, op}; +use term::{self, cell, Cell}; use font::{Rasterizer, RasterizedGlyph, FontDesc}; -use grid::{self, Grid}; -use term::{self, cell, Cell}; use super::Rgb; @@ -576,7 +577,7 @@ impl<'a> RenderApi<'a> { } } - pub fn render_cursor(&mut self, cursor: &grid::index::Cursor, glyph_cache: &mut GlyphCache) { + pub fn render_cursor(&mut self, cursor: &index::Cursor, glyph_cache: &mut GlyphCache) { if let Some(glyph) = glyph_cache.get(term::CURSOR_SHAPE, self) { let cell = Cell { c: term::CURSOR_SHAPE, diff --git a/src/term.rs b/src/term.rs index 2c30812b..86908d33 100644 --- a/src/term.rs +++ b/src/term.rs @@ -18,7 +18,9 @@ use std::fmt; use ansi::{self, Attr}; use grid::{Grid, ClearRegion}; +use index::{Cursor, Column, Line}; use tty; + use ::Rgb; /// coerce val to be between min and max @@ -122,8 +124,6 @@ pub const DEFAULT_FG: Rgb = Rgb { r: 0xea, g: 0xea, b: 0xea}; pub const DEFAULT_BG: Rgb = Rgb { r: 0, g: 0, b: 0}; pub const TAB_SPACES: usize = 8; -use grid::index::{Cursor, Column, Line}; - pub struct Term { /// The grid grid: Grid,