assemble-core 0.2.0

The core crate of the assemble-rs package
Documentation
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
//! Defines different parts of the logging utilities for assemble-daemon

use crate::identifier::{ProjectId, TaskId};
use crate::unstable::text_factory::AssembleFormatter;
use atty::Stream;
use colored::Colorize;
use fern::{Dispatch, FormatCallback, Output};
use indicatif::MultiProgress;
use log::{Level, LevelFilter, Log, Record, SetLoggerError};
use merge::Merge;
use once_cell::sync::{Lazy, OnceCell};

use std::cell::RefCell;
use std::collections::{HashMap, VecDeque};

use std::io::{stdout, ErrorKind, Write};
use std::path::{Path, PathBuf};

use std::ffi::OsStr;
use std::sync::atomic::AtomicBool;
use std::sync::mpsc::{channel, Sender};
use std::sync::{Arc, Mutex};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};
use std::{fmt, io, thread};
use thread_local::ThreadLocal;
use time::format_description::FormatItem;
use time::macros::format_description;
use time::OffsetDateTime;

/// Provides helpful logging args for clap clis
#[derive(Debug, clap::Args, Clone, merge::Merge)]
#[clap(next_help_heading = "Log Level")]
pub struct LoggingArgs {
    /// Only display error level log messages
    #[clap(short, long)]
    #[clap(conflicts_with_all(&["warn", "info", "debug", "trace"]))]
    #[clap(display_order = 1)]
    #[clap(global = true)]
    #[merge(strategy = merge::bool::overwrite_false)]
    error: bool,

    /// Display warning and above level log messages
    #[clap(short, long)]
    #[clap(conflicts_with_all(&["error", "info", "debug", "trace"]))]
    #[clap(display_order = 2)]
    #[clap(global = true)]
    #[merge(strategy = merge::bool::overwrite_false)]
    warn: bool,

    /// Display info and above level log messages
    #[clap(short, long)]
    #[clap(conflicts_with_all(&["error", "warn", "debug", "trace"]))]
    #[clap(display_order = 3)]
    #[clap(global = true)]
    #[merge(strategy = merge::bool::overwrite_false)]
    info: bool,

    /// Display debug and above level log messages
    #[clap(long, short)]
    #[clap(conflicts_with_all(&["error", "warn", "info", "trace"]))]
    #[clap(display_order = 4)]
    #[clap(global = true)]
    #[merge(strategy = merge::bool::overwrite_false)]
    debug: bool,

    /// Display trace and above level log messages
    #[clap(long)]
    #[clap(conflicts_with_all(&["error", "warn", "info", "debug"]))]
    #[clap(display_order = 5)]
    #[clap(global = true)]
    #[merge(strategy = merge::bool::overwrite_false)]
    trace: bool,

    /// Show the source of a logging statement when running in any non complicated mode
    #[clap(long)]
    #[clap(help_heading = "Logging Settings")]
    #[clap(global = true)]
    #[merge(strategy =merge::bool::overwrite_false)]
    pub show_source: bool,

    /// Outputs everything as json
    #[clap(long)]
    #[clap(help_heading = "Logging Settings")]
    #[clap(global = true)]
    #[merge(strategy = merge::bool::overwrite_false)]
    pub json: bool,

    /// The console output mode.
    #[clap(long, value_enum, default_value_t = ConsoleMode::Auto)]
    #[clap(help_heading = "Logging Settings")]
    #[clap(global = true)]
    pub console: ConsoleMode,
}

impl Default for LoggingArgs {
    fn default() -> Self {
        Self {
            show_source: false,
            error: false,
            warn: false,
            info: false,
            debug: true,
            trace: false,
            json: false,
            console: ConsoleMode::Plain,
        }
    }
}

#[derive(Default, Eq, PartialEq)]
pub enum OutputType {
    #[default]
    Basic,
    TimeOnly,
    Complicated,
    Json,
}

#[derive(Debug, Copy, Clone, clap::ValueEnum, Eq, PartialEq)]
#[repr(u8)]
pub enum ConsoleMode {
    Auto,
    Rich,
    Plain,
}

impl Merge for ConsoleMode {
    fn merge(&mut self, other: Self) {
        if self == &Self::Auto {
            *self = other;
        }
    }
}

impl ConsoleMode {
    pub fn resolve(self) -> Self {
        match &self {
            ConsoleMode::Auto => {
                if atty::is(Stream::Stdout) {
                    ConsoleMode::Rich
                } else {
                    ConsoleMode::Plain
                }
            }
            ConsoleMode::Rich => self,
            ConsoleMode::Plain => self,
        }
    }
}

