Implement ability to rate window titles

This commit is contained in:
2024-07-05 16:06:21 -04:00
parent 38a03f063d
commit 860fe615ab

View File

@@ -15,6 +15,7 @@ use ratatui::{
ExecutableCommand, ExecutableCommand,
}, },
prelude::*, prelude::*,
style::Color,
widgets::*, widgets::*,
}; };
@@ -23,9 +24,16 @@ enum State {
InputIntention, InputIntention,
InputDuration, InputDuration,
InProgress, InProgress,
End,
ShouldQuit, ShouldQuit,
} }
struct SessionRating {
window_title: Rc<String>,
duration: Duration,
rating: u8,
}
struct App { struct App {
state: State, state: State,
user_intention: String, user_intention: String,
@@ -36,6 +44,8 @@ struct App {
session_start: Instant, session_start: Instant,
session_stats: HashMap<Rc<String>, Duration>, session_stats: HashMap<Rc<String>, Duration>,
session_remaining: Duration, session_remaining: Duration,
session_ratings: Vec<SessionRating>,
session_ratings_index: usize,
last_tick_50ms: Instant, last_tick_50ms: Instant,
last_tick_1s: Instant, last_tick_1s: Instant,
} }
@@ -54,6 +64,8 @@ impl App {
session_start: Instant::now(), session_start: Instant::now(),
session_stats: HashMap::new(), session_stats: HashMap::new(),
session_remaining: Duration::new(0, 0), session_remaining: Duration::new(0, 0),
session_ratings: Vec::new(),
session_ratings_index: 0,
last_tick_50ms: Instant::now(), last_tick_50ms: Instant::now(),
last_tick_1s: Instant::now(), last_tick_1s: Instant::now(),
} }
@@ -79,6 +91,13 @@ impl App {
self.current_window_time = Instant::now(); self.current_window_time = Instant::now();
self.session_start = self.current_window_time; self.session_start = self.current_window_time;
self.session_stats = HashMap::new(); self.session_stats = HashMap::new();
self.session_ratings_index = 0;
}
fn to_end(&mut self) {
self.state = State::End;
self.session_ratings = session_stats_as_vec(&self.session_stats);
self.user_intention = "Press 1, 2, 3 to rate titles".to_string();
} }
fn tick_50ms(&mut self) { fn tick_50ms(&mut self) {
@@ -92,11 +111,11 @@ impl App {
update_session_stats(self); update_session_stats(self);
if self.session_remaining == Duration::new(0, 0) { if self.session_remaining == Duration::new(0, 0) {
self.state = State::InputIntention; self.to_end();
self.user_intention = "Session complete. New session?".to_string();
} }
} }
State::ShouldQuit => {} State::ShouldQuit => {}
State::End => {}
} }
self.last_tick_50ms = Instant::now(); self.last_tick_50ms = Instant::now();
@@ -109,6 +128,34 @@ impl App {
fn timeout(&self) -> Duration { fn timeout(&self) -> Duration {
Duration::from_millis(50).saturating_sub(self.last_tick_50ms.elapsed()) Duration::from_millis(50).saturating_sub(self.last_tick_50ms.elapsed())
} }
fn get_session_stats(&self) -> Vec<Line> {
let mut zero_encountered = if self.state != State::End {
true
} else {
false
};
self.session_ratings
.iter()
.map(|s| {
Line::from(Span::styled(
format!("{}: {}", duration_as_str(&s.duration), s.window_title),
match s.rating {
0 if !zero_encountered => {
zero_encountered = true;
Style::new().fg(Color::LightBlue)
}
0 if zero_encountered => Style::new(),
1 => Style::new().fg(Color::LightRed),
2 => Style::new().fg(Color::LightYellow),
3 => Style::new().fg(Color::LightGreen),
_ => Style::new().fg(Color::LightBlue),
},
))
})
.collect()
}
} }
fn duration_as_str(duration: &Duration) -> String { fn duration_as_str(duration: &Duration) -> String {
@@ -119,22 +166,17 @@ fn duration_as_str(duration: &Duration) -> String {
) )
} }
fn session_stats_to_lines(session_stats: &HashMap<Rc<String>, Duration>) -> Vec<Line> { fn session_stats_as_vec(session_stats: &HashMap<Rc<String>, Duration>) -> Vec<SessionRating> {
let mut stats: Vec<_> = session_stats let mut stats: Vec<_> = session_stats
.iter() .iter()
.map(|(title, duration)| (title.clone(), *duration)) .map(|(title, duration)| SessionRating {
.collect(); window_title: title.clone(),
stats.sort_by(|a, b| b.1.cmp(&a.1)); duration: duration.clone(),
stats rating: 0,
.iter()
.map(|(title, duration)| {
Line::from(Span::raw(format!(
"{}: {}",
duration_as_str(&duration),
title
)))
}) })
.collect() .collect();
stats.sort_by(|a, b| b.duration.cmp(&a.duration));
stats
} }
fn main() -> Result<()> { fn main() -> Result<()> {
@@ -199,7 +241,7 @@ fn handle_events(app: &mut App) -> Result<()> {
} else if app.state == State::InProgress { } else if app.state == State::InProgress {
match key.code { match key.code {
KeyCode::Char('q') => { KeyCode::Char('q') => {
app.state = State::InputIntention; app.to_end();
} }
KeyCode::Char('a') => { KeyCode::Char('a') => {
app.user_duration = app.user_duration.saturating_add(Duration::from_secs(60)); app.user_duration = app.user_duration.saturating_add(Duration::from_secs(60));
@@ -209,6 +251,23 @@ fn handle_events(app: &mut App) -> Result<()> {
} }
_ => {} _ => {}
} }
} else if app.state == State::End {
let code = match key.code {
KeyCode::Char('1') => 1,
KeyCode::Char('2') => 2,
KeyCode::Char('3') => 3,
_ => 0,
};
if app.session_ratings_index < app.session_ratings.len() && code != 0 {
app.session_ratings[app.session_ratings_index].rating = code;
app.session_ratings_index += 1;
}
if app.session_ratings_index >= app.session_ratings.len() {
app.state = State::InputIntention;
app.user_intention = "Next session!".to_string();
}
} }
Ok(()) Ok(())
@@ -225,6 +284,7 @@ fn update_session_stats(app: &mut App) {
*entry += app.current_window_time.elapsed(); *entry += app.current_window_time.elapsed();
app.current_window_time = Instant::now(); app.current_window_time = Instant::now();
app.current_window_title = window_title; app.current_window_title = window_title;
app.session_ratings = session_stats_as_vec(&app.session_stats);
} }
} }
@@ -262,7 +322,6 @@ fn ui(frame: &mut Frame, app: &App) {
} else { } else {
BorderType::Plain BorderType::Plain
}; };
frame.render_widget( frame.render_widget(
Paragraph::new(input_duration).block( Paragraph::new(input_duration).block(
Block::bordered() Block::bordered()
@@ -272,7 +331,7 @@ fn ui(frame: &mut Frame, app: &App) {
layout_duration, layout_duration,
); );
let stats = session_stats_to_lines(&app.session_stats); let stats = app.get_session_stats();
frame.render_widget( frame.render_widget(
Paragraph::new(stats).block(Block::bordered().title("Session")), Paragraph::new(stats).block(Block::bordered().title("Session")),
layout_titles, layout_titles,