Show all session results to resolve #3
This commit is contained in:
82
src/main.rs
82
src/main.rs
@@ -28,6 +28,43 @@ enum State {
|
||||
ShouldQuit,
|
||||
}
|
||||
|
||||
struct SessionResult {
|
||||
intention: String,
|
||||
duration: Duration,
|
||||
session_ratings: Vec<SessionRating>,
|
||||
rating: u8,
|
||||
rating_f64: f64,
|
||||
}
|
||||
|
||||
impl SessionResult {
|
||||
fn rate(&mut self) {
|
||||
let mut rating = 0_f64;
|
||||
let total_duration = self
|
||||
.session_ratings
|
||||
.iter()
|
||||
.map(|r| r.duration)
|
||||
.fold(Duration::ZERO, |acc, dur| acc.saturating_add(dur));
|
||||
|
||||
for session_rating in &self.session_ratings {
|
||||
let ratio: f64 = session_rating.duration.as_secs_f64() / total_duration.as_secs_f64();
|
||||
rating += (session_rating.rating as f64) * ratio;
|
||||
}
|
||||
|
||||
self.rating_f64 = rating;
|
||||
|
||||
if rating > 2.5 {
|
||||
self.rating = 3;
|
||||
} else if rating > 2.0 {
|
||||
self.rating = 2;
|
||||
} else if rating > 1.0 {
|
||||
self.rating = 1;
|
||||
} else {
|
||||
self.rating = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct SessionRating {
|
||||
window_title: Rc<String>,
|
||||
duration: Duration,
|
||||
@@ -46,6 +83,7 @@ struct App {
|
||||
session_remaining: Duration,
|
||||
session_ratings: Vec<SessionRating>,
|
||||
session_ratings_index: usize,
|
||||
session_results: Vec<SessionResult>,
|
||||
last_tick_50ms: Instant,
|
||||
last_tick_1s: Instant,
|
||||
}
|
||||
@@ -66,6 +104,7 @@ impl App {
|
||||
session_remaining: Duration::new(0, 0),
|
||||
session_ratings: Vec::new(),
|
||||
session_ratings_index: 0,
|
||||
session_results: Vec::new(),
|
||||
last_tick_50ms: Instant::now(),
|
||||
last_tick_1s: Instant::now(),
|
||||
}
|
||||
@@ -133,6 +172,22 @@ impl App {
|
||||
Duration::from_millis(50).saturating_sub(self.last_tick_50ms.elapsed())
|
||||
}
|
||||
|
||||
fn get_session_results(&self) -> Vec<Line> {
|
||||
self.session_results
|
||||
.iter()
|
||||
.map(|r| {
|
||||
Line::from(Span::styled(
|
||||
format!("{} {}", duration_as_str(&r.duration), r.intention,),
|
||||
match r.rating {
|
||||
2 => Style::new().fg(Color::LightYellow),
|
||||
3 => Style::new().fg(Color::LightGreen),
|
||||
_ => Style::new().fg(Color::LightRed),
|
||||
},
|
||||
))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn get_session_stats(&self) -> Vec<Line> {
|
||||
let mut zero_encountered = if self.state != State::End {
|
||||
true
|
||||
@@ -270,6 +325,15 @@ fn handle_events(app: &mut App) -> Result<()> {
|
||||
|
||||
if app.session_ratings_index >= app.session_ratings.len() {
|
||||
app.state = State::InputIntention;
|
||||
let mut session_result = SessionResult {
|
||||
intention: app.user_intention.clone(),
|
||||
duration: app.session_start.elapsed(),
|
||||
session_ratings: std::mem::take(&mut app.session_ratings),
|
||||
rating: 0,
|
||||
rating_f64: 0.0,
|
||||
};
|
||||
session_result.rate();
|
||||
app.session_results.push(session_result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -336,11 +400,19 @@ fn ui(frame: &mut Frame, app: &App) {
|
||||
layout_duration,
|
||||
);
|
||||
|
||||
let stats = app.get_session_stats();
|
||||
frame.render_widget(
|
||||
Paragraph::new(stats).block(Block::bordered().title("Session")),
|
||||
layout_titles,
|
||||
);
|
||||
if app.state == State::InputIntention || app.state == State::InputDuration {
|
||||
let results = app.get_session_results();
|
||||
frame.render_widget(
|
||||
Paragraph::new(results).block(Block::bordered().title("Previous Sessions")),
|
||||
layout_titles,
|
||||
);
|
||||
} else {
|
||||
let stats = app.get_session_stats();
|
||||
frame.render_widget(
|
||||
Paragraph::new(stats).block(Block::bordered().title("Session Stats")),
|
||||
layout_titles,
|
||||
);
|
||||
}
|
||||
|
||||
let mut spans: Vec<Span> = Vec::new();
|
||||
if app.user_intention.len() == 0 {
|
||||
|
||||
Reference in New Issue
Block a user