Skip to content

Commit

Permalink
Want program suspension method on Terminal (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
ulyssa committed Aug 4, 2022
1 parent 65af188 commit ad2cf40
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ anymap2 = "0.13.0"
derive_more = "0.99.16"
thiserror = "1.0.30"
crossterm = "0.17"
libc = "0.2"
tui = { version = "0.17", optional = true }
xi-rope = "0.3.0"
intervaltree = { version = "0.2.6", optional = true }
Expand Down
47 changes: 45 additions & 2 deletions src/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@
//!
//! [tui]: https://docs.rs/tui/latest/tui/
//!
use tui::buffer::Buffer;
use tui::layout::Rect;
use std::io::{stdout, Stdout, Write};
use std::process;

use libc;

use tui::{backend::CrosstermBackend, buffer::Buffer, layout::Rect, Terminal};

use crossterm::{
execute,
terminal::{EnterAlternateScreen, LeaveAlternateScreen},
};

use crate::input::InputContext;

Expand All @@ -17,6 +26,7 @@ use crate::editing::base::{
Axis,
CloseFlags,
Count,
EditInfo,
EditResult,
MoveDir1D,
ScrollStyle,
Expand Down Expand Up @@ -89,3 +99,36 @@ pub trait WindowContainer<W: Window, C> {

fn window_command(&mut self, action: WindowAction, ctx: &C) -> EditResult;
}

pub trait TerminalExtOps {
type Result;

fn program_suspend(&mut self) -> Self::Result;
}

impl TerminalExtOps for Terminal<CrosstermBackend<Stdout>> {
type Result = crossterm::Result<Option<EditInfo>>;

fn program_suspend(&mut self) -> Self::Result {
let mut stdout = stdout();

// Restore old terminal state.
crossterm::terminal::disable_raw_mode()?;
execute!(self.backend_mut(), LeaveAlternateScreen)?;
self.show_cursor()?;

// Send SIGTSTP to process.
let pid = process::id();

unsafe {
libc::kill(pid as i32, libc::SIGTSTP);
}

// Restore application terminal state.
crossterm::terminal::enable_raw_mode()?;
crossterm::execute!(stdout, EnterAlternateScreen)?;
self.clear()?;

Ok(None)
}
}

0 comments on commit ad2cf40

Please sign in to comment.