Skip to main content

workflow_terminal/
macros.rs

1use crate::terminal::Terminal;
2use std::sync::Arc;
3use workflow_log::style;
4
5/// Implementation backing the `tprint!` macro; writes `args` to the
6/// terminal without a trailing newline.
7#[inline(always)]
8pub fn tprint_impl<T>(term: T, args: &str)
9where
10    T: Into<Arc<Terminal>>,
11{
12    term.into().write(args);
13}
14
15/// Implementation backing the `tprintln!` macro; writes `args` to the
16/// terminal followed by a newline.
17#[inline(always)]
18pub fn tprintln_impl<T>(term: T, args: &str)
19where
20    T: Into<Arc<Terminal>>,
21{
22    term.into().writeln(args);
23}
24
25/// Implementation backing the `terrorln!` macro; writes `args` to the
26/// terminal styled in red followed by a newline.
27#[inline(always)]
28pub fn terrorln_impl<T>(term: T, args: &str)
29where
30    T: Into<Arc<Terminal>>,
31{
32    term.into()
33        .writeln(style(args.to_string()).red().to_string());
34}
35
36/// Implementation backing the `twarnln!` macro; writes `args` to the
37/// terminal styled in yellow followed by a newline.
38#[inline(always)]
39pub fn twarnln_impl<T>(term: T, args: &str)
40where
41    T: Into<Arc<Terminal>>,
42{
43    term.into()
44        .writeln(style(args.to_string()).yellow().to_string());
45}
46
47/// Implementation backing the `tpara!` macro; writes `args` to the
48/// terminal as a word-wrapped paragraph.
49#[inline(always)]
50pub fn tpara_impl<T>(term: T, args: &str)
51where
52    T: Into<Arc<Terminal>>,
53{
54    term.into().para(args.to_string());
55}
56
57/// Write a formatted warning line (styled in yellow) to the given terminal.
58/// Usage: `twarnln!(term, "format {}", value)`.
59#[macro_export]
60macro_rules! twarnln {
61    ($target:expr_2021) => {
62        compile_error!("twarnln! macro requires at least two arguments");
63    };
64
65    ($dest:expr_2021, $($arg:tt)*) => {
66        $crate::twarnln_impl($dest.deref().clone(), &format_args!($($arg)*).to_string().as_str())
67    };
68}
69
70/// Write a formatted error line (styled in red) to the given terminal.
71/// Usage: `terrorln!(term, "format {}", value)`.
72#[macro_export]
73macro_rules! terrorln {
74    ($target:expr_2021) => {
75        compile_error!("terrorln! macro requires at least two arguments");
76    };
77
78    ($dest:expr_2021, $($arg:tt)*) => {
79        $crate::terrorln_impl($dest.deref().clone(), &format_args!($($arg)*).to_string().as_str())
80    };
81}
82
83/// Write a formatted line followed by a newline to the given terminal.
84/// Usage: `tprintln!(term)` or `tprintln!(term, "format {}", value)`.
85#[macro_export]
86macro_rules! tprintln {
87    ($dest:expr_2021) => {
88        $crate::tprintln_impl($dest.as_ref(), &"")
89    };
90
91    ($dest:expr_2021, $($arg:tt)*) => {
92        $crate::tprintln_impl($dest.deref().clone(), &format_args!($($arg)*).to_string().as_str())
93    };
94}
95
96/// Write formatted text without a trailing newline to the given terminal.
97/// Usage: `tprint!(term)` or `tprint!(term, "format {}", value)`.
98#[macro_export]
99macro_rules! tprint {
100    ($dest:expr_2021) => {
101        $crate::tprint_impl($dest.as_ref(), &"")
102    };
103
104    ($dest:expr_2021, $($arg:tt)*) => {
105        $crate::tprint_impl($dest.deref().clone(), &format_args!($($arg)*).to_string().as_str())
106    };
107}
108
109/// Write formatted text to the given terminal as a word-wrapped paragraph.
110/// Usage: `tpara!(term, "format {}", value)`.
111#[macro_export]
112macro_rules! tpara {
113    ($target:expr_2021) => {
114        compile_error!("tpara! macro requires at least two arguments");
115    };
116
117    ($dest:expr_2021, $($arg:tt)*) => {
118        $crate::tpara_impl($dest.as_ref(), &format_args!($($arg)*).to_string().as_str())
119    };
120}