impl LoggingArgs {
    /// Gets the log level
    pub fn log_level_filter(&self) -> LevelFilter {
        if self.error {
            LevelFilter::Error
        } else if self.warn {
            LevelFilter::Warn
        } else if self.info {
            LevelFilter::Info
        } else if self.debug {
            LevelFilter::Debug
        } else if self.trace {
            LevelFilter::Trace
        } else {
            LevelFilter::Info
        }
    }

    /// Get the level filter from this args
    fn config_from_settings(&self) -> (LevelFilter, OutputType) {
        let level = self.log_level_filter();
        let mut output_type = if self.error {
            OutputType::Basic
        } else if self.warn {
            OutputType::Basic
        } else if self.info {
            OutputType::Basic
        } else if self.debug {
            OutputType::Basic
        } else if self.trace {
            OutputType::TimeOnly
        } else {
            OutputType::Basic
        };
        if self.json {
            output_type = OutputType::Json;
        }
        (level, output_type)
    }

    pub fn init_root_logger(&self) -> Result<Option<JoinHandle<()>>, SetLoggerError> {
        let (dispatch, handle) = self.create_logger();
        dispatch.apply().map(|_| handle)
    }

    pub fn init_root_logger_with(filter: LevelFilter, mode: OutputType) {
        Self::try_init_root_logger_with(filter, mode).expect("couldn't create dispatch");
    }

    pub fn try_init_root_logger_with(
        filter: LevelFilter,
        mode: OutputType,
    ) -> Result<(), SetLoggerError> {
        Self::create_logger_with(filter, mode, false, None).apply()
    }

    pub fn create_logger(&self) -> (Dispatch, Option<JoinHandle<()>>) {
        let (filter, output_mode) = self.config_from_settings();
        let rich: bool = match self.console.resolve() {
            ConsoleMode::Auto => {
                unreachable!()
            }
            ConsoleMode::Rich => true,
            ConsoleMode::Plain => false,
        };
        if !rich {
            colored::control::set_override(false);
        }
        let (started, handle) = start_central_logger(rich);
        let central = CentralLoggerInput { sender: started };
        let output = Output::from(Box::new(central) as Box<dyn Write + Send>);
        (
            Self::create_logger_with(filter, output_mode, self.show_source, output),
            Some(handle),
        )
    }

    pub fn create_logger_with(
        filter: LevelFilter,
        mode: OutputType,
        show_source: bool,
        output: impl Into<Option<Output>>,
    ) -> Dispatch {
        let dispatch = Dispatch::new()
            .level(filter)
            .chain(output.into().unwrap_or(Output::stdout("\n")));
        match mode {
            OutputType::Json => dispatch.format(Self::json_message_format),
            other => dispatch.format(Self::message_format(other, show_source)),
        }
    }

