Skip to main content

rocketmq_admin_cli/ui/
output.rs

1// Copyright 2023 The RocketMQ Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use colored::Colorize;
16
17pub fn print_success(message: &str) {
18    println!("{} {}", "[OK]".green().bold(), message);
19}
20
21pub fn print_error(message: &str) {
22    eprintln!("{} {}", "[ERROR]".red().bold(), message);
23}
24
25pub fn print_warning(message: &str) {
26    println!("{} {}", "[WARN]".yellow().bold(), message);
27}
28
29pub fn print_info(message: &str) {
30    println!("{} {}", "[INFO]".blue().bold(), message);
31}
32
33pub fn print_header(title: &str) {
34    println!("\n{}", title.cyan().bold().underline());
35}
36
37pub fn print_subheader(title: &str) {
38    println!("\n{}", title.cyan().bold());
39}
40
41pub fn print_key_value(key: &str, value: &str) {
42    println!("  {}: {}", key.bright_white().bold(), value.bright_white());
43}
44
45pub fn print_summary(operation: &str, success: bool, details: Option<&str>) {
46    if success {
47        print_success(&format!("{operation} completed successfully"));
48    } else {
49        print_error(&format!("{operation} failed"));
50    }
51
52    if let Some(details) = details {
53        println!("  {}", details.bright_black());
54    }
55}
56
57pub fn print_list(title: &str, items: &[String]) {
58    print_header(title);
59    for (i, item) in items.iter().enumerate() {
60        println!("  {}. {}", (i + 1).to_string().bright_black(), item);
61    }
62}
63
64pub fn format_count(count: usize, singular: &str, plural: &str) -> String {
65    if count == 1 {
66        format!("{count} {singular}")
67    } else {
68        format!("{count} {plural}")
69    }
70}
71
72pub fn print_empty_result(entity: &str) {
73    print_info(&format!("No {entity} found"));
74}
75
76pub fn print_operation_start(operation: &str) {
77    println!("{} {operation}...", "[RUN]".blue().bold());
78}
79
80pub fn print_divider() {
81    println!("{}", "-".repeat(80).bright_black());
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87
88    #[test]
89    fn formats_counts() {
90        assert_eq!(format_count(1, "topic", "topics"), "1 topic");
91        assert_eq!(format_count(5, "topic", "topics"), "5 topics");
92        assert_eq!(format_count(0, "broker", "brokers"), "0 brokers");
93    }
94
95    #[test]
96    fn print_helpers_do_not_panic() {
97        print_success("success");
98        print_error("error");
99        print_warning("warning");
100        print_info("info");
101        print_header("Header");
102        print_subheader("Subheader");
103        print_key_value("Name", "TestTopic");
104        print_summary("Operation", true, Some("details"));
105        print_list("Items", &["one".to_string(), "two".to_string()]);
106        print_empty_result("topics");
107        print_operation_start("Running");
108        print_divider();
109    }
110}