Skip to content

Commit

Permalink
test: spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
aidanaden committed Jun 24, 2024
1 parent 939a2f6 commit fb1866c
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 17 deletions.
11 changes: 11 additions & 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 rm-main/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ crossterm.workspace = true
ratatui.workspace = true
tui-input.workspace = true
tui-tree-widget.workspace = true
throbber-widgets-tui = "0.5.0"
91 changes: 74 additions & 17 deletions rm-main/src/ui/tabs/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ use ratatui::{
prelude::*,
widgets::{Cell, Paragraph, Row, Table},
};
use tokio::sync::mpsc::{self, UnboundedSender};
use throbber_widgets_tui::ThrobberState;
use tokio::{
sync::mpsc::{self, UnboundedSender},
task::JoinSet,
};
use tui_input::Input;

use crate::{
Expand Down Expand Up @@ -50,21 +54,67 @@ impl SearchTab {
let ctx_clone = ctx.clone();
let table_clone = Arc::clone(&table);
let search_result_info_clone = Arc::clone(&search_result_info);
let state = Arc::new(Mutex::new(throbber_widgets_tui::ThrobberState::default()));

tokio::task::spawn(async move {
let magnetease = Magnetease::new();
while let Some(search_phrase) = rx.recv().await {
search_result_info_clone.lock().unwrap().searching();
search_result_info_clone
.lock()
.unwrap()
.searching(Arc::clone(&state));
ctx_clone.send_action(Action::Render);
let res = magnetease.search(&search_phrase).await;
if res.is_empty() {
search_result_info_clone.lock().unwrap().not_found();
} else {
search_result_info_clone.lock().unwrap().found(res.len());

let state_clone = Arc::clone(&state);
let mut set = JoinSet::new();
set.spawn(async move {
let magnetease = Magnetease::new();
let res = magnetease.search(&search_phrase).await;
res
});

set.spawn(async move {
let tick_rate = tokio::time::Duration::from_millis(100);
let mut last_tick = tokio::time::Instant::now();
loop {
if last_tick.elapsed() >= tick_rate {
state_clone.lock().unwrap().calc_next();
last_tick = tokio::time::Instant::now();
// ctx_clone_next.send_action(Action::Render);
}
}
});

while let Some(res) = set.join_next().await {
match res {
Ok(res) => {
if res.is_empty() {
search_result_info_clone.lock().unwrap().not_found();
} else {
search_result_info_clone.lock().unwrap().found(res.len());
}

// TODO: add an X icon if no results, else V when results
table_clone.lock().unwrap().set_items(res);
ctx_clone.send_action(Action::Render);
}
Err(e) => {}
}
}

// TODO: add an X icon if no results, else V when results
table_clone.lock().unwrap().set_items(res);
ctx_clone.send_action(Action::Render);
// let animate_handle = tokio::task::spawn(async move {
// let tick_rate = tokio::time::Duration::from_millis(100);
// let mut last_tick = tokio::time::Instant::now();
// loop {
// if last_tick.elapsed() >= tick_rate {
// state_clone.lock().unwrap().calc_next();
// last_tick = tokio::time::Instant::now();
// // ctx_clone_next.send_action(Action::Render);
// }
// }
// true
// });

// animate_handle.abort();
}
});

Expand Down Expand Up @@ -278,11 +328,11 @@ impl Component for SearchTab {
}
}

#[derive(Clone, Copy)]
#[derive(Clone)]
enum SearchResultState {
Nothing,
NoResults,
Searching,
Searching(Arc<Mutex<ThrobberState>>),
Found(usize),
}

Expand All @@ -291,8 +341,8 @@ impl SearchResultState {
Self::Nothing
}

fn searching(&mut self) {
*self = Self::Searching;
fn searching(&mut self, state: Arc<Mutex<ThrobberState>>) {
*self = Self::Searching(state);
}

fn not_found(&mut self) {
Expand All @@ -308,8 +358,15 @@ impl Component for SearchResultState {
fn render(&mut self, f: &mut Frame, rect: Rect) {
match self {
SearchResultState::Nothing => return,
SearchResultState::Searching => {
f.render_widget("󱗼 Searching...", rect);
SearchResultState::Searching(state) => {
let default_throbber = throbber_widgets_tui::Throbber::default()
.label("Searching...")
.style(ratatui::style::Style::default().fg(ratatui::style::Color::Yellow));
f.render_stateful_widget(
default_throbber.clone(),
rect,
&mut state.lock().unwrap(),
);
}
SearchResultState::NoResults => {
let mut line = Line::default();
Expand Down

0 comments on commit fb1866c

Please sign in to comment.