use std::time::{Duration, Instant};
use lang::by_code;
use ratatui::{buffer::Buffer, layout::Rect};
use super::{
PoolCache, done::DoneFeed, draw_done, draw_foot, draw_pool, draw_sec_head, draw_task, row_rect,
};
use crate::{
board::{Act, Board, Ended},
event::Event,
i18n::T,
tty::FOOT_ROW,
};
pub(super) const FADE: Duration = Duration::from_secs(3);
const SRC_SEP: &str = " / ";
pub(super) struct Fade {
pub(super) ended: Ended,
pub(super) begin: Instant,
until: Instant,
}
#[derive(Default)]
pub(super) struct Stage {
pool: PoolCache,
pub(super) fade_li: Vec<Fade>,
pub(super) done: DoneFeed,
src_li: Vec<String>,
src: String,
src_n: usize,
}
impl Stage {
pub(super) fn apply(&mut self, board: &mut Board, event: Event) -> bool {
match board.apply(event) {
Act::None | Act::Plan { .. } | Act::Retry(_) => {}
Act::Start(id) => {
if let Some(run) = board.run(id) {
let from = run.name.from();
if !self.src_li.iter().any(|s| s == from) {
self.src_li.push(from.to_owned());
}
}
}
Act::Text { id, text, reply } => {
if let Some(run) = board.run(id) {
run.push_tail(&text, reply);
}
}
Act::End(mut ended) => {
ended.think = String::new();
if ended.err.is_some() {
let now = Instant::now();
let begin = Duration::try_from_secs_f32(ended.sec)
.ok()
.and_then(|d| now.checked_sub(d))
.unwrap_or(now);
self.fade_li.push(Fade {
begin,
until: now + FADE,
ended,
});
} else {
self.done.push_ended(ended);
}
}
Act::Log(text) => self.done.push(super::done::DoneItem::Log(text)),
Act::Close => return true,
}
false
}
pub(super) fn reap(&mut self, now: Instant) {
for fade in self.fade_li.extract_if(.., |f| f.until <= now) {
self.done.push_ended(fade.ended);
}
self.done.reap_expired(now);
}
pub(super) fn flush(&mut self) {
for fade in self.fade_li.drain(..) {
self.done.push_ended(fade.ended);
}
}
fn src_title(&mut self) -> &str {
if self.src_n != self.src_li.len() {
self.src_n = self.src_li.len();
self.src.clear();
for (i, code) in self.src_li.iter().enumerate() {
if i > 0 {
self.src.push_str(SRC_SEP);
}
self.src.push_str(src_name(code));
}
}
&self.src
}
}
fn src_name(code: &str) -> &str {
by_code(code).map_or(code, |c| c.name())
}
pub(super) fn draw(area: Rect, buf: &mut Buffer, board: &Board, stage: &mut Stage, frame: usize) {
if area.width == 0 || area.height == 0 {
return;
}
let w = usize::from(area.width);
let avail = area.height.saturating_sub(FOOT_ROW);
let mut y = area.y;
let slot_cnt = board.slot_li.len();
let pool_row = if slot_cnt == 0 || avail == 0 {
0
} else {
let max_pool = ((u32::from(avail) * 2) / 5)
.clamp(1, 12)
.min(u32::from(avail.saturating_sub(4).max(1))) as u16;
(slot_cnt.min(usize::from(u16::MAX)) as u16).min(max_pool)
};
if pool_row > 0 {
draw_pool(
Rect::new(area.x, y, area.width, pool_row),
buf,
board,
&mut stage.pool,
);
y += pool_row;
}
let rem = avail.saturating_sub(pool_row);
if rem >= 1 {
let name = stage.src_title();
let run_title = if board.pool_stat.queued > 0 {
T.ui
.sec_run_q(board.active_conc(), name, board.pool_stat.queued)
} else {
T.ui.sec_run(board.active_conc(), name)
};
draw_sec_head(row_rect(area, y), buf, &run_title, w);
y += 1;
let body_rem = rem.saturating_sub(1);
let rain_zone = Rect::new(area.x, y, area.width, body_rem);
let done_sec = if body_rem >= 3 {
((u32::from(body_rem) * 2) / 5)
.max(2)
.min(u32::from(body_rem.saturating_sub(1))) as u16
} else {
0
};
let task_row = body_rem.saturating_sub(done_sec);
if task_row > 0 {
draw_task(
Rect::new(area.x, y, area.width, task_row),
rain_zone,
buf,
board,
&stage.fade_li,
frame,
);
y += task_row;
}
if done_sec > 0 {
let done_title = if board.fail > 0 {
T.ui.sec_done_fail(board.ok, board.fail)
} else {
T.ui.sec_done(board.ok)
};
draw_sec_head(row_rect(area, y), buf, &done_title, w);
y += 1;
let done_row = done_sec.saturating_sub(1);
draw_done(
Rect::new(area.x, y, area.width, done_row),
rain_zone,
buf,
&stage.done,
frame,
);
}
}
draw_foot(
board,
row_rect(area, area.bottom().saturating_sub(1)),
buf,
w,
);
}