How to pause/resume TUI app to launch vim?

Hello everyone,
I am building a simple tui for dnote using ratatui’s component template. I am trying to get my application to pause to allow vim to edit notes file and resume my tui once user is done editing. I am just testing this out in Home component from template: templates/component/template/src/components/home.rs at main · ratatui-org/templates · GitHub

What am I doing wrong in handle_key_events in my code? It spawns vim but I can’t provide input and it just gets hung up.

// imports ...

#[derive(Default)]
pub struct Home {
    command_tx: Option<UnboundedSender<Action>>,
    config: Config,
}

impl Home {
    pub fn new() -> Self {
        Self::default()
    }
}

impl Component for Home {
    // register_action_handler, register_config_handler, ...

    fn handle_key_events(&mut self, key: KeyEvent) -> Result<Option<Action>> {
        match key.code {
            KeyCode::Char('v') => {
                if let Some(tx) = &self.command_tx {
                    tx.send(Action::Suspend)?;
                }

                disable_raw_mode()?;
                Command::new("sh")
                    .arg("-c")
                    .arg("vim /tmp/a.txt")
                    .status()
                    .expect("Failed to execute vim");
                enable_raw_mode()?;

                if let Some(tx) = &self.command_tx {
                    tx.send(Action::Resume)?;
                }

                Ok(None)
            }
            _ => Ok(None),
        }
    }

    // update, draw, other functions...
}
1 Like

I found this active issue Add section on how to spawn vim from a ratatui application · Issue #406 · ratatui-org/ratatui-website · GitHub

Here’s a proof of concept solution Add section on how to spawn vim from a ratatui application · Issue #406 · ratatui-org/ratatui-website · GitHub

I’m not sure if this matches your use case, but I use edit. By using edit, you can launch the user-specified $EDITOR from the TUI, and when the user exits the $EDITOR, you can retrieve the buffer (edited content) from the TUI.

2 Likes

Thanks, that’s good to know. Since dnote handles opening vim using commands like dnote edit 15, I will have to use that. I am working on adding a simple example on ratatui docs to spawn vim. I will definitely include this info in the documentation

1 Like