paris/output/
mod.rs

1//! Helper functions for writing to stdout/stderr
2//!
3//! Some can format, some cannot
4#[cfg(any(feature = "macros", not(feature = "no_logger")))]
5use std::fmt::Display;
6
7#[cfg(feature = "macros")]
8use crate::formatter;
9
10/// Gets the current timestamp or empty string
11/// based on whether timestamps feature is enabled
12#[cfg(any(feature = "macros", not(feature = "no_logger")))]
13fn current_time() -> String {
14    #[cfg(feature = "timestamps")]
15    {
16        crate::timestamp::now()
17    }
18
19    #[cfg(not(feature = "timestamps"))]
20    {
21        String::new()
22    }
23}
24
25/// Writes to stdout without replacing keys
26#[cfg(not(feature = "no_logger"))]
27pub fn stdout<T>(message: T, line_ending: &str, with_carriage: bool)
28where
29    T: Display,
30{
31    let mut carriage = "";
32
33    if with_carriage {
34        carriage = "\r";
35    }
36
37    let timestamp = current_time();
38    let message = format!("{}{}{}{}", carriage, timestamp, message, line_ending);
39    print!("{}", message);
40}
41
42/// Writes to stderr without replacing keys
43#[cfg(not(feature = "no_logger"))]
44pub fn stderr<T>(message: T, line_ending: &str, with_carriage: bool)
45where
46    T: Display,
47{
48    let mut carriage = "";
49
50    if with_carriage {
51        carriage = "\r";
52    }
53
54    let timestamp = current_time();
55    let message = format!("{}{}{}{}", carriage, timestamp, message, line_ending);
56    eprint!("{}", message);
57}
58
59/// Writes to stdout and replaces keys inside the given string
60#[cfg(feature = "macros")]
61pub fn format_stdout<T>(message: T, line_ending: &str)
62where
63    T: Display,
64{
65    let timestamp = current_time();
66    let message = format!("{}{}{}", timestamp, message, line_ending);
67    print!("{}", formatter::colorize_string(message));
68}
69
70/// Writes to stderr and replaces keys inside the given string
71#[cfg(feature = "macros")]
72pub fn format_stderr<T>(message: T, line_ending: &str)
73where
74    T: Display,
75{
76    let timestamp = current_time();
77    let message = format!("{}{}{}", timestamp, message, line_ending);
78    eprint!("{}", formatter::colorize_string(message));
79}