Skip to content

Commit

Permalink
feat: handle suspend signal
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw committed Jul 31, 2024
1 parent 27e3472 commit d223916
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ console = "0.15.8"
thiserror = "1"
unicode-width = "0.1"
os_pipe = "1.1.5"
libc = "0.2.155"

[dependencies.syntect]
version = "5.2"
Expand Down
7 changes: 7 additions & 0 deletions config-file-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@
"$ref": "#/definitions/KeyBinding"
}
},
"suspend": {
"description": "The key binding to suspend the application.",
"type": "array",
"items": {
"$ref": "#/definitions/KeyBinding"
}
},
"toggle_bindings": {
"description": "The key binding to toggle the key bindings modal.",
"type": "array",
Expand Down
4 changes: 4 additions & 0 deletions config.sample.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
# yaml-language-server: $schema=https://raw.githubusercontent.com/mfontanini/presenterm/master/config-file-schema.json
defaults:
# override the terminal font size when in windows or when using sixel.
Expand Down Expand Up @@ -83,3 +84,6 @@ bindings:

# the key binding to close the application.
exit: ["<c-c>", "q"]

# the key binding to suspend the application.
suspend: ["<c-z>"]
9 changes: 9 additions & 0 deletions src/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@ pub struct KeyBindingsConfig {
/// The key binding to close the application.
#[serde(default = "default_exit_bindings")]
pub(crate) exit: Vec<KeyBinding>,

/// The key binding to suspend the application.
#[serde(default = "default_suspend_bindings")]
pub(crate) suspend: Vec<KeyBinding>,
}

impl Default for KeyBindingsConfig {
Expand All @@ -348,6 +352,7 @@ impl Default for KeyBindingsConfig {
toggle_bindings: default_toggle_bindings_modal_bindings(),
close_modal: default_close_modal_bindings(),
exit: default_exit_bindings(),
suspend: default_suspend_bindings(),
}
}
}
Expand Down Expand Up @@ -412,6 +417,10 @@ fn default_exit_bindings() -> Vec<KeyBinding> {
make_keybindings(["<c-c>", "q"])
}

fn default_suspend_bindings() -> Vec<KeyBinding> {
make_keybindings(["<c-z>"])
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
3 changes: 3 additions & 0 deletions src/input/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ pub(crate) enum Command {
/// Exit the presentation.
Exit,

/// Suspend the presentation.
Suspend,

/// The presentation has changed and needs to be reloaded.
Reload,

Expand Down
2 changes: 2 additions & 0 deletions src/input/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ impl CommandKeyBindings {
}
RenderAsyncOperations => Command::RenderAsyncOperations,
Exit => Command::Exit,
Suspend => Command::Suspend,
Reload => Command::Reload,
HardReload => Command::HardReload,
ToggleSlideIndex => Command::ToggleSlideIndex,
Expand Down Expand Up @@ -133,6 +134,7 @@ impl TryFrom<KeyBindingsConfig> for CommandKeyBindings {
.chain(zip(CommandDiscriminants::LastSlide, config.last_slide))
.chain(zip(CommandDiscriminants::GoToSlide, config.go_to_slide))
.chain(zip(CommandDiscriminants::Exit, config.exit))
.chain(zip(CommandDiscriminants::Suspend, config.suspend))
.chain(zip(CommandDiscriminants::HardReload, config.reload))
.chain(zip(CommandDiscriminants::ToggleSlideIndex, config.toggle_slide_index))
.chain(zip(CommandDiscriminants::ToggleKeyBindingsConfig, config.toggle_bindings))
Expand Down
17 changes: 16 additions & 1 deletion src/presenter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ impl<'a> Presenter<'a> {
};
match self.apply_command(command) {
CommandSideEffect::Exit => return Ok(()),
CommandSideEffect::Suspend => {
self.suspend(&mut drawer);
break;
}
CommandSideEffect::Reload => {
self.try_reload(path, false);
break;
Expand Down Expand Up @@ -196,6 +200,7 @@ impl<'a> Presenter<'a> {
return CommandSideEffect::Reload;
}
Command::Exit => return CommandSideEffect::Exit,
Command::Suspend => return CommandSideEffect::Suspend,
_ => (),
};
if matches!(command, Command::Redraw) {
Expand Down Expand Up @@ -245,7 +250,7 @@ impl<'a> Presenter<'a> {
true
}
// These are handled above as they don't require the presentation
Command::Reload | Command::HardReload | Command::Exit | Command::Redraw => {
Command::Reload | Command::HardReload | Command::Exit | Command::Suspend | Command::Redraw => {
panic!("unreachable commands")
}
};
Expand Down Expand Up @@ -348,10 +353,20 @@ impl<'a> Presenter<'a> {
other => self.state = other,
}
}

fn suspend(&self, drawer: &mut TerminalDrawer<Stdout>) {
drawer.terminal.suspend();
#[cfg(unix)]
unsafe {
libc::raise(libc::SIGTSTP);
}
drawer.terminal.resume();
}
}

enum CommandSideEffect {
Exit,
Suspend,
Redraw,
Reload,
None,
Expand Down
2 changes: 1 addition & 1 deletion src/render/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(crate) type RenderResult = Result<(), RenderError>;

/// Allows drawing elements in the terminal.
pub(crate) struct TerminalDrawer<W: TerminalWrite> {
terminal: Terminal<W>,
pub(crate) terminal: Terminal<W>,
font_size_fallback: u8,
}

Expand Down
8 changes: 8 additions & 0 deletions src/render/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ impl<W: TerminalWrite> Terminal<W> {
self.cursor_row += options.rows;
Ok(())
}

pub(crate) fn suspend(&mut self) {
self.writer.deinit();
}

pub(crate) fn resume(&mut self) {
let _ = self.writer.init();
}
}

impl<W> Drop for Terminal<W>
Expand Down

0 comments on commit d223916

Please sign in to comment.