Skip to content

Commit 0c85a94

Browse files
committed
make use a runtime inside non-async main to block_on the app.run method
1 parent e5f2d52 commit 0c85a94

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

src/app.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,17 @@ impl App {
116116
}
117117

118118
/// Runs the main loop of the application, handling events and actions
119-
pub fn run(
119+
pub async fn run(
120120
&mut self,
121121
tui: &mut DefaultTerminal,
122-
runtime: tokio::runtime::Handle,
123-
events: Events,
122+
mut events: Events,
124123
query: Option<String>,
125124
) -> Result<()> {
126125
// uncomment to test error handling
127126
// panic!("test panic");
128127
// Err(color_eyre::eyre::eyre!("Error"))?;
129128
self.tx.send(Action::Init { query })?;
130129

131-
std::thread::spawn(|| runtime.block_on(async move { self.async_run(tui, events).await }))
132-
.join()?
133-
}
134-
135-
async fn async_run(&mut self, tui: &mut DefaultTerminal, mut events: Events) -> Result<()> {
136130
loop {
137131
if let Some(e) = events.next().await {
138132
self.handle_event(e)?.map(|action| self.tx.send(action));

src/main.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ mod widgets;
1313
use app::App;
1414
use color_eyre::eyre::Result;
1515

16-
#[tokio::main]
17-
async fn main() -> Result<()> {
16+
fn main() -> Result<()> {
1817
let cli = cli::parse();
1918
config::init(&cli)?;
2019
logging::init()?;
@@ -25,7 +24,11 @@ async fn main() -> Result<()> {
2524
return Ok(());
2625
}
2726

28-
let events = events::Events::new();
29-
let runtime = tokio::runtime::Handle::current();
30-
ratatui::run(move |tui| App::new().run(tui, runtime, events, cli.query))
27+
let runtime = tokio::runtime::Runtime::new()?;
28+
ratatui::run(move |tui| {
29+
runtime.block_on(async {
30+
let events = events::Events::new();
31+
App::new().run(tui, events, cli.query).await
32+
})
33+
})
3134
}

0 commit comments

Comments
 (0)