    fn message_format(
        output_mode: OutputType,
        show_source: bool,
    ) -> impl Fn(FormatCallback, &fmt::Arguments, &log::Record) + Sync + Send + 'static {
        move |out, message, record| {
            out.finish(format_args!(
                "{}{}",
                {
                    let prefix = Self::format_prefix(&output_mode, record, show_source);
                    if prefix.is_empty() {
                        prefix
                    } else {
                        format!("{} ", prefix)
                    }
                },
                match record.level() {
                    Level::Error => {
                        format!("{}", message.to_string().red())
                    }
                    Level::Warn => {
                        format!("{}", message.to_string().yellow())
                    }
                    Level::Info | Level::Debug => {
                        message.to_string()
                    }
                    Level::Trace => {
                        format!("{}", message.to_string().bright_blue())
                    }
                }
            ))
        }
    }

    fn json_message_format(format: FormatCallback, args: &fmt::Arguments, record: &log::Record) {
        let message = format!("{}", args);
        let level = record.level();
        let origin = Origin::None;

        let message_info = JsonMessageInfo {
            level,
            origin,
            message,
        };

        let as_string = serde_json::to_string(&message_info).unwrap();

        format.finish(format_args!("{}", as_string));
    }

    fn format_prefix(output_mode: &OutputType, record: &Record, show_source: bool) -> String {
        use colored::Colorize;
        let mut level_string = record.level().to_string().to_lowercase();

        level_string = match record.level() {
            Level::Error => level_string.red().to_string(),
            Level::Warn => level_string.yellow().to_string(),
            Level::Info => level_string.green().to_string(),
            Level::Debug => level_string.blue().to_string(),
            Level::Trace => level_string.bright_black().to_string(),
        };
        let output = match output_mode {
            OutputType::Basic => String::new(),
            OutputType::TimeOnly => {
                static DATE_TIME_FORMAT: &[FormatItem] =
                    format_description!("[hour]:[minute]:[second].[subsecond digits:4]");

                let time = OffsetDateTime::now_local().unwrap_or(OffsetDateTime::now_utc());
                format!(
                    "[{}] {: >7}:",
                    time.format(DATE_TIME_FORMAT).unwrap(),
                    level_string
                )
            }
            OutputType::Complicated => {
                static DATE_TIME_FORMAT: &[FormatItem] = format_description!("[year]/[month]/[day] [hour]:[minute]:[second].[subsecond digits:4] [offset_hour sign:mandatory padding:none] UTC");

                let time = OffsetDateTime::now_utc();
                let file_path = Path::new(record.file().unwrap_or("unknown"));
                format!(
                    "[{} {}{} {}]",
                    time.format(DATE_TIME_FORMAT).unwrap(),
                    file_path.file_name().and_then(|s| s.to_str()).unwrap(),
                    record
                        .line()
                        .map(|l| format!(":{l}"))
                        .unwrap_or("".to_string()),
                    level_string
                )
            }
            _ => {
                unreachable!()
            }
        };
        if show_source {
            if let Some((module, file)) = record.module_path().zip(record.file()) {
                let line = record.line().map(|i| format!(":{}", i)).unwrap_or_default();
                let crate_name = module.split("::").next().unwrap();
                let source: PathBuf = Path::new(file)
                    .iter()
                    .skip_while(|&p| p != OsStr::new("src"))
                    .skip(1)
                    .collect();

                let source = format!(
                    "({crate_name} :: {source}{line})",
                    source = source.to_string_lossy()
                )
                .italic();

                format!("{source} {output}")
            } else {
                format!("(<unknown source>) {output}")
            }
        } else {
            output
        }
    }
}

pub fn init_root_log(level: LevelFilter, mode: impl Into<Option<OutputType>>) {
    let mode = mode.into().unwrap_or_default();
    let _ = LoggingArgs::try_init_root_logger_with(level, mode);
}

#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq, Hash)]
pub enum Origin {
    Project(ProjectId),
    Task(TaskId),
    None,
}

impl From<ProjectId> for Origin {
    fn from(p: ProjectId) -> Self {
        Self::Project(p)
    }
}

