on 2158366 help sign in
helix/helix-tui/src/backend/crossterm.rs
197 lines 6.3 kB history raw
1use crate::{backend::Backend, buffer::Cell};
2use crossterm::{
3 cursor::{CursorShape, Hide, MoveTo, SetCursorShape, Show},
4 execute, queue,
5 style::{
6 Attribute as CAttribute, Color as CColor, Print, SetAttribute, SetBackgroundColor,
7 SetForegroundColor,
8 },
9 terminal::{self, Clear, ClearType},
10};
11use helix_view::graphics::{Color, CursorKind, Modifier, Rect};
12use std::io::{self, Write};
14pub struct CrosstermBackend<W: Write> {
15 buffer: W,
18impl<W> CrosstermBackend<W>
19where
20 W: Write,
22 pub fn new(buffer: W) -> CrosstermBackend<W> {
23 CrosstermBackend { buffer }
24 }
27impl<W> Write for CrosstermBackend<W>
28where
29 W: Write,
31 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
32 self.buffer.write(buf)
33 }
35 fn flush(&mut self) -> io::Result<()> {
36 self.buffer.flush()
37 }
40impl<W> Backend for CrosstermBackend<W>
41where
42 W: Write,
44 fn draw<'a, I>(&mut self, content: I) -> io::Result<()>
45 where
46 I: Iterator<Item = (u16, u16, &'a Cell)>,
47 {
48 let mut fg = Color::Reset;
49 let mut bg = Color::Reset;
50 let mut modifier = Modifier::empty();
51 let mut last_pos: Option<(u16, u16)> = None;
52 for (x, y, cell) in content {
53 // Move the cursor if the previous location was not (x - 1, y)
54 if !matches!(last_pos, Some(p) if x == p.0 + 1 && y == p.1) {
55 map_error(queue!(self.buffer, MoveTo(x, y)))?;
56 }
57 last_pos = Some((x, y));
58 if cell.modifier != modifier {
59 let diff = ModifierDiff {
60 from: modifier,
61 to: cell.modifier,
62 };
63 diff.queue(&mut self.buffer)?;
64 modifier = cell.modifier;
65 }
66 if cell.fg != fg {
67 let color = CColor::from(cell.fg);
68 map_error(queue!(self.buffer, SetForegroundColor(color)))?;
69 fg = cell.fg;
70 }
71 if cell.bg != bg {
72 let color = CColor::from(cell.bg);
73 map_error(queue!(self.buffer, SetBackgroundColor(color)))?;
74 bg = cell.bg;
75 }
77 map_error(queue!(self.buffer, Print(&cell.symbol)))?;
78 }
80 map_error(queue!(
81 self.buffer,
82 SetForegroundColor(CColor::Reset),
83 SetBackgroundColor(CColor::Reset),
84 SetAttribute(CAttribute::Reset)
85 ))
86 }
88 fn hide_cursor(&mut self) -> io::Result<()> {
89 map_error(execute!(self.buffer, Hide))
90 }
92 fn show_cursor(&mut self, kind: CursorKind) -> io::Result<()> {
93 let shape = match kind {
94 CursorKind::Block => CursorShape::Block,
95 CursorKind::Bar => CursorShape::Line,
96 CursorKind::Underline => CursorShape::UnderScore,
97 CursorKind::Hidden => unreachable!(),
98 };
99 map_error(execute!(self.buffer, Show, SetCursorShape(shape)))
100 }
102 fn get_cursor(&mut self) -> io::Result<(u16, u16)> {
103 crossterm::cursor::position()
104 .map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))
105 }
107 fn set_cursor(&mut self, x: u16, y: u16) -> io::Result<()> {
108 map_error(execute!(self.buffer, MoveTo(x, y)))
109 }
111 fn clear(&mut self) -> io::Result<()> {
112 map_error(execute!(self.buffer, Clear(ClearType::All)))
113 }
115 fn size(&self) -> io::Result<Rect> {
116 let (width, height) =
117 terminal::size().map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?;
119 Ok(Rect::new(0, 0, width, height))
120 }
122 fn flush(&mut self) -> io::Result<()> {
123 self.buffer.flush()
124 }
127fn map_error(error: crossterm::Result<()>) -> io::Result<()> {
128 error.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))
131#[derive(Debug)]
132struct ModifierDiff {
133 pub from: Modifier,
134 pub to: Modifier,
137impl ModifierDiff {
138 fn queue<W>(&self, mut w: W) -> io::Result<()>
139 where
140 W: io::Write,
141 {
142 //use crossterm::Attribute;
143 let removed = self.from - self.to;
144 if removed.contains(Modifier::REVERSED) {
145 map_error(queue!(w, SetAttribute(CAttribute::NoReverse)))?;
146 }
147 if removed.contains(Modifier::BOLD) {
148 map_error(queue!(w, SetAttribute(CAttribute::NormalIntensity)))?;
149 if self.to.contains(Modifier::DIM) {
150 map_error(queue!(w, SetAttribute(CAttribute::Dim)))?;
151 }
152 }
153 if removed.contains(Modifier::ITALIC) {
154 map_error(queue!(w, SetAttribute(CAttribute::NoItalic)))?;
155 }
156 if removed.contains(Modifier::UNDERLINED) {
157 map_error(queue!(w, SetAttribute(CAttribute::NoUnderline)))?;
158 }
159 if removed.contains(Modifier::DIM) {
160 map_error(queue!(w, SetAttribute(CAttribute::NormalIntensity)))?;
161 }
162 if removed.contains(Modifier::CROSSED_OUT) {
163 map_error(queue!(w, SetAttribute(CAttribute::NotCrossedOut)))?;
164 }
165 if removed.contains(Modifier::SLOW_BLINK) || removed.contains(Modifier::RAPID_BLINK) {
166 map_error(queue!(w, SetAttribute(CAttribute::NoBlink)))?;
167 }
169 let added = self.to - self.from;
170 if added.contains(Modifier::REVERSED) {
171 map_error(queue!(w, SetAttribute(CAttribute::Reverse)))?;
172 }
173 if added.contains(Modifier::BOLD) {
174 map_error(queue!(w, SetAttribute(CAttribute::Bold)))?;
175 }
176 if added.contains(Modifier::ITALIC) {
177 map_error(queue!(w, SetAttribute(CAttribute::Italic)))?;
178 }
179 if added.contains(Modifier::UNDERLINED) {
180 map_error(queue!(w, SetAttribute(CAttribute::Underlined)))?;
181 }
182 if added.contains(Modifier::DIM) {
183 map_error(queue!(w, SetAttribute(CAttribute::Dim)))?;
184 }
185 if added.contains(Modifier::CROSSED_OUT) {
186 map_error(queue!(w, SetAttribute(CAttribute::CrossedOut)))?;
187 }
188 if added.contains(Modifier::SLOW_BLINK) {
189 map_error(queue!(w, SetAttribute(CAttribute::SlowBlink)))?;
190 }
191 if added.contains(Modifier::RAPID_BLINK) {
192 map_error(queue!(w, SetAttribute(CAttribute::RapidBlink)))?;
193 }
195 Ok(())
196 }