Skip to content

Commit d26291a

Browse files
committed
feat: add spinner
1 parent 07fcca2 commit d26291a

5 files changed

Lines changed: 33 additions & 1 deletion

File tree

src/app.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::collections::HashMap;
33

44
use crate::config::Config;
55
use crate::notification::Notification;
6+
use crate::spinner::Spinner;
67
use crossterm::event::KeyCode;
78

89
use std::sync::Arc;
@@ -39,6 +40,7 @@ pub struct App {
3940
pub history_thread_index: usize,
4041
pub config: Arc<Config>,
4142
pub notifications: Vec<Notification>,
43+
pub spinner: Spinner,
4244
}
4345

4446
impl App {
@@ -58,11 +60,19 @@ impl App {
5860
history_thread_index: 0,
5961
config,
6062
notifications: Vec::new(),
63+
spinner: Spinner::default(),
6164
}
6265
}
6366

6467
pub fn tick(&mut self) {
6568
self.notifications.retain(|n| n.ttl > 0);
6669
self.notifications.iter_mut().for_each(|n| n.ttl -= 1);
70+
71+
if self.spinner.active {
72+
self.chat.pop();
73+
self.chat
74+
.push(format!("🤖: Waiting {}", self.spinner.draw()));
75+
self.spinner.update();
76+
}
6777
}
6878
}

src/handler.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ pub fn handle_key_events(
5555
}))
5656
.unwrap();
5757
});
58-
app.chat.push("🤖: Waiting ...".to_string());
58+
app.spinner.active = true;
59+
app.chat.push("🤖: Waiting ..".to_string());
5960
}
6061

6162
// scroll down

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ pub mod ui;
1717
pub mod notification;
1818

1919
pub mod llm;
20+
21+
pub mod spinner;

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ fn main() -> AppResult<()> {
3939
Event::Resize(_, _) => {}
4040
Event::LLMAnswer(answer) => {
4141
app.chat.pop();
42+
app.spinner.active = false;
4243
app.chat.push(format!("🤖: {}\n", answer));
4344
app.chat.push("\n".to_string());
4445
let mut conv: HashMap<String, String> = HashMap::new();

src/spinner.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
static SPINNER_CHARS: &[char] = &['⣷', '⣯', '⣟', '⡿', '⢿', '⣻', '⣽', '⣾'];
2+
3+
#[derive(Default, Debug)]
4+
pub struct Spinner {
5+
pub active: bool,
6+
pub index: usize,
7+
}
8+
9+
impl Spinner {
10+
pub fn draw(&self) -> char {
11+
SPINNER_CHARS[self.index]
12+
}
13+
14+
pub fn update(&mut self) {
15+
self.index += 1;
16+
self.index %= SPINNER_CHARS.len();
17+
}
18+
}

0 commit comments

Comments
 (0)