Skip to content

Commit

Permalink
Handle switch from crossterm::Result to io::Result
Browse files Browse the repository at this point in the history
  • Loading branch information
the-mikedavis authored and archseer committed Aug 22, 2023
1 parent 050c019 commit 546c8ca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 38 deletions.
11 changes: 4 additions & 7 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl Application {

pub async fn event_loop<S>(&mut self, input_stream: &mut S)
where
S: Stream<Item = crossterm::Result<crossterm::event::Event>> + Unpin,
S: Stream<Item = std::io::Result<crossterm::event::Event>> + Unpin,
{
self.render().await;

Expand All @@ -301,7 +301,7 @@ impl Application {

pub async fn event_loop_until_idle<S>(&mut self, input_stream: &mut S) -> bool
where
S: Stream<Item = crossterm::Result<crossterm::event::Event>> + Unpin,
S: Stream<Item = std::io::Result<crossterm::event::Event>> + Unpin,
{
loop {
if self.editor.should_close() {
Expand Down Expand Up @@ -615,10 +615,7 @@ impl Application {
false
}

pub async fn handle_terminal_events(
&mut self,
event: Result<CrosstermEvent, crossterm::ErrorKind>,
) {
pub async fn handle_terminal_events(&mut self, event: std::io::Result<CrosstermEvent>) {
let mut cx = crate::compositor::Context {
editor: &mut self.editor,
jobs: &mut self.jobs,
Expand Down Expand Up @@ -1159,7 +1156,7 @@ impl Application {

pub async fn run<S>(&mut self, input_stream: &mut S) -> Result<i32, Error>
where
S: Stream<Item = crossterm::Result<crossterm::event::Event>> + Unpin,
S: Stream<Item = std::io::Result<crossterm::event::Event>> + Unpin,
{
self.claim_term().await?;

Expand Down
58 changes: 27 additions & 31 deletions helix-tui/src/backend/crossterm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ where
for (x, y, cell) in content {
// Move the cursor if the previous location was not (x - 1, y)
if !matches!(last_pos, Some(p) if x == p.0 + 1 && y == p.1) {
map_error(queue!(self.buffer, MoveTo(x, y)))?;
queue!(self.buffer, MoveTo(x, y))?;
}
last_pos = Some((x, y));
if cell.modifier != modifier {
Expand All @@ -214,20 +214,20 @@ where
}
if cell.fg != fg {
let color = CColor::from(cell.fg);
map_error(queue!(self.buffer, SetForegroundColor(color)))?;
queue!(self.buffer, SetForegroundColor(color))?;
fg = cell.fg;
}
if cell.bg != bg {
let color = CColor::from(cell.bg);
map_error(queue!(self.buffer, SetBackgroundColor(color)))?;
queue!(self.buffer, SetBackgroundColor(color))?;
bg = cell.bg;
}

let mut new_underline_style = cell.underline_style;
if self.capabilities.has_extended_underlines {
if cell.underline_color != underline_color {
let color = CColor::from(cell.underline_color);
map_error(queue!(self.buffer, SetUnderlineColor(color)))?;
queue!(self.buffer, SetUnderlineColor(color))?;
underline_color = cell.underline_color;
}
} else {
Expand All @@ -239,24 +239,24 @@ where

if new_underline_style != underline_style {
let attr = CAttribute::from(new_underline_style);
map_error(queue!(self.buffer, SetAttribute(attr)))?;
queue!(self.buffer, SetAttribute(attr))?;
underline_style = new_underline_style;
}

map_error(queue!(self.buffer, Print(&cell.symbol)))?;
queue!(self.buffer, Print(&cell.symbol))?;
}

map_error(queue!(
queue!(
self.buffer,
SetUnderlineColor(CColor::Reset),
SetForegroundColor(CColor::Reset),
SetBackgroundColor(CColor::Reset),
SetAttribute(CAttribute::Reset)
))
)
}

fn hide_cursor(&mut self) -> io::Result<()> {
map_error(execute!(self.buffer, Hide))
execute!(self.buffer, Hide)
}

fn show_cursor(&mut self, kind: CursorKind) -> io::Result<()> {
Expand All @@ -266,7 +266,7 @@ where
CursorKind::Underline => SetCursorStyle::SteadyUnderScore,
CursorKind::Hidden => unreachable!(),
};
map_error(execute!(self.buffer, Show, shape))
execute!(self.buffer, Show, shape)
}

fn get_cursor(&mut self) -> io::Result<(u16, u16)> {
Expand All @@ -275,11 +275,11 @@ where
}

fn set_cursor(&mut self, x: u16, y: u16) -> io::Result<()> {
map_error(execute!(self.buffer, MoveTo(x, y)))
execute!(self.buffer, MoveTo(x, y))
}

fn clear(&mut self) -> io::Result<()> {
map_error(execute!(self.buffer, Clear(ClearType::All)))
execute!(self.buffer, Clear(ClearType::All))
}

fn size(&self) -> io::Result<Rect> {
Expand All @@ -294,10 +294,6 @@ where
}
}

fn map_error(error: crossterm::Result<()>) -> io::Result<()> {
error.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))
}

#[derive(Debug)]
struct ModifierDiff {
pub from: Modifier,
Expand All @@ -312,48 +308,48 @@ impl ModifierDiff {
//use crossterm::Attribute;
let removed = self.from - self.to;
if removed.contains(Modifier::REVERSED) {
map_error(queue!(w, SetAttribute(CAttribute::NoReverse)))?;
queue!(w, SetAttribute(CAttribute::NoReverse))?;
}
if removed.contains(Modifier::BOLD) {
map_error(queue!(w, SetAttribute(CAttribute::NormalIntensity)))?;
queue!(w, SetAttribute(CAttribute::NormalIntensity))?;
if self.to.contains(Modifier::DIM) {
map_error(queue!(w, SetAttribute(CAttribute::Dim)))?;
queue!(w, SetAttribute(CAttribute::Dim))?;
}
}
if removed.contains(Modifier::ITALIC) {
map_error(queue!(w, SetAttribute(CAttribute::NoItalic)))?;
queue!(w, SetAttribute(CAttribute::NoItalic))?;
}
if removed.contains(Modifier::DIM) {
map_error(queue!(w, SetAttribute(CAttribute::NormalIntensity)))?;
queue!(w, SetAttribute(CAttribute::NormalIntensity))?;
}
if removed.contains(Modifier::CROSSED_OUT) {
map_error(queue!(w, SetAttribute(CAttribute::NotCrossedOut)))?;
queue!(w, SetAttribute(CAttribute::NotCrossedOut))?;
}
if removed.contains(Modifier::SLOW_BLINK) || removed.contains(Modifier::RAPID_BLINK) {
map_error(queue!(w, SetAttribute(CAttribute::NoBlink)))?;
queue!(w, SetAttribute(CAttribute::NoBlink))?;
}

let added = self.to - self.from;
if added.contains(Modifier::REVERSED) {
map_error(queue!(w, SetAttribute(CAttribute::Reverse)))?;
queue!(w, SetAttribute(CAttribute::Reverse))?;
}
if added.contains(Modifier::BOLD) {
map_error(queue!(w, SetAttribute(CAttribute::Bold)))?;
queue!(w, SetAttribute(CAttribute::Bold))?;
}
if added.contains(Modifier::ITALIC) {
map_error(queue!(w, SetAttribute(CAttribute::Italic)))?;
queue!(w, SetAttribute(CAttribute::Italic))?;
}
if added.contains(Modifier::DIM) {
map_error(queue!(w, SetAttribute(CAttribute::Dim)))?;
queue!(w, SetAttribute(CAttribute::Dim))?;
}
if added.contains(Modifier::CROSSED_OUT) {
map_error(queue!(w, SetAttribute(CAttribute::CrossedOut)))?;
queue!(w, SetAttribute(CAttribute::CrossedOut))?;
}
if added.contains(Modifier::SLOW_BLINK) {
map_error(queue!(w, SetAttribute(CAttribute::SlowBlink)))?;
queue!(w, SetAttribute(CAttribute::SlowBlink))?;
}
if added.contains(Modifier::RAPID_BLINK) {
map_error(queue!(w, SetAttribute(CAttribute::RapidBlink)))?;
queue!(w, SetAttribute(CAttribute::RapidBlink))?;
}

Ok(())
Expand Down Expand Up @@ -407,7 +403,7 @@ impl Command for SetUnderlineColor {
}

#[cfg(windows)]
fn execute_winapi(&self) -> crossterm::Result<()> {
fn execute_winapi(&self) -> io::Result<()> {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
"SetUnderlineColor not supported by winapi.",
Expand Down

0 comments on commit 546c8ca

Please sign in to comment.