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
Keith Simmons 3 years ago committed by GitHub
parent 74cc4b4a49
commit 4418e17547
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

2
Cargo.lock generated

@ -371,6 +371,7 @@ dependencies = [
"cassowary", "cassowary",
"crossterm", "crossterm",
"helix-core", "helix-core",
"helix-view",
"serde", "serde",
"unicode-segmentation", "unicode-segmentation",
"unicode-width", "unicode-width",
@ -381,6 +382,7 @@ name = "helix-view"
version = "0.2.0" version = "0.2.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bitflags",
"chardetng", "chardetng",
"crossterm", "crossterm",
"encoding_rs", "encoding_rs",

@ -23,8 +23,8 @@ path = "src/main.rs"
[dependencies] [dependencies]
helix-core = { version = "0.2", path = "../helix-core" } helix-core = { version = "0.2", path = "../helix-core" }
helix-view = { version = "0.2", path = "../helix-view", features = ["term"]} helix-view = { version = "0.2", path = "../helix-view" }
helix-lsp = { version = "0.2", path = "../helix-lsp"} helix-lsp = { version = "0.2", path = "../helix-lsp" }
anyhow = "1" anyhow = "1"
once_cell = "1.8" once_cell = "1.8"

@ -1,6 +1,6 @@
use helix_core::syntax; use helix_core::syntax;
use helix_lsp::{lsp, LspProgressMap}; use helix_lsp::{lsp, LspProgressMap};
use helix_view::{document::Mode, theme, Document, Editor, Theme, View}; use helix_view::{document::Mode, graphics::Rect, theme, Document, Editor, Theme, View};
use crate::{ use crate::{
args::Args, args::Args,
@ -29,8 +29,6 @@ use crossterm::{
execute, terminal, execute, terminal,
}; };
use tui::layout::Rect;
use futures_util::{future, stream::FuturesUnordered}; use futures_util::{future, stream::FuturesUnordered};
type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>; type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;

@ -15,7 +15,8 @@ use helix_core::{
use helix_view::{ use helix_view::{
document::{IndentStyle, Mode}, document::{IndentStyle, Mode},
input::{KeyCode, KeyEvent}, input::KeyEvent,
keyboard::KeyCode,
view::{View, PADDING}, view::{View, PADDING},
Document, DocumentId, Editor, ViewId, Document, DocumentId, Editor, ViewId,
}; };

@ -3,9 +3,10 @@
// cursive does compositor.screen_mut().add_layer_at(pos::absolute(x, y), <component>) // cursive does compositor.screen_mut().add_layer_at(pos::absolute(x, y), <component>)
use helix_core::Position; use helix_core::Position;
use helix_lsp::LspProgressMap; use helix_lsp::LspProgressMap;
use helix_view::graphics::{CursorKind, Rect};
use crossterm::event::Event; use crossterm::event::Event;
use tui::{buffer::Buffer as Surface, layout::Rect, terminal::CursorKind}; use tui::buffer::Buffer as Surface;
pub type Callback = Box<dyn FnOnce(&mut Compositor)>; pub type Callback = Box<dyn FnOnce(&mut Compositor)>;

@ -23,8 +23,11 @@ pub struct LspConfig {
#[test] #[test]
fn parsing_keymaps_config_file() { fn parsing_keymaps_config_file() {
use helix_core::hashmap; use helix_core::hashmap;
use helix_view::document::Mode; use helix_view::{
use helix_view::input::{KeyCode, KeyEvent, KeyModifiers}; document::Mode,
input::KeyEvent,
keyboard::{KeyCode, KeyModifiers},
};
let sample_keymaps = r#" let sample_keymaps = r#"
[keys.insert] [keys.insert]

@ -3,8 +3,11 @@ pub use crate::commands::Command;
use crate::config::Config; use crate::config::Config;
use anyhow::{anyhow, Error, Result}; use anyhow::{anyhow, Error, Result};
use helix_core::hashmap; use helix_core::hashmap;
use helix_view::document::Mode; use helix_view::{
use helix_view::input::{KeyCode, KeyEvent, KeyModifiers}; document::Mode,
input::KeyEvent,
keyboard::{KeyCode, KeyModifiers},
};
use serde::Deserialize; use serde::Deserialize;
use std::{ use std::{
collections::HashMap, collections::HashMap,

@ -1,15 +1,14 @@
use crate::compositor::{Component, Compositor, Context, EventResult}; use crate::compositor::{Component, Compositor, Context, EventResult};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use tui::{ use tui::buffer::Buffer as Surface;
buffer::Buffer as Surface,
layout::Rect,
style::{Color, Style},
};
use std::borrow::Cow; use std::borrow::Cow;
use helix_core::{Position, Transaction}; use helix_core::{Position, Transaction};
use helix_view::Editor; use helix_view::{
graphics::{Color, Rect, Style},
Editor,
};
use crate::commands; use crate::commands;
use crate::ui::{menu, Markdown, Menu, Popup, PromptEvent}; use crate::ui::{menu, Markdown, Menu, Popup, PromptEvent};

@ -12,21 +12,20 @@ use helix_core::{
LineEnding, Position, Range, LineEnding, Position, Range,
}; };
use helix_lsp::LspProgressMap; use helix_lsp::LspProgressMap;
use helix_view::input::{KeyCode, KeyEvent, KeyModifiers}; use helix_view::{
use helix_view::{document::Mode, Document, Editor, Theme, View}; document::Mode,
graphics::{Color, CursorKind, Modifier, Rect, Style},
input::KeyEvent,
keyboard::{KeyCode, KeyModifiers},
Document, Editor, Theme, View,
};
use std::borrow::Cow; use std::borrow::Cow;
use crossterm::{ use crossterm::{
cursor, cursor,
event::{read, Event, EventStream}, event::{read, Event, EventStream},
}; };
use tui::{ use tui::{backend::CrosstermBackend, buffer::Buffer as Surface};
backend::CrosstermBackend,
buffer::Buffer as Surface,
layout::Rect,
style::{Color, Modifier, Style},
terminal::CursorKind,
};
pub struct EditorView { pub struct EditorView {
keymaps: Keymaps, keymaps: Keymaps,

@ -1,16 +1,14 @@
use crate::compositor::{Component, Compositor, Context, EventResult}; use crate::compositor::{Component, Compositor, Context, EventResult};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use tui::{ use tui::{buffer::Buffer as Surface, text::Text};
buffer::Buffer as Surface,
layout::Rect,
style::{Color, Style},
text::Text,
};
use std::{borrow::Cow, sync::Arc}; use std::{borrow::Cow, sync::Arc};
use helix_core::{syntax, Position}; use helix_core::{syntax, Position};
use helix_view::{Editor, Theme}; use helix_view::{
graphics::{Color, Rect, Style},
Editor, Theme,
};
pub struct Markdown { pub struct Markdown {
contents: String, contents: String,

@ -1,11 +1,6 @@
use crate::compositor::{Component, Compositor, Context, EventResult}; use crate::compositor::{Component, Compositor, Context, EventResult};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use tui::{ use tui::{buffer::Buffer as Surface, widgets::Table};
buffer::Buffer as Surface,
layout::Rect,
style::{Color, Style},
widgets::Table,
};
pub use tui::widgets::{Cell, Row}; pub use tui::widgets::{Cell, Row};
@ -15,7 +10,10 @@ use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
use fuzzy_matcher::FuzzyMatcher; use fuzzy_matcher::FuzzyMatcher;
use helix_core::Position; use helix_core::Position;
use helix_view::Editor; use helix_view::{
graphics::{Color, Rect, Style},
Editor,
};
pub trait Item { pub trait Item {
// TODO: sort_text // TODO: sort_text

@ -18,12 +18,12 @@ pub use prompt::{Prompt, PromptEvent};
pub use spinner::{ProgressSpinners, Spinner}; pub use spinner::{ProgressSpinners, Spinner};
pub use text::Text; pub use text::Text;
pub use tui::layout::Rect;
pub use tui::style::{Color, Modifier, Style};
use helix_core::regex::Regex; use helix_core::regex::Regex;
use helix_core::register::Registers; use helix_core::register::Registers;
use helix_view::{Document, Editor, View}; use helix_view::{
graphics::{Color, Modifier, Rect, Style},
Document, Editor, View,
};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};

@ -2,8 +2,6 @@ use crate::compositor::{Component, Compositor, Context, EventResult};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use tui::{ use tui::{
buffer::Buffer as Surface, buffer::Buffer as Surface,
layout::Rect,
style::{Color, Style},
widgets::{Block, BorderType, Borders}, widgets::{Block, BorderType, Borders},
}; };
@ -14,9 +12,11 @@ use std::borrow::Cow;
use crate::ui::{Prompt, PromptEvent}; use crate::ui::{Prompt, PromptEvent};
use helix_core::Position; use helix_core::Position;
use helix_view::editor::Action; use helix_view::{
use helix_view::Editor; editor::Action,
use tui::terminal::CursorKind; graphics::{Color, CursorKind, Rect, Style},
Editor,
};
pub struct Picker<T> { pub struct Picker<T> {
options: Vec<T>, options: Vec<T>,

@ -1,15 +1,14 @@
use crate::compositor::{Component, Compositor, Context, EventResult}; use crate::compositor::{Component, Compositor, Context, EventResult};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use tui::{ use tui::buffer::Buffer as Surface;
buffer::Buffer as Surface,
layout::Rect,
style::{Color, Style},
};
use std::borrow::Cow; use std::borrow::Cow;
use helix_core::Position; use helix_core::Position;
use helix_view::Editor; use helix_view::{
graphics::{Color, Rect, Style},
Editor,
};
// TODO: share logic with Menu, it's essentially Popup(render_fn), but render fn needs to return // TODO: share logic with Menu, it's essentially Popup(render_fn), but render fn needs to return
// a width/height hint. maybe Popup(Box<Component>) // a width/height hint. maybe Popup(Box<Component>)

@ -1,14 +1,17 @@
use crate::compositor::{Component, Compositor, Context, EventResult}; use crate::compositor::{Component, Compositor, Context, EventResult};
use crate::ui; use crate::ui;
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use helix_core::Position;
use helix_view::{Editor, Theme};
use std::{borrow::Cow, ops::RangeFrom}; use std::{borrow::Cow, ops::RangeFrom};
use tui::terminal::CursorKind; use tui::buffer::Buffer as Surface;
use helix_core::{ use helix_core::{
unicode::segmentation::{GraphemeCursor, GraphemeIncomplete}, unicode::segmentation::{GraphemeCursor, GraphemeIncomplete},
unicode::width::UnicodeWidthStr, unicode::width::UnicodeWidthStr,
Position,
};
use helix_view::{
graphics::{Color, CursorKind, Margin, Modifier, Rect, Style},
Editor, Theme,
}; };
pub type Completion = (RangeFrom<usize>, Cow<'static, str>); pub type Completion = (RangeFrom<usize>, Cow<'static, str>);
@ -251,12 +254,6 @@ impl Prompt {
} }
} }
use tui::{
buffer::Buffer as Surface,
layout::Rect,
style::{Color, Modifier, Style},
};
const BASE_WIDTH: u16 = 30; const BASE_WIDTH: u16 = 30;
impl Prompt { impl Prompt {
@ -343,7 +340,6 @@ impl Prompt {
let background = theme.get("ui.help"); let background = theme.get("ui.help");
surface.clear_with(area, background); surface.clear_with(area, background);
use tui::layout::Margin;
text.render( text.render(
area.inner(&Margin { area.inner(&Margin {
vertical: 1, vertical: 1,

@ -1,15 +1,14 @@
use crate::compositor::{Component, Compositor, Context, EventResult}; use crate::compositor::{Component, Compositor, Context, EventResult};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use tui::{ use tui::buffer::Buffer as Surface;
buffer::Buffer as Surface,
layout::Rect,
style::{Color, Style},
};
use std::borrow::Cow; use std::borrow::Cow;
use helix_core::Position; use helix_core::Position;
use helix_view::Editor; use helix_view::{
graphics::{Color, Rect, Style},
Editor,
};
pub struct Text { pub struct Text {
contents: String, contents: String,

@ -22,4 +22,5 @@ unicode-segmentation = "1.2"
unicode-width = "0.1" unicode-width = "0.1"
crossterm = { version = "0.20", optional = true } crossterm = { version = "0.20", optional = true }
serde = { version = "1", "optional" = true, features = ["derive"]} serde = { version = "1", "optional" = true, features = ["derive"]}
helix-view = { path = "../helix-view", features = ["term"] }
helix-core = { version = "0.2", path = "../helix-core" } helix-core = { version = "0.2", path = "../helix-core" }

@ -1,10 +1,4 @@
use crate::{ use crate::{backend::Backend, buffer::Cell};
backend::Backend,
buffer::Cell,
layout::Rect,
style::{Color, Modifier},
terminal::CursorKind,
};
use crossterm::{ use crossterm::{
cursor::{CursorShape, Hide, MoveTo, SetCursorShape, Show}, cursor::{CursorShape, Hide, MoveTo, SetCursorShape, Show},
execute, queue, execute, queue,
@ -14,6 +8,7 @@ use crossterm::{
}, },
terminal::{self, Clear, ClearType}, terminal::{self, Clear, ClearType},
}; };
use helix_view::graphics::{Color, CursorKind, Modifier, Rect};
use std::io::{self, Write}; use std::io::{self, Write};
pub struct CrosstermBackend<W: Write> { pub struct CrosstermBackend<W: Write> {
@ -133,32 +128,6 @@ fn map_error(error: crossterm::Result<()>) -> io::Result<()> {
error.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string())) error.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))
} }
impl From<Color> for CColor {
fn from(color: Color) -> Self {
match color {
Color::Reset => CColor::Reset,
Color::Black => CColor::Black,
Color::Red => CColor::DarkRed,
Color::Green => CColor::DarkGreen,
Color::Yellow => CColor::DarkYellow,
Color::Blue => CColor::DarkBlue,
Color::Magenta => CColor::DarkMagenta,
Color::Cyan => CColor::DarkCyan,
Color::Gray => CColor::Grey,
Color::DarkGray => CColor::DarkGrey,
Color::LightRed => CColor::Red,
Color::LightGreen => CColor::Green,
Color::LightBlue => CColor::Blue,
Color::LightYellow => CColor::Yellow,
Color::LightMagenta => CColor::Magenta,
Color::LightCyan => CColor::Cyan,
Color::White => CColor::White,
Color::Indexed(i) => CColor::AnsiValue(i),
Color::Rgb(r, g, b) => CColor::Rgb { r, g, b },
}
}
}
#[derive(Debug)] #[derive(Debug)]
struct ModifierDiff { struct ModifierDiff {
pub from: Modifier, pub from: Modifier,

@ -1,8 +1,8 @@
use std::io; use std::io;
use crate::buffer::Cell; use crate::buffer::Cell;
use crate::layout::Rect;
use crate::terminal::CursorKind; use helix_view::graphics::{CursorKind, Rect};
#[cfg(feature = "crossterm")] #[cfg(feature = "crossterm")]
mod crossterm; mod crossterm;

@ -1,9 +1,8 @@
use crate::{ use crate::{
backend::Backend, backend::Backend,
buffer::{Buffer, Cell}, buffer::{Buffer, Cell},
layout::Rect,
terminal::CursorKind,
}; };
use helix_view::graphics::{CursorKind, Rect};
use std::{fmt::Write, io}; use std::{fmt::Write, io};
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;

@ -1,12 +1,10 @@
use crate::{ use crate::text::{Span, Spans};
layout::Rect,
style::{Color, Modifier, Style},
text::{Span, Spans},
};
use std::cmp::min; use std::cmp::min;
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
use helix_view::graphics::{Color, Modifier, Rect, Style};
/// A buffer cell /// A buffer cell
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct Cell { pub struct Cell {
@ -89,8 +87,7 @@ impl Default for Cell {
/// ///
/// ``` /// ```
/// use helix_tui::buffer::{Buffer, Cell}; /// use helix_tui::buffer::{Buffer, Cell};
/// use helix_tui::layout::Rect; /// use helix_view::graphics::{Rect, Color, Style, Modifier};
/// use helix_tui::style::{Color, Style, Modifier};
/// ///
/// let mut buf = Buffer::empty(Rect{x: 0, y: 0, width: 10, height: 5}); /// let mut buf = Buffer::empty(Rect{x: 0, y: 0, width: 10, height: 5});
/// buf.get_mut(0, 2).set_symbol("x"); /// buf.get_mut(0, 2).set_symbol("x");
@ -193,7 +190,7 @@ impl Buffer {
/// ///
/// ``` /// ```
/// # use helix_tui::buffer::Buffer; /// # use helix_tui::buffer::Buffer;
/// # use helix_tui::layout::Rect; /// # use helix_view::graphics::Rect;
/// let rect = Rect::new(200, 100, 10, 10); /// let rect = Rect::new(200, 100, 10, 10);
/// let buffer = Buffer::empty(rect); /// let buffer = Buffer::empty(rect);
/// // Global coordinates to the top corner of this buffer's area /// // Global coordinates to the top corner of this buffer's area
@ -225,7 +222,7 @@ impl Buffer {
/// ///
/// ``` /// ```
/// # use helix_tui::buffer::Buffer; /// # use helix_tui::buffer::Buffer;
/// # use helix_tui::layout::Rect; /// # use helix_view::graphics::Rect;
/// let rect = Rect::new(200, 100, 10, 10); /// let rect = Rect::new(200, 100, 10, 10);
/// let buffer = Buffer::empty(rect); /// let buffer = Buffer::empty(rect);
/// assert_eq!(buffer.pos_of(0), (200, 100)); /// assert_eq!(buffer.pos_of(0), (200, 100));

@ -1,11 +1,12 @@
use std::cell::RefCell; use std::cell::RefCell;
use std::cmp::{max, min};
use std::collections::HashMap; use std::collections::HashMap;
use cassowary::strength::{REQUIRED, WEAK}; use cassowary::strength::{REQUIRED, WEAK};
use cassowary::WeightedRelation::*; use cassowary::WeightedRelation::*;
use cassowary::{Constraint as CassowaryConstraint, Expression, Solver, Variable}; use cassowary::{Constraint as CassowaryConstraint, Expression, Solver, Variable};
use helix_view::graphics::{Margin, Rect};
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)]
pub enum Corner { pub enum Corner {
TopLeft, TopLeft,
@ -45,12 +46,6 @@ impl Constraint {
} }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Margin {
pub vertical: u16,
pub horizontal: u16,
}
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub enum Alignment { pub enum Alignment {
Left, Left,
@ -119,7 +114,8 @@ impl Layout {
/// ///
/// # Examples /// # Examples
/// ``` /// ```
/// # use helix_tui::layout::{Rect, Constraint, Direction, Layout}; /// # use helix_tui::layout::{Constraint, Direction, Layout};
/// # use helix_view::graphics::Rect;
/// let chunks = Layout::default() /// let chunks = Layout::default()
/// .direction(Direction::Vertical) /// .direction(Direction::Vertical)
/// .constraints([Constraint::Length(5), Constraint::Min(0)].as_ref()) /// .constraints([Constraint::Length(5), Constraint::Min(0)].as_ref())
@ -348,117 +344,6 @@ impl Element {
} }
} }
/// A simple rectangle used in the computation of the layout and to give widgets an hint about the
/// area they are supposed to render to.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct Rect {
pub x: u16,
pub y: u16,
pub width: u16,
pub height: u16,
}
impl Default for Rect {
fn default() -> Rect {
Rect {
x: 0,
y: 0,
width: 0,
height: 0,
}
}
}
impl Rect {
/// Creates a new rect, with width and height limited to keep the area under max u16.
/// If clipped, aspect ratio will be preserved.
pub fn new(x: u16, y: u16, width: u16, height: u16) -> Rect {
let max_area = u16::max_value();
let (clipped_width, clipped_height) =
if u32::from(width) * u32::from(height) > u32::from(max_area) {
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;
(width_f as u16, height_f as u16)
} else {
(width, height)
};
Rect {
x,
y,
width: clipped_width,
height: clipped_height,
}
}
pub fn area(self) -> u16 {
self.width * self.height
}
pub fn left(self) -> u16 {
self.x
}
pub fn right(self) -> u16 {
self.x.saturating_add(self.width)
}
pub fn top(self) -> u16 {
self.y
}
pub fn bottom(self) -> u16 {
self.y.saturating_add(self.height)
}
pub fn inner(self, margin: &Margin) -> Rect {
if self.width < 2 * margin.horizontal || self.height < 2 * margin.vertical {
Rect::default()
} else {
Rect {
x: self.x + margin.horizontal,
y: self.y + margin.vertical,
width: self.width - 2 * margin.horizontal,
height: self.height - 2 * margin.vertical,
}
}
}
pub fn union(self, other: Rect) -> Rect {
let x1 = min(self.x, other.x);
let y1 = min(self.y, other.y);
let x2 = max(self.x + self.width, other.x + other.width);
let y2 = max(self.y + self.height, other.y + other.height);
Rect {
x: x1,
y: y1,
width: x2 - x1,
height: y2 - y1,
}
}
pub fn intersection(self, other: Rect) -> Rect {
let x1 = max(self.x, other.x);
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);
Rect {
x: x1,
y: y1,
width: x2 - x1,
height: y2 - y1,
}
}
pub fn intersects(self, other: Rect) -> bool {
self.x < other.x + other.width
&& self.x + self.width > other.x
&& self.y < other.y + other.height
&& self.y + self.height > other.y
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -487,48 +372,4 @@ mod tests {
assert_eq!(target.height, chunks.iter().map(|r| r.height).sum::<u16>()); assert_eq!(target.height, chunks.iter().map(|r| r.height).sum::<u16>());
chunks.windows(2).for_each(|w| assert!(w[0].y <= w[1].y)); chunks.windows(2).for_each(|w| assert!(w[0].y <= w[1].y));
} }
#[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);
}
} }

@ -125,7 +125,6 @@
pub mod backend; pub mod backend;
pub mod buffer; pub mod buffer;
pub mod layout; pub mod layout;
pub mod style;
pub mod symbols; pub mod symbols;
pub mod terminal; pub mod terminal;
pub mod text; pub mod text;

@ -1,4 +1,5 @@
use crate::{backend::Backend, buffer::Buffer, layout::Rect}; use crate::{backend::Backend, buffer::Buffer};
use helix_view::graphics::{CursorKind, Rect};
use std::io; use std::io;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
@ -8,19 +9,6 @@ enum ResizeBehavior {
Auto, Auto,
} }
#[derive(Debug)]
/// UNSTABLE
pub enum CursorKind {
/// █
Block,
/// |
Bar,
/// _
Underline,
/// Hidden cursor, can set cursor position with this to let IME have correct cursor position.
Hidden,
}
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
/// UNSTABLE /// UNSTABLE
pub struct Viewport { pub struct Viewport {

@ -21,7 +21,7 @@
//! ```rust //! ```rust
//! # use helix_tui::widgets::Block; //! # use helix_tui::widgets::Block;
//! # use helix_tui::text::{Span, Spans}; //! # use helix_tui::text::{Span, Spans};
//! # use helix_tui::style::{Color, Style}; //! # use helix_view::graphics::{Color, Style};
//! // A simple string with no styling. //! // A simple string with no styling.
//! // Converted to Spans(vec![ //! // Converted to Spans(vec![
//! // Span { content: Cow::Borrowed("My title"), style: Style { .. } } //! // Span { content: Cow::Borrowed("My title"), style: Style { .. } }
@ -46,8 +46,8 @@
//! Span::raw(" title"), //! Span::raw(" title"),
//! ]); //! ]);
//! ``` //! ```
use crate::style::Style;
use helix_core::line_ending::str_is_line_ending; use helix_core::line_ending::str_is_line_ending;
use helix_view::graphics::Style;
use std::borrow::Cow; use std::borrow::Cow;
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
@ -92,7 +92,7 @@ impl<'a> Span<'a> {
/// ///
/// ```rust /// ```rust
/// # use helix_tui::text::Span; /// # use helix_tui::text::Span;
/// # use helix_tui::style::{Color, Modifier, Style}; /// # use helix_view::graphics::{Color, Modifier, Style};
/// let style = Style::default().fg(Color::Yellow).add_modifier(Modifier::ITALIC); /// let style = Style::default().fg(Color::Yellow).add_modifier(Modifier::ITALIC);
/// Span::styled("My text", style); /// Span::styled("My text", style);
/// Span::styled(String::from("My text"), style); /// Span::styled(String::from("My text"), style);
@ -121,7 +121,7 @@ impl<'a> Span<'a> {
/// ///
/// ```rust /// ```rust
/// # use helix_tui::text::{Span, StyledGrapheme}; /// # use helix_tui::text::{Span, StyledGrapheme};
/// # use helix_tui::style::{Color, Modifier, Style}; /// # use helix_view::graphics::{Color, Modifier, Style};
/// # use std::iter::Iterator; /// # use std::iter::Iterator;
/// let style = Style::default().fg(Color::Yellow); /// let style = Style::default().fg(Color::Yellow);
/// let span = Span::styled("Text", style); /// let span = Span::styled("Text", style);
@ -211,7 +211,7 @@ impl<'a> Spans<'a> {
/// ///
/// ```rust /// ```rust
/// # use helix_tui::text::{Span, Spans}; /// # use helix_tui::text::{Span, Spans};
/// # use helix_tui::style::{Color, Style}; /// # use helix_view::graphics::{Color, Style};
/// let spans = Spans::from(vec![ /// let spans = Spans::from(vec![
/// Span::styled("My", Style::default().fg(Color::Yellow)), /// Span::styled("My", Style::default().fg(Color::Yellow)),
/// Span::raw(" text"), /// Span::raw(" text"),
@ -265,7 +265,7 @@ impl<'a> From<Spans<'a>> for String {
/// ///
/// ```rust /// ```rust
/// # use helix_tui::text::Text; /// # use helix_tui::text::Text;
/// # use helix_tui::style::{Color, Modifier, Style}; /// # use helix_view::graphics::{Color, Modifier, Style};
/// let style = Style::default().fg(Color::Yellow).add_modifier(Modifier::ITALIC); /// let style = Style::default().fg(Color::Yellow).add_modifier(Modifier::ITALIC);
/// ///
/// // An initial two lines of `Text` built from a `&str` /// // An initial two lines of `Text` built from a `&str`
@ -319,7 +319,7 @@ impl<'a> Text<'a> {
/// ///
/// ```rust /// ```rust
/// # use helix_tui::text::Text; /// # use helix_tui::text::Text;
/// # use helix_tui::style::{Color, Modifier, Style}; /// # use helix_view::graphics::{Color, Modifier, Style};
/// let style = Style::default().fg(Color::Yellow).add_modifier(Modifier::ITALIC); /// let style = Style::default().fg(Color::Yellow).add_modifier(Modifier::ITALIC);
/// Text::styled("The first line\nThe second line", style); /// Text::styled("The first line\nThe second line", style);
/// Text::styled(String::from("The first line\nThe second line"), style); /// Text::styled(String::from("The first line\nThe second line"), style);
@ -369,7 +369,7 @@ impl<'a> Text<'a> {
/// ///
/// ```rust /// ```rust
/// # use helix_tui::text::Text; /// # use helix_tui::text::Text;
/// # use helix_tui::style::{Color, Modifier, Style}; /// # use helix_view::graphics::{Color, Modifier, Style};
/// let style = Style::default().fg(Color::Yellow).add_modifier(Modifier::ITALIC); /// let style = Style::default().fg(Color::Yellow).add_modifier(Modifier::ITALIC);
/// let mut raw_text = Text::raw("The first line\nThe second line"); /// let mut raw_text = Text::raw("The first line\nThe second line");
/// let styled_text = Text::styled(String::from("The first line\nThe second line"), style); /// let styled_text = Text::styled(String::from("The first line\nThe second line"), style);

@ -1,11 +1,10 @@
use crate::{ use crate::{
buffer::Buffer, buffer::Buffer,
layout::Rect,
style::Style,
symbols::line, symbols::line,
text::{Span, Spans}, text::{Span, Spans},
widgets::{Borders, Widget}, widgets::{Borders, Widget},
}; };
use helix_view::graphics::{Rect, Style};
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub enum BorderType { pub enum BorderType {
@ -33,7 +32,7 @@ impl BorderType {
/// ///
/// ``` /// ```
/// # use helix_tui::widgets::{Block, BorderType, Borders}; /// # use helix_tui::widgets::{Block, BorderType, Borders};
/// # use helix_tui::style::{Style, Color}; /// # use helix_view::graphics::{Style, Color};
/// Block::default() /// Block::default()
/// .title("Block") /// .title("Block")
/// .borders(Borders::LEFT | Borders::RIGHT) /// .borders(Borders::LEFT | Borders::RIGHT)
@ -212,7 +211,6 @@ impl<'a> Widget for Block<'a> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::layout::Rect;
#[test] #[test]
fn inner_takes_into_account_the_borders() { fn inner_takes_into_account_the_borders() {

@ -20,9 +20,11 @@ pub use self::block::{Block, BorderType};
pub use self::paragraph::{Paragraph, Wrap}; pub use self::paragraph::{Paragraph, Wrap};
pub use self::table::{Cell, Row, Table, TableState}; pub use self::table::{Cell, Row, Table, TableState};
use crate::{buffer::Buffer, layout::Rect}; use crate::buffer::Buffer;
use bitflags::bitflags; use bitflags::bitflags;
use helix_view::graphics::Rect;
bitflags! { bitflags! {
/// Bitflags that can be composed to set the visible borders essentially on the block widget. /// Bitflags that can be composed to set the visible borders essentially on the block widget.
pub struct Borders: u32 { pub struct Borders: u32 {

@ -1,13 +1,13 @@
use crate::{ use crate::{
buffer::Buffer, buffer::Buffer,
layout::{Alignment, Rect}, layout::Alignment,
style::Style,
text::{StyledGrapheme, Text}, text::{StyledGrapheme, Text},
widgets::{ widgets::{
reflow::{LineComposer, LineTruncator, WordWrapper}, reflow::{LineComposer, LineTruncator, WordWrapper},
Block, Widget, Block, Widget,
}, },
}; };
use helix_view::graphics::{Rect, Style};
use std::iter; use std::iter;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
@ -26,8 +26,8 @@ fn get_line_offset(line_width: u16, text_area_width: u16, alignment: Alignment)
/// ``` /// ```
/// # use helix_tui::text::{Text, Spans, Span}; /// # use helix_tui::text::{Text, Spans, Span};
/// # use helix_tui::widgets::{Block, Borders, Paragraph, Wrap}; /// # use helix_tui::widgets::{Block, Borders, Paragraph, Wrap};
/// # use helix_tui::style::{Style, Color, Modifier};
/// # use helix_tui::layout::{Alignment}; /// # use helix_tui::layout::{Alignment};
/// # use helix_view::graphics::{Style, Color, Modifier};
/// let text = vec![ /// let text = vec![
/// Spans::from(vec![ /// Spans::from(vec![
/// Span::raw("First"), /// Span::raw("First"),

@ -1,7 +1,6 @@
use crate::{ use crate::{
buffer::Buffer, buffer::Buffer,
layout::{Constraint, Rect}, layout::Constraint,
style::Style,
text::Text, text::Text,
widgets::{Block, Widget}, widgets::{Block, Widget},
}; };
@ -10,6 +9,7 @@ use cassowary::{
WeightedRelation::*, WeightedRelation::*,
{Expression, Solver}, {Expression, Solver},
}; };
use helix_view::graphics::{Rect, Style};
use std::{ use std::{
collections::HashMap, collections::HashMap,
iter::{self, Iterator}, iter::{self, Iterator},
@ -21,8 +21,8 @@ use unicode_width::UnicodeWidthStr;
/// It can be created from anything that can be converted to a [`Text`]. /// It can be created from anything that can be converted to a [`Text`].
/// ```rust /// ```rust
/// # use helix_tui::widgets::Cell; /// # use helix_tui::widgets::Cell;
/// # use helix_tui::style::{Style, Modifier};
/// # use helix_tui::text::{Span, Spans, Text}; /// # use helix_tui::text::{Span, Spans, Text};
/// # use helix_view::graphics::{Style, Modifier};
/// Cell::from("simple string"); /// Cell::from("simple string");
/// ///
/// Cell::from(Span::from("span")); /// Cell::from(Span::from("span"));
@ -74,7 +74,7 @@ where
/// But if you need a bit more control over individual cells, you can explicity create [`Cell`]s: /// But if you need a bit more control over individual cells, you can explicity create [`Cell`]s:
/// ```rust /// ```rust
/// # use helix_tui::widgets::{Row, Cell}; /// # use helix_tui::widgets::{Row, Cell};
/// # use helix_tui::style::{Style, Color}; /// # use helix_view::graphics::{Style, Color};
/// Row::new(vec![ /// Row::new(vec![
/// Cell::from("Cell1"), /// Cell::from("Cell1"),
/// Cell::from("Cell2").style(Style::default().fg(Color::Yellow)), /// Cell::from("Cell2").style(Style::default().fg(Color::Yellow)),
@ -137,7 +137,7 @@ impl<'a> Row<'a> {
/// ```rust /// ```rust
/// # use helix_tui::widgets::{Block, Borders, Table, Row, Cell}; /// # use helix_tui::widgets::{Block, Borders, Table, Row, Cell};
/// # use helix_tui::layout::Constraint; /// # use helix_tui::layout::Constraint;
/// # use helix_tui::style::{Style, Color, Modifier}; /// # use helix_view::graphics::{Style, Color, Modifier};
/// # use helix_tui::text::{Text, Spans, Span}; /// # use helix_tui::text::{Text, Spans, Span};
/// Table::new(vec![ /// Table::new(vec![
/// // Row can be created from simple strings. /// // Row can be created from simple strings.

@ -9,20 +9,18 @@ categories = ["editor"]
repository = "https://github.com/helix-editor/helix" repository = "https://github.com/helix-editor/helix"
homepage = "https://helix-editor.com" homepage = "https://helix-editor.com"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features] [features]
term = ["tui", "crossterm"] default = []
default = ["term"] term = ["crossterm"]
[dependencies] [dependencies]
bitflags = "1.0"
anyhow = "1" anyhow = "1"
helix-core = { version = "0.2", path = "../helix-core" } helix-core = { version = "0.2", path = "../helix-core" }
helix-lsp = { version = "0.2", path = "../helix-lsp"} helix-lsp = { version = "0.2", path = "../helix-lsp"}
crossterm = { version = "0.20", optional = true }
# Conversion traits # Conversion traits
tui = { path = "../helix-tui", package = "helix-tui", default-features = false, features = ["crossterm"], optional = true }
crossterm = { version = "0.20", features = ["event-stream"], optional = true }
once_cell = "1.8" once_cell = "1.8"
url = "2" url = "2"
@ -39,3 +37,6 @@ toml = "0.5"
log = "~0.4" log = "~0.4"
which = "4.1" which = "4.1"
[dev-dependencies]
helix-tui = { path = "../helix-tui" }

@ -1,12 +1,10 @@
use crate::clipboard::{get_clipboard_provider, ClipboardProvider};
use crate::{ use crate::{
clipboard::{get_clipboard_provider, ClipboardProvider},
graphics::{CursorKind, Rect},
theme::{self, Theme}, theme::{self, Theme},
tree::Tree, tree::Tree,
Document, DocumentId, RegisterSelection, View, ViewId, Document, DocumentId, RegisterSelection, View, ViewId,
}; };
use helix_core::syntax;
use tui::layout::Rect;
use tui::terminal::CursorKind;
use futures_util::future; use futures_util::future;
use std::{path::PathBuf, sync::Arc, time::Duration}; use std::{path::PathBuf, sync::Arc, time::Duration};
@ -17,6 +15,7 @@ use anyhow::Error;
pub use helix_core::diagnostic::Severity; pub use helix_core::diagnostic::Severity;
pub use helix_core::register::Registers; pub use helix_core::register::Registers;
use helix_core::syntax;
use helix_core::Position; use helix_core::Position;
#[derive(Debug)] #[derive(Debug)]
@ -45,7 +44,7 @@ pub enum Action {
impl Editor { impl Editor {
pub fn new( pub fn new(
mut area: tui::layout::Rect, mut area: Rect,
themes: Arc<theme::Loader>, themes: Arc<theme::Loader>,
config_loader: Arc<syntax::Loader>, config_loader: Arc<syntax::Loader>,
) -> Self { ) -> Self {

@ -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)
);
}
}
}
}
}
}

@ -1,10 +1,9 @@
//! Input event handling, currently backed by crossterm. //! Input event handling, currently backed by crossterm.
use anyhow::{anyhow, Error}; use anyhow::{anyhow, Error};
use crossterm::event;
use serde::de::{self, Deserialize, Deserializer}; use serde::de::{self, Deserialize, Deserializer};
use std::fmt; use std::fmt;
pub use crossterm::event::{KeyCode, KeyModifiers}; use crate::keyboard::{KeyCode, KeyModifiers};
/// Represents a key event. /// Represents a key event.
// We use a newtype here because we want to customize Deserialize and Display. // We use a newtype here because we want to customize Deserialize and Display.
@ -132,9 +131,15 @@ impl<'de> Deserialize<'de> for KeyEvent {
} }
} }
impl From<event::KeyEvent> for KeyEvent { #[cfg(feature = "term")]
fn from(event::KeyEvent { code, modifiers }: event::KeyEvent) -> KeyEvent { impl From<crossterm::event::KeyEvent> for KeyEvent {
KeyEvent { code, modifiers } fn from(
crossterm::event::KeyEvent { code, modifiers }: crossterm::event::KeyEvent,
) -> KeyEvent {
KeyEvent {
code: code.into(),
modifiers: modifiers.into(),
}
} }
} }

@ -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,
}
}
}

@ -4,7 +4,9 @@ pub mod macros;
pub mod clipboard; pub mod clipboard;
pub mod document; pub mod document;
pub mod editor; pub mod editor;
pub mod graphics;
pub mod input; pub mod input;
pub mod keyboard;
pub mod register_selection; pub mod register_selection;
pub mod theme; pub mod theme;
pub mod tree; pub mod tree;

@ -9,86 +9,7 @@ use once_cell::sync::Lazy;
use serde::{Deserialize, Deserializer}; use serde::{Deserialize, Deserializer};
use toml::Value; use toml::Value;
#[cfg(feature = "term")] pub use crate::graphics::{Color, Modifier, Style};
pub use tui::style::{Color, Modifier, Style};
// #[derive(Clone, Copy, PartialEq, Eq, Default, Hash)]
// pub struct Color {
// pub r: u8,
// pub g: u8,
// pub b: u8,
// }
// impl Color {
// pub fn new(r: u8, g: u8, b: u8) -> Self {
// Self { r, g, b }
// }
// }
// #[cfg(feature = "term")]
// impl Into<tui::style::Color> for Color {
// fn into(self) -> tui::style::Color {
// tui::style::Color::Rgb(self.r, self.g, self.b)
// }
// }
// impl std::str::FromStr for Color {
// type Err = ();
// /// Tries to parse a string (`'#FFFFFF'` or `'FFFFFF'`) into RGB.
// fn from_str(input: &str) -> Result<Self, Self::Err> {
// let input = input.trim();
// let input = match (input.chars().next(), input.len()) {
// (Some('#'), 7) => &input[1..],
// (_, 6) => input,
// _ => return Err(()),
// };
// u32::from_str_radix(&input, 16)
// .map(|s| Color {
// r: ((s >> 16) & 0xFF) as u8,
// g: ((s >> 8) & 0xFF) as u8,
// b: (s & 0xFF) as u8,
// })
// .map_err(|_| ())
// }
// }
// #[derive(Clone, Copy, PartialEq, Eq, Default, Hash)]
// pub struct Style {
// pub fg: Option<Color>,
// pub bg: Option<Color>,
// // TODO: modifiers (bold, underline, italic, etc)
// }
// impl Style {
// pub fn fg(mut self, fg: Color) -> Self {
// self.fg = Some(fg);
// self
// }
// pub fn bg(mut self, bg: Color) -> Self {
// self.bg = Some(bg);
// self
// }
// }
// #[cfg(feature = "term")]
// impl Into<tui::style::Style> for Style {
// fn into(self) -> tui::style::Style {
// let style = tui::style::Style::default();
// if let Some(fg) = self.fg {
// style.fg(fg.into());
// }
// if let Some(bg) = self.bg {
// style.bg(bg.into());
// }
// style
// }
// }
/// Color theme for syntax highlighting. /// Color theme for syntax highlighting.

@ -1,6 +1,5 @@
use crate::{View, ViewId}; use crate::{graphics::Rect, View, ViewId};
use slotmap::HopSlotMap; use slotmap::HopSlotMap;
use tui::layout::Rect;
// the dimensions are recomputed on windo resize/tree change. // the dimensions are recomputed on windo resize/tree change.
// //

@ -1,12 +1,11 @@
use std::borrow::Cow; use std::borrow::Cow;
use crate::{Document, DocumentId, ViewId}; use crate::{graphics::Rect, Document, DocumentId, ViewId};
use helix_core::{ use helix_core::{
coords_at_pos, coords_at_pos,
graphemes::{grapheme_width, RopeGraphemes}, graphemes::{grapheme_width, RopeGraphemes},
Position, RopeSlice, Selection, Position, RopeSlice, Selection,
}; };
use tui::layout::Rect;
pub const PADDING: usize = 5; pub const PADDING: usize = 5;

Loading…
Cancel
Save