sui_aws_orchestrator/
display.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use std::{fmt::Display, io::stdout};
5
6use crossterm::{
7    cursor::{RestorePosition, SavePosition},
8    style::{Print, PrintStyledContent, Stylize},
9    terminal::{Clear, ClearType},
10};
11use prettytable::format::{self};
12
13pub fn header<S: Display>(message: S) {
14    crossterm::execute!(
15        stdout(),
16        PrintStyledContent(format!("\n{message}\n").green().bold()),
17    )
18    .unwrap();
19}
20
21pub fn error<S: Display>(message: S) {
22    crossterm::execute!(
23        stdout(),
24        PrintStyledContent(format!("\n{message}\n").red().bold()),
25    )
26    .unwrap();
27}
28
29pub fn warn<S: Display>(message: S) {
30    crossterm::execute!(
31        stdout(),
32        PrintStyledContent(format!("\n{message}\n").bold()),
33    )
34    .unwrap();
35}
36
37pub fn config<N: Display, V: Display>(name: N, value: V) {
38    crossterm::execute!(
39        stdout(),
40        PrintStyledContent(format!("{name}: ").bold()),
41        Print(format!("{value}\n"))
42    )
43    .unwrap();
44}
45
46pub fn action<S: Display>(message: S) {
47    crossterm::execute!(stdout(), Print(format!("{message} ... ")), SavePosition).unwrap();
48}
49
50pub fn status<S: Display>(status: S) {
51    crossterm::execute!(
52        stdout(),
53        RestorePosition,
54        SavePosition,
55        Clear(ClearType::UntilNewLine),
56        Print(format!("[{status}]"))
57    )
58    .unwrap();
59}
60
61pub fn done() {
62    crossterm::execute!(
63        stdout(),
64        RestorePosition,
65        Clear(ClearType::UntilNewLine),
66        Print(format!("[{}]\n", "Ok".green()))
67    )
68    .unwrap();
69}
70
71pub fn newline() {
72    crossterm::execute!(stdout(), Print("\n")).unwrap();
73}
74
75/// Default style for tables printed to stdout.
76pub fn default_table_format() -> format::TableFormat {
77    format::FormatBuilder::new()
78        .separators(
79            &[
80                format::LinePosition::Top,
81                format::LinePosition::Bottom,
82                format::LinePosition::Title,
83            ],
84            format::LineSeparator::new('-', '-', '-', '-'),
85        )
86        .padding(1, 1)
87        .build()
88}
89
90#[cfg(test)]
91mod test {
92    use std::time::Duration;
93
94    use tokio::time::sleep;
95
96    use crate::display::status;
97
98    use super::{action, config, done, error, header, newline, warn};
99
100    #[tokio::test]
101    #[ignore = "only used to manually check if prints work correctly"]
102    async fn display() {
103        header("This is a header");
104        config("This is a config", 2);
105        action("Running a long function");
106        for i in 0..5 {
107            sleep(Duration::from_secs(1)).await;
108            if i == 2 {
109                warn("This is a warning!");
110            }
111            status(format!("{}/5", i + 1));
112        }
113        done();
114        error("This is an error!");
115        warn("This is a warning!");
116        newline();
117    }
118}