Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-tui/src/terminal.rs')
| -rw-r--r-- | helix-tui/src/terminal.rs | 68 |
1 files changed, 22 insertions, 46 deletions
diff --git a/helix-tui/src/terminal.rs b/helix-tui/src/terminal.rs index 5e4007fc..22e9232f 100644 --- a/helix-tui/src/terminal.rs +++ b/helix-tui/src/terminal.rs @@ -1,8 +1,4 @@ -//! Terminal interface provided through the [Terminal] type. -//! Frontend for [Backend] - use crate::{backend::Backend, buffer::Buffer}; -use helix_view::editor::{Config as EditorConfig, KittyKeyboardProtocolConfig}; use helix_view::graphics::{CursorKind, Rect}; use std::io; @@ -20,24 +16,6 @@ pub struct Viewport { resize_behavior: ResizeBehavior, } -/// Terminal configuration -#[derive(Debug)] -pub struct Config { - pub enable_mouse_capture: bool, - pub force_enable_extended_underlines: bool, - pub kitty_keyboard_protocol: KittyKeyboardProtocolConfig, -} - -impl From<&EditorConfig> for Config { - fn from(config: &EditorConfig) -> Self { - Self { - enable_mouse_capture: config.mouse, - force_enable_extended_underlines: config.undercurl, - kitty_keyboard_protocol: config.kitty_keyboard_protocol, - } - } -} - impl Viewport { /// UNSTABLE pub fn fixed(area: Rect) -> Viewport { @@ -55,7 +33,7 @@ pub struct TerminalOptions { pub viewport: Viewport, } -/// Interface to the terminal backed by crossterm +/// Interface to the terminal backed by Termion #[derive(Debug)] pub struct Terminal<B> where @@ -73,13 +51,19 @@ where viewport: Viewport, } -/// Default terminal size: 80 columns, 24 lines -pub const DEFAULT_TERMINAL_SIZE: Rect = Rect { - x: 0, - y: 0, - width: 80, - height: 24, -}; +impl<B> Drop for Terminal<B> +where + B: Backend, +{ + fn drop(&mut self) { + // Attempt to restore the cursor state + if self.cursor_kind == CursorKind::Hidden { + if let Err(err) = self.show_cursor(CursorKind::Block) { + eprintln!("Failed to show the cursor: {}", err); + } + } + } +} impl<B> Terminal<B> where @@ -88,7 +72,7 @@ where /// Wrapper around Terminal initialization. Each buffer is initialized with a blank string and /// default colors for the foreground and the background pub fn new(backend: B) -> io::Result<Terminal<B>> { - let size = backend.size().unwrap_or(DEFAULT_TERMINAL_SIZE); + let size = backend.size()?; Terminal::with_options( backend, TerminalOptions { @@ -114,18 +98,6 @@ where }) } - pub fn claim(&mut self) -> io::Result<()> { - self.backend.claim() - } - - pub fn reconfigure(&mut self, config: Config) -> io::Result<()> { - self.backend.reconfigure(config) - } - - pub fn restore(&mut self) -> io::Result<()> { - self.backend.restore() - } - // /// Get a Frame object which provides a consistent view into the terminal state for rendering. // pub fn get_frame(&mut self) -> Frame<B> { // Frame { @@ -167,7 +139,7 @@ where /// Queries the backend for size and resizes if it doesn't match the previous size. pub fn autoresize(&mut self) -> io::Result<Rect> { - let size = self.size(); + let size = self.size()?; if size != self.viewport.area { self.resize(size)?; }; @@ -230,6 +202,10 @@ where Ok(()) } + pub fn get_cursor(&mut self) -> io::Result<(u16, u16)> { + self.backend.get_cursor() + } + pub fn set_cursor(&mut self, x: u16, y: u16) -> io::Result<()> { self.backend.set_cursor(x, y) } @@ -243,7 +219,7 @@ where } /// Queries the real size of the backend. - pub fn size(&self) -> Rect { - self.backend.size().unwrap_or(DEFAULT_TERMINAL_SIZE) + pub fn size(&self) -> io::Result<Rect> { + self.backend.size() } } |