summaryrefslogtreecommitdiff
path: root/cli/util/progress_bar/renderer.rs
blob: 6b08dada12949cc07c70da8787bbdf59bd42ffd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::time::Duration;

use deno_terminal::colors;

use crate::util::display::human_download_size;

use super::ProgressMessagePrompt;

#[derive(Clone)]
pub struct ProgressDataDisplayEntry {
  pub prompt: ProgressMessagePrompt,
  pub message: String,
  pub position: u64,
  pub total_size: u64,
}

#[derive(Clone)]
pub struct ProgressData {
  pub terminal_width: u32,
  pub display_entries: Vec<ProgressDataDisplayEntry>,
  pub pending_entries: usize,
  pub percent_done: f64,
  pub total_entries: usize,
  pub duration: Duration,
}

pub trait ProgressBarRenderer: Send + Sync + std::fmt::Debug {
  fn render(&self, data: ProgressData) -> String;
}

/// Indicatif style progress bar.
#[derive(Debug)]
pub struct BarProgressBarRenderer {
  pub display_human_download_size: bool,
}

impl ProgressBarRenderer for BarProgressBarRenderer {
  fn render(&self, data: ProgressData) -> String {
    // In `ProgressBarRenderer` we only care about first entry.
    let Some(display_entry) = &data.display_entries.first() else {
      return String::new();
    };
    let (bytes_text, bytes_text_max_width) = {
      let total_size = display_entry.total_size;
      let pos = display_entry.position;
      if total_size == 0 {
        (String::new(), 0)
      } else {
        let (pos_str, total_size_str) = if self.display_human_download_size {
          (
            human_download_size(pos, total_size),
            human_download_size(total_size, total_size),
          )
        } else {
          (pos.to_string(), total_size.to_string())
        };
        (
          format!(" {}/{}", pos_str, total_size_str,),
          2 + total_size_str.len() * 2,
        )
      }
    };
    let (total_text, total_text_max_width) = if data.total_entries <= 1 {
      (String::new(), 0)
    } else {
      let total_entries_str = data.total_entries.to_string();
      (
        format!(
          " ({}/{})",
          data.total_entries - data.pending_entries,
          data.total_entries
        ),
        4 + total_entries_str.len() * 2,
      )
    };

    let elapsed_text = get_elapsed_text(data.duration);
    let mut text = String::new();
    if !display_entry.message.is_empty() {
      text.push_str(&format!(
        "{} {}{}\n",
        colors::green("Download"),
        display_entry.message,
        bytes_text,
      ));
    }
    text.push_str(&elapsed_text);
    let max_width = (data.terminal_width as i32 - 5).clamp(10, 75) as usize;
    let same_line_text_width =
      elapsed_text.len() + total_text_max_width + bytes_text_max_width + 3; // space, open and close brace
    let total_bars = if same_line_text_width > max_width {
      1
    } else {
      max_width - same_line_text_width
    };
    let completed_bars =
      (total_bars as f64 * data.percent_done).floor() as usize;
    text.push_str(" [");
    if completed_bars != total_bars {
      if completed_bars > 0 {
        text.push_str(&format!(
          "{}",
          colors::cyan(format!("{}{}", "#".repeat(completed_bars - 1), ">"))
        ))
      }
      text.push_str(&format!(
        "{}",
        colors::intense_blue("-".repeat(total_bars - completed_bars))
      ))
    } else {
      text.push_str(&format!("{}", colors::cyan("#".repeat(completed_bars))))
    }
    text.push(']');

    // suffix
    if display_entry.message.is_empty() {
      text.push_str(&colors::gray(bytes_text).to_string());
    }
    text.push_str(&colors::gray(total_text).to_string());

    text
  }
}

#[derive(Debug)]
pub struct TextOnlyProgressBarRenderer {
  last_tick: AtomicUsize,
  start_time: std::time::Instant,
}

impl Default for TextOnlyProgressBarRenderer {
  fn default() -> Self {
    Self {
      last_tick: Default::default(),
      start_time: std::time::Instant::now(),
    }
  }
}

const SPINNER_CHARS: [&str; 8] = ["⣷", "⣯", "⣟", "⡿", "⢿", "⣻", "⣽", "⣾"];
impl ProgressBarRenderer for TextOnlyProgressBarRenderer {
  fn render(&self, data: ProgressData) -> String {
    let last_tick = {
      let last_tick = self.last_tick.load(Ordering::Relaxed);
      let last_tick = (last_tick + 1) % 8;
      self.last_tick.store(last_tick, Ordering::Relaxed);
      last_tick
    };
    let current_time = std::time::Instant::now();

    let mut display_str = format!(
      "{} {} ",
      data.display_entries[0].prompt.as_text(),
      SPINNER_CHARS[last_tick]
    );

    let elapsed_time = current_time - self.start_time;
    let fmt_elapsed_time = get_elapsed_text(elapsed_time);

    let total_text = if data.total_entries <= 1 {
      String::new()
    } else {
      format!(
        " {}/{}",
        data.total_entries - data.pending_entries,
        data.total_entries
      )
    };

    display_str.push_str(&format!("{}{}\n", fmt_elapsed_time, total_text));

    for i in 0..4 {
      let Some(display_entry) = data.display_entries.get(i) else {
        display_str.push('\n');
        continue;
      };

      let bytes_text = {
        let total_size = display_entry.total_size;
        let pos = display_entry.position;
        if total_size == 0 {
          String::new()
        } else {
          format!(
            " {}/{}",
            human_download_size(pos, total_size),
            human_download_size(total_size, total_size)
          )
        }
      };

      // TODO(@marvinhagemeister): We're trying to reconstruct the original
      // specifier from the resolved one, but we lack the information about
      // private registries URLs and other things here.
      let message = display_entry
        .message
        .replace("https://registry.npmjs.org/", "npm:")
        .replace("https://jsr.io/", "jsr:")
        .replace("%2f", "/")
        .replace("%2F", "/");

      display_str.push_str(
        &colors::gray(format!(" - {}{}\n", message, bytes_text)).to_string(),
      );
    }

    display_str
  }
}

fn get_elapsed_text(elapsed: Duration) -> String {
  let elapsed_secs = elapsed.as_secs();
  let seconds = elapsed_secs % 60;
  let minutes = elapsed_secs / 60;
  format!("[{minutes:0>2}:{seconds:0>2}]")
}

#[cfg(test)]
mod test {
  use super::*;
  use pretty_assertions::assert_eq;
  use std::time::Duration;
  use test_util::assert_contains;

  #[test]
  fn should_get_elapsed_text() {
    assert_eq!(get_elapsed_text(Duration::from_secs(1)), "[00:01]");
    assert_eq!(get_elapsed_text(Duration::from_secs(20)), "[00:20]");
    assert_eq!(get_elapsed_text(Duration::from_secs(59)), "[00:59]");
    assert_eq!(get_elapsed_text(Duration::from_secs(60)), "[01:00]");
    assert_eq!(
      get_elapsed_text(Duration::from_secs(60 * 5 + 23)),
      "[05:23]"
    );
    assert_eq!(
      get_elapsed_text(Duration::from_secs(60 * 59 + 59)),
      "[59:59]"
    );
    assert_eq!(get_elapsed_text(Duration::from_secs(60 * 60)), "[60:00]");
    assert_eq!(
      get_elapsed_text(Duration::from_secs(60 * 60 * 3 + 20 * 60 + 2)),
      "[200:02]"
    );
    assert_eq!(
      get_elapsed_text(Duration::from_secs(60 * 60 * 99)),
      "[5940:00]"
    );
  }

  const BYTES_TO_KIB: u64 = 2u64.pow(10);

  #[test]
  fn should_render_bar_progress() {
    let renderer = BarProgressBarRenderer {
      display_human_download_size: true,
    };
    let mut data = ProgressData {
      display_entries: vec![ProgressDataDisplayEntry {
        prompt: ProgressMessagePrompt::Download,
        message: "data".to_string(),
        position: 0,
        total_size: 10 * BYTES_TO_KIB,
      }],
      duration: Duration::from_secs(1),
      pending_entries: 1,
      total_entries: 1,
      percent_done: 0f64,
      terminal_width: 50,
    };
    let text = renderer.render(data.clone());
    let text = test_util::strip_ansi_codes(&text);
    assert_eq!(
      text,
      concat!(
        "Download data 0.00KiB/10.00KiB\n",
        "[00:01] [-----------------]",
      ),
    );

    data.percent_done = 0.5f64;
    data.display_entries[0].position = 5 * BYTES_TO_KIB;
    data.display_entries[0].message = "".to_string();
    data.total_entries = 3;
    let text = renderer.render(data.clone());
    let text = test_util::strip_ansi_codes(&text);
    assert_eq!(text, "[00:01] [####>------] 5.00KiB/10.00KiB (2/3)",);

    // just ensure this doesn't panic
    data.terminal_width = 0;
    let text = renderer.render(data.clone());
    let text = test_util::strip_ansi_codes(&text);
    assert_eq!(text, "[00:01] [-] 5.00KiB/10.00KiB (2/3)",);

    data.terminal_width = 50;
    data.pending_entries = 0;
    data.display_entries[0].position = 10 * BYTES_TO_KIB;
    data.percent_done = 1.0f64;
    let text = renderer.render(data.clone());
    let text = test_util::strip_ansi_codes(&text);
    assert_eq!(text, "[00:01] [###########] 10.00KiB/10.00KiB (3/3)",);

    data.display_entries[0].position = 0;
    data.display_entries[0].total_size = 0;
    data.pending_entries = 0;
    data.total_entries = 1;
    let text = renderer.render(data);
    let text = test_util::strip_ansi_codes(&text);
    assert_eq!(text, "[00:01] [###################################]",);
  }

  #[test]
  fn should_render_text_only_progress() {
    let renderer = TextOnlyProgressBarRenderer::default();
    let mut data = ProgressData {
      display_entries: vec![ProgressDataDisplayEntry {
        prompt: ProgressMessagePrompt::Blocking,
        message: "data".to_string(),
        position: 0,
        total_size: 10 * BYTES_TO_KIB,
      }],
      duration: Duration::from_secs(1),
      pending_entries: 1,
      total_entries: 3,
      percent_done: 0f64,
      terminal_width: 50,
    };
    let text = renderer.render(data.clone());
    let text = test_util::strip_ansi_codes(&text);
    assert_contains!(text, "Blocking ⣯");
    assert_contains!(text, "2/3\n - data 0.00KiB/10.00KiB\n\n\n\n");

    data.pending_entries = 0;
    data.total_entries = 1;
    data.display_entries[0].position = 0;
    data.display_entries[0].total_size = 0;
    let text = renderer.render(data);
    let text = test_util::strip_ansi_codes(&text);
    assert_contains!(text, "Blocking ⣟");
    assert_contains!(text, "\n - data\n\n\n\n");
  }
}