impl From<TaskId> for Origin {
    fn from(t: TaskId) -> Self {
        Self::Task(t)
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct JsonMessageInfo {
    #[serde(with = "LevelDef")]
    pub level: Level,
    pub origin: Origin,
    pub message: String,
}

#[derive(Serialize, Deserialize)]
#[serde(remote = "Level")]
enum LevelDef {
    Error,
    Warn,
    Info,
    Debug,
    Trace,
}

static THREAD_ORIGIN: Lazy<ThreadLocal<RefCell<Origin>>> = Lazy::new(ThreadLocal::new);

fn thread_origin() -> Origin {
    THREAD_ORIGIN
        .get_or(|| RefCell::new(Origin::None))
        .borrow()
        .clone()
}

pub struct LoggingControl(());

/// Provides access to the logging control of the entire program
pub static LOGGING_CONTROL: Lazy<LoggingControl> = Lazy::new(|| LoggingControl(()));

impl LoggingControl {
    /// Sets the thread local origin
    fn use_origin(&self, new_origin: Origin) {
        trace!(
            "setting the origin for thread {:?} to {:?}",
            thread::current().id(),
            new_origin
        );
        let origin = THREAD_ORIGIN.get_or(|| RefCell::new(Origin::None));
        let mut ref_mut = origin.borrow_mut();
        *ref_mut = new_origin;
    }

    pub fn in_project(&self, project: ProjectId) {
        self.use_origin(Origin::Project(project))
        // trace!("set origin to {:?}", ref_mut);
    }

    pub fn in_task(&self, task: TaskId) {
        self.use_origin(Origin::Task(task))
        // trace!("set origin to {:?}", ref_mut);
    }

    pub fn reset(&self) {
        self.use_origin(Origin::None)
        // trace!("set origin to {:?}", ref_mut);
    }

    pub fn stop_logging(&self) {
        let lock = LOG_COMMAND_SENDER.get().unwrap();
        let sender = lock.lock().unwrap();

        sender.send(LoggingCommand::Stop).unwrap();
    }

    pub fn start_task(&self, id: &TaskId) {
        let lock = LOG_COMMAND_SENDER.get().unwrap();
        let sender = lock.lock().unwrap();

        sender
            .send(LoggingCommand::TaskStarted(id.clone()))
            .unwrap();
    }

    pub fn end_task(&self, id: &TaskId) {
        let lock = LOG_COMMAND_SENDER.get().unwrap();
        let sender = lock.lock().unwrap();

        sender.send(LoggingCommand::TaskEnded(id.clone())).unwrap();
    }

    /// Start a progress bar. Returns err if a progress bar has already been started. If Ok, the
    /// returned value is a clone of the multi-progress bar
    pub fn start_progress_bar(&self, bar: &MultiProgress) -> Result<MultiProgress, ()> {
        let lock = LOG_COMMAND_SENDER.get().unwrap();
        let sender = lock.lock().unwrap();
        sender
            .send(LoggingCommand::StartMultiProgress(bar.clone()))
            .unwrap();
        Ok(bar.clone())
    }

    /// End a progress bar if it exists
    pub fn end_progress_bar(&self) {
        let lock = LOG_COMMAND_SENDER.get().unwrap();
        let sender = lock.lock().unwrap();

        sender.send(LoggingCommand::EndMultiProgress).unwrap();
    }

    /// Run a closure within an origin context
    #[cfg(feature = "log_origin_control")]
    pub fn with_origin<O: Into<Origin>, F: FnOnce() -> R, R>(&self, origin: O, func: F) -> R {
        let origin = origin.into();

        self.use_origin(origin);
        let ret = (func)();
        self.reset();
        ret
    }

    /// Gets the origin currently set
    #[cfg(feature = "log_origin_control")]
    pub fn get_origin(&self) -> Origin {
        THREAD_ORIGIN
            .get_or(|| RefCell::new(Origin::None))
            .borrow()
            .clone()
    }
}

static CONTINUE_LOGGING: AtomicBool = AtomicBool::new(true);
static LOG_COMMAND_SENDER: OnceCell<Arc<Mutex<Sender<LoggingCommand>>>> = OnceCell::new();

fn start_central_logger(rich: bool) -> (Sender<LoggingCommand>, JoinHandle<()>) {
    let (send, recv) = channel();
    let _ = LOG_COMMAND_SENDER.set(Arc::new(Mutex::new(send.clone())));
    let handle = thread::spawn(move || {
        let mut central_logger = CentralLoggerOutput::new();
        loop {
            let command = match recv.recv() {
                Ok(s) => s,
                Err(_) => break,
            };

            match command {
                LoggingCommand::LogString(o, s) => {
                    central_logger.add_output(o, &s);
                    central_logger.flush_current_origin();
                }
                LoggingCommand::Flush => central_logger.flush(),
                LoggingCommand::Stop => {
                    break;
                }
                LoggingCommand::TaskStarted(s) => {
                    if !rich {
                        central_logger.add_output(Origin::Task(s), "");
                        central_logger.flush_current_origin();
                    }
                }
                LoggingCommand::TaskEnded(_s) => {}
                LoggingCommand::TaskStatus(_, _) => {}
                LoggingCommand::StartMultiProgress(b) => {
                    central_logger.start_progress_bar(&b).unwrap();
                }
                LoggingCommand::EndMultiProgress => {
                    central_logger.end_progress_bar();
                }
            }
        }

        central_logger.flush();
    });
    LOGGING_CONTROL.reset();
    (send, handle)
}

pub enum LoggingCommand {
    LogString(Origin, String),
    TaskStarted(TaskId),
    TaskEnded(TaskId),
    TaskStatus(TaskId, String),
    StartMultiProgress(MultiProgress),
    EndMultiProgress,
    Flush,
    Stop,
}

pub struct CentralLoggerInput {
    sender: Sender<LoggingCommand>,
}

assert_impl_all!(CentralLoggerInput: Send, Write);

impl io::Write for CentralLoggerInput {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let string = String::from_utf8_lossy(buf).to_string();

        let origin = thread_origin();
        // println!("sending from origin: {origin:?}");
        self.sender
            .send(LoggingCommand::LogString(origin, string))
            .map_err(|e| io::Error::new(ErrorKind::Interrupted, e))?;
        Ok(buf.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        self.sender
            .send(LoggingCommand::Flush)
            .map_err(|e| io::Error::new(ErrorKind::Interrupted, e))
    }
}

#[derive(Debug)]
pub struct CentralLoggerOutput {
    saved_output: HashMap<Origin, String>,
    origin_buffers: HashMap<Origin, String>,
    origin_queue: VecDeque<Origin>,
    previous: Option<Origin>,
    last_query: Option<Instant>,
    progress_bar: Option<MultiProgress>,
}

impl CentralLoggerOutput {
    pub fn new() -> Self {
        Self {
            saved_output: Default::default(),
            origin_buffers: Default::default(),
            origin_queue: Default::default(),
            previous: None,
            last_query: None,
            progress_bar: None,
        }
    }

    pub fn add_output(&mut self, origin: Origin, msg: &str) {
        let buffer = self.origin_buffers.entry(origin.clone()).or_default();
        *buffer = format!("{}{}", buffer, msg);
        if let Some(front) = self.origin_queue.front() {
            if front != &origin {
                if self.last_query.unwrap().elapsed() >= Duration::from_millis(100) {
                    self.origin_queue.pop_front();
                }
                self.origin_queue.push_back(origin);
            }
            self.last_query = Some(Instant::now());
        } else {
            self.origin_queue.push_back(origin);
        }
    }

    /// Flushes current lines from an origin
    pub fn flush_current_origin(&mut self) {
        self.last_query = Some(Instant::now());
        let origin = self.origin_queue.front().cloned().unwrap_or(Origin::None);

        if Some(&origin) != self.previous.as_ref() {
            match &origin {
                Origin::Project(p) => {
                    self.println(format!(
                        "{}",
                        AssembleFormatter::default()
                            .project_status(p, "configuring")
                            .unwrap()
                    ))
                    .unwrap();
                }
                Origin::Task(t) => {
                    self.println(format!(
                        "{}",
                        AssembleFormatter::default().task_status(t, "").unwrap()
                    ))
                    .unwrap();
                }
                Origin::None => {}
            }
        }

        self.previous = Some(origin.clone());
        let printer = self.logger_stdout();
        let saved = self.saved_output.entry(origin.clone()).or_default();
        if let Some(buffer) = self.origin_buffers.get_mut(&origin) {
            let mut lines = Vec::new();
            while let Some(position) = buffer.chars().position(|c| c == '\n') {
                let head = &buffer[..position];
                let tail = buffer.get((position + 1)..).unwrap_or_default();

                lines.push(head.to_string());

                *buffer = tail.to_string();
            }

            for line in lines {
                if !(saved.trim().is_empty() && line.trim().is_empty()) {
                    printer.println(&line).unwrap();
                    *saved = format!("{}{}", saved, line);
                }
            }

            if buffer.trim().is_empty() {
                self.origin_queue.pop_front();
            }
        }
    }

    pub fn flush(&mut self) {
        let printer = self.logger_stdout();
        let drained = self.origin_queue.drain(..).collect::<Vec<_>>();
        for origin in drained {
            if let Some(str) = self.origin_buffers.get_mut(&origin) {
                printer.println(format!("{origin:?}: {}", str)).unwrap();
                str.clear();
            }
        }
        stdout().flush().unwrap();
    }

    pub fn println(&self, string: impl AsRef<str>) -> io::Result<()> {
        match &self.progress_bar {
            None => {
                writeln!(stdout(), "{}", string.as_ref())
            }
            Some(p) => p.println(string),
        }
    }

    pub fn logger_stdout(&self) -> LoggerStdout {
        LoggerStdout {
            progress: self.progress_bar.clone(),
        }
    }

    /// Start a progress bar. Returns err if a progress bar has already been started. If Ok, the
    /// returned value is a clone of the multi-progress bar
    pub fn start_progress_bar(&mut self, bar: &MultiProgress) -> Result<MultiProgress, ()> {
        self.progress_bar = Some(bar.clone());
        Ok(bar.clone())
    }

    /// End a progress bar if it exists
    pub fn end_progress_bar(&mut self) {
        let replaced = std::mem::replace(&mut self.progress_bar, None);
        if let Some(replaced) = replaced {
            replaced.clear().unwrap();
        }
    }
}

pub struct LoggerStdout {
    progress: Option<MultiProgress>,
}

impl LoggerStdout {
    pub fn println(&self, string: impl AsRef<str>) -> io::Result<()> {
        match &self.progress {
            None => {
                writeln!(stdout(), "{}", string.as_ref())
            }
            Some(p) => p.println(string),
        }
    }
}