From 9dd9515a8d4f7fcb0cedfa6b0315dde304e124d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Tue, 29 Mar 2022 15:11:26 +0900 Subject: [PATCH] Term backend --- helix-view/src/backend/term.rs | 93 ++++++++++++++++++++++++++++++ helix-view/src/input.rs | 100 +-------------------------------- helix-view/src/lib.rs | 4 ++ 3 files changed, 99 insertions(+), 98 deletions(-) create mode 100644 helix-view/src/backend/term.rs diff --git a/helix-view/src/backend/term.rs b/helix-view/src/backend/term.rs new file mode 100644 index 000000000..3e4566eba --- /dev/null +++ b/helix-view/src/backend/term.rs @@ -0,0 +1,93 @@ +use crate::input::{ + Event, KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind, +}; + +impl From for Event { + fn from(event: crossterm::event::Event) -> Self { + match event { + crossterm::event::Event::Key(key) => Self::Key(key.into()), + crossterm::event::Event::Mouse(mouse) => Self::Mouse(mouse.into()), + crossterm::event::Event::Resize(w, h) => Self::Resize(w, h), + } + } +} + +impl From for MouseEvent { + fn from( + crossterm::event::MouseEvent { + kind, + column, + row, + modifiers, + }: crossterm::event::MouseEvent, + ) -> Self { + Self { + kind: kind.into(), + column, + row, + modifiers: modifiers.into(), + } + } +} + +impl From for MouseEventKind { + fn from(kind: crossterm::event::MouseEventKind) -> Self { + match kind { + crossterm::event::MouseEventKind::Down(button) => Self::Down(button.into()), + crossterm::event::MouseEventKind::Up(button) => Self::Down(button.into()), + crossterm::event::MouseEventKind::Drag(button) => Self::Drag(button.into()), + crossterm::event::MouseEventKind::Moved => Self::Moved, + crossterm::event::MouseEventKind::ScrollDown => Self::ScrollDown, + crossterm::event::MouseEventKind::ScrollUp => Self::ScrollUp, + } + } +} + +#[cfg(feature = "term")] +impl From for MouseButton { + fn from(button: crossterm::event::MouseButton) -> Self { + match button { + crossterm::event::MouseButton::Left => MouseButton::Left, + crossterm::event::MouseButton::Right => MouseButton::Right, + crossterm::event::MouseButton::Middle => MouseButton::Middle, + } + } +} + +impl From for KeyEvent { + fn from(crossterm::event::KeyEvent { code, modifiers }: crossterm::event::KeyEvent) -> Self { + if code == crossterm::event::KeyCode::BackTab { + // special case for BackTab -> Shift-Tab + let mut modifiers: KeyModifiers = modifiers.into(); + modifiers.insert(KeyModifiers::SHIFT); + Self { + code: KeyCode::Tab, + modifiers, + } + } else { + Self { + code: code.into(), + modifiers: modifiers.into(), + } + } + } +} + +impl From for crossterm::event::KeyEvent { + fn from(KeyEvent { code, modifiers }: KeyEvent) -> Self { + if code == KeyCode::Tab && modifiers.contains(KeyModifiers::SHIFT) { + // special case for Shift-Tab -> BackTab + let mut modifiers = modifiers; + modifiers.remove(KeyModifiers::SHIFT); + crossterm::event::KeyEvent { + code: crossterm::event::KeyCode::BackTab, + modifiers: modifiers.into(), + } + } else { + crossterm::event::KeyEvent { + code: code.into(), + modifiers: modifiers.into(), + } + } + } +} diff --git a/helix-view/src/input.rs b/helix-view/src/input.rs index 282824752..e67b7a0fb 100644 --- a/helix-view/src/input.rs +++ b/helix-view/src/input.rs @@ -2,7 +2,6 @@ use anyhow::{anyhow, Error}; use helix_core::unicode::width::UnicodeWidthStr; use serde::de::{self, Deserialize, Deserializer}; -use std::fmt; pub use crate::keyboard::{KeyCode, KeyModifiers}; @@ -94,8 +93,8 @@ pub(crate) mod keys { pub(crate) const PERCENT: &str = "percent"; } -impl fmt::Display for KeyEvent { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result { +impl std::fmt::Display for KeyEvent { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_fmt(format_args!( "{}{}{}", if self.modifiers.contains(KeyModifiers::SHIFT) { @@ -259,101 +258,6 @@ fn deserialize(deserializer: D) -> Result } } -#[cfg(feature = "term")] -impl From for Event { - fn from(event: crossterm::event::Event) -> Self { - match event { - crossterm::event::Event::Key(key) => Self::Key(key.into()), - crossterm::event::Event::Mouse(mouse) => Self::Mouse(mouse.into()), - crossterm::event::Event::Resize(w, h) => Self::Resize(w, h), - } - } -} - -#[cfg(feature = "term")] -impl From for MouseEvent { - fn from( - crossterm::event::MouseEvent { - kind, - column, - row, - modifiers, - }: crossterm::event::MouseEvent, - ) -> Self { - Self { - kind: kind.into(), - column, - row, - modifiers: modifiers.into(), - } - } -} - -#[cfg(feature = "term")] -impl From for MouseEventKind { - fn from(kind: crossterm::event::MouseEventKind) -> Self { - match kind { - crossterm::event::MouseEventKind::Down(button) => Self::Down(button.into()), - crossterm::event::MouseEventKind::Up(button) => Self::Down(button.into()), - crossterm::event::MouseEventKind::Drag(button) => Self::Drag(button.into()), - crossterm::event::MouseEventKind::Moved => Self::Moved, - crossterm::event::MouseEventKind::ScrollDown => Self::ScrollDown, - crossterm::event::MouseEventKind::ScrollUp => Self::ScrollUp, - } - } -} - -#[cfg(feature = "term")] -impl From for MouseButton { - fn from(button: crossterm::event::MouseButton) -> Self { - match button { - crossterm::event::MouseButton::Left => MouseButton::Left, - crossterm::event::MouseButton::Right => MouseButton::Right, - crossterm::event::MouseButton::Middle => MouseButton::Middle, - } - } -} - -#[cfg(feature = "term")] -impl From for KeyEvent { - fn from(crossterm::event::KeyEvent { code, modifiers }: crossterm::event::KeyEvent) -> Self { - if code == crossterm::event::KeyCode::BackTab { - // special case for BackTab -> Shift-Tab - let mut modifiers: KeyModifiers = modifiers.into(); - modifiers.insert(KeyModifiers::SHIFT); - Self { - code: KeyCode::Tab, - modifiers, - } - } else { - Self { - code: code.into(), - modifiers: modifiers.into(), - } - } - } -} - -#[cfg(feature = "term")] -impl From for crossterm::event::KeyEvent { - fn from(KeyEvent { code, modifiers }: KeyEvent) -> Self { - if code == KeyCode::Tab && modifiers.contains(KeyModifiers::SHIFT) { - // special case for Shift-Tab -> BackTab - let mut modifiers = modifiers; - modifiers.remove(KeyModifiers::SHIFT); - crossterm::event::KeyEvent { - code: crossterm::event::KeyCode::BackTab, - modifiers: modifiers.into(), - } - } else { - crossterm::event::KeyEvent { - code: code.into(), - modifiers: modifiers.into(), - } - } - } -} - pub fn parse_macro(keys_str: &str) -> anyhow::Result> { use anyhow::Context; let mut keys_res: anyhow::Result<_> = Ok(Vec::new()); diff --git a/helix-view/src/lib.rs b/helix-view/src/lib.rs index a77ce1556..182c89c38 100644 --- a/helix-view/src/lib.rs +++ b/helix-view/src/lib.rs @@ -1,6 +1,10 @@ #[macro_use] pub mod macros; +pub mod backend { + #[cfg(feature = "term")] + pub mod term; +} pub mod clipboard; pub mod document; pub mod editor;