Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-view/src/graphics.rs')
-rw-r--r--helix-view/src/graphics.rs150
1 files changed, 46 insertions, 104 deletions
diff --git a/helix-view/src/graphics.rs b/helix-view/src/graphics.rs
index 3a4eee3d..9264c50f 100644
--- a/helix-view/src/graphics.rs
+++ b/helix-view/src/graphics.rs
@@ -25,52 +25,62 @@ impl Default for CursorKind {
}
}
-#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Margin {
- pub horizontal: u16,
- pub vertical: u16,
+ pub left: u16,
+ pub right: u16,
+ pub top: u16,
+ pub bottom: u16,
}
impl Margin {
pub fn none() -> Self {
Self {
- horizontal: 0,
- vertical: 0,
+ left: 0,
+ right: 0,
+ top: 0,
+ bottom: 0,
}
}
/// Set uniform margin for all sides.
- pub const fn all(value: u16) -> Self {
+ pub fn all(value: u16) -> Self {
Self {
- horizontal: value,
- vertical: value,
+ left: value,
+ right: value,
+ top: value,
+ bottom: value,
}
}
/// Set the margin of left and right sides to specified value.
- pub const fn horizontal(value: u16) -> Self {
+ pub fn horizontal(value: u16) -> Self {
Self {
- horizontal: value,
- vertical: 0,
+ left: value,
+ right: value,
+ top: 0,
+ bottom: 0,
}
}
/// Set the margin of top and bottom sides to specified value.
- pub const fn vertical(value: u16) -> Self {
+ pub fn vertical(value: u16) -> Self {
Self {
- horizontal: 0,
- vertical: value,
+ left: 0,
+ right: 0,
+ top: value,
+ bottom: value,
}
}
/// Get the total width of the margin (left + right)
- pub const fn width(&self) -> u16 {
- self.horizontal * 2
+ pub fn width(&self) -> u16 {
+ self.left + self.right
}
/// Get the total height of the margin (top + bottom)
- pub const fn height(&self) -> u16 {
- self.vertical * 2
+ pub fn height(&self) -> u16 {
+ self.top + self.bottom
}
}
@@ -171,13 +181,13 @@ impl Rect {
Self::new(self.x, self.y, width, self.height)
}
- pub fn inner(self, margin: Margin) -> Rect {
+ pub fn inner(self, margin: &Margin) -> Rect {
if self.width < margin.width() || self.height < margin.height() {
Rect::default()
} else {
Rect {
- x: self.x + margin.horizontal,
- y: self.y + margin.vertical,
+ x: self.x + margin.left,
+ y: self.y + margin.top,
width: self.width - margin.width(),
height: self.height - margin.height(),
}
@@ -227,8 +237,8 @@ impl Rect {
Rect {
x: x1,
y: y1,
- width: x2.saturating_sub(x1),
- height: y2.saturating_sub(y1),
+ width: x2 - x1,
+ height: y2 - y1,
}
}
@@ -241,6 +251,7 @@ impl Rect {
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Color {
Reset,
Black,
@@ -263,59 +274,7 @@ pub enum Color {
Indexed(u8),
}
-impl Color {
- /// Creates a `Color` from a hex string
- ///
- /// # Examples
- ///
- /// ```rust
- /// use helix_view::theme::Color;
- ///
- /// let color1 = Color::from_hex("#c0ffee").unwrap();
- /// let color2 = Color::Rgb(192, 255, 238);
- ///
- /// assert_eq!(color1, color2);
- /// ```
- pub fn from_hex(hex: &str) -> Option<Self> {
- if !(hex.starts_with('#') && hex.len() == 7) {
- return None;
- }
- match [1..=2, 3..=4, 5..=6].map(|i| hex.get(i).and_then(|c| u8::from_str_radix(c, 16).ok()))
- {
- [Some(r), Some(g), Some(b)] => Some(Self::Rgb(r, g, b)),
- _ => None,
- }
- }
-}
-
#[cfg(feature = "term")]
-impl From<Color> for termina::style::ColorSpec {
- fn from(color: Color) -> Self {
- match color {
- Color::Reset => Self::Reset,
- Color::Black => Self::BLACK,
- Color::Red => Self::RED,
- Color::Green => Self::GREEN,
- Color::Yellow => Self::YELLOW,
- Color::Blue => Self::BLUE,
- Color::Magenta => Self::MAGENTA,
- Color::Cyan => Self::CYAN,
- Color::Gray => Self::BRIGHT_BLACK,
- Color::White => Self::BRIGHT_WHITE,
- Color::LightRed => Self::BRIGHT_RED,
- Color::LightGreen => Self::BRIGHT_GREEN,
- Color::LightBlue => Self::BRIGHT_BLUE,
- Color::LightYellow => Self::BRIGHT_YELLOW,
- Color::LightMagenta => Self::BRIGHT_MAGENTA,
- Color::LightCyan => Self::BRIGHT_CYAN,
- Color::LightGray => Self::WHITE,
- Color::Indexed(i) => Self::PaletteIndex(i),
- Color::Rgb(r, g, b) => termina::style::RgbColor::new(r, g, b).into(),
- }
- }
-}
-
-#[cfg(all(feature = "term", windows))]
impl From<Color> for crossterm::style::Color {
fn from(color: Color) -> Self {
use crossterm::style::Color as CColor;
@@ -343,6 +302,7 @@ impl From<Color> for crossterm::style::Color {
}
}
}
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnderlineStyle {
Reset,
@@ -368,21 +328,6 @@ impl FromStr for UnderlineStyle {
}
}
-#[cfg(feature = "term")]
-impl From<UnderlineStyle> for termina::style::Underline {
- fn from(style: UnderlineStyle) -> Self {
- match style {
- UnderlineStyle::Reset => Self::None,
- UnderlineStyle::Line => Self::Single,
- UnderlineStyle::Curl => Self::Curly,
- UnderlineStyle::Dotted => Self::Dotted,
- UnderlineStyle::Dashed => Self::Dashed,
- UnderlineStyle::DoubleLine => Self::Double,
- }
- }
-}
-
-#[cfg(all(feature = "term", windows))]
impl From<UnderlineStyle> for crossterm::style::Attribute {
fn from(style: UnderlineStyle) -> Self {
match style {
@@ -408,7 +353,7 @@ bitflags! {
///
/// let m = Modifier::BOLD | Modifier::ITALIC;
/// ```
- #[derive(PartialEq, Eq, Debug, Clone, Copy)]
+ #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Modifier: u16 {
const BOLD = 0b0000_0000_0001;
const DIM = 0b0000_0000_0010;
@@ -505,6 +450,7 @@ impl FromStr for Modifier {
/// );
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Style {
pub fg: Option<Color>,
pub bg: Option<Color>,
@@ -515,13 +461,7 @@ pub struct Style {
}
impl Default for Style {
- fn default() -> Self {
- Self::new()
- }
-}
-
-impl Style {
- pub const fn new() -> Self {
+ fn default() -> Style {
Style {
fg: None,
bg: None,
@@ -531,10 +471,12 @@ impl Style {
sub_modifier: Modifier::empty(),
}
}
+}
+impl Style {
/// Returns a `Style` resetting all properties.
- pub const fn reset() -> Self {
- Self {
+ pub fn reset() -> Style {
+ Style {
fg: Some(Color::Reset),
bg: Some(Color::Reset),
underline_color: None,
@@ -554,7 +496,7 @@ impl Style {
/// let diff = Style::default().fg(Color::Red);
/// assert_eq!(style.patch(diff), Style::default().fg(Color::Red));
/// ```
- pub const fn fg(mut self, color: Color) -> Style {
+ pub fn fg(mut self, color: Color) -> Style {
self.fg = Some(color);
self
}
@@ -569,7 +511,7 @@ impl Style {
/// let diff = Style::default().bg(Color::Red);
/// assert_eq!(style.patch(diff), Style::default().bg(Color::Red));
/// ```
- pub const fn bg(mut self, color: Color) -> Style {
+ pub fn bg(mut self, color: Color) -> Style {
self.bg = Some(color);
self
}
@@ -584,7 +526,7 @@ impl Style {
/// let diff = Style::default().underline_color(Color::Red);
/// assert_eq!(style.patch(diff), Style::default().underline_color(Color::Red));
/// ```
- pub const fn underline_color(mut self, color: Color) -> Style {
+ pub fn underline_color(mut self, color: Color) -> Style {
self.underline_color = Some(color);
self
}
@@ -599,7 +541,7 @@ impl Style {
/// let diff = Style::default().underline_style(UnderlineStyle::Curl);
/// assert_eq!(style.patch(diff), Style::default().underline_style(UnderlineStyle::Curl));
/// ```
- pub const fn underline_style(mut self, style: UnderlineStyle) -> Style {
+ pub fn underline_style(mut self, style: UnderlineStyle) -> Style {
self.underline_style = Some(style);
self
}