modcli/output/
print.rs

1use std::{fs::File, io::{self, BufRead}, path::Path, thread, time::Duration};
2use crate::output::style::build;
3use crate::output::themes::current_theme;
4
5/// Prints a single line with optional delay (ms)
6pub fn line(text: &str) {
7    println!("{}", text);
8}
9
10/// Prints text without newline
11pub fn write(text: &str) {
12    print!("{}", text);
13}
14
15
16/// Prints just a newline
17pub fn newline() {
18    println!();
19}
20
21/// Prints just a newline
22pub fn end() {
23    println!();
24}
25
26
27/// Scrolls through a multi-line string with optional delay
28pub fn scroll(multiline: &[&str], delay_ms: u64) {
29    for text_line in multiline {
30        line(text_line);
31        if delay_ms > 0 {
32            std::thread::sleep(std::time::Duration::from_millis(delay_ms));
33        }
34    }
35}
36
37/// Prints each line from a file with optional delay
38pub fn file(path: &str, delay_ms: u64) {
39    if let Ok(lines) = read_lines(path) {
40        for text_line in lines.flatten() {
41            line(&text_line);
42            if delay_ms > 0 {
43                thread::sleep(Duration::from_millis(delay_ms));
44            }
45        }
46    } else {
47        error("Failed to open file");
48    }
49}
50
51// Reads lines from a file, returns an iterator
52fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
53where P: AsRef<Path> {
54    let file = File::open(filename)?;
55    Ok(io::BufReader::new(file).lines())
56}
57
58// --- Message Shortcodes ---
59
60pub fn debug(msg: &str) {
61    let theme = current_theme();
62    let styled = build()
63        .part("Debug:").color(theme.get_log_color("debug")).space()
64        .part(msg).get();
65    line(&styled);
66}
67
68pub fn info(msg: &str) {
69    let theme = current_theme();
70    let styled = build()
71        .part("Info:").color(theme.get_log_color("info")).bold().space()
72        .part(msg).get();
73    line(&styled);
74}
75
76pub fn warn(msg: &str) {
77    let theme = current_theme();
78    let styled = build()
79        .part("Warning:").color(theme.get_log_color("warn")).bold().space()
80        .part(msg).get();
81    line(&styled);
82}
83
84pub fn error(msg: &str) {
85    let theme = current_theme();
86    let styled = build()
87        .part("Error:").color(theme.get_log_color("error")).bold().space()
88        .part(msg).get();
89    line(&styled);
90}
91
92pub fn success(msg: &str) {
93    let theme = current_theme();
94    let styled = build()
95        .part("Success:").color(theme.get_log_color("success")).bold().space()
96        .part(msg).get();
97    line(&styled);
98}
99
100pub fn status(msg: &str) {
101    let theme = current_theme();
102    let styled = build()
103        .part("Status:").color(theme.get_log_color("status")).bold().space()
104        .part(msg).get();
105    line(&styled);
106}
107
108pub fn deprecated(msg: &str) {
109    let theme = current_theme();
110    let styled = build()
111        .part("Deprecated:").color(theme.get_log_color("notice")).bold().space()
112        .part(msg).get();
113    line(&styled);
114}
115
116pub fn unknown(msg: &str) {
117    let theme = current_theme();
118    let styled = build()
119        .part("Unknown Command:").color(theme.get_log_color("notice")).bold().space()
120        .part(msg).get();
121    line(&styled);
122}