Open kdheepak opened 5 months ago
I believe this should be about the same for any other TUI: lazygit, yazi, even bash.
Also one important thing to note, this is about spawning a program and gaining back control. Which is different (and somewhat harder) than spawning a program on end.
Otherwise +1 for this.
I got it to launch vim on newly generated test app (cargo generate ratatui-org/templates
) with component
template with the following changes
diff --git a/src/action.rs b/src/action.rs
index f422a62..ee157c2 100644
--- a/src/action.rs
+++ b/src/action.rs
@@ -17,4 +17,5 @@ pub enum Action {
Refresh,
Error(String),
Help,
+ EditFile,
}
diff --git a/src/app.rs b/src/app.rs
index e528d05..c378c00 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -126,6 +126,17 @@ impl App {
}
})?;
},
+ Action::EditFile => {
+ tui.exit()?;
+ let status = std::process::Command::new("vim").arg("/tmp/a.txt").status()?;
+ if status.success() {
+ log::info!("Successfully launched vim");
+ } else {
+ log::error!("Failed to launch vim");
+ }
+ tui = tui::Tui::new()?.tick_rate(self.tick_rate).frame_rate(self.frame_rate);
+ tui.enter()?;
+ },
_ => {},
}
for component in self.components.iter_mut() {
diff --git a/src/components/home.rs b/src/components/home.rs
index a1e9cc6..f1fd170 100644
--- a/src/components/home.rs
+++ b/src/components/home.rs
@@ -35,6 +35,13 @@ impl Component for Home {
Ok(())
}
+ fn handle_key_events(&mut self, key: KeyEvent) -> Result<Option<Action>> {
+ match key.code {
+ KeyCode::Char('v') => Ok(Some(Action::EditFile)),
+ _ => Ok(None),
+ }
+ }
+
fn update(&mut self, action: Action) -> Result<Option<Action>> {
match action {
Action::Tick => {
@@ -45,7 +52,7 @@ impl Component for Home {
}
fn draw(&mut self, f: &mut Frame<'_>, area: Rect) -> Result<()> {
- f.render_widget(Paragraph::new("hello world"), area);
+ f.render_widget(Paragraph::new("Hello! Press 'v' to launch vim"), area);
Ok(())
}
}
I would like to contribute by adding this example (how-to-spawn-vim
) to the repo. I will open a PR soon
I have created a PR with basic example on how to spawn vim here https://github.com/ratatui-org/ratatui-website/pull/651
Example of how to do this with an async event loop:
https://github.com/ratatui-org/templates/blob/b75771463c00088b75e90986979c60720d024bbf/component/template/src/tui.rs#L171-L198