1use crate::{backend::Backend, buffer::Cell};
3 cursor::{CursorShape, Hide, MoveTo, SetCursorShape, Show},
6 Attribute as CAttribute, Color as CColor, Print, SetAttribute, SetBackgroundColor,
9 terminal::{self, Clear, ClearType},
11use helix_view::graphics::{Color, CursorKind, Modifier, Rect};
12use std::io::{self, Write};
14pub struct CrosstermBackend<W: Write> {
18impl<W> CrosstermBackend<W>
22 pub fn new(buffer: W) -> CrosstermBackend<W> {
23 CrosstermBackend { buffer }
27impl<W> Write for CrosstermBackend<W>
31 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
32 self.buffer.write(buf)
35 fn flush(&mut self) -> io::Result<()> {
40impl<W> Backend for CrosstermBackend<W>
44 fn draw<'a, I>(&mut self, content: I) -> io::Result<()>
46 I: Iterator<Item = (u16, u16, &'a Cell)>,
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)))?;
57 last_pos = Some((x, y));
58 if cell.modifier != modifier {
59 let diff = ModifierDiff {
63 diff.queue(&mut self.buffer)?;
64 modifier = cell.modifier;
67 let color = CColor::from(cell.fg);
68 map_error(queue!(self.buffer, SetForegroundColor(color)))?;
72 let color = CColor::from(cell.bg);
73 map_error(queue!(self.buffer, SetBackgroundColor(color)))?;
77 map_error(queue!(self.buffer, Print(&cell.symbol)))?;
82 SetForegroundColor(CColor::Reset),
83 SetBackgroundColor(CColor::Reset),
84 SetAttribute(CAttribute::Reset)
88 fn hide_cursor(&mut self) -> io::Result<()> {
89 map_error(execute!(self.buffer, Hide))
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!(),
99 map_error(execute!(self.buffer, Show, SetCursorShape(shape)))
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()))
107 fn set_cursor(&mut self, x: u16, y: u16) -> io::Result<()> {
108 map_error(execute!(self.buffer, MoveTo(x, y)))
111 fn clear(&mut self) -> io::Result<()> {
112 map_error(execute!(self.buffer, Clear(ClearType::All)))
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))
122 fn flush(&mut self) -> io::Result<()> {
127fn map_error(error: crossterm::Result<()>) -> io::Result<()> {
128 error.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))
138 fn queue<W>(&self, mut w: W) -> io::Result<()>
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)))?;
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)))?;
153 if removed.contains(Modifier::ITALIC) {
154 map_error(queue!(w, SetAttribute(CAttribute::NoItalic)))?;
156 if removed.contains(Modifier::UNDERLINED) {
157 map_error(queue!(w, SetAttribute(CAttribute::NoUnderline)))?;
159 if removed.contains(Modifier::DIM) {
160 map_error(queue!(w, SetAttribute(CAttribute::NormalIntensity)))?;
162 if removed.contains(Modifier::CROSSED_OUT) {
163 map_error(queue!(w, SetAttribute(CAttribute::NotCrossedOut)))?;
165 if removed.contains(Modifier::SLOW_BLINK) || removed.contains(Modifier::RAPID_BLINK) {
166 map_error(queue!(w, SetAttribute(CAttribute::NoBlink)))?;
169 let added = self.to - self.from;
170 if added.contains(Modifier::REVERSED) {
171 map_error(queue!(w, SetAttribute(CAttribute::Reverse)))?;
173 if added.contains(Modifier::BOLD) {
174 map_error(queue!(w, SetAttribute(CAttribute::Bold)))?;
176 if added.contains(Modifier::ITALIC) {
177 map_error(queue!(w, SetAttribute(CAttribute::Italic)))?;
179 if added.contains(Modifier::UNDERLINED) {
180 map_error(queue!(w, SetAttribute(CAttribute::Underlined)))?;
182 if added.contains(Modifier::DIM) {
183 map_error(queue!(w, SetAttribute(CAttribute::Dim)))?;
185 if added.contains(Modifier::CROSSED_OUT) {
186 map_error(queue!(w, SetAttribute(CAttribute::CrossedOut)))?;
188 if added.contains(Modifier::SLOW_BLINK) {
189 map_error(queue!(w, SetAttribute(CAttribute::SlowBlink)))?;
191 if added.contains(Modifier::RAPID_BLINK) {
192 map_error(queue!(w, SetAttribute(CAttribute::RapidBlink)))?;
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list