2022-12-12 20:52:10 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2022-12-19 11:19:33 -05:00
|
|
|
use std::sync::atomic::AtomicU64;
|
|
|
|
use std::sync::atomic::Ordering;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::SystemTime;
|
|
|
|
|
|
|
|
use deno_core::parking_lot::Mutex;
|
|
|
|
use deno_runtime::ops::tty::ConsoleSize;
|
|
|
|
|
2022-12-12 20:52:10 -05:00
|
|
|
use crate::colors;
|
|
|
|
|
2022-12-19 11:19:33 -05:00
|
|
|
use self::renderer::ProgressBarRenderer;
|
|
|
|
use self::renderer::ProgressData;
|
|
|
|
use self::renderer::ProgressDataDisplayEntry;
|
2022-12-12 20:52:10 -05:00
|
|
|
|
2022-12-19 11:19:33 -05:00
|
|
|
use super::draw_thread::DrawThread;
|
|
|
|
use super::draw_thread::DrawThreadGuard;
|
|
|
|
use super::draw_thread::DrawThreadRenderer;
|
2022-12-12 20:52:10 -05:00
|
|
|
|
|
|
|
mod renderer;
|
|
|
|
|
|
|
|
// Inspired by Indicatif, but this custom implementation allows
|
|
|
|
// for more control over what's going on under the hood.
|
|
|
|
|
|
|
|
pub struct UpdateGuard {
|
|
|
|
maybe_entry: Option<ProgressBarEntry>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for UpdateGuard {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if let Some(entry) = &self.maybe_entry {
|
|
|
|
entry.finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UpdateGuard {
|
|
|
|
pub fn set_position(&self, value: u64) {
|
|
|
|
if let Some(entry) = &self.maybe_entry {
|
|
|
|
entry.set_position(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_total_size(&self, value: u64) {
|
|
|
|
if let Some(entry) = &self.maybe_entry {
|
|
|
|
entry.set_total_size(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum ProgressBarStyle {
|
|
|
|
DownloadBars,
|
|
|
|
TextOnly,
|
|
|
|
}
|
|
|
|
|
2022-12-19 11:19:33 -05:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
struct ProgressBarEntry {
|
|
|
|
id: usize,
|
|
|
|
pub message: String,
|
|
|
|
pos: Arc<AtomicU64>,
|
|
|
|
total_size: Arc<AtomicU64>,
|
|
|
|
progress_bar: ProgressBarInner,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ProgressBarEntry {
|
|
|
|
pub fn position(&self) -> u64 {
|
|
|
|
self.pos.load(Ordering::Relaxed)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_position(&self, new_pos: u64) {
|
|
|
|
self.pos.store(new_pos, Ordering::Relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn total_size(&self) -> u64 {
|
|
|
|
self.total_size.load(Ordering::Relaxed)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_total_size(&self, new_size: u64) {
|
|
|
|
self.total_size.store(new_size, Ordering::Relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn finish(&self) {
|
|
|
|
self.progress_bar.finish_entry(self.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn percent(&self) -> f64 {
|
|
|
|
let pos = self.pos.load(Ordering::Relaxed) as f64;
|
|
|
|
let total_size = self.total_size.load(Ordering::Relaxed) as f64;
|
|
|
|
if total_size == 0f64 {
|
|
|
|
0f64
|
|
|
|
} else {
|
|
|
|
pos / total_size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct InternalState {
|
|
|
|
/// If this guard exists, then it means the progress
|
|
|
|
/// bar is displaying in the draw thread.
|
|
|
|
draw_thread_guard: Option<DrawThreadGuard>,
|
|
|
|
start_time: SystemTime,
|
|
|
|
keep_alive_count: usize,
|
|
|
|
total_entries: usize,
|
|
|
|
entries: Vec<ProgressBarEntry>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
struct ProgressBarInner {
|
|
|
|
state: Arc<Mutex<InternalState>>,
|
|
|
|
renderer: Arc<dyn ProgressBarRenderer>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ProgressBarInner {
|
|
|
|
fn new(renderer: Arc<dyn ProgressBarRenderer>) -> Self {
|
|
|
|
Self {
|
|
|
|
state: Arc::new(Mutex::new(InternalState {
|
|
|
|
draw_thread_guard: None,
|
|
|
|
start_time: SystemTime::now(),
|
|
|
|
keep_alive_count: 0,
|
|
|
|
total_entries: 0,
|
|
|
|
entries: Vec::new(),
|
|
|
|
})),
|
|
|
|
renderer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_entry(&self, message: String) -> ProgressBarEntry {
|
|
|
|
let mut internal_state = self.state.lock();
|
|
|
|
let id = internal_state.total_entries;
|
|
|
|
let entry = ProgressBarEntry {
|
|
|
|
id,
|
|
|
|
message,
|
|
|
|
pos: Default::default(),
|
|
|
|
total_size: Default::default(),
|
|
|
|
progress_bar: self.clone(),
|
|
|
|
};
|
|
|
|
internal_state.entries.push(entry.clone());
|
|
|
|
internal_state.total_entries += 1;
|
|
|
|
internal_state.keep_alive_count += 1;
|
|
|
|
|
|
|
|
self.maybe_start_draw_thread(&mut internal_state);
|
|
|
|
|
|
|
|
entry
|
|
|
|
}
|
|
|
|
|
|
|
|
fn finish_entry(&self, entry_id: usize) {
|
|
|
|
let mut internal_state = self.state.lock();
|
|
|
|
|
|
|
|
if let Ok(index) = internal_state
|
|
|
|
.entries
|
|
|
|
.binary_search_by(|e| e.id.cmp(&entry_id))
|
|
|
|
{
|
|
|
|
internal_state.entries.remove(index);
|
|
|
|
self.decrement_keep_alive(&mut internal_state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn increment_clear(&self) {
|
|
|
|
let mut internal_state = self.state.lock();
|
|
|
|
internal_state.keep_alive_count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn decrement_clear(&self) {
|
|
|
|
let mut internal_state = self.state.lock();
|
|
|
|
self.decrement_keep_alive(&mut internal_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn decrement_keep_alive(&self, state: &mut InternalState) {
|
|
|
|
state.keep_alive_count -= 1;
|
|
|
|
|
|
|
|
if state.keep_alive_count == 0 {
|
|
|
|
// drop the guard to remove this from the draw thread
|
|
|
|
state.draw_thread_guard.take();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn maybe_start_draw_thread(&self, internal_state: &mut InternalState) {
|
|
|
|
if internal_state.draw_thread_guard.is_none()
|
|
|
|
&& internal_state.keep_alive_count > 0
|
|
|
|
{
|
|
|
|
internal_state.start_time = SystemTime::now();
|
|
|
|
internal_state.draw_thread_guard =
|
|
|
|
Some(DrawThread::add_entry(0, Arc::new(self.clone())));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DrawThreadRenderer for ProgressBarInner {
|
|
|
|
fn render(&self, size: &ConsoleSize) -> String {
|
|
|
|
let data = {
|
|
|
|
let state = self.state.lock();
|
|
|
|
if state.entries.is_empty() {
|
|
|
|
return String::new();
|
|
|
|
}
|
|
|
|
let preferred_entry = state
|
|
|
|
.entries
|
|
|
|
.iter()
|
|
|
|
.find(|e| e.percent() > 0f64)
|
|
|
|
.or_else(|| state.entries.iter().last())
|
|
|
|
.unwrap();
|
|
|
|
ProgressData {
|
|
|
|
duration: state.start_time.elapsed().unwrap(),
|
|
|
|
terminal_width: size.cols,
|
|
|
|
pending_entries: state.entries.len(),
|
|
|
|
total_entries: state.total_entries,
|
|
|
|
display_entry: ProgressDataDisplayEntry {
|
|
|
|
message: preferred_entry.message.clone(),
|
|
|
|
position: preferred_entry.position(),
|
|
|
|
total_size: preferred_entry.total_size(),
|
|
|
|
},
|
|
|
|
percent_done: {
|
|
|
|
let mut total_percent_sum = 0f64;
|
|
|
|
for entry in &state.entries {
|
|
|
|
total_percent_sum += entry.percent();
|
|
|
|
}
|
|
|
|
total_percent_sum +=
|
|
|
|
(state.total_entries - state.entries.len()) as f64;
|
|
|
|
total_percent_sum / (state.total_entries as f64)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
self.renderer.render(data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-12 20:52:10 -05:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct ProgressBar {
|
2022-12-19 11:19:33 -05:00
|
|
|
inner: Option<ProgressBarInner>,
|
2022-12-12 20:52:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ProgressBar {
|
|
|
|
/// Checks if progress bars are supported
|
|
|
|
pub fn are_supported() -> bool {
|
2022-12-19 11:19:33 -05:00
|
|
|
DrawThread::is_supported()
|
2022-12-12 20:52:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(style: ProgressBarStyle) -> Self {
|
|
|
|
Self {
|
2022-12-19 11:19:33 -05:00
|
|
|
inner: match Self::are_supported() {
|
|
|
|
true => Some(ProgressBarInner::new(match style {
|
2022-12-12 20:52:10 -05:00
|
|
|
ProgressBarStyle::DownloadBars => {
|
2022-12-19 11:19:33 -05:00
|
|
|
Arc::new(renderer::BarProgressBarRenderer)
|
2022-12-12 20:52:10 -05:00
|
|
|
}
|
|
|
|
ProgressBarStyle::TextOnly => {
|
2022-12-19 11:19:33 -05:00
|
|
|
Arc::new(renderer::TextOnlyProgressBarRenderer)
|
2022-12-12 20:52:10 -05:00
|
|
|
}
|
|
|
|
})),
|
|
|
|
false => None,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update(&self, msg: &str) -> UpdateGuard {
|
2022-12-19 11:19:33 -05:00
|
|
|
match &self.inner {
|
|
|
|
Some(inner) => {
|
|
|
|
let entry = inner.add_entry(msg.to_string());
|
2022-12-12 20:52:10 -05:00
|
|
|
UpdateGuard {
|
|
|
|
maybe_entry: Some(entry),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// if we're not running in TTY, fallback to using logger crate
|
|
|
|
if !msg.is_empty() {
|
|
|
|
log::log!(log::Level::Info, "{} {}", colors::green("Download"), msg);
|
|
|
|
}
|
|
|
|
UpdateGuard { maybe_entry: None }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear_guard(&self) -> ClearGuard {
|
2022-12-19 11:19:33 -05:00
|
|
|
if let Some(inner) = &self.inner {
|
|
|
|
inner.increment_clear();
|
2022-12-12 20:52:10 -05:00
|
|
|
}
|
|
|
|
ClearGuard { pb: self.clone() }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn decrement_clear(&self) {
|
2022-12-19 11:19:33 -05:00
|
|
|
if let Some(inner) = &self.inner {
|
|
|
|
inner.decrement_clear();
|
2022-12-12 20:52:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ClearGuard {
|
|
|
|
pb: ProgressBar,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for ClearGuard {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.pb.decrement_clear();
|
|
|
|
}
|
|
|
|
}
|