mirror of https://github.com/helix-editor/helix
reverse the dependency between helix-tui and helix-view (#366)
* reverse the dependency between helix-tui and helix-view by moving a fiew types to view * fix tests * clippy and format fixes Co-authored-by: Keith Simmons <keithsim@microsoft.com>pull/375/head
parent
74cc4b4a49
commit
4418e17547
@ -1,281 +1,481 @@
|
|||||||
//! `style` contains the primitives used to control how your user interface will look.
|
use bitflags::bitflags;
|
||||||
|
use std::cmp::{max, min};
|
||||||
use bitflags::bitflags;
|
|
||||||
|
#[derive(Debug)]
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
/// UNSTABLE
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
pub enum CursorKind {
|
||||||
pub enum Color {
|
/// █
|
||||||
Reset,
|
Block,
|
||||||
Black,
|
/// |
|
||||||
Red,
|
Bar,
|
||||||
Green,
|
/// _
|
||||||
Yellow,
|
Underline,
|
||||||
Blue,
|
/// Hidden cursor, can set cursor position with this to let IME have correct cursor position.
|
||||||
Magenta,
|
Hidden,
|
||||||
Cyan,
|
}
|
||||||
Gray,
|
|
||||||
DarkGray,
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
LightRed,
|
pub struct Margin {
|
||||||
LightGreen,
|
pub vertical: u16,
|
||||||
LightYellow,
|
pub horizontal: u16,
|
||||||
LightBlue,
|
}
|
||||||
LightMagenta,
|
|
||||||
LightCyan,
|
/// A simple rectangle used in the computation of the layout and to give widgets an hint about the
|
||||||
White,
|
/// area they are supposed to render to.
|
||||||
Rgb(u8, u8, u8),
|
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
|
||||||
Indexed(u8),
|
pub struct Rect {
|
||||||
}
|
pub x: u16,
|
||||||
|
pub y: u16,
|
||||||
bitflags! {
|
pub width: u16,
|
||||||
/// Modifier changes the way a piece of text is displayed.
|
pub height: u16,
|
||||||
///
|
}
|
||||||
/// They are bitflags so they can easily be composed.
|
|
||||||
///
|
impl Default for Rect {
|
||||||
/// ## Examples
|
fn default() -> Rect {
|
||||||
///
|
Rect {
|
||||||
/// ```rust
|
x: 0,
|
||||||
/// # use helix_tui::style::Modifier;
|
y: 0,
|
||||||
///
|
width: 0,
|
||||||
/// let m = Modifier::BOLD | Modifier::ITALIC;
|
height: 0,
|
||||||
/// ```
|
}
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
}
|
||||||
pub struct Modifier: u16 {
|
}
|
||||||
const BOLD = 0b0000_0000_0001;
|
|
||||||
const DIM = 0b0000_0000_0010;
|
impl Rect {
|
||||||
const ITALIC = 0b0000_0000_0100;
|
/// Creates a new rect, with width and height limited to keep the area under max u16.
|
||||||
const UNDERLINED = 0b0000_0000_1000;
|
/// If clipped, aspect ratio will be preserved.
|
||||||
const SLOW_BLINK = 0b0000_0001_0000;
|
pub fn new(x: u16, y: u16, width: u16, height: u16) -> Rect {
|
||||||
const RAPID_BLINK = 0b0000_0010_0000;
|
let max_area = u16::max_value();
|
||||||
const REVERSED = 0b0000_0100_0000;
|
let (clipped_width, clipped_height) =
|
||||||
const HIDDEN = 0b0000_1000_0000;
|
if u32::from(width) * u32::from(height) > u32::from(max_area) {
|
||||||
const CROSSED_OUT = 0b0001_0000_0000;
|
let aspect_ratio = f64::from(width) / f64::from(height);
|
||||||
}
|
let max_area_f = f64::from(max_area);
|
||||||
}
|
let height_f = (max_area_f / aspect_ratio).sqrt();
|
||||||
|
let width_f = height_f * aspect_ratio;
|
||||||
/// Style let you control the main characteristics of the displayed elements.
|
(width_f as u16, height_f as u16)
|
||||||
///
|
} else {
|
||||||
/// ```rust
|
(width, height)
|
||||||
/// # use helix_tui::style::{Color, Modifier, Style};
|
};
|
||||||
/// Style::default()
|
Rect {
|
||||||
/// .fg(Color::Black)
|
x,
|
||||||
/// .bg(Color::Green)
|
y,
|
||||||
/// .add_modifier(Modifier::ITALIC | Modifier::BOLD);
|
width: clipped_width,
|
||||||
/// ```
|
height: clipped_height,
|
||||||
///
|
}
|
||||||
/// It represents an incremental change. If you apply the styles S1, S2, S3 to a cell of the
|
}
|
||||||
/// terminal buffer, the style of this cell will be the result of the merge of S1, S2 and S3, not
|
|
||||||
/// just S3.
|
pub fn area(self) -> u16 {
|
||||||
///
|
self.width * self.height
|
||||||
/// ```rust
|
}
|
||||||
/// # use helix_tui::style::{Color, Modifier, Style};
|
|
||||||
/// # use helix_tui::buffer::Buffer;
|
pub fn left(self) -> u16 {
|
||||||
/// # use helix_tui::layout::Rect;
|
self.x
|
||||||
/// let styles = [
|
}
|
||||||
/// Style::default().fg(Color::Blue).add_modifier(Modifier::BOLD | Modifier::ITALIC),
|
|
||||||
/// Style::default().bg(Color::Red),
|
pub fn right(self) -> u16 {
|
||||||
/// Style::default().fg(Color::Yellow).remove_modifier(Modifier::ITALIC),
|
self.x.saturating_add(self.width)
|
||||||
/// ];
|
}
|
||||||
/// let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 1));
|
|
||||||
/// for style in &styles {
|
pub fn top(self) -> u16 {
|
||||||
/// buffer.get_mut(0, 0).set_style(*style);
|
self.y
|
||||||
/// }
|
}
|
||||||
/// assert_eq!(
|
|
||||||
/// Style {
|
pub fn bottom(self) -> u16 {
|
||||||
/// fg: Some(Color::Yellow),
|
self.y.saturating_add(self.height)
|
||||||
/// bg: Some(Color::Red),
|
}
|
||||||
/// add_modifier: Modifier::BOLD,
|
|
||||||
/// sub_modifier: Modifier::empty(),
|
pub fn inner(self, margin: &Margin) -> Rect {
|
||||||
/// },
|
if self.width < 2 * margin.horizontal || self.height < 2 * margin.vertical {
|
||||||
/// buffer.get(0, 0).style(),
|
Rect::default()
|
||||||
/// );
|
} else {
|
||||||
/// ```
|
Rect {
|
||||||
///
|
x: self.x + margin.horizontal,
|
||||||
/// The default implementation returns a `Style` that does not modify anything. If you wish to
|
y: self.y + margin.vertical,
|
||||||
/// reset all properties until that point use [`Style::reset`].
|
width: self.width - 2 * margin.horizontal,
|
||||||
///
|
height: self.height - 2 * margin.vertical,
|
||||||
/// ```
|
}
|
||||||
/// # use helix_tui::style::{Color, Modifier, Style};
|
}
|
||||||
/// # use helix_tui::buffer::Buffer;
|
}
|
||||||
/// # use helix_tui::layout::Rect;
|
|
||||||
/// let styles = [
|
pub fn union(self, other: Rect) -> Rect {
|
||||||
/// Style::default().fg(Color::Blue).add_modifier(Modifier::BOLD | Modifier::ITALIC),
|
let x1 = min(self.x, other.x);
|
||||||
/// Style::reset().fg(Color::Yellow),
|
let y1 = min(self.y, other.y);
|
||||||
/// ];
|
let x2 = max(self.x + self.width, other.x + other.width);
|
||||||
/// let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 1));
|
let y2 = max(self.y + self.height, other.y + other.height);
|
||||||
/// for style in &styles {
|
Rect {
|
||||||
/// buffer.get_mut(0, 0).set_style(*style);
|
x: x1,
|
||||||
/// }
|
y: y1,
|
||||||
/// assert_eq!(
|
width: x2 - x1,
|
||||||
/// Style {
|
height: y2 - y1,
|
||||||
/// fg: Some(Color::Yellow),
|
}
|
||||||
/// bg: Some(Color::Reset),
|
}
|
||||||
/// add_modifier: Modifier::empty(),
|
|
||||||
/// sub_modifier: Modifier::empty(),
|
pub fn intersection(self, other: Rect) -> Rect {
|
||||||
/// },
|
let x1 = max(self.x, other.x);
|
||||||
/// buffer.get(0, 0).style(),
|
let y1 = max(self.y, other.y);
|
||||||
/// );
|
let x2 = min(self.x + self.width, other.x + other.width);
|
||||||
/// ```
|
let y2 = min(self.y + self.height, other.y + other.height);
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
Rect {
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
x: x1,
|
||||||
pub struct Style {
|
y: y1,
|
||||||
pub fg: Option<Color>,
|
width: x2 - x1,
|
||||||
pub bg: Option<Color>,
|
height: y2 - y1,
|
||||||
pub add_modifier: Modifier,
|
}
|
||||||
pub sub_modifier: Modifier,
|
}
|
||||||
}
|
|
||||||
|
pub fn intersects(self, other: Rect) -> bool {
|
||||||
impl Default for Style {
|
self.x < other.x + other.width
|
||||||
fn default() -> Style {
|
&& self.x + self.width > other.x
|
||||||
Style {
|
&& self.y < other.y + other.height
|
||||||
fg: None,
|
&& self.y + self.height > other.y
|
||||||
bg: None,
|
}
|
||||||
add_modifier: Modifier::empty(),
|
}
|
||||||
sub_modifier: Modifier::empty(),
|
|
||||||
}
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
}
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
}
|
pub enum Color {
|
||||||
|
Reset,
|
||||||
impl Style {
|
Black,
|
||||||
/// Returns a `Style` resetting all properties.
|
Red,
|
||||||
pub fn reset() -> Style {
|
Green,
|
||||||
Style {
|
Yellow,
|
||||||
fg: Some(Color::Reset),
|
Blue,
|
||||||
bg: Some(Color::Reset),
|
Magenta,
|
||||||
add_modifier: Modifier::empty(),
|
Cyan,
|
||||||
sub_modifier: Modifier::all(),
|
Gray,
|
||||||
}
|
DarkGray,
|
||||||
}
|
LightRed,
|
||||||
|
LightGreen,
|
||||||
/// Changes the foreground color.
|
LightYellow,
|
||||||
///
|
LightBlue,
|
||||||
/// ## Examples
|
LightMagenta,
|
||||||
///
|
LightCyan,
|
||||||
/// ```rust
|
White,
|
||||||
/// # use helix_tui::style::{Color, Style};
|
Rgb(u8, u8, u8),
|
||||||
/// let style = Style::default().fg(Color::Blue);
|
Indexed(u8),
|
||||||
/// let diff = Style::default().fg(Color::Red);
|
}
|
||||||
/// assert_eq!(style.patch(diff), Style::default().fg(Color::Red));
|
|
||||||
/// ```
|
#[cfg(feature = "term")]
|
||||||
pub fn fg(mut self, color: Color) -> Style {
|
impl From<Color> for crossterm::style::Color {
|
||||||
self.fg = Some(color);
|
fn from(color: Color) -> Self {
|
||||||
self
|
use crossterm::style::Color as CColor;
|
||||||
}
|
|
||||||
|
match color {
|
||||||
/// Changes the background color.
|
Color::Reset => CColor::Reset,
|
||||||
///
|
Color::Black => CColor::Black,
|
||||||
/// ## Examples
|
Color::Red => CColor::DarkRed,
|
||||||
///
|
Color::Green => CColor::DarkGreen,
|
||||||
/// ```rust
|
Color::Yellow => CColor::DarkYellow,
|
||||||
/// # use helix_tui::style::{Color, Style};
|
Color::Blue => CColor::DarkBlue,
|
||||||
/// let style = Style::default().bg(Color::Blue);
|
Color::Magenta => CColor::DarkMagenta,
|
||||||
/// let diff = Style::default().bg(Color::Red);
|
Color::Cyan => CColor::DarkCyan,
|
||||||
/// assert_eq!(style.patch(diff), Style::default().bg(Color::Red));
|
Color::Gray => CColor::Grey,
|
||||||
/// ```
|
Color::DarkGray => CColor::DarkGrey,
|
||||||
pub fn bg(mut self, color: Color) -> Style {
|
Color::LightRed => CColor::Red,
|
||||||
self.bg = Some(color);
|
Color::LightGreen => CColor::Green,
|
||||||
self
|
Color::LightBlue => CColor::Blue,
|
||||||
}
|
Color::LightYellow => CColor::Yellow,
|
||||||
|
Color::LightMagenta => CColor::Magenta,
|
||||||
/// Changes the text emphasis.
|
Color::LightCyan => CColor::Cyan,
|
||||||
///
|
Color::White => CColor::White,
|
||||||
/// When applied, it adds the given modifier to the `Style` modifiers.
|
Color::Indexed(i) => CColor::AnsiValue(i),
|
||||||
///
|
Color::Rgb(r, g, b) => CColor::Rgb { r, g, b },
|
||||||
/// ## Examples
|
}
|
||||||
///
|
}
|
||||||
/// ```rust
|
}
|
||||||
/// # use helix_tui::style::{Color, Modifier, Style};
|
|
||||||
/// let style = Style::default().add_modifier(Modifier::BOLD);
|
bitflags! {
|
||||||
/// let diff = Style::default().add_modifier(Modifier::ITALIC);
|
/// Modifier changes the way a piece of text is displayed.
|
||||||
/// let patched = style.patch(diff);
|
///
|
||||||
/// assert_eq!(patched.add_modifier, Modifier::BOLD | Modifier::ITALIC);
|
/// They are bitflags so they can easily be composed.
|
||||||
/// assert_eq!(patched.sub_modifier, Modifier::empty());
|
///
|
||||||
/// ```
|
/// ## Examples
|
||||||
pub fn add_modifier(mut self, modifier: Modifier) -> Style {
|
///
|
||||||
self.sub_modifier.remove(modifier);
|
/// ```rust
|
||||||
self.add_modifier.insert(modifier);
|
/// # use helix_view::graphics::Modifier;
|
||||||
self
|
///
|
||||||
}
|
/// let m = Modifier::BOLD | Modifier::ITALIC;
|
||||||
|
/// ```
|
||||||
/// Changes the text emphasis.
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
///
|
pub struct Modifier: u16 {
|
||||||
/// When applied, it removes the given modifier from the `Style` modifiers.
|
const BOLD = 0b0000_0000_0001;
|
||||||
///
|
const DIM = 0b0000_0000_0010;
|
||||||
/// ## Examples
|
const ITALIC = 0b0000_0000_0100;
|
||||||
///
|
const UNDERLINED = 0b0000_0000_1000;
|
||||||
/// ```rust
|
const SLOW_BLINK = 0b0000_0001_0000;
|
||||||
/// # use helix_tui::style::{Color, Modifier, Style};
|
const RAPID_BLINK = 0b0000_0010_0000;
|
||||||
/// let style = Style::default().add_modifier(Modifier::BOLD | Modifier::ITALIC);
|
const REVERSED = 0b0000_0100_0000;
|
||||||
/// let diff = Style::default().remove_modifier(Modifier::ITALIC);
|
const HIDDEN = 0b0000_1000_0000;
|
||||||
/// let patched = style.patch(diff);
|
const CROSSED_OUT = 0b0001_0000_0000;
|
||||||
/// assert_eq!(patched.add_modifier, Modifier::BOLD);
|
}
|
||||||
/// assert_eq!(patched.sub_modifier, Modifier::ITALIC);
|
}
|
||||||
/// ```
|
|
||||||
pub fn remove_modifier(mut self, modifier: Modifier) -> Style {
|
/// Style let you control the main characteristics of the displayed elements.
|
||||||
self.add_modifier.remove(modifier);
|
///
|
||||||
self.sub_modifier.insert(modifier);
|
/// ```rust
|
||||||
self
|
/// # use helix_view::graphics::{Color, Modifier, Style};
|
||||||
}
|
/// Style::default()
|
||||||
|
/// .fg(Color::Black)
|
||||||
/// Results in a combined style that is equivalent to applying the two individual styles to
|
/// .bg(Color::Green)
|
||||||
/// a style one after the other.
|
/// .add_modifier(Modifier::ITALIC | Modifier::BOLD);
|
||||||
///
|
/// ```
|
||||||
/// ## Examples
|
///
|
||||||
/// ```
|
/// It represents an incremental change. If you apply the styles S1, S2, S3 to a cell of the
|
||||||
/// # use helix_tui::style::{Color, Modifier, Style};
|
/// terminal buffer, the style of this cell will be the result of the merge of S1, S2 and S3, not
|
||||||
/// let style_1 = Style::default().fg(Color::Yellow);
|
/// just S3.
|
||||||
/// let style_2 = Style::default().bg(Color::Red);
|
///
|
||||||
/// let combined = style_1.patch(style_2);
|
/// ```rust
|
||||||
/// assert_eq!(
|
/// # use helix_view::graphics::{Rect, Color, Modifier, Style};
|
||||||
/// Style::default().patch(style_1).patch(style_2),
|
/// # use helix_tui::buffer::Buffer;
|
||||||
/// Style::default().patch(combined));
|
/// let styles = [
|
||||||
/// ```
|
/// Style::default().fg(Color::Blue).add_modifier(Modifier::BOLD | Modifier::ITALIC),
|
||||||
pub fn patch(mut self, other: Style) -> Style {
|
/// Style::default().bg(Color::Red),
|
||||||
self.fg = other.fg.or(self.fg);
|
/// Style::default().fg(Color::Yellow).remove_modifier(Modifier::ITALIC),
|
||||||
self.bg = other.bg.or(self.bg);
|
/// ];
|
||||||
|
/// let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 1));
|
||||||
self.add_modifier.remove(other.sub_modifier);
|
/// for style in &styles {
|
||||||
self.add_modifier.insert(other.add_modifier);
|
/// buffer.get_mut(0, 0).set_style(*style);
|
||||||
self.sub_modifier.remove(other.add_modifier);
|
/// }
|
||||||
self.sub_modifier.insert(other.sub_modifier);
|
/// assert_eq!(
|
||||||
|
/// Style {
|
||||||
self
|
/// fg: Some(Color::Yellow),
|
||||||
}
|
/// bg: Some(Color::Red),
|
||||||
}
|
/// add_modifier: Modifier::BOLD,
|
||||||
|
/// sub_modifier: Modifier::empty(),
|
||||||
#[cfg(test)]
|
/// },
|
||||||
mod tests {
|
/// buffer.get(0, 0).style(),
|
||||||
use super::*;
|
/// );
|
||||||
|
/// ```
|
||||||
fn styles() -> Vec<Style> {
|
///
|
||||||
vec![
|
/// The default implementation returns a `Style` that does not modify anything. If you wish to
|
||||||
Style::default(),
|
/// reset all properties until that point use [`Style::reset`].
|
||||||
Style::default().fg(Color::Yellow),
|
///
|
||||||
Style::default().bg(Color::Yellow),
|
/// ```
|
||||||
Style::default().add_modifier(Modifier::BOLD),
|
/// # use helix_view::graphics::{Rect, Color, Modifier, Style};
|
||||||
Style::default().remove_modifier(Modifier::BOLD),
|
/// # use helix_tui::buffer::Buffer;
|
||||||
Style::default().add_modifier(Modifier::ITALIC),
|
/// let styles = [
|
||||||
Style::default().remove_modifier(Modifier::ITALIC),
|
/// Style::default().fg(Color::Blue).add_modifier(Modifier::BOLD | Modifier::ITALIC),
|
||||||
Style::default().add_modifier(Modifier::ITALIC | Modifier::BOLD),
|
/// Style::reset().fg(Color::Yellow),
|
||||||
Style::default().remove_modifier(Modifier::ITALIC | Modifier::BOLD),
|
/// ];
|
||||||
]
|
/// let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 1));
|
||||||
}
|
/// for style in &styles {
|
||||||
|
/// buffer.get_mut(0, 0).set_style(*style);
|
||||||
#[test]
|
/// }
|
||||||
fn combined_patch_gives_same_result_as_individual_patch() {
|
/// assert_eq!(
|
||||||
let styles = styles();
|
/// Style {
|
||||||
for &a in &styles {
|
/// fg: Some(Color::Yellow),
|
||||||
for &b in &styles {
|
/// bg: Some(Color::Reset),
|
||||||
for &c in &styles {
|
/// add_modifier: Modifier::empty(),
|
||||||
for &d in &styles {
|
/// sub_modifier: Modifier::empty(),
|
||||||
let combined = a.patch(b.patch(c.patch(d)));
|
/// },
|
||||||
|
/// buffer.get(0, 0).style(),
|
||||||
assert_eq!(
|
/// );
|
||||||
Style::default().patch(a).patch(b).patch(c).patch(d),
|
/// ```
|
||||||
Style::default().patch(combined)
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
);
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
}
|
pub struct Style {
|
||||||
}
|
pub fg: Option<Color>,
|
||||||
}
|
pub bg: Option<Color>,
|
||||||
}
|
pub add_modifier: Modifier,
|
||||||
}
|
pub sub_modifier: Modifier,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for Style {
|
||||||
|
fn default() -> Style {
|
||||||
|
Style {
|
||||||
|
fg: None,
|
||||||
|
bg: None,
|
||||||
|
add_modifier: Modifier::empty(),
|
||||||
|
sub_modifier: Modifier::empty(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Style {
|
||||||
|
/// Returns a `Style` resetting all properties.
|
||||||
|
pub fn reset() -> Style {
|
||||||
|
Style {
|
||||||
|
fg: Some(Color::Reset),
|
||||||
|
bg: Some(Color::Reset),
|
||||||
|
add_modifier: Modifier::empty(),
|
||||||
|
sub_modifier: Modifier::all(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Changes the foreground color.
|
||||||
|
///
|
||||||
|
/// ## Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use helix_view::graphics::{Color, Style};
|
||||||
|
/// let style = Style::default().fg(Color::Blue);
|
||||||
|
/// let diff = Style::default().fg(Color::Red);
|
||||||
|
/// assert_eq!(style.patch(diff), Style::default().fg(Color::Red));
|
||||||
|
/// ```
|
||||||
|
pub fn fg(mut self, color: Color) -> Style {
|
||||||
|
self.fg = Some(color);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Changes the background color.
|
||||||
|
///
|
||||||
|
/// ## Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use helix_view::graphics::{Color, Style};
|
||||||
|
/// let style = Style::default().bg(Color::Blue);
|
||||||
|
/// let diff = Style::default().bg(Color::Red);
|
||||||
|
/// assert_eq!(style.patch(diff), Style::default().bg(Color::Red));
|
||||||
|
/// ```
|
||||||
|
pub fn bg(mut self, color: Color) -> Style {
|
||||||
|
self.bg = Some(color);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Changes the text emphasis.
|
||||||
|
///
|
||||||
|
/// When applied, it adds the given modifier to the `Style` modifiers.
|
||||||
|
///
|
||||||
|
/// ## Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use helix_view::graphics::{Color, Modifier, Style};
|
||||||
|
/// let style = Style::default().add_modifier(Modifier::BOLD);
|
||||||
|
/// let diff = Style::default().add_modifier(Modifier::ITALIC);
|
||||||
|
/// let patched = style.patch(diff);
|
||||||
|
/// assert_eq!(patched.add_modifier, Modifier::BOLD | Modifier::ITALIC);
|
||||||
|
/// assert_eq!(patched.sub_modifier, Modifier::empty());
|
||||||
|
/// ```
|
||||||
|
pub fn add_modifier(mut self, modifier: Modifier) -> Style {
|
||||||
|
self.sub_modifier.remove(modifier);
|
||||||
|
self.add_modifier.insert(modifier);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Changes the text emphasis.
|
||||||
|
///
|
||||||
|
/// When applied, it removes the given modifier from the `Style` modifiers.
|
||||||
|
///
|
||||||
|
/// ## Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use helix_view::graphics::{Color, Modifier, Style};
|
||||||
|
/// let style = Style::default().add_modifier(Modifier::BOLD | Modifier::ITALIC);
|
||||||
|
/// let diff = Style::default().remove_modifier(Modifier::ITALIC);
|
||||||
|
/// let patched = style.patch(diff);
|
||||||
|
/// assert_eq!(patched.add_modifier, Modifier::BOLD);
|
||||||
|
/// assert_eq!(patched.sub_modifier, Modifier::ITALIC);
|
||||||
|
/// ```
|
||||||
|
pub fn remove_modifier(mut self, modifier: Modifier) -> Style {
|
||||||
|
self.add_modifier.remove(modifier);
|
||||||
|
self.sub_modifier.insert(modifier);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Results in a combined style that is equivalent to applying the two individual styles to
|
||||||
|
/// a style one after the other.
|
||||||
|
///
|
||||||
|
/// ## Examples
|
||||||
|
/// ```
|
||||||
|
/// # use helix_view::graphics::{Color, Modifier, Style};
|
||||||
|
/// let style_1 = Style::default().fg(Color::Yellow);
|
||||||
|
/// let style_2 = Style::default().bg(Color::Red);
|
||||||
|
/// let combined = style_1.patch(style_2);
|
||||||
|
/// assert_eq!(
|
||||||
|
/// Style::default().patch(style_1).patch(style_2),
|
||||||
|
/// Style::default().patch(combined));
|
||||||
|
/// ```
|
||||||
|
pub fn patch(mut self, other: Style) -> Style {
|
||||||
|
self.fg = other.fg.or(self.fg);
|
||||||
|
self.bg = other.bg.or(self.bg);
|
||||||
|
|
||||||
|
self.add_modifier.remove(other.sub_modifier);
|
||||||
|
self.add_modifier.insert(other.add_modifier);
|
||||||
|
self.sub_modifier.remove(other.add_modifier);
|
||||||
|
self.sub_modifier.insert(other.sub_modifier);
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rect_size_truncation() {
|
||||||
|
for width in 256u16..300u16 {
|
||||||
|
for height in 256u16..300u16 {
|
||||||
|
let rect = Rect::new(0, 0, width, height);
|
||||||
|
rect.area(); // Should not panic.
|
||||||
|
assert!(rect.width < width || rect.height < height);
|
||||||
|
// The target dimensions are rounded down so the math will not be too precise
|
||||||
|
// but let's make sure the ratios don't diverge crazily.
|
||||||
|
assert!(
|
||||||
|
(f64::from(rect.width) / f64::from(rect.height)
|
||||||
|
- f64::from(width) / f64::from(height))
|
||||||
|
.abs()
|
||||||
|
< 1.0
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// One dimension below 255, one above. Area above max u16.
|
||||||
|
let width = 900;
|
||||||
|
let height = 100;
|
||||||
|
let rect = Rect::new(0, 0, width, height);
|
||||||
|
assert_ne!(rect.width, 900);
|
||||||
|
assert_ne!(rect.height, 100);
|
||||||
|
assert!(rect.width < width || rect.height < height);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rect_size_preservation() {
|
||||||
|
for width in 0..256u16 {
|
||||||
|
for height in 0..256u16 {
|
||||||
|
let rect = Rect::new(0, 0, width, height);
|
||||||
|
rect.area(); // Should not panic.
|
||||||
|
assert_eq!(rect.width, width);
|
||||||
|
assert_eq!(rect.height, height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// One dimension below 255, one above. Area below max u16.
|
||||||
|
let rect = Rect::new(0, 0, 300, 100);
|
||||||
|
assert_eq!(rect.width, 300);
|
||||||
|
assert_eq!(rect.height, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn styles() -> Vec<Style> {
|
||||||
|
vec![
|
||||||
|
Style::default(),
|
||||||
|
Style::default().fg(Color::Yellow),
|
||||||
|
Style::default().bg(Color::Yellow),
|
||||||
|
Style::default().add_modifier(Modifier::BOLD),
|
||||||
|
Style::default().remove_modifier(Modifier::BOLD),
|
||||||
|
Style::default().add_modifier(Modifier::ITALIC),
|
||||||
|
Style::default().remove_modifier(Modifier::ITALIC),
|
||||||
|
Style::default().add_modifier(Modifier::ITALIC | Modifier::BOLD),
|
||||||
|
Style::default().remove_modifier(Modifier::ITALIC | Modifier::BOLD),
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn combined_patch_gives_same_result_as_individual_patch() {
|
||||||
|
let styles = styles();
|
||||||
|
for &a in &styles {
|
||||||
|
for &b in &styles {
|
||||||
|
for &c in &styles {
|
||||||
|
for &d in &styles {
|
||||||
|
let combined = a.patch(b.patch(c.patch(d)));
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
Style::default().patch(a).patch(b).patch(c).patch(d),
|
||||||
|
Style::default().patch(combined)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,160 @@
|
|||||||
|
use bitflags::bitflags;
|
||||||
|
|
||||||
|
bitflags! {
|
||||||
|
/// Represents key modifiers (shift, control, alt).
|
||||||
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
|
pub struct KeyModifiers: u8 {
|
||||||
|
const SHIFT = 0b0000_0001;
|
||||||
|
const CONTROL = 0b0000_0010;
|
||||||
|
const ALT = 0b0000_0100;
|
||||||
|
const NONE = 0b0000_0000;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "term")]
|
||||||
|
impl From<KeyModifiers> for crossterm::event::KeyModifiers {
|
||||||
|
fn from(key_modifiers: KeyModifiers) -> Self {
|
||||||
|
use crossterm::event::KeyModifiers as CKeyModifiers;
|
||||||
|
|
||||||
|
let mut result = CKeyModifiers::NONE;
|
||||||
|
|
||||||
|
if key_modifiers & KeyModifiers::SHIFT != KeyModifiers::NONE {
|
||||||
|
result &= CKeyModifiers::SHIFT;
|
||||||
|
}
|
||||||
|
|
||||||
|
if key_modifiers & KeyModifiers::CONTROL != KeyModifiers::NONE {
|
||||||
|
result &= CKeyModifiers::CONTROL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if key_modifiers & KeyModifiers::ALT != KeyModifiers::NONE {
|
||||||
|
result &= CKeyModifiers::ALT;
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "term")]
|
||||||
|
impl From<crossterm::event::KeyModifiers> for KeyModifiers {
|
||||||
|
fn from(val: crossterm::event::KeyModifiers) -> Self {
|
||||||
|
use crossterm::event::KeyModifiers as CKeyModifiers;
|
||||||
|
|
||||||
|
let mut result = KeyModifiers::NONE;
|
||||||
|
|
||||||
|
if val & CKeyModifiers::SHIFT != CKeyModifiers::NONE {
|
||||||
|
result &= KeyModifiers::SHIFT;
|
||||||
|
}
|
||||||
|
|
||||||
|
if val & CKeyModifiers::CONTROL != CKeyModifiers::NONE {
|
||||||
|
result &= KeyModifiers::CONTROL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if val & CKeyModifiers::ALT != CKeyModifiers::NONE {
|
||||||
|
result &= KeyModifiers::ALT;
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Represents a key.
|
||||||
|
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
|
pub enum KeyCode {
|
||||||
|
/// Backspace key.
|
||||||
|
Backspace,
|
||||||
|
/// Enter key.
|
||||||
|
Enter,
|
||||||
|
/// Left arrow key.
|
||||||
|
Left,
|
||||||
|
/// Right arrow key.
|
||||||
|
Right,
|
||||||
|
/// Up arrow key.
|
||||||
|
Up,
|
||||||
|
/// Down arrow key.
|
||||||
|
Down,
|
||||||
|
/// Home key.
|
||||||
|
Home,
|
||||||
|
/// End key.
|
||||||
|
End,
|
||||||
|
/// Page up key.
|
||||||
|
PageUp,
|
||||||
|
/// Page dow key.
|
||||||
|
PageDown,
|
||||||
|
/// Tab key.
|
||||||
|
Tab,
|
||||||
|
/// Shift + Tab key.
|
||||||
|
BackTab,
|
||||||
|
/// Delete key.
|
||||||
|
Delete,
|
||||||
|
/// Insert key.
|
||||||
|
Insert,
|
||||||
|
/// F key.
|
||||||
|
///
|
||||||
|
/// `KeyCode::F(1)` represents F1 key, etc.
|
||||||
|
F(u8),
|
||||||
|
/// A character.
|
||||||
|
///
|
||||||
|
/// `KeyCode::Char('c')` represents `c` character, etc.
|
||||||
|
Char(char),
|
||||||
|
/// Null.
|
||||||
|
Null,
|
||||||
|
/// Escape key.
|
||||||
|
Esc,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "term")]
|
||||||
|
impl From<KeyCode> for crossterm::event::KeyCode {
|
||||||
|
fn from(key_code: KeyCode) -> Self {
|
||||||
|
use crossterm::event::KeyCode as CKeyCode;
|
||||||
|
|
||||||
|
match key_code {
|
||||||
|
KeyCode::Backspace => CKeyCode::Backspace,
|
||||||
|
KeyCode::Enter => CKeyCode::Enter,
|
||||||
|
KeyCode::Left => CKeyCode::Left,
|
||||||
|
KeyCode::Right => CKeyCode::Right,
|
||||||
|
KeyCode::Up => CKeyCode::Up,
|
||||||
|
KeyCode::Down => CKeyCode::Down,
|
||||||
|
KeyCode::Home => CKeyCode::Home,
|
||||||
|
KeyCode::End => CKeyCode::End,
|
||||||
|
KeyCode::PageUp => CKeyCode::PageUp,
|
||||||
|
KeyCode::PageDown => CKeyCode::PageDown,
|
||||||
|
KeyCode::Tab => CKeyCode::Tab,
|
||||||
|
KeyCode::BackTab => CKeyCode::BackTab,
|
||||||
|
KeyCode::Delete => CKeyCode::Delete,
|
||||||
|
KeyCode::Insert => CKeyCode::Insert,
|
||||||
|
KeyCode::F(f_number) => CKeyCode::F(f_number),
|
||||||
|
KeyCode::Char(character) => CKeyCode::Char(character),
|
||||||
|
KeyCode::Null => CKeyCode::Null,
|
||||||
|
KeyCode::Esc => CKeyCode::Esc,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "term")]
|
||||||
|
impl From<crossterm::event::KeyCode> for KeyCode {
|
||||||
|
fn from(val: crossterm::event::KeyCode) -> Self {
|
||||||
|
use crossterm::event::KeyCode as CKeyCode;
|
||||||
|
|
||||||
|
match val {
|
||||||
|
CKeyCode::Backspace => KeyCode::Backspace,
|
||||||
|
CKeyCode::Enter => KeyCode::Enter,
|
||||||
|
CKeyCode::Left => KeyCode::Left,
|
||||||
|
CKeyCode::Right => KeyCode::Right,
|
||||||
|
CKeyCode::Up => KeyCode::Up,
|
||||||
|
CKeyCode::Down => KeyCode::Down,
|
||||||
|
CKeyCode::Home => KeyCode::Home,
|
||||||
|
CKeyCode::End => KeyCode::End,
|
||||||
|
CKeyCode::PageUp => KeyCode::PageUp,
|
||||||
|
CKeyCode::PageDown => KeyCode::PageDown,
|
||||||
|
CKeyCode::Tab => KeyCode::Tab,
|
||||||
|
CKeyCode::BackTab => KeyCode::BackTab,
|
||||||
|
CKeyCode::Delete => KeyCode::Delete,
|
||||||
|
CKeyCode::Insert => KeyCode::Insert,
|
||||||
|
CKeyCode::F(f_number) => KeyCode::F(f_number),
|
||||||
|
CKeyCode::Char(character) => KeyCode::Char(character),
|
||||||
|
CKeyCode::Null => KeyCode::Null,
|
||||||
|
CKeyCode::Esc => KeyCode::Esc,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue