interop/
util.rs

1// Wire
2// Copyright (C) 2022 Wire Swiss GmbH
3
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see http://www.gnu.org/licenses/.
16
17use spinoff::Spinner;
18
19pub(crate) struct RunningProcess {
20    spinner: Option<Spinner>,
21    is_task: bool,
22}
23
24impl std::fmt::Debug for RunningProcess {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        f.debug_struct("RunningProcess")
27            .field("is_task", &self.is_task)
28            .finish()
29    }
30}
31
32impl RunningProcess {
33    pub(crate) fn new(msg: impl AsRef<str> + std::fmt::Display, is_task: bool) -> Self {
34        let spinner = if std::env::var("CI").is_err() {
35            Some(Spinner::new(
36                spinoff::spinners::Aesthetic,
37                msg.as_ref().to_owned(),
38                if is_task {
39                    spinoff::Color::Green
40                } else {
41                    spinoff::Color::Blue
42                },
43            ))
44        } else {
45            if is_task {
46                log::info!("{msg}");
47            } else {
48                log::debug!("{msg}");
49            }
50
51            None
52        };
53
54        Self { spinner, is_task }
55    }
56
57    pub(crate) fn update(&mut self, msg: impl AsRef<str> + std::fmt::Display) {
58        if let Some(spinner) = &mut self.spinner {
59            spinner.update_text(msg.as_ref().to_owned());
60        } else if self.is_task {
61            log::info!("{msg}");
62        } else {
63            log::debug!("{msg}");
64        }
65    }
66
67    pub(crate) fn success(self, msg: impl AsRef<str> + std::fmt::Display) {
68        if let Some(mut spinner) = self.spinner {
69            spinner.success(msg.as_ref());
70        } else {
71            log::info!("{msg}");
72        }
73    }
74}