use std::cell::RefCell;
use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
use crate::cells::cell_len;
use crate::console::{Console, ConsoleOptions};
use crate::filesize;
use crate::progress_bar::ProgressBar;
use crate::protocol::Renderable;
use crate::segment::Segment;
use crate::spinner::Spinner;
use crate::style::{Style, StyleType};
use crate::text::Text;
const BAR_MAX_WIDTH: usize = 40;
const MAX_SAMPLES: usize = 1000;
pub type GetTime = Arc<dyn Fn() -> f64 + Send + Sync>;
fn monotonic() -> f64 {
static START: std::sync::OnceLock<std::time::Instant> = std::sync::OnceLock::new();
START
.get_or_init(std::time::Instant::now)
.elapsed()
.as_secs_f64()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct TaskId(pub usize);
pub struct TimeRemainingColumn {
compact: bool,
elapsed_when_finished: bool,
cache: RefCell<HashMap<TaskId, (f64, Text)>>,
}
impl TimeRemainingColumn {
pub fn new(compact: bool, elapsed_when_finished: bool) -> Self {
TimeRemainingColumn {
compact,
elapsed_when_finished,
cache: RefCell::new(HashMap::new()),
}
}
}
pub struct SpinnerColumn {
spinner: Spinner,
style: StyleType,
finished_text: String,
}
impl SpinnerColumn {
pub fn new(name: &str, finished_text: impl Into<String>) -> Self {
SpinnerColumn {
spinner: Spinner::new(name),
style: StyleType::Name("progress.spinner".to_string()),
finished_text: finished_text.into(),
}
}
pub fn speed(mut self, speed: f64) -> Self {
self.spinner = self.spinner.speed(speed);
self
}
pub fn style(mut self, style: impl Into<StyleType>) -> Self {
self.style = style.into();
self
}
}
pub enum ProgressColumn {
Description,
Text(String, Style),
Bar,
Percentage,
TaskProgress { show_speed: bool },
MofN,
Download,
BinaryDownload,
TimeElapsed,
TimeRemaining(TimeRemainingColumn),
TransferSpeed,
FileSize,
TotalFileSize,
Spinner(SpinnerColumn),
}
impl ProgressColumn {
pub fn time_remaining() -> Self {
ProgressColumn::TimeRemaining(TimeRemainingColumn::new(false, false))
}
pub fn spinner() -> Self {
ProgressColumn::Spinner(SpinnerColumn::new("dots", " "))
}
fn is_bar(&self) -> bool {
matches!(self, ProgressColumn::Bar)
}
fn cell(&self, task: &Task) -> Text {
let named = |plain: String, style: &str| Text::styled(plain, style);
match self {
ProgressColumn::Description => {
let markup = format!("[progress.description]{}", task.description);
Text::from_markup(&markup).unwrap_or_else(|_| Text::new(task.description.clone()))
}
ProgressColumn::Text(text, style) => Text::styled(text.clone(), style.clone()),
ProgressColumn::Bar => unreachable!("bar column has no text cell"),
ProgressColumn::Percentage => task.percentage_cell(),
ProgressColumn::TaskProgress { show_speed } => {
if task.total.is_none() && *show_speed {
render_speed(
task.finished_speed
.filter(|s| *s != 0.0)
.or_else(|| task.speed()),
)
} else {
task.percentage_cell()
}
}
ProgressColumn::MofN => named(task.mofn_text(), "progress.download"),
ProgressColumn::Download => named(task.download_text(false), "progress.download"),
ProgressColumn::BinaryDownload => named(task.download_text(true), "progress.download"),
ProgressColumn::TimeElapsed => {
let elapsed = if task.finished() {
task.finished_time
} else {
task.elapsed()
};
let text = match elapsed {
None => "-:--:--".to_string(),
Some(elapsed) => timedelta(elapsed.max(0.0) as i64),
};
named(text, "progress.elapsed")
}
ProgressColumn::TimeRemaining(column) => column.render(task),
ProgressColumn::TransferSpeed => {
let speed = task
.finished_speed
.filter(|s| *s != 0.0)
.or_else(|| task.speed());
let text = match speed {
None => "?".to_string(),
Some(speed) => format!("{}/s", filesize::decimal(speed as u64)),
};
named(text, "progress.data.speed")
}
ProgressColumn::FileSize => named(
filesize::decimal(task.completed as u64),
"progress.filesize",
),
ProgressColumn::TotalFileSize => named(
task.total
.map_or_else(String::new, |total| filesize::decimal(total as u64)),
"progress.filesize.total",
),
ProgressColumn::Spinner(column) => {
if task.finished() {
Text::from_markup(&column.finished_text)
.unwrap_or_else(|_| Text::new(column.finished_text.clone()))
} else {
let mut frame = column.spinner.render(task.now());
frame.set_base_style(column.style.clone());
frame
}
}
}
}
}
impl TimeRemainingColumn {
fn render(&self, task: &Task) -> Text {
let now = task.now();
if task.completed == 0.0 {
if let Some((timestamp, text)) = self.cache.borrow().get(&task.id) {
if timestamp + 0.5 > now {
return text.clone();
}
}
}
let (task_time, style) = if self.elapsed_when_finished && task.finished() {
(task.finished_time, "progress.elapsed")
} else {
(task.time_remaining(), "progress.remaining")
};
let text = if task.total.is_none() {
Text::styled("", style)
} else {
match task_time {
None => Text::styled(if self.compact { "--:--" } else { "-:--:--" }, style),
Some(task_time) => {
let whole = task_time as i64;
let (minutes, seconds) = (whole.div_euclid(60), whole.rem_euclid(60));
let (hours, minutes) = (minutes.div_euclid(60), minutes.rem_euclid(60));
let formatted = if self.compact && hours == 0 {
format!("{minutes:02}:{seconds:02}")
} else {
format!("{hours}:{minutes:02}:{seconds:02}")
};
Text::styled(formatted, style)
}
}
};
self.cache.borrow_mut().insert(task.id, (now, text.clone()));
text
}
}
fn render_speed(speed: Option<f64>) -> Text {
let Some(speed) = speed else {
return Text::styled("", "progress.percentage");
};
let (unit, suffix) =
filesize::pick_unit_and_suffix(speed as u64, &["", "×10³", "×10⁶", "×10⁹", "×10¹²"], 1000);
let data_speed = speed / unit as f64;
Text::styled(
format!("{data_speed:.1}{suffix} it/s"),
"progress.percentage",
)
}
fn timedelta(total_seconds: i64) -> String {
let days = total_seconds / 86_400;
let rest = total_seconds % 86_400;
let clock = format!("{}:{:02}:{:02}", rest / 3600, rest % 3600 / 60, rest % 60);
match days {
0 => clock,
1 => format!("1 day, {clock}"),
days => format!("{days} days, {clock}"),
}
}
fn grouped(value: f64, precision: usize) -> String {
let formatted = format!("{value:.precision$}");
let (sign, digits) = match formatted.strip_prefix('-') {
Some(rest) => ("-", rest),
None => ("", formatted.as_str()),
};
let (integer, fraction) = match digits.split_once('.') {
Some((integer, fraction)) => (integer, Some(fraction)),
None => (digits, None),
};
let mut grouped = String::new();
for (index, digit) in integer.chars().enumerate() {
if index > 0 && (integer.len() - index) % 3 == 0 {
grouped.push(',');
}
grouped.push(digit);
}
match fraction {
Some(fraction) => format!("{sign}{grouped}.{fraction}"),
None => format!("{sign}{grouped}"),
}
}
pub struct Task {
id: TaskId,
description: String,
total: Option<f64>,
completed: f64,
visible: bool,
start_time: Option<f64>,
stop_time: Option<f64>,
finished_time: Option<f64>,
finished_speed: Option<f64>,
samples: VecDeque<(f64, f64)>,
get_time: GetTime,
}
impl Task {
fn now(&self) -> f64 {
(self.get_time)()
}
pub fn id(&self) -> TaskId {
self.id
}
pub fn description(&self) -> &str {
&self.description
}
pub fn total(&self) -> Option<f64> {
self.total
}
pub fn completed(&self) -> f64 {
self.completed
}
pub fn visible(&self) -> bool {
self.visible
}
pub fn started(&self) -> bool {
self.start_time.is_some()
}
pub fn remaining(&self) -> Option<f64> {
self.total.map(|total| total - self.completed)
}
pub fn elapsed(&self) -> Option<f64> {
let start = self.start_time?;
Some(self.stop_time.unwrap_or_else(|| self.now()) - start)
}
pub fn finished(&self) -> bool {
self.finished_time.is_some()
}
pub fn finished_time(&self) -> Option<f64> {
self.finished_time
}
pub fn percentage(&self) -> f64 {
match self.total {
Some(total) if total != 0.0 => (self.completed / total * 100.0).clamp(0.0, 100.0),
_ => 0.0,
}
}
pub fn speed(&self) -> Option<f64> {
self.start_time?;
let (first, _) = *self.samples.front()?;
let (last, _) = *self.samples.back()?;
let total_time = last - first;
if total_time == 0.0 {
return None;
}
let total_completed: f64 = self.samples.iter().skip(1).map(|(_, done)| done).sum();
Some(total_completed / total_time)
}
pub fn time_remaining(&self) -> Option<f64> {
if self.finished() {
return Some(0.0);
}
let speed = self.speed().filter(|speed| *speed != 0.0)?;
let remaining = self.remaining()?;
Some((remaining / speed).ceil())
}
fn clear_progress(&mut self) {
self.samples.clear();
self.finished_time = None;
self.finished_speed = None;
}
fn percentage_cell(&self) -> Text {
if self.total.is_none() {
return Text::new("");
}
let mut text = Text::new(format!("{:>3.0}%", self.percentage()));
let len = text.plain().len();
text.stylize("progress.percentage", 0, len);
text
}
fn mofn_text(&self) -> String {
let completed = self.completed as i64;
let total = self
.total
.map_or_else(|| "?".to_string(), |total| (total as i64).to_string());
let total_width = total.chars().count();
format!("{completed:>total_width$}/{total}")
}
fn download_text(&self, binary: bool) -> String {
const DECIMAL: &[&str] = &["bytes", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
const BINARY: &[&str] = &[
"bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB",
];
let completed = self.completed as u64;
let base_size = self.total.map_or(completed, |total| total as u64);
let (unit, suffix) = if binary {
filesize::pick_unit_and_suffix(base_size, BINARY, 1024)
} else {
filesize::pick_unit_and_suffix(base_size, DECIMAL, 1000)
};
let precision = if unit == 1 { 0 } else { 1 };
let completed_str = grouped(completed as f64 / unit as f64, precision);
let total_str = self.total.map_or_else(
|| "?".to_string(),
|total| grouped((total as u64) as f64 / unit as f64, precision),
);
format!("{completed_str}/{total_str} {suffix}")
}
}
#[derive(Debug, Clone, Default)]
pub struct TaskUpdate {
pub total: Option<f64>,
pub completed: Option<f64>,
pub advance: Option<f64>,
pub description: Option<String>,
pub visible: Option<bool>,
}
impl TaskUpdate {
pub fn total(mut self, total: f64) -> Self {
self.total = Some(total);
self
}
pub fn completed(mut self, completed: f64) -> Self {
self.completed = Some(completed);
self
}
pub fn advance(mut self, advance: f64) -> Self {
self.advance = Some(advance);
self
}
pub fn description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
pub fn visible(mut self, visible: bool) -> Self {
self.visible = Some(visible);
self
}
}
pub struct Progress {
tasks: Vec<Task>,
next_id: usize,
columns: Vec<ProgressColumn>,
get_time: GetTime,
speed_estimate_period: f64,
}
impl Default for Progress {
fn default() -> Self {
Progress {
tasks: Vec::new(),
next_id: 0,
columns: Progress::default_columns(),
get_time: Arc::new(monotonic),
speed_estimate_period: 30.0,
}
}
}
impl Progress {
pub fn new() -> Self {
Progress::default()
}
pub fn default_columns() -> Vec<ProgressColumn> {
vec![
ProgressColumn::Description,
ProgressColumn::Bar,
ProgressColumn::Percentage,
ProgressColumn::time_remaining(),
]
}
pub fn columns(mut self, columns: Vec<ProgressColumn>) -> Self {
self.columns = columns;
self
}
pub fn clock(mut self, clock: impl Fn() -> f64 + Send + Sync + 'static) -> Self {
self.get_time = Arc::new(clock);
for task in &mut self.tasks {
task.get_time = self.get_time.clone();
}
self
}
pub fn speed_estimate_period(mut self, seconds: f64) -> Self {
self.speed_estimate_period = seconds;
self
}
fn now(&self) -> f64 {
(self.get_time)()
}
fn task_mut(&mut self, id: TaskId) -> Option<&mut Task> {
self.tasks.iter_mut().find(|task| task.id == id)
}
pub fn add_task(
&mut self,
description: impl Into<String>,
total: impl Into<Option<f64>>,
completed: f64,
) -> TaskId {
let id = self.push_task(description.into(), total.into(), completed);
self.start_task(id);
id
}
pub fn add_unstarted_task(
&mut self,
description: impl Into<String>,
total: impl Into<Option<f64>>,
completed: f64,
) -> TaskId {
self.push_task(description.into(), total.into(), completed)
}
fn push_task(&mut self, description: String, total: Option<f64>, completed: f64) -> TaskId {
let id = TaskId(self.next_id);
self.next_id += 1;
self.tasks.push(Task {
id,
description,
total,
completed,
visible: true,
start_time: None,
stop_time: None,
finished_time: None,
finished_speed: None,
samples: VecDeque::new(),
get_time: self.get_time.clone(),
});
id
}
pub fn task(&self, id: TaskId) -> Option<&Task> {
self.tasks.iter().find(|task| task.id == id)
}
pub fn tasks(&self) -> &[Task] {
&self.tasks
}
pub fn finished(&self) -> bool {
self.tasks.iter().all(Task::finished)
}
pub fn start_task(&mut self, id: TaskId) {
let now = self.now();
if let Some(task) = self.task_mut(id) {
task.start_time.get_or_insert(now);
}
}
pub fn stop_task(&mut self, id: TaskId) {
let now = self.now();
if let Some(task) = self.task_mut(id) {
task.start_time.get_or_insert(now);
task.stop_time = Some(now);
}
}
pub fn update(&mut self, id: TaskId, update: TaskUpdate) {
let now = self.now();
let period = self.speed_estimate_period;
let Some(task) = self.task_mut(id) else {
return;
};
let completed_start = task.completed;
if let Some(total) = update.total {
if Some(total) != task.total {
task.total = Some(total);
task.clear_progress();
}
}
if let Some(advance) = update.advance {
task.completed += advance;
}
if let Some(completed) = update.completed {
task.completed = completed;
}
if let Some(description) = update.description {
task.description = description;
}
if let Some(visible) = update.visible {
task.visible = visible;
}
let update_completed = task.completed - completed_start;
let old_sample_time = now - period;
while task
.samples
.front()
.is_some_and(|(time, _)| *time < old_sample_time)
{
task.samples.pop_front();
}
if update_completed > 0.0 {
task.samples.push_back((now, update_completed));
if task.samples.len() > MAX_SAMPLES {
task.samples.pop_front();
}
}
if task.total.is_some_and(|total| task.completed >= total) && task.finished_time.is_none() {
task.finished_time = task.elapsed();
}
}
pub fn advance(&mut self, id: TaskId, amount: f64) {
let now = self.now();
let period = self.speed_estimate_period;
let Some(task) = self.task_mut(id) else {
return;
};
let completed_start = task.completed;
task.completed += amount;
let update_completed = task.completed - completed_start;
let old_sample_time = now - period;
while task
.samples
.front()
.is_some_and(|(time, _)| *time < old_sample_time)
{
task.samples.pop_front();
}
while task.samples.len() > MAX_SAMPLES {
task.samples.pop_front();
}
task.samples.push_back((now, update_completed));
if task.samples.len() > MAX_SAMPLES {
task.samples.pop_front();
}
if task.total.is_some_and(|total| task.completed >= total) && task.finished_time.is_none() {
task.finished_time = task.elapsed();
task.finished_speed = task.speed();
}
}
pub fn reset(&mut self, id: TaskId, start: bool, total: Option<f64>, completed: f64) {
let now = self.now();
let Some(task) = self.task_mut(id) else {
return;
};
task.clear_progress();
task.start_time = start.then_some(now);
if let Some(total) = total {
task.total = Some(total);
}
task.completed = completed;
task.finished_time = None;
}
pub fn remove_task(&mut self, id: TaskId) {
self.tasks.retain(|task| task.id != id);
}
}
impl Renderable for Progress {
fn rich_render(&self, console: &Console, options: &ConsoleOptions) -> Vec<Segment> {
let width = options.max_width;
let ncols = self.columns.len();
let tasks: Vec<&Task> = self.tasks.iter().filter(|task| task.visible).collect();
let cells: Vec<Vec<Option<Text>>> = tasks
.iter()
.map(|task| {
self.columns
.iter()
.map(|column| (!column.is_bar()).then(|| column.cell(task)))
.collect()
})
.collect();
let mut col_widths = vec![0usize; ncols];
for row in &cells {
for (index, cell) in row.iter().enumerate() {
if let Some(cell) = cell {
col_widths[index] = col_widths[index].max(cell_len(cell.plain()));
}
}
}
let gaps = ncols.saturating_sub(1);
let fixed_sum: usize = col_widths.iter().sum();
let bar_count = self.columns.iter().filter(|c| c.is_bar()).count();
let bar_width = width
.saturating_sub(fixed_sum + gaps)
.checked_div(bar_count)
.map_or(0, |per_bar| BAR_MAX_WIDTH.min(per_bar));
for (index, column) in self.columns.iter().enumerate() {
if column.is_bar() {
col_widths[index] = bar_width;
}
}
let theme = console.theme();
let mut lines: Vec<Vec<Segment>> = Vec::with_capacity(tasks.len());
for (task, row_cells) in tasks.iter().zip(cells) {
let mut row: Vec<Segment> = Vec::new();
for (index, (column, cell)) in self.columns.iter().zip(row_cells).enumerate() {
if index > 0 {
row.push(Segment::new(" ", None));
}
if column.is_bar() {
let bar = ProgressBar::new(
task.total.unwrap_or(0.0).max(0.0),
task.completed.max(0.0),
)
.width(bar_width);
row.extend(bar.rich_render(console, &options.update_width(bar_width)));
} else if let Some(mut cell) = cell {
cell.truncate(col_widths[index], None, true);
row.extend(cell.render(theme, &Style::new()));
}
}
lines.push(row);
}
let mut segments = Vec::new();
let last = lines.len().saturating_sub(1);
for (index, line) in lines.into_iter().enumerate() {
segments.extend(line);
if index != last {
segments.push(Segment::line());
}
}
segments
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::color::ColorSystem;
fn render(progress: &Progress) -> String {
Console::builder()
.force_terminal(true)
.color_system(Some(ColorSystem::Truecolor))
.width(50)
.no_color(false)
.build()
.render_to_string(progress)
}
#[test]
fn three_tasks_match_upstream() {
let mut progress = Progress::new().columns(vec![
ProgressColumn::Description,
ProgressColumn::Bar,
ProgressColumn::Percentage,
]);
progress.add_task("Downloading", 100.0, 50.0);
progress.add_task("Processing", 100.0, 100.0);
progress.add_task("Waiting", 100.0, 0.0);
let expected = concat!(
"Downloading \x1b[38;2;249;38;114m━━━━━━━━━━━━━━━━\x1b[0m",
"\x1b[38;2;249;38;114m╸\x1b[0m\x1b[38;5;237m━━━━━━━━━━━━━━━━\x1b[0m \x1b[35m 50%\x1b[0m\n",
"Processing \x1b[38;2;114;156;31m",
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m \x1b[35m100%\x1b[0m\n",
"Waiting \x1b[38;5;237m",
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m \x1b[35m 0%\x1b[0m",
);
assert_eq!(render(&progress), expected);
}
#[test]
fn download_text_matches_upstream() {
let dl = |completed: f64, total: f64| {
let mut progress = Progress::new();
let id = progress.add_task("", total, completed);
progress.task(id).unwrap().download_text(false)
};
assert_eq!(dl(500.0, 1000.0), "0.5/1.0 kB");
assert_eq!(dl(500.0, 999.0), "500/999 bytes");
assert_eq!(dl(1_500_000.0, 3_000_000.0), "1.5/3.0 MB");
assert_eq!(dl(0.0, 1024.0), "0.0/1.0 kB");
assert_eq!(dl(2_500_000_000.0, 10_000_000_000.0), "2.5/10.0 GB");
assert_eq!(dl(250.0, 250.0), "250/250 bytes");
}
#[test]
fn download_column_in_grid_matches_upstream() {
let mut progress = Progress::new().columns(vec![
ProgressColumn::Description,
ProgressColumn::Bar,
ProgressColumn::Download,
]);
progress.add_task("File", 1000.0, 500.0);
let expected = concat!(
"File \x1b[38;2;249;38;114m━━━━━━━━━━━━━━━━━\x1b[0m\x1b[38;5;237m╺\x1b[0m",
"\x1b[38;5;237m━━━━━━━━━━━━━━━━\x1b[0m \x1b[32m0.5/1.0 kB\x1b[0m",
);
assert_eq!(render(&progress), expected);
}
#[test]
fn custom_columns_with_mofn_match_upstream() {
let mut progress = Progress::new().columns(vec![
ProgressColumn::Description,
ProgressColumn::Bar,
ProgressColumn::MofN,
]);
progress.add_task("A", 5.0, 3.0);
progress.add_task("B", 100.0, 50.0);
let console = Console::builder()
.force_terminal(true)
.color_system(Some(ColorSystem::Truecolor))
.width(40)
.no_color(false)
.build();
let expected = concat!(
"A \x1b[38;2;249;38;114m━━━━━━━━━━━━━━━━━━\x1b[0m\x1b[38;5;237m╺\x1b[0m",
"\x1b[38;5;237m━━━━━━━━━━━\x1b[0m \x1b[32m3/5 \x1b[0m\n",
"B \x1b[38;2;249;38;114m━━━━━━━━━━━━━━━\x1b[0m\x1b[38;5;237m╺\x1b[0m",
"\x1b[38;5;237m━━━━━━━━━━━━━━\x1b[0m \x1b[32m 50/100\x1b[0m",
);
assert_eq!(console.render_to_string(&progress), expected);
}
}