on master help sign in
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>

commit4418e17547562e211952b9e8585916e04b858b3b authorKeith Simmons <keith@the-simmons.net> dateThu 24 Jun 2021, 20:58 (5 years ago) parent74cc4b4
38 files changed+773−697
Cargo.lock+2 helix-term/Cargo.toml+2−2 helix-term/src/application.rs+1−3 helix-term/src/commands.rs+2−1 helix-term/src/compositor.rs+2−1 helix-term/src/config.rs+5−2 helix-term/src/keymap.rs+5−2 helix-term/src/ui/completion.rs+5−6 helix-term/src/ui/editor.rs+8−9 helix-term/src/ui/markdown.rs+5−7 helix-term/src/ui/menu.rs+5−7 helix-term/src/ui/mod.rs+4−4 helix-term/src/ui/picker.rs+5−5 helix-term/src/ui/popup.rs+5−6 helix-term/src/ui/prompt.rs+6−10 helix-term/src/ui/text.rs+5−6 helix-tui/Cargo.toml+1 helix-tui/src/backend/crossterm.rs+2−33 helix-tui/src/backend/mod.rs+2−2 helix-tui/src/backend/test.rs+1−2 helix-tui/src/buffer.rs+6−9 helix-tui/src/layout.rs+4−163 helix-tui/src/lib.rs−1 helix-tui/src/terminal.rs+2−14 helix-tui/src/text.rs+8−8 helix-tui/src/widgets/block.rs+2−4 helix-tui/src/widgets/mod.rs+3−1 helix-tui/src/widgets/paragraph.rs+3−3 helix-tui/src/widgets/table.rs+5−5 helix-view/Cargo.toml+7−6 helix-view/src/editor.rs+4−5 helix-tui/src/style.rs → helix-view/src/graphics.rs+481−281 helix-view/src/input.rs+10−5 helix-view/src/keyboard.rs+160 helix-view/src/lib.rs+2 helix-view/src/theme.rs+1−80 helix-view/src/tree.rs+1−2 helix-view/src/view.rs+1−2
Cargo.lock+2
@@ -371,6 +371,7 @@ dependencies = [
371371 "cassowary",
372372 "crossterm",
373373 "helix-core",
374+ "helix-view",
374375 "serde",
375376 "unicode-segmentation",
376377 "unicode-width",
@@ -381,6 +382,7 @@ name = "helix-view"
381382version = "0.2.0"
382383dependencies = [
383384 "anyhow",
385+ "bitflags",
384386 "chardetng",
385387 "crossterm",
386388 "encoding_rs",
helix-term/Cargo.toml+2−2
@@ -23,8 +23,8 @@ path = "src/main.rs"
2323
2424[dependencies]
2525helix-core = { version = "0.2", path = "../helix-core" }
26−helix-view = { version = "0.2", path = "../helix-view", features = ["term"]}
27−helix-lsp = { version = "0.2", path = "../helix-lsp"}
26+helix-view = { version = "0.2", path = "../helix-view" }
27+helix-lsp = { version = "0.2", path = "../helix-lsp" }
2828
2929anyhow = "1"
3030once_cell = "1.8"
helix-term/src/application.rs+1−3
@@ -1,6 +1,6 @@
11use helix_core::syntax;
22use helix_lsp::{lsp, LspProgressMap};
3−use helix_view::{document::Mode, theme, Document, Editor, Theme, View};
3+use helix_view::{document::Mode, graphics::Rect, theme, Document, Editor, Theme, View};
44
55use crate::{
66 args::Args,
@@ -29,8 +29,6 @@ use crossterm::{
2929 execute, terminal,
3030};
3131
32−use tui::layout::Rect;
33−
3432use futures_util::{future, stream::FuturesUnordered};
3533
3634type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;
helix-term/src/commands.rs+2−1
@@ -15,7 +15,8 @@ use helix_core::{
1515
1616use helix_view::{
1717 document::{IndentStyle, Mode},
18− input::{KeyCode, KeyEvent},
18+ input::KeyEvent,
19+ keyboard::KeyCode,
1920 view::{View, PADDING},
2021 Document, DocumentId, Editor, ViewId,
2122};
helix-term/src/compositor.rs+2−1
@@ -3,9 +3,10 @@
33// cursive does compositor.screen_mut().add_layer_at(pos::absolute(x, y), <component>)
44use helix_core::Position;
55use helix_lsp::LspProgressMap;
6+use helix_view::graphics::{CursorKind, Rect};
67
78use crossterm::event::Event;
8−use tui::{buffer::Buffer as Surface, layout::Rect, terminal::CursorKind};
9+use tui::buffer::Buffer as Surface;
910
1011pub type Callback = Box<dyn FnOnce(&mut Compositor)>;
1112
helix-term/src/config.rs+5−2
@@ -23,8 +23,11 @@ pub struct LspConfig {
2323#[test]
2424fn parsing_keymaps_config_file() {
2525 use helix_core::hashmap;
26− use helix_view::document::Mode;
27− use helix_view::input::{KeyCode, KeyEvent, KeyModifiers};
26+ use helix_view::{
27+ document::Mode,
28+ input::KeyEvent,
29+ keyboard::{KeyCode, KeyModifiers},
30+ };
2831
2932 let sample_keymaps = r#"
3033 [keys.insert]
helix-term/src/keymap.rs+5−2
@@ -3,8 +3,11 @@ pub use crate::commands::Command;
33use crate::config::Config;
44use anyhow::{anyhow, Error, Result};
55use helix_core::hashmap;
6−use helix_view::document::Mode;
7−use helix_view::input::{KeyCode, KeyEvent, KeyModifiers};
6+use helix_view::{
7+ document::Mode,
8+ input::KeyEvent,
9+ keyboard::{KeyCode, KeyModifiers},
10+};
811use serde::Deserialize;
912use std::{
1013 collections::HashMap,
helix-term/src/ui/completion.rs+5−6
@@ -1,15 +1,14 @@
11use crate::compositor::{Component, Compositor, Context, EventResult};
22use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
3−use tui::{
4− buffer::Buffer as Surface,
5− layout::Rect,
6− style::{Color, Style},
7−};
3+use tui::buffer::Buffer as Surface;
84
95use std::borrow::Cow;
106
117use helix_core::{Position, Transaction};
12−use helix_view::Editor;
8+use helix_view::{
9+ graphics::{Color, Rect, Style},
10+ Editor,
11+};
1312
1413use crate::commands;
1514use crate::ui::{menu, Markdown, Menu, Popup, PromptEvent};
helix-term/src/ui/editor.rs+8−9
@@ -12,21 +12,20 @@ use helix_core::{
1212 LineEnding, Position, Range,
1313};
1414use helix_lsp::LspProgressMap;
15−use helix_view::input::{KeyCode, KeyEvent, KeyModifiers};
16−use helix_view::{document::Mode, Document, Editor, Theme, View};
15+use helix_view::{
16+ document::Mode,
17+ graphics::{Color, CursorKind, Modifier, Rect, Style},
18+ input::KeyEvent,
19+ keyboard::{KeyCode, KeyModifiers},
20+ Document, Editor, Theme, View,
21+};
1722use std::borrow::Cow;
1823
1924use crossterm::{
2025 cursor,
2126 event::{read, Event, EventStream},
2227};
23−use tui::{
24− backend::CrosstermBackend,
25− buffer::Buffer as Surface,
26− layout::Rect,
27− style::{Color, Modifier, Style},
28− terminal::CursorKind,
29−};
28+use tui::{backend::CrosstermBackend, buffer::Buffer as Surface};
3029
3130pub struct EditorView {
3231 keymaps: Keymaps,
helix-term/src/ui/markdown.rs+5−7
@@ -1,16 +1,14 @@
11use crate::compositor::{Component, Compositor, Context, EventResult};
22use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
3−use tui::{
4− buffer::Buffer as Surface,
5− layout::Rect,
6− style::{Color, Style},
7− text::Text,
8−};
3+use tui::{buffer::Buffer as Surface, text::Text};
94
105use std::{borrow::Cow, sync::Arc};
116
127use helix_core::{syntax, Position};
13−use helix_view::{Editor, Theme};
8+use helix_view::{
9+ graphics::{Color, Rect, Style},
10+ Editor, Theme,
11+};
1412
1513pub struct Markdown {
1614 contents: String,
helix-term/src/ui/menu.rs+5−7
@@ -1,11 +1,6 @@
11use crate::compositor::{Component, Compositor, Context, EventResult};
22use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
3−use tui::{
4− buffer::Buffer as Surface,
5− layout::Rect,
6− style::{Color, Style},
7− widgets::Table,
8−};
3+use tui::{buffer::Buffer as Surface, widgets::Table};
94
105pub use tui::widgets::{Cell, Row};
116
@@ -15,7 +10,10 @@ use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
1510use fuzzy_matcher::FuzzyMatcher;
1611
1712use helix_core::Position;
18−use helix_view::Editor;
13+use helix_view::{
14+ graphics::{Color, Rect, Style},
15+ Editor,
16+};
1917
2018pub trait Item {
2119 // TODO: sort_text
helix-term/src/ui/mod.rs+4−4
@@ -18,12 +18,12 @@ pub use prompt::{Prompt, PromptEvent};
1818pub use spinner::{ProgressSpinners, Spinner};
1919pub use text::Text;
2020
21−pub use tui::layout::Rect;
22−pub use tui::style::{Color, Modifier, Style};
23−
2421use helix_core::regex::Regex;
2522use helix_core::register::Registers;
26−use helix_view::{Document, Editor, View};
23+use helix_view::{
24+ graphics::{Color, Modifier, Rect, Style},
25+ Document, Editor, View,
26+};
2727
2828use std::path::{Path, PathBuf};
2929
helix-term/src/ui/picker.rs+5−5
@@ -2,8 +2,6 @@ use crate::compositor::{Component, Compositor, Context, EventResult};
22use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
33use tui::{
44 buffer::Buffer as Surface,
5− layout::Rect,
6− style::{Color, Style},
75 widgets::{Block, BorderType, Borders},
86};
97
@@ -14,9 +12,11 @@ use std::borrow::Cow;
1412
1513use crate::ui::{Prompt, PromptEvent};
1614use helix_core::Position;
17−use helix_view::editor::Action;
18−use helix_view::Editor;
19−use tui::terminal::CursorKind;
15+use helix_view::{
16+ editor::Action,
17+ graphics::{Color, CursorKind, Rect, Style},
18+ Editor,
19+};
2020
2121pub struct Picker<T> {
2222 options: Vec<T>,
helix-term/src/ui/popup.rs+5−6
@@ -1,15 +1,14 @@
11use crate::compositor::{Component, Compositor, Context, EventResult};
22use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
3−use tui::{
4− buffer::Buffer as Surface,
5− layout::Rect,
6− style::{Color, Style},
7−};
3+use tui::buffer::Buffer as Surface;
84
95use std::borrow::Cow;
106
117use helix_core::Position;
12−use helix_view::Editor;
8+use helix_view::{
9+ graphics::{Color, Rect, Style},
10+ Editor,
11+};
1312
1413// TODO: share logic with Menu, it's essentially Popup(render_fn), but render fn needs to return
1514// a width/height hint. maybe Popup(Box<Component>)
helix-term/src/ui/prompt.rs+6−10
@@ -1,14 +1,17 @@
11use crate::compositor::{Component, Compositor, Context, EventResult};
22use crate::ui;
33use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
4−use helix_core::Position;
5−use helix_view::{Editor, Theme};
64use std::{borrow::Cow, ops::RangeFrom};
7−use tui::terminal::CursorKind;
5+use tui::buffer::Buffer as Surface;
86
97use helix_core::{
108 unicode::segmentation::{GraphemeCursor, GraphemeIncomplete},
119 unicode::width::UnicodeWidthStr,
10+ Position,
11+};
12+use helix_view::{
13+ graphics::{Color, CursorKind, Margin, Modifier, Rect, Style},
14+ Editor, Theme,
1215};
1316
1417pub type Completion = (RangeFrom<usize>, Cow<'static, str>);
@@ -251,12 +254,6 @@ impl Prompt {
251254 }
252255}
253256
254−use tui::{
255− buffer::Buffer as Surface,
256− layout::Rect,
257− style::{Color, Modifier, Style},
258−};
259−
260257const BASE_WIDTH: u16 = 30;
261258
262259impl Prompt {
@@ -343,7 +340,6 @@ impl Prompt {
343340 let background = theme.get("ui.help");
344341 surface.clear_with(area, background);
345342
346− use tui::layout::Margin;
347343 text.render(
348344 area.inner(&Margin {
349345 vertical: 1,
helix-term/src/ui/text.rs+5−6
@@ -1,15 +1,14 @@
11use crate::compositor::{Component, Compositor, Context, EventResult};
22use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
3−use tui::{
4− buffer::Buffer as Surface,
5− layout::Rect,
6− style::{Color, Style},
7−};
3+use tui::buffer::Buffer as Surface;
84
95use std::borrow::Cow;
106
117use helix_core::Position;
12−use helix_view::Editor;
8+use helix_view::{
9+ graphics::{Color, Rect, Style},
10+ Editor,
11+};
1312
1413pub struct Text {
1514 contents: String,
helix-tui/Cargo.toml+1
@@ -22,4 +22,5 @@ unicode-segmentation = "1.2"
2222unicode-width = "0.1"
2323crossterm = { version = "0.20", optional = true }
2424serde = { version = "1", "optional" = true, features = ["derive"]}
25+helix-view = { path = "../helix-view", features = ["term"] }
2526helix-core = { version = "0.2", path = "../helix-core" }
helix-tui/src/backend/crossterm.rs+2−33
@@ -1,10 +1,4 @@
1−use crate::{
2− backend::Backend,
3− buffer::Cell,
4− layout::Rect,
5− style::{Color, Modifier},
6− terminal::CursorKind,
7−};
1+use crate::{backend::Backend, buffer::Cell};
82use crossterm::{
93 cursor::{CursorShape, Hide, MoveTo, SetCursorShape, Show},
104 execute, queue,
@@ -14,6 +8,7 @@ use crossterm::{
148 },
159 terminal::{self, Clear, ClearType},
1610};
11+use helix_view::graphics::{Color, CursorKind, Modifier, Rect};
1712use std::io::{self, Write};
1813
1914pub struct CrosstermBackend<W: Write> {
@@ -133,32 +128,6 @@ fn map_error(error: crossterm::Result<()>) -> io::Result<()> {
133128 error.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))
134129}
135130
136−impl From<Color> for CColor {
137− fn from(color: Color) -> Self {
138− match color {
139− Color::Reset => CColor::Reset,
140− Color::Black => CColor::Black,
141− Color::Red => CColor::DarkRed,
142− Color::Green => CColor::DarkGreen,
143− Color::Yellow => CColor::DarkYellow,
144− Color::Blue => CColor::DarkBlue,
145− Color::Magenta => CColor::DarkMagenta,
146− Color::Cyan => CColor::DarkCyan,
147− Color::Gray => CColor::Grey,
148− Color::DarkGray => CColor::DarkGrey,
149− Color::LightRed => CColor::Red,
150− Color::LightGreen => CColor::Green,
151− Color::LightBlue => CColor::Blue,
152− Color::LightYellow => CColor::Yellow,
153− Color::LightMagenta => CColor::Magenta,
154− Color::LightCyan => CColor::Cyan,
155− Color::White => CColor::White,
156− Color::Indexed(i) => CColor::AnsiValue(i),
157− Color::Rgb(r, g, b) => CColor::Rgb { r, g, b },
158− }
159− }
160−}
161−
162131#[derive(Debug)]
163132struct ModifierDiff {
164133 pub from: Modifier,
helix-tui/src/backend/mod.rs+2−2
@@ -1,8 +1,8 @@
11use std::io;
22
33use crate::buffer::Cell;
4−use crate::layout::Rect;
5−use crate::terminal::CursorKind;
4+
5+use helix_view::graphics::{CursorKind, Rect};
66
77#[cfg(feature = "crossterm")]
88mod crossterm;
helix-tui/src/backend/test.rs+1−2
@@ -1,9 +1,8 @@
11use crate::{
22 backend::Backend,
33 buffer::{Buffer, Cell},
4− layout::Rect,
5− terminal::CursorKind,
64};
5+use helix_view::graphics::{CursorKind, Rect};
76use std::{fmt::Write, io};
87use unicode_width::UnicodeWidthStr;
98
helix-tui/src/buffer.rs+6−9
@@ -1,12 +1,10 @@
1−use crate::{
2− layout::Rect,
3− style::{Color, Modifier, Style},
4− text::{Span, Spans},
5−};
1+use crate::text::{Span, Spans};
62use std::cmp::min;
73use unicode_segmentation::UnicodeSegmentation;
84use unicode_width::UnicodeWidthStr;
95
6+use helix_view::graphics::{Color, Modifier, Rect, Style};
7+
108/// A buffer cell
119#[derive(Debug, Clone, PartialEq)]
1210pub struct Cell {
@@ -89,8 +87,7 @@ impl Default for Cell {
8987///
9088/// ```
9189/// use helix_tui::buffer::{Buffer, Cell};
92−/// use helix_tui::layout::Rect;
93−/// use helix_tui::style::{Color, Style, Modifier};
90+/// use helix_view::graphics::{Rect, Color, Style, Modifier};
9491///
9592/// let mut buf = Buffer::empty(Rect{x: 0, y: 0, width: 10, height: 5});
9693/// buf.get_mut(0, 2).set_symbol("x");
@@ -193,7 +190,7 @@ impl Buffer {
193190 ///
194191 /// ```
195192 /// # use helix_tui::buffer::Buffer;
196− /// # use helix_tui::layout::Rect;
193+ /// # use helix_view::graphics::Rect;
197194 /// let rect = Rect::new(200, 100, 10, 10);
198195 /// let buffer = Buffer::empty(rect);
199196 /// // Global coordinates to the top corner of this buffer's area
@@ -225,7 +222,7 @@ impl Buffer {
225222 ///
226223 /// ```
227224 /// # use helix_tui::buffer::Buffer;
228− /// # use helix_tui::layout::Rect;
225+ /// # use helix_view::graphics::Rect;
229226 /// let rect = Rect::new(200, 100, 10, 10);
230227 /// let buffer = Buffer::empty(rect);
231228 /// assert_eq!(buffer.pos_of(0), (200, 100));
helix-tui/src/layout.rs+4−163
@@ -1,11 +1,12 @@
11use std::cell::RefCell;
2−use std::cmp::{max, min};
32use std::collections::HashMap;
43
54use cassowary::strength::{REQUIRED, WEAK};
65use cassowary::WeightedRelation::*;
76use cassowary::{Constraint as CassowaryConstraint, Expression, Solver, Variable};
87
8+use helix_view::graphics::{Margin, Rect};
9+
910#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)]
1011pub enum Corner {
1112 TopLeft,
@@ -45,12 +46,6 @@ impl Constraint {
4546 }
4647}
4748
48−#[derive(Debug, Clone, PartialEq, Eq, Hash)]
49−pub struct Margin {
50− pub vertical: u16,
51− pub horizontal: u16,
52−}
53−
5449#[derive(Debug, Clone, Copy, PartialEq)]
5550pub enum Alignment {
5651 Left,
@@ -119,7 +114,8 @@ impl Layout {
119114 ///
120115 /// # Examples
121116 /// ```
122− /// # use helix_tui::layout::{Rect, Constraint, Direction, Layout};
117+ /// # use helix_tui::layout::{Constraint, Direction, Layout};
118+ /// # use helix_view::graphics::Rect;
123119 /// let chunks = Layout::default()
124120 /// .direction(Direction::Vertical)
125121 /// .constraints([Constraint::Length(5), Constraint::Min(0)].as_ref())
@@ -348,117 +344,6 @@ impl Element {
348344 }
349345}
350346
351−/// A simple rectangle used in the computation of the layout and to give widgets an hint about the
352−/// area they are supposed to render to.
353−#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
354−pub struct Rect {
355− pub x: u16,
356− pub y: u16,
357− pub width: u16,
358− pub height: u16,
359−}
360−
361−impl Default for Rect {
362− fn default() -> Rect {
363− Rect {
364− x: 0,
365− y: 0,
366− width: 0,
367− height: 0,
368− }
369− }
370−}
371−
372−impl Rect {
373− /// Creates a new rect, with width and height limited to keep the area under max u16.
374− /// If clipped, aspect ratio will be preserved.
375− pub fn new(x: u16, y: u16, width: u16, height: u16) -> Rect {
376− let max_area = u16::max_value();
377− let (clipped_width, clipped_height) =
378− if u32::from(width) * u32::from(height) > u32::from(max_area) {
379− let aspect_ratio = f64::from(width) / f64::from(height);
380− let max_area_f = f64::from(max_area);
381− let height_f = (max_area_f / aspect_ratio).sqrt();
382− let width_f = height_f * aspect_ratio;
383− (width_f as u16, height_f as u16)
384− } else {
385− (width, height)
386− };
387− Rect {
388− x,
389− y,
390− width: clipped_width,
391− height: clipped_height,
392− }
393− }
394−
395− pub fn area(self) -> u16 {
396− self.width * self.height
397− }
398−
399− pub fn left(self) -> u16 {
400− self.x
401− }
402−
403− pub fn right(self) -> u16 {
404− self.x.saturating_add(self.width)
405− }
406−
407− pub fn top(self) -> u16 {
408− self.y
409− }
410−
411− pub fn bottom(self) -> u16 {
412− self.y.saturating_add(self.height)
413− }
414−
415− pub fn inner(self, margin: &Margin) -> Rect {
416− if self.width < 2 * margin.horizontal || self.height < 2 * margin.vertical {
417− Rect::default()
418− } else {
419− Rect {
420− x: self.x + margin.horizontal,
421− y: self.y + margin.vertical,
422− width: self.width - 2 * margin.horizontal,
423− height: self.height - 2 * margin.vertical,
424− }
425− }
426− }
427−
428− pub fn union(self, other: Rect) -> Rect {
429− let x1 = min(self.x, other.x);
430− let y1 = min(self.y, other.y);
431− let x2 = max(self.x + self.width, other.x + other.width);
432− let y2 = max(self.y + self.height, other.y + other.height);
433− Rect {
434− x: x1,
435− y: y1,
436− width: x2 - x1,
437− height: y2 - y1,
438− }
439− }
440−
441− pub fn intersection(self, other: Rect) -> Rect {
442− let x1 = max(self.x, other.x);
443− let y1 = max(self.y, other.y);
444− let x2 = min(self.x + self.width, other.x + other.width);
445− let y2 = min(self.y + self.height, other.y + other.height);
446− Rect {
447− x: x1,
448− y: y1,
449− width: x2 - x1,
450− height: y2 - y1,
451− }
452− }
453−
454− pub fn intersects(self, other: Rect) -> bool {
455− self.x < other.x + other.width
456− && self.x + self.width > other.x
457− && self.y < other.y + other.height
458− && self.y + self.height > other.y
459− }
460−}
461−
462347#[cfg(test)]
463348mod tests {
464349 use super::*;
@@ -487,48 +372,4 @@ mod tests {
487372 assert_eq!(target.height, chunks.iter().map(|r| r.height).sum::<u16>());
488373 chunks.windows(2).for_each(|w| assert!(w[0].y <= w[1].y));
489374 }
490−
491− #[test]
492− fn test_rect_size_truncation() {
493− for width in 256u16..300u16 {
494− for height in 256u16..300u16 {
495− let rect = Rect::new(0, 0, width, height);
496− rect.area(); // Should not panic.
497− assert!(rect.width < width || rect.height < height);
498− // The target dimensions are rounded down so the math will not be too precise
499− // but let's make sure the ratios don't diverge crazily.
500− assert!(
501− (f64::from(rect.width) / f64::from(rect.height)
502− - f64::from(width) / f64::from(height))
503− .abs()
504− < 1.0
505− )
506− }
507− }
508−
509− // One dimension below 255, one above. Area above max u16.
510− let width = 900;
511− let height = 100;
512− let rect = Rect::new(0, 0, width, height);
513− assert_ne!(rect.width, 900);
514− assert_ne!(rect.height, 100);
515− assert!(rect.width < width || rect.height < height);
516− }
517−
518− #[test]
519− fn test_rect_size_preservation() {
520− for width in 0..256u16 {
521− for height in 0..256u16 {
522− let rect = Rect::new(0, 0, width, height);
523− rect.area(); // Should not panic.
524− assert_eq!(rect.width, width);
525− assert_eq!(rect.height, height);
526− }
527− }
528−
529− // One dimension below 255, one above. Area below max u16.
530− let rect = Rect::new(0, 0, 300, 100);
531− assert_eq!(rect.width, 300);
532− assert_eq!(rect.height, 100);
533− }
534375}
helix-tui/src/lib.rs−1
@@ -125,7 +125,6 @@
125125pub mod backend;
126126pub mod buffer;
127127pub mod layout;
128−pub mod style;
129128pub mod symbols;
130129pub mod terminal;
131130pub mod text;
helix-tui/src/terminal.rs+2−14
@@ -1,4 +1,5 @@
1−use crate::{backend::Backend, buffer::Buffer, layout::Rect};
1+use crate::{backend::Backend, buffer::Buffer};
2+use helix_view::graphics::{CursorKind, Rect};
23use std::io;
34
45#[derive(Debug, Clone, PartialEq)]
@@ -8,19 +9,6 @@ enum ResizeBehavior {
89 Auto,
910}
1011
11−#[derive(Debug)]
12−/// UNSTABLE
13−pub enum CursorKind {
14− /// █
15− Block,
16− /// |
17− Bar,
18− /// _
19− Underline,
20− /// Hidden cursor, can set cursor position with this to let IME have correct cursor position.
21− Hidden,
22−}
23−
2412#[derive(Debug, Clone, PartialEq)]
2513/// UNSTABLE
2614pub struct Viewport {
helix-tui/src/text.rs+8−8
@@ -21,7 +21,7 @@
2121//! ```rust
2222//! # use helix_tui::widgets::Block;
2323//! # use helix_tui::text::{Span, Spans};
24−//! # use helix_tui::style::{Color, Style};
24+//! # use helix_view::graphics::{Color, Style};
2525//! // A simple string with no styling.
2626//! // Converted to Spans(vec![
2727//! // Span { content: Cow::Borrowed("My title"), style: Style { .. } }
@@ -46,8 +46,8 @@
4646//! Span::raw(" title"),
4747//! ]);
4848//! ```
49−use crate::style::Style;
5049use helix_core::line_ending::str_is_line_ending;
50+use helix_view::graphics::Style;
5151use std::borrow::Cow;
5252use unicode_segmentation::UnicodeSegmentation;
5353use unicode_width::UnicodeWidthStr;
@@ -92,7 +92,7 @@ impl<'a> Span<'a> {
9292 ///
9393 /// ```rust
9494 /// # use helix_tui::text::Span;
95− /// # use helix_tui::style::{Color, Modifier, Style};
95+ /// # use helix_view::graphics::{Color, Modifier, Style};
9696 /// let style = Style::default().fg(Color::Yellow).add_modifier(Modifier::ITALIC);
9797 /// Span::styled("My text", style);
9898 /// Span::styled(String::from("My text"), style);
@@ -121,7 +121,7 @@ impl<'a> Span<'a> {
121121 ///
122122 /// ```rust
123123 /// # use helix_tui::text::{Span, StyledGrapheme};
124− /// # use helix_tui::style::{Color, Modifier, Style};
124+ /// # use helix_view::graphics::{Color, Modifier, Style};
125125 /// # use std::iter::Iterator;
126126 /// let style = Style::default().fg(Color::Yellow);
127127 /// let span = Span::styled("Text", style);
@@ -211,7 +211,7 @@ impl<'a> Spans<'a> {
211211 ///
212212 /// ```rust
213213 /// # use helix_tui::text::{Span, Spans};
214− /// # use helix_tui::style::{Color, Style};
214+ /// # use helix_view::graphics::{Color, Style};
215215 /// let spans = Spans::from(vec![
216216 /// Span::styled("My", Style::default().fg(Color::Yellow)),
217217 /// Span::raw(" text"),
@@ -265,7 +265,7 @@ impl<'a> From<Spans<'a>> for String {
265265///
266266/// ```rust
267267/// # use helix_tui::text::Text;
268−/// # use helix_tui::style::{Color, Modifier, Style};
268+/// # use helix_view::graphics::{Color, Modifier, Style};
269269/// let style = Style::default().fg(Color::Yellow).add_modifier(Modifier::ITALIC);
270270///
271271/// // An initial two lines of `Text` built from a `&str`
@@ -319,7 +319,7 @@ impl<'a> Text<'a> {
319319 ///
320320 /// ```rust
321321 /// # use helix_tui::text::Text;
322− /// # use helix_tui::style::{Color, Modifier, Style};
322+ /// # use helix_view::graphics::{Color, Modifier, Style};
323323 /// let style = Style::default().fg(Color::Yellow).add_modifier(Modifier::ITALIC);
324324 /// Text::styled("The first line\nThe second line", style);
325325 /// Text::styled(String::from("The first line\nThe second line"), style);
@@ -369,7 +369,7 @@ impl<'a> Text<'a> {
369369 ///
370370 /// ```rust
371371 /// # use helix_tui::text::Text;
372− /// # use helix_tui::style::{Color, Modifier, Style};
372+ /// # use helix_view::graphics::{Color, Modifier, Style};
373373 /// let style = Style::default().fg(Color::Yellow).add_modifier(Modifier::ITALIC);
374374 /// let mut raw_text = Text::raw("The first line\nThe second line");
375375 /// let styled_text = Text::styled(String::from("The first line\nThe second line"), style);
helix-tui/src/widgets/block.rs+2−4
@@ -1,11 +1,10 @@
11use crate::{
22 buffer::Buffer,
3− layout::Rect,
4− style::Style,
53 symbols::line,
64 text::{Span, Spans},
75 widgets::{Borders, Widget},
86};
7+use helix_view::graphics::{Rect, Style};
98
109#[derive(Debug, Clone, Copy, PartialEq)]
1110pub enum BorderType {
@@ -33,7 +32,7 @@ impl BorderType {
3332///
3433/// ```
3534/// # use helix_tui::widgets::{Block, BorderType, Borders};
36−/// # use helix_tui::style::{Style, Color};
35+/// # use helix_view::graphics::{Style, Color};
3736/// Block::default()
3837/// .title("Block")
3938/// .borders(Borders::LEFT | Borders::RIGHT)
@@ -212,7 +211,6 @@ impl<'a> Widget for Block<'a> {
212211#[cfg(test)]
213212mod tests {
214213 use super::*;
215− use crate::layout::Rect;
216214
217215 #[test]
218216 fn inner_takes_into_account_the_borders() {
helix-tui/src/widgets/mod.rs+3−1
@@ -20,9 +20,11 @@ pub use self::block::{Block, BorderType};
2020pub use self::paragraph::{Paragraph, Wrap};
2121pub use self::table::{Cell, Row, Table, TableState};
2222
23−use crate::{buffer::Buffer, layout::Rect};
23+use crate::buffer::Buffer;
2424use bitflags::bitflags;
2525
26+use helix_view::graphics::Rect;
27+
2628bitflags! {
2729 /// Bitflags that can be composed to set the visible borders essentially on the block widget.
2830 pub struct Borders: u32 {
helix-tui/src/widgets/paragraph.rs+3−3
@@ -1,13 +1,13 @@
11use crate::{
22 buffer::Buffer,
3− layout::{Alignment, Rect},
4− style::Style,
3+ layout::Alignment,
54 text::{StyledGrapheme, Text},
65 widgets::{
76 reflow::{LineComposer, LineTruncator, WordWrapper},
87 Block, Widget,
98 },
109};
10+use helix_view::graphics::{Rect, Style};
1111use std::iter;
1212use unicode_width::UnicodeWidthStr;
1313
@@ -26,8 +26,8 @@ fn get_line_offset(line_width: u16, text_area_width: u16, alignment: Alignment)
2626/// ```
2727/// # use helix_tui::text::{Text, Spans, Span};
2828/// # use helix_tui::widgets::{Block, Borders, Paragraph, Wrap};
29−/// # use helix_tui::style::{Style, Color, Modifier};
3029/// # use helix_tui::layout::{Alignment};
30+/// # use helix_view::graphics::{Style, Color, Modifier};
3131/// let text = vec![
3232/// Spans::from(vec![
3333/// Span::raw("First"),
helix-tui/src/widgets/table.rs+5−5
@@ -1,7 +1,6 @@
11use crate::{
22 buffer::Buffer,
3− layout::{Constraint, Rect},
4− style::Style,
3+ layout::Constraint,
54 text::Text,
65 widgets::{Block, Widget},
76};
@@ -10,6 +9,7 @@ use cassowary::{
109 WeightedRelation::*,
1110 {Expression, Solver},
1211};
12+use helix_view::graphics::{Rect, Style};
1313use std::{
1414 collections::HashMap,
1515 iter::{self, Iterator},
@@ -21,8 +21,8 @@ use unicode_width::UnicodeWidthStr;
2121/// It can be created from anything that can be converted to a [`Text`].
2222/// ```rust
2323/// # use helix_tui::widgets::Cell;
24−/// # use helix_tui::style::{Style, Modifier};
2524/// # use helix_tui::text::{Span, Spans, Text};
25+/// # use helix_view::graphics::{Style, Modifier};
2626/// Cell::from("simple string");
2727///
2828/// Cell::from(Span::from("span"));
@@ -74,7 +74,7 @@ where
7474/// But if you need a bit more control over individual cells, you can explicity create [`Cell`]s:
7575/// ```rust
7676/// # use helix_tui::widgets::{Row, Cell};
77−/// # use helix_tui::style::{Style, Color};
77+/// # use helix_view::graphics::{Style, Color};
7878/// Row::new(vec![
7979/// Cell::from("Cell1"),
8080/// Cell::from("Cell2").style(Style::default().fg(Color::Yellow)),
@@ -137,7 +137,7 @@ impl<'a> Row<'a> {
137137/// ```rust
138138/// # use helix_tui::widgets::{Block, Borders, Table, Row, Cell};
139139/// # use helix_tui::layout::Constraint;
140−/// # use helix_tui::style::{Style, Color, Modifier};
140+/// # use helix_view::graphics::{Style, Color, Modifier};
141141/// # use helix_tui::text::{Text, Spans, Span};
142142/// Table::new(vec![
143143/// // Row can be created from simple strings.
helix-view/Cargo.toml+7−6
@@ -9,20 +9,18 @@ categories = ["editor"]
99repository = "https://github.com/helix-editor/helix"
1010homepage = "https://helix-editor.com"
1111
12−# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
13−
1412[features]
15−term = ["tui", "crossterm"]
16−default = ["term"]
13+default = []
14+term = ["crossterm"]
1715
1816[dependencies]
17+bitflags = "1.0"
1918anyhow = "1"
2019helix-core = { version = "0.2", path = "../helix-core" }
2120helix-lsp = { version = "0.2", path = "../helix-lsp"}
21+crossterm = { version = "0.20", optional = true }
2222
2323# Conversion traits
24−tui = { path = "../helix-tui", package = "helix-tui", default-features = false, features = ["crossterm"], optional = true }
25−crossterm = { version = "0.20", features = ["event-stream"], optional = true }
2624once_cell = "1.8"
2725url = "2"
2826
@@ -39,3 +37,6 @@ toml = "0.5"
3937log = "~0.4"
4038
4139which = "4.1"
40+
41+[dev-dependencies]
42+helix-tui = { path = "../helix-tui" }
helix-view/src/editor.rs+4−5
@@ -1,12 +1,10 @@
1−use crate::clipboard::{get_clipboard_provider, ClipboardProvider};
21use crate::{
2+ clipboard::{get_clipboard_provider, ClipboardProvider},
3+ graphics::{CursorKind, Rect},
34 theme::{self, Theme},
45 tree::Tree,
56 Document, DocumentId, RegisterSelection, View, ViewId,
67};
7−use helix_core::syntax;
8−use tui::layout::Rect;
9−use tui::terminal::CursorKind;
108
119use futures_util::future;
1210use std::{path::PathBuf, sync::Arc, time::Duration};
@@ -17,6 +15,7 @@ use anyhow::Error;
1715
1816pub use helix_core::diagnostic::Severity;
1917pub use helix_core::register::Registers;
18+use helix_core::syntax;
2019use helix_core::Position;
2120
2221#[derive(Debug)]
@@ -45,7 +44,7 @@ pub enum Action {
4544
4645impl Editor {
4746 pub fn new(
48− mut area: tui::layout::Rect,
47+ mut area: Rect,
4948 themes: Arc<theme::Loader>,
5049 config_loader: Arc<syntax::Loader>,
5150 ) -> Self {
helix-tui/src/style.rs → helix-view/src/graphics.rsrenamed+481−281
@@ -1,281 +1,481 @@
1−//! `style` contains the primitives used to control how your user interface will look.
2−
3−use bitflags::bitflags;
4−
5−#[derive(Debug, Clone, Copy, PartialEq)]
6−#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7−pub enum Color {
8− Reset,
9− Black,
10− Red,
11− Green,
12− Yellow,
13− Blue,
14− Magenta,
15− Cyan,
16− Gray,
17− DarkGray,
18− LightRed,
19− LightGreen,
20− LightYellow,
21− LightBlue,
22− LightMagenta,
23− LightCyan,
24− White,
25− Rgb(u8, u8, u8),
26− Indexed(u8),
27−}
28−
29−bitflags! {
30− /// Modifier changes the way a piece of text is displayed.
31− ///
32− /// They are bitflags so they can easily be composed.
33− ///
34− /// ## Examples
35− ///
36− /// ```rust
37− /// # use helix_tui::style::Modifier;
38− ///
39− /// let m = Modifier::BOLD | Modifier::ITALIC;
40− /// ```
41− #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
42− pub struct Modifier: u16 {
43− const BOLD = 0b0000_0000_0001;
44− const DIM = 0b0000_0000_0010;
45− const ITALIC = 0b0000_0000_0100;
46− const UNDERLINED = 0b0000_0000_1000;
47− const SLOW_BLINK = 0b0000_0001_0000;
48− const RAPID_BLINK = 0b0000_0010_0000;
49− const REVERSED = 0b0000_0100_0000;
50− const HIDDEN = 0b0000_1000_0000;
51− const CROSSED_OUT = 0b0001_0000_0000;
52− }
53−}
54−
55−/// Style let you control the main characteristics of the displayed elements.
56−///
57−/// ```rust
58−/// # use helix_tui::style::{Color, Modifier, Style};
59−/// Style::default()
60−/// .fg(Color::Black)
61−/// .bg(Color::Green)
62−/// .add_modifier(Modifier::ITALIC | Modifier::BOLD);
63−/// ```
64−///
65−/// It represents an incremental change. If you apply the styles S1, S2, S3 to a cell of the
66−/// terminal buffer, the style of this cell will be the result of the merge of S1, S2 and S3, not
67−/// just S3.
68−///
69−/// ```rust
70−/// # use helix_tui::style::{Color, Modifier, Style};
71−/// # use helix_tui::buffer::Buffer;
72−/// # use helix_tui::layout::Rect;
73−/// let styles = [
74−/// Style::default().fg(Color::Blue).add_modifier(Modifier::BOLD | Modifier::ITALIC),
75−/// Style::default().bg(Color::Red),
76−/// Style::default().fg(Color::Yellow).remove_modifier(Modifier::ITALIC),
77−/// ];
78−/// let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 1));
79−/// for style in &styles {
80−/// buffer.get_mut(0, 0).set_style(*style);
81−/// }
82−/// assert_eq!(
83−/// Style {
84−/// fg: Some(Color::Yellow),
85−/// bg: Some(Color::Red),
86−/// add_modifier: Modifier::BOLD,
87−/// sub_modifier: Modifier::empty(),
88−/// },
89−/// buffer.get(0, 0).style(),
90−/// );
91−/// ```
92−///
93−/// The default implementation returns a `Style` that does not modify anything. If you wish to
94−/// reset all properties until that point use [`Style::reset`].
95−///
96−/// ```
97−/// # use helix_tui::style::{Color, Modifier, Style};
98−/// # use helix_tui::buffer::Buffer;
99−/// # use helix_tui::layout::Rect;
100−/// let styles = [
101−/// Style::default().fg(Color::Blue).add_modifier(Modifier::BOLD | Modifier::ITALIC),
102−/// Style::reset().fg(Color::Yellow),
103−/// ];
104−/// let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 1));
105−/// for style in &styles {
106−/// buffer.get_mut(0, 0).set_style(*style);
107−/// }
108−/// assert_eq!(
109−/// Style {
110−/// fg: Some(Color::Yellow),
111−/// bg: Some(Color::Reset),
112−/// add_modifier: Modifier::empty(),
113−/// sub_modifier: Modifier::empty(),
114−/// },
115−/// buffer.get(0, 0).style(),
116−/// );
117−/// ```
118−#[derive(Debug, Clone, Copy, PartialEq)]
119−#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
120−pub struct Style {
121− pub fg: Option<Color>,
122− pub bg: Option<Color>,
123− pub add_modifier: Modifier,
124− pub sub_modifier: Modifier,
125−}
126−
127−impl Default for Style {
128− fn default() -> Style {
129− Style {
130− fg: None,
131− bg: None,
132− add_modifier: Modifier::empty(),
133− sub_modifier: Modifier::empty(),
134− }
135− }
136−}
137−
138−impl Style {
139− /// Returns a `Style` resetting all properties.
140− pub fn reset() -> Style {
141− Style {
142− fg: Some(Color::Reset),
143− bg: Some(Color::Reset),
144− add_modifier: Modifier::empty(),
145− sub_modifier: Modifier::all(),
146− }
147− }
148−
149− /// Changes the foreground color.
150− ///
151− /// ## Examples
152− ///
153− /// ```rust
154− /// # use helix_tui::style::{Color, Style};
155− /// let style = Style::default().fg(Color::Blue);
156− /// let diff = Style::default().fg(Color::Red);
157− /// assert_eq!(style.patch(diff), Style::default().fg(Color::Red));
158− /// ```
159− pub fn fg(mut self, color: Color) -> Style {
160− self.fg = Some(color);
161− self
162− }
163−
164− /// Changes the background color.
165− ///
166− /// ## Examples
167− ///
168− /// ```rust
169− /// # use helix_tui::style::{Color, Style};
170− /// let style = Style::default().bg(Color::Blue);
171− /// let diff = Style::default().bg(Color::Red);
172− /// assert_eq!(style.patch(diff), Style::default().bg(Color::Red));
173− /// ```
174− pub fn bg(mut self, color: Color) -> Style {
175− self.bg = Some(color);
176− self
177− }
178−
179− /// Changes the text emphasis.
180− ///
181− /// When applied, it adds the given modifier to the `Style` modifiers.
182− ///
183− /// ## Examples
184− ///
185− /// ```rust
186− /// # use helix_tui::style::{Color, Modifier, Style};
187− /// let style = Style::default().add_modifier(Modifier::BOLD);
188− /// let diff = Style::default().add_modifier(Modifier::ITALIC);
189− /// let patched = style.patch(diff);
190− /// assert_eq!(patched.add_modifier, Modifier::BOLD | Modifier::ITALIC);
191− /// assert_eq!(patched.sub_modifier, Modifier::empty());
192− /// ```
193− pub fn add_modifier(mut self, modifier: Modifier) -> Style {
194− self.sub_modifier.remove(modifier);
195− self.add_modifier.insert(modifier);
196− self
197− }
198−
199− /// Changes the text emphasis.
200− ///
201− /// When applied, it removes the given modifier from the `Style` modifiers.
202− ///
203− /// ## Examples
204− ///
205− /// ```rust
206− /// # use helix_tui::style::{Color, Modifier, Style};
207− /// let style = Style::default().add_modifier(Modifier::BOLD | Modifier::ITALIC);
208− /// let diff = Style::default().remove_modifier(Modifier::ITALIC);
209− /// let patched = style.patch(diff);
210− /// assert_eq!(patched.add_modifier, Modifier::BOLD);
211− /// assert_eq!(patched.sub_modifier, Modifier::ITALIC);
212− /// ```
213− pub fn remove_modifier(mut self, modifier: Modifier) -> Style {
214− self.add_modifier.remove(modifier);
215− self.sub_modifier.insert(modifier);
216− self
217− }
218−
219− /// Results in a combined style that is equivalent to applying the two individual styles to
220− /// a style one after the other.
221− ///
222− /// ## Examples
223− /// ```
224− /// # use helix_tui::style::{Color, Modifier, Style};
225− /// let style_1 = Style::default().fg(Color::Yellow);
226− /// let style_2 = Style::default().bg(Color::Red);
227− /// let combined = style_1.patch(style_2);
228− /// assert_eq!(
229− /// Style::default().patch(style_1).patch(style_2),
230− /// Style::default().patch(combined));
231− /// ```
232− pub fn patch(mut self, other: Style) -> Style {
233− self.fg = other.fg.or(self.fg);
234− self.bg = other.bg.or(self.bg);
235−
236− self.add_modifier.remove(other.sub_modifier);
237− self.add_modifier.insert(other.add_modifier);
238− self.sub_modifier.remove(other.add_modifier);
239− self.sub_modifier.insert(other.sub_modifier);
240−
241− self
242− }
243−}
244−
245−#[cfg(test)]
246−mod tests {
247− use super::*;
248−
249− fn styles() -> Vec<Style> {
250− vec![
251− Style::default(),
252− Style::default().fg(Color::Yellow),
253− Style::default().bg(Color::Yellow),
254− Style::default().add_modifier(Modifier::BOLD),
255− Style::default().remove_modifier(Modifier::BOLD),
256− Style::default().add_modifier(Modifier::ITALIC),
257− Style::default().remove_modifier(Modifier::ITALIC),
258− Style::default().add_modifier(Modifier::ITALIC | Modifier::BOLD),
259− Style::default().remove_modifier(Modifier::ITALIC | Modifier::BOLD),
260− ]
261− }
262−
263− #[test]
264− fn combined_patch_gives_same_result_as_individual_patch() {
265− let styles = styles();
266− for &a in &styles {
267− for &b in &styles {
268− for &c in &styles {
269− for &d in &styles {
270− let combined = a.patch(b.patch(c.patch(d)));
271−
272− assert_eq!(
273− Style::default().patch(a).patch(b).patch(c).patch(d),
274− Style::default().patch(combined)
275− );
276− }
277− }
278− }
279− }
280− }
281−}
1+use bitflags::bitflags;
2+use std::cmp::{max, min};
3+
4+#[derive(Debug)]
5+/// UNSTABLE
6+pub enum CursorKind {
7+ /// █
8+ Block,
9+ /// |
10+ Bar,
11+ /// _
12+ Underline,
13+ /// Hidden cursor, can set cursor position with this to let IME have correct cursor position.
14+ Hidden,
15+}
16+
17+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
18+pub struct Margin {
19+ pub vertical: u16,
20+ pub horizontal: u16,
21+}
22+
23+/// A simple rectangle used in the computation of the layout and to give widgets an hint about the
24+/// area they are supposed to render to.
25+#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
26+pub struct Rect {
27+ pub x: u16,
28+ pub y: u16,
29+ pub width: u16,
30+ pub height: u16,
31+}
32+
33+impl Default for Rect {
34+ fn default() -> Rect {
35+ Rect {
36+ x: 0,
37+ y: 0,
38+ width: 0,
39+ height: 0,
40+ }
41+ }
42+}
43+
44+impl Rect {
45+ /// Creates a new rect, with width and height limited to keep the area under max u16.
46+ /// If clipped, aspect ratio will be preserved.
47+ pub fn new(x: u16, y: u16, width: u16, height: u16) -> Rect {
48+ let max_area = u16::max_value();
49+ let (clipped_width, clipped_height) =
50+ if u32::from(width) * u32::from(height) > u32::from(max_area) {
51+ let aspect_ratio = f64::from(width) / f64::from(height);
52+ let max_area_f = f64::from(max_area);
53+ let height_f = (max_area_f / aspect_ratio).sqrt();
54+ let width_f = height_f * aspect_ratio;
55+ (width_f as u16, height_f as u16)
56+ } else {
57+ (width, height)
58+ };
59+ Rect {
60+ x,
61+ y,
62+ width: clipped_width,
63+ height: clipped_height,
64+ }
65+ }
66+
67+ pub fn area(self) -> u16 {
68+ self.width * self.height
69+ }
70+
71+ pub fn left(self) -> u16 {
72+ self.x
73+ }
74+
75+ pub fn right(self) -> u16 {
76+ self.x.saturating_add(self.width)
77+ }
78+
79+ pub fn top(self) -> u16 {
80+ self.y
81+ }
82+
83+ pub fn bottom(self) -> u16 {
84+ self.y.saturating_add(self.height)
85+ }
86+
87+ pub fn inner(self, margin: &Margin) -> Rect {
88+ if self.width < 2 * margin.horizontal || self.height < 2 * margin.vertical {
89+ Rect::default()
90+ } else {
91+ Rect {
92+ x: self.x + margin.horizontal,
93+ y: self.y + margin.vertical,
94+ width: self.width - 2 * margin.horizontal,
95+ height: self.height - 2 * margin.vertical,
96+ }
97+ }
98+ }
99+
100+ pub fn union(self, other: Rect) -> Rect {
101+ let x1 = min(self.x, other.x);
102+ let y1 = min(self.y, other.y);
103+ let x2 = max(self.x + self.width, other.x + other.width);
104+ let y2 = max(self.y + self.height, other.y + other.height);
105+ Rect {
106+ x: x1,
107+ y: y1,
108+ width: x2 - x1,
109+ height: y2 - y1,
110+ }
111+ }
112+
113+ pub fn intersection(self, other: Rect) -> Rect {
114+ let x1 = max(self.x, other.x);
115+ let y1 = max(self.y, other.y);
116+ let x2 = min(self.x + self.width, other.x + other.width);
117+ let y2 = min(self.y + self.height, other.y + other.height);
118+ Rect {
119+ x: x1,
120+ y: y1,
121+ width: x2 - x1,
122+ height: y2 - y1,
123+ }
124+ }
125+
126+ pub fn intersects(self, other: Rect) -> bool {
127+ self.x < other.x + other.width
128+ && self.x + self.width > other.x
129+ && self.y < other.y + other.height
130+ && self.y + self.height > other.y
131+ }
132+}
133+
134+#[derive(Debug, Clone, Copy, PartialEq)]
135+#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
136+pub enum Color {
137+ Reset,
138+ Black,
139+ Red,
140+ Green,
141+ Yellow,
142+ Blue,
143+ Magenta,
144+ Cyan,
145+ Gray,
146+ DarkGray,
147+ LightRed,
148+ LightGreen,
149+ LightYellow,
150+ LightBlue,
151+ LightMagenta,
152+ LightCyan,
153+ White,
154+ Rgb(u8, u8, u8),
155+ Indexed(u8),
156+}
157+
158+#[cfg(feature = "term")]
159+impl From<Color> for crossterm::style::Color {
160+ fn from(color: Color) -> Self {
161+ use crossterm::style::Color as CColor;
162+
163+ match color {
164+ Color::Reset => CColor::Reset,
165+ Color::Black => CColor::Black,
166+ Color::Red => CColor::DarkRed,
167+ Color::Green => CColor::DarkGreen,
168+ Color::Yellow => CColor::DarkYellow,
169+ Color::Blue => CColor::DarkBlue,
170+ Color::Magenta => CColor::DarkMagenta,
171+ Color::Cyan => CColor::DarkCyan,
172+ Color::Gray => CColor::Grey,
173+ Color::DarkGray => CColor::DarkGrey,
174+ Color::LightRed => CColor::Red,
175+ Color::LightGreen => CColor::Green,
176+ Color::LightBlue => CColor::Blue,
177+ Color::LightYellow => CColor::Yellow,
178+ Color::LightMagenta => CColor::Magenta,
179+ Color::LightCyan => CColor::Cyan,
180+ Color::White => CColor::White,
181+ Color::Indexed(i) => CColor::AnsiValue(i),
182+ Color::Rgb(r, g, b) => CColor::Rgb { r, g, b },
183+ }
184+ }
185+}
186+
187+bitflags! {
188+ /// Modifier changes the way a piece of text is displayed.
189+ ///
190+ /// They are bitflags so they can easily be composed.
191+ ///
192+ /// ## Examples
193+ ///
194+ /// ```rust
195+ /// # use helix_view::graphics::Modifier;
196+ ///
197+ /// let m = Modifier::BOLD | Modifier::ITALIC;
198+ /// ```
199+ #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
200+ pub struct Modifier: u16 {
201+ const BOLD = 0b0000_0000_0001;
202+ const DIM = 0b0000_0000_0010;
203+ const ITALIC = 0b0000_0000_0100;
204+ const UNDERLINED = 0b0000_0000_1000;
205+ const SLOW_BLINK = 0b0000_0001_0000;
206+ const RAPID_BLINK = 0b0000_0010_0000;
207+ const REVERSED = 0b0000_0100_0000;
208+ const HIDDEN = 0b0000_1000_0000;
209+ const CROSSED_OUT = 0b0001_0000_0000;
210+ }
211+}
212+
213+/// Style let you control the main characteristics of the displayed elements.
214+///
215+/// ```rust
216+/// # use helix_view::graphics::{Color, Modifier, Style};
217+/// Style::default()
218+/// .fg(Color::Black)
219+/// .bg(Color::Green)
220+/// .add_modifier(Modifier::ITALIC | Modifier::BOLD);
221+/// ```
222+///
223+/// It represents an incremental change. If you apply the styles S1, S2, S3 to a cell of the
224+/// terminal buffer, the style of this cell will be the result of the merge of S1, S2 and S3, not
225+/// just S3.
226+///
227+/// ```rust
228+/// # use helix_view::graphics::{Rect, Color, Modifier, Style};
229+/// # use helix_tui::buffer::Buffer;
230+/// let styles = [
231+/// Style::default().fg(Color::Blue).add_modifier(Modifier::BOLD | Modifier::ITALIC),
232+/// Style::default().bg(Color::Red),
233+/// Style::default().fg(Color::Yellow).remove_modifier(Modifier::ITALIC),
234+/// ];
235+/// let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 1));
236+/// for style in &styles {
237+/// buffer.get_mut(0, 0).set_style(*style);
238+/// }
239+/// assert_eq!(
240+/// Style {
241+/// fg: Some(Color::Yellow),
242+/// bg: Some(Color::Red),
243+/// add_modifier: Modifier::BOLD,
244+/// sub_modifier: Modifier::empty(),
245+/// },
246+/// buffer.get(0, 0).style(),
247+/// );
248+/// ```
249+///
250+/// The default implementation returns a `Style` that does not modify anything. If you wish to
251+/// reset all properties until that point use [`Style::reset`].
252+///
253+/// ```
254+/// # use helix_view::graphics::{Rect, Color, Modifier, Style};
255+/// # use helix_tui::buffer::Buffer;
256+/// let styles = [
257+/// Style::default().fg(Color::Blue).add_modifier(Modifier::BOLD | Modifier::ITALIC),
258+/// Style::reset().fg(Color::Yellow),
259+/// ];
260+/// let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 1));
261+/// for style in &styles {
262+/// buffer.get_mut(0, 0).set_style(*style);
263+/// }
264+/// assert_eq!(
265+/// Style {
266+/// fg: Some(Color::Yellow),
267+/// bg: Some(Color::Reset),
268+/// add_modifier: Modifier::empty(),
269+/// sub_modifier: Modifier::empty(),
270+/// },
271+/// buffer.get(0, 0).style(),
272+/// );
273+/// ```
274+#[derive(Debug, Clone, Copy, PartialEq)]
275+#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
276+pub struct Style {
277+ pub fg: Option<Color>,
278+ pub bg: Option<Color>,
279+ pub add_modifier: Modifier,
280+ pub sub_modifier: Modifier,
281+}
282+
283+impl Default for Style {
284+ fn default() -> Style {
285+ Style {
286+ fg: None,
287+ bg: None,
288+ add_modifier: Modifier::empty(),
289+ sub_modifier: Modifier::empty(),
290+ }
291+ }
292+}
293+
294+impl Style {
295+ /// Returns a `Style` resetting all properties.
296+ pub fn reset() -> Style {
297+ Style {
298+ fg: Some(Color::Reset),
299+ bg: Some(Color::Reset),
300+ add_modifier: Modifier::empty(),
301+ sub_modifier: Modifier::all(),
302+ }
303+ }
304+
305+ /// Changes the foreground color.
306+ ///
307+ /// ## Examples
308+ ///
309+ /// ```rust
310+ /// # use helix_view::graphics::{Color, Style};
311+ /// let style = Style::default().fg(Color::Blue);
312+ /// let diff = Style::default().fg(Color::Red);
313+ /// assert_eq!(style.patch(diff), Style::default().fg(Color::Red));
314+ /// ```
315+ pub fn fg(mut self, color: Color) -> Style {
316+ self.fg = Some(color);
317+ self
318+ }
319+
320+ /// Changes the background color.
321+ ///
322+ /// ## Examples
323+ ///
324+ /// ```rust
325+ /// # use helix_view::graphics::{Color, Style};
326+ /// let style = Style::default().bg(Color::Blue);
327+ /// let diff = Style::default().bg(Color::Red);
328+ /// assert_eq!(style.patch(diff), Style::default().bg(Color::Red));
329+ /// ```
330+ pub fn bg(mut self, color: Color) -> Style {
331+ self.bg = Some(color);
332+ self
333+ }
334+
335+ /// Changes the text emphasis.
336+ ///
337+ /// When applied, it adds the given modifier to the `Style` modifiers.
338+ ///
339+ /// ## Examples
340+ ///
341+ /// ```rust
342+ /// # use helix_view::graphics::{Color, Modifier, Style};
343+ /// let style = Style::default().add_modifier(Modifier::BOLD);
344+ /// let diff = Style::default().add_modifier(Modifier::ITALIC);
345+ /// let patched = style.patch(diff);
346+ /// assert_eq!(patched.add_modifier, Modifier::BOLD | Modifier::ITALIC);
347+ /// assert_eq!(patched.sub_modifier, Modifier::empty());
348+ /// ```
349+ pub fn add_modifier(mut self, modifier: Modifier) -> Style {
350+ self.sub_modifier.remove(modifier);
351+ self.add_modifier.insert(modifier);
352+ self
353+ }
354+
355+ /// Changes the text emphasis.
356+ ///
357+ /// When applied, it removes the given modifier from the `Style` modifiers.
358+ ///
359+ /// ## Examples
360+ ///
361+ /// ```rust
362+ /// # use helix_view::graphics::{Color, Modifier, Style};
363+ /// let style = Style::default().add_modifier(Modifier::BOLD | Modifier::ITALIC);
364+ /// let diff = Style::default().remove_modifier(Modifier::ITALIC);
365+ /// let patched = style.patch(diff);
366+ /// assert_eq!(patched.add_modifier, Modifier::BOLD);
367+ /// assert_eq!(patched.sub_modifier, Modifier::ITALIC);
368+ /// ```
369+ pub fn remove_modifier(mut self, modifier: Modifier) -> Style {
370+ self.add_modifier.remove(modifier);
371+ self.sub_modifier.insert(modifier);
372+ self
373+ }
374+
375+ /// Results in a combined style that is equivalent to applying the two individual styles to
376+ /// a style one after the other.
377+ ///
378+ /// ## Examples
379+ /// ```
380+ /// # use helix_view::graphics::{Color, Modifier, Style};
381+ /// let style_1 = Style::default().fg(Color::Yellow);
382+ /// let style_2 = Style::default().bg(Color::Red);
383+ /// let combined = style_1.patch(style_2);
384+ /// assert_eq!(
385+ /// Style::default().patch(style_1).patch(style_2),
386+ /// Style::default().patch(combined));
387+ /// ```
388+ pub fn patch(mut self, other: Style) -> Style {
389+ self.fg = other.fg.or(self.fg);
390+ self.bg = other.bg.or(self.bg);
391+
392+ self.add_modifier.remove(other.sub_modifier);
393+ self.add_modifier.insert(other.add_modifier);
394+ self.sub_modifier.remove(other.add_modifier);
395+ self.sub_modifier.insert(other.sub_modifier);
396+
397+ self
398+ }
399+}
400+
401+#[cfg(test)]
402+mod tests {
403+ use super::*;
404+
405+ #[test]
406+ fn test_rect_size_truncation() {
407+ for width in 256u16..300u16 {
408+ for height in 256u16..300u16 {
409+ let rect = Rect::new(0, 0, width, height);
410+ rect.area(); // Should not panic.
411+ assert!(rect.width < width || rect.height < height);
412+ // The target dimensions are rounded down so the math will not be too precise
413+ // but let's make sure the ratios don't diverge crazily.
414+ assert!(
415+ (f64::from(rect.width) / f64::from(rect.height)
416+ - f64::from(width) / f64::from(height))
417+ .abs()
418+ < 1.0
419+ )
420+ }
421+ }
422+
423+ // One dimension below 255, one above. Area above max u16.
424+ let width = 900;
425+ let height = 100;
426+ let rect = Rect::new(0, 0, width, height);
427+ assert_ne!(rect.width, 900);
428+ assert_ne!(rect.height, 100);
429+ assert!(rect.width < width || rect.height < height);
430+ }
431+
432+ #[test]
433+ fn test_rect_size_preservation() {
434+ for width in 0..256u16 {
435+ for height in 0..256u16 {
436+ let rect = Rect::new(0, 0, width, height);
437+ rect.area(); // Should not panic.
438+ assert_eq!(rect.width, width);
439+ assert_eq!(rect.height, height);
440+ }
441+ }
442+
443+ // One dimension below 255, one above. Area below max u16.
444+ let rect = Rect::new(0, 0, 300, 100);
445+ assert_eq!(rect.width, 300);
446+ assert_eq!(rect.height, 100);
447+ }
448+
449+ fn styles() -> Vec<Style> {
450+ vec![
451+ Style::default(),
452+ Style::default().fg(Color::Yellow),
453+ Style::default().bg(Color::Yellow),
454+ Style::default().add_modifier(Modifier::BOLD),
455+ Style::default().remove_modifier(Modifier::BOLD),
456+ Style::default().add_modifier(Modifier::ITALIC),
457+ Style::default().remove_modifier(Modifier::ITALIC),
458+ Style::default().add_modifier(Modifier::ITALIC | Modifier::BOLD),
459+ Style::default().remove_modifier(Modifier::ITALIC | Modifier::BOLD),
460+ ]
461+ }
462+
463+ #[test]
464+ fn combined_patch_gives_same_result_as_individual_patch() {
465+ let styles = styles();
466+ for &a in &styles {
467+ for &b in &styles {
468+ for &c in &styles {
469+ for &d in &styles {
470+ let combined = a.patch(b.patch(c.patch(d)));
471+
472+ assert_eq!(
473+ Style::default().patch(a).patch(b).patch(c).patch(d),
474+ Style::default().patch(combined)
475+ );
476+ }
477+ }
478+ }
479+ }
480+ }
481+}
helix-view/src/input.rs+10−5
@@ -1,10 +1,9 @@
11//! Input event handling, currently backed by crossterm.
22use anyhow::{anyhow, Error};
3−use crossterm::event;
43use serde::de::{self, Deserialize, Deserializer};
54use std::fmt;
65
7−pub use crossterm::event::{KeyCode, KeyModifiers};
6+use crate::keyboard::{KeyCode, KeyModifiers};
87
98/// Represents a key event.
109// We use a newtype here because we want to customize Deserialize and Display.
@@ -132,9 +131,15 @@ impl<'de> Deserialize<'de> for KeyEvent {
132131 }
133132}
134133
135−impl From<event::KeyEvent> for KeyEvent {
136− fn from(event::KeyEvent { code, modifiers }: event::KeyEvent) -> KeyEvent {
137− KeyEvent { code, modifiers }
134+#[cfg(feature = "term")]
135+impl From<crossterm::event::KeyEvent> for KeyEvent {
136+ fn from(
137+ crossterm::event::KeyEvent { code, modifiers }: crossterm::event::KeyEvent,
138+ ) -> KeyEvent {
139+ KeyEvent {
140+ code: code.into(),
141+ modifiers: modifiers.into(),
142+ }
138143 }
139144}
140145
helix-view/src/keyboard.rsnew file+160
@@ -0,0 +1,160 @@
1+use bitflags::bitflags;
2+
3+bitflags! {
4+ /// Represents key modifiers (shift, control, alt).
5+ #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6+ pub struct KeyModifiers: u8 {
7+ const SHIFT = 0b0000_0001;
8+ const CONTROL = 0b0000_0010;
9+ const ALT = 0b0000_0100;
10+ const NONE = 0b0000_0000;
11+ }
12+}
13+
14+#[cfg(feature = "term")]
15+impl From<KeyModifiers> for crossterm::event::KeyModifiers {
16+ fn from(key_modifiers: KeyModifiers) -> Self {
17+ use crossterm::event::KeyModifiers as CKeyModifiers;
18+
19+ let mut result = CKeyModifiers::NONE;
20+
21+ if key_modifiers & KeyModifiers::SHIFT != KeyModifiers::NONE {
22+ result &= CKeyModifiers::SHIFT;
23+ }
24+
25+ if key_modifiers & KeyModifiers::CONTROL != KeyModifiers::NONE {
26+ result &= CKeyModifiers::CONTROL;
27+ }
28+
29+ if key_modifiers & KeyModifiers::ALT != KeyModifiers::NONE {
30+ result &= CKeyModifiers::ALT;
31+ }
32+
33+ result
34+ }
35+}
36+
37+#[cfg(feature = "term")]
38+impl From<crossterm::event::KeyModifiers> for KeyModifiers {
39+ fn from(val: crossterm::event::KeyModifiers) -> Self {
40+ use crossterm::event::KeyModifiers as CKeyModifiers;
41+
42+ let mut result = KeyModifiers::NONE;
43+
44+ if val & CKeyModifiers::SHIFT != CKeyModifiers::NONE {
45+ result &= KeyModifiers::SHIFT;
46+ }
47+
48+ if val & CKeyModifiers::CONTROL != CKeyModifiers::NONE {
49+ result &= KeyModifiers::CONTROL;
50+ }
51+
52+ if val & CKeyModifiers::ALT != CKeyModifiers::NONE {
53+ result &= KeyModifiers::ALT;
54+ }
55+
56+ result
57+ }
58+}
59+
60+/// Represents a key.
61+#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
62+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
63+pub enum KeyCode {
64+ /// Backspace key.
65+ Backspace,
66+ /// Enter key.
67+ Enter,
68+ /// Left arrow key.
69+ Left,
70+ /// Right arrow key.
71+ Right,
72+ /// Up arrow key.
73+ Up,
74+ /// Down arrow key.
75+ Down,
76+ /// Home key.
77+ Home,
78+ /// End key.
79+ End,
80+ /// Page up key.
81+ PageUp,
82+ /// Page dow key.
83+ PageDown,
84+ /// Tab key.
85+ Tab,
86+ /// Shift + Tab key.
87+ BackTab,
88+ /// Delete key.
89+ Delete,
90+ /// Insert key.
91+ Insert,
92+ /// F key.
93+ ///
94+ /// `KeyCode::F(1)` represents F1 key, etc.
95+ F(u8),
96+ /// A character.
97+ ///
98+ /// `KeyCode::Char('c')` represents `c` character, etc.
99+ Char(char),
100+ /// Null.
101+ Null,
102+ /// Escape key.
103+ Esc,
104+}
105+
106+#[cfg(feature = "term")]
107+impl From<KeyCode> for crossterm::event::KeyCode {
108+ fn from(key_code: KeyCode) -> Self {
109+ use crossterm::event::KeyCode as CKeyCode;
110+
111+ match key_code {
112+ KeyCode::Backspace => CKeyCode::Backspace,
113+ KeyCode::Enter => CKeyCode::Enter,
114+ KeyCode::Left => CKeyCode::Left,
115+ KeyCode::Right => CKeyCode::Right,
116+ KeyCode::Up => CKeyCode::Up,
117+ KeyCode::Down => CKeyCode::Down,
118+ KeyCode::Home => CKeyCode::Home,
119+ KeyCode::End => CKeyCode::End,
120+ KeyCode::PageUp => CKeyCode::PageUp,
121+ KeyCode::PageDown => CKeyCode::PageDown,
122+ KeyCode::Tab => CKeyCode::Tab,
123+ KeyCode::BackTab => CKeyCode::BackTab,
124+ KeyCode::Delete => CKeyCode::Delete,
125+ KeyCode::Insert => CKeyCode::Insert,
126+ KeyCode::F(f_number) => CKeyCode::F(f_number),
127+ KeyCode::Char(character) => CKeyCode::Char(character),
128+ KeyCode::Null => CKeyCode::Null,
129+ KeyCode::Esc => CKeyCode::Esc,
130+ }
131+ }
132+}
133+
134+#[cfg(feature = "term")]
135+impl From<crossterm::event::KeyCode> for KeyCode {
136+ fn from(val: crossterm::event::KeyCode) -> Self {
137+ use crossterm::event::KeyCode as CKeyCode;
138+
139+ match val {
140+ CKeyCode::Backspace => KeyCode::Backspace,
141+ CKeyCode::Enter => KeyCode::Enter,
142+ CKeyCode::Left => KeyCode::Left,
143+ CKeyCode::Right => KeyCode::Right,
144+ CKeyCode::Up => KeyCode::Up,
145+ CKeyCode::Down => KeyCode::Down,
146+ CKeyCode::Home => KeyCode::Home,
147+ CKeyCode::End => KeyCode::End,
148+ CKeyCode::PageUp => KeyCode::PageUp,
149+ CKeyCode::PageDown => KeyCode::PageDown,
150+ CKeyCode::Tab => KeyCode::Tab,
151+ CKeyCode::BackTab => KeyCode::BackTab,
152+ CKeyCode::Delete => KeyCode::Delete,
153+ CKeyCode::Insert => KeyCode::Insert,
154+ CKeyCode::F(f_number) => KeyCode::F(f_number),
155+ CKeyCode::Char(character) => KeyCode::Char(character),
156+ CKeyCode::Null => KeyCode::Null,
157+ CKeyCode::Esc => KeyCode::Esc,
158+ }
159+ }
160+}
helix-view/src/lib.rs+2
@@ -4,7 +4,9 @@ pub mod macros;
44pub mod clipboard;
55pub mod document;
66pub mod editor;
7+pub mod graphics;
78pub mod input;
9+pub mod keyboard;
810pub mod register_selection;
911pub mod theme;
1012pub mod tree;
helix-view/src/theme.rs+1−80
@@ -9,86 +9,7 @@ use once_cell::sync::Lazy;
99use serde::{Deserialize, Deserializer};
1010use toml::Value;
1111
12−#[cfg(feature = "term")]
13−pub use tui::style::{Color, Modifier, Style};
14−
15−// #[derive(Clone, Copy, PartialEq, Eq, Default, Hash)]
16−// pub struct Color {
17−// pub r: u8,
18−// pub g: u8,
19−// pub b: u8,
20−// }
21−
22−// impl Color {
23−// pub fn new(r: u8, g: u8, b: u8) -> Self {
24−// Self { r, g, b }
25−// }
26−// }
27−
28−// #[cfg(feature = "term")]
29−// impl Into<tui::style::Color> for Color {
30−// fn into(self) -> tui::style::Color {
31−// tui::style::Color::Rgb(self.r, self.g, self.b)
32−// }
33−// }
34−
35−// impl std::str::FromStr for Color {
36−// type Err = ();
37−
38−// /// Tries to parse a string (`'#FFFFFF'` or `'FFFFFF'`) into RGB.
39−// fn from_str(input: &str) -> Result<Self, Self::Err> {
40−// let input = input.trim();
41−// let input = match (input.chars().next(), input.len()) {
42−// (Some('#'), 7) => &input[1..],
43−// (_, 6) => input,
44−// _ => return Err(()),
45−// };
46−
47−// u32::from_str_radix(&input, 16)
48−// .map(|s| Color {
49−// r: ((s >> 16) & 0xFF) as u8,
50−// g: ((s >> 8) & 0xFF) as u8,
51−// b: (s & 0xFF) as u8,
52−// })
53−// .map_err(|_| ())
54−// }
55−// }
56−
57−// #[derive(Clone, Copy, PartialEq, Eq, Default, Hash)]
58−// pub struct Style {
59−// pub fg: Option<Color>,
60−// pub bg: Option<Color>,
61−// // TODO: modifiers (bold, underline, italic, etc)
62−// }
63−
64−// impl Style {
65−// pub fn fg(mut self, fg: Color) -> Self {
66−// self.fg = Some(fg);
67−// self
68−// }
69−
70−// pub fn bg(mut self, bg: Color) -> Self {
71−// self.bg = Some(bg);
72−// self
73−// }
74−// }
75−
76−// #[cfg(feature = "term")]
77−// impl Into<tui::style::Style> for Style {
78−// fn into(self) -> tui::style::Style {
79−// let style = tui::style::Style::default();
80−
81−// if let Some(fg) = self.fg {
82−// style.fg(fg.into());
83−// }
84−
85−// if let Some(bg) = self.bg {
86−// style.bg(bg.into());
87−// }
88−
89−// style
90−// }
91−// }
12+pub use crate::graphics::{Color, Modifier, Style};
9213
9314/// Color theme for syntax highlighting.
9415
helix-view/src/tree.rs+1−2
@@ -1,6 +1,5 @@
1−use crate::{View, ViewId};
1+use crate::{graphics::Rect, View, ViewId};
22use slotmap::HopSlotMap;
3−use tui::layout::Rect;
43
54// the dimensions are recomputed on windo resize/tree change.
65//
helix-view/src/view.rs+1−2
@@ -1,12 +1,11 @@
11use std::borrow::Cow;
22
3−use crate::{Document, DocumentId, ViewId};
3+use crate::{graphics::Rect, Document, DocumentId, ViewId};
44use helix_core::{
55 coords_at_pos,
66 graphemes::{grapheme_width, RopeGraphemes},
77 Position, RopeSlice, Selection,
88};
9−use tui::layout::Rect;
109
1110pub const PADDING: usize = 5;
1211