termint/
macros.rs

1/// Macro to combine [`Border`] sides
2///
3/// ## Usage:
4/// ```rust
5/// # use termint::{borders, enums::Border};
6/// // Without macro:
7/// let top_left_right = Border::TOP | Border::LEFT | Border::RIGHT;
8/// // With macro:
9/// let top_left_right = borders!(TOP, LEFT, RIGHT);
10/// ```
11#[macro_export]
12macro_rules! borders {
13    ($($side:ident),* $(,)?) => {
14        $crate::enums::Border::NONE $(| $crate::enums::Border::$side)*
15    };
16}
17
18/// Makes creating help easier
19///
20/// ### Header:
21/// - Printed in green
22/// ```ignore
23/// "header":
24/// ```
25///
26/// ### Command help:
27/// - Command is printed in yellow and has left padding of 2
28/// - Others in default color
29/// - Description has left padding of 4
30/// ```ignore
31/// // One description literals
32/// "command" ["params"]* => "description"
33/// // Multiple description literals
34/// "command" ["params"]* => {
35///     "description1",
36///     "description2"
37/// }
38/// ```
39///
40/// ## Usage:
41/// ```rust
42/// # use termint::{enums::Color, help, widgets::ToSpan};
43/// help!(
44///     "Usage":
45///     "-t" ["value"] => "Tests program with [value]"
46///     "-d" => {
47///         "Creates documentation",
48///         "When used with -t, also tests the documentation",
49///     }
50///     "Special case":
51///     "-ntd" => "Creates documentation and runs test without testing docs"
52/// );
53/// ```
54#[macro_export]
55macro_rules! help {
56    // Rule for parsing header
57    ($header:literal: $($rest:tt)*) => {
58        println!(
59            "{}:",
60            $crate::widgets::Span::new($header).fg($crate::enums::Color::Green)
61        );
62        help!($($rest)*);
63    };
64
65    // Rule for parsing command help without curly braces
66    (
67        $cmd:literal $([$param:literal])* => $description:literal
68        $($rest:tt)*
69    ) => {
70        print!(
71            "  {}",
72            $crate::widgets::Span::new($cmd).fg($crate::enums::Color::Yellow)
73        );
74        $(print!(" [{}]", $param);)*
75        println!();
76        println!("    {}", $description);
77        help!($($rest)*);
78    };
79
80    // Rule for parsing command help with curly braces
81    (
82        $cmd:literal $([$param:literal])* => {
83            $($description:literal),* $(,)?
84        }
85        $($rest:tt)*
86    ) => {
87        print!(
88            "  {}",
89            $crate::widgets::Span::new($cmd).fg($crate::enums::Color::Yellow)
90        );
91        $(print!(" [{}]", $param);)*
92        println!();
93        $(println!("    {}", $description);)*
94        help!($($rest)*);
95    };
96
97    () => {};
98}
99
100/// Creates vector with given given Modifiers
101///
102/// ## Usage:
103/// ```rust
104/// # use termint::{enums::Modifier, modifiers};
105/// // Without macro:
106/// let mods = Modifier::BOLD | Modifier::ITALIC;
107/// // With macro:
108/// let mods = modifiers!(BOLD, ITALIC);
109/// ```
110#[macro_export]
111macro_rules! modifiers {
112    ($($mod:ident),* $(,)?) => {
113        $crate::enums::Modifier::NONE $(| $crate::enums::Modifier::$mod)*
114    };
115}
116
117/// Creates new paragraph in more simple way
118///
119/// ## Usage:
120/// ```rust
121/// # use termint::{
122/// #     enums::Color,
123/// #     paragraph,
124/// #     widgets::{Paragraph, ToSpan},
125/// # };
126/// // Without macro:
127/// let p = Paragraph::new(vec![
128///     Box::new("Macro".to_span()),
129///     Box::new("test".fg(Color::Red))
130/// ]);
131/// // With macro:
132/// let p = paragraph!(
133///     "Macro".to_span(),
134///     "test".fg(Color::Red)
135/// );
136/// ```
137#[macro_export]
138macro_rules! paragraph {
139    ($($text:expr),* $(,)?) => {
140        $crate::widgets::Paragraph::new(vec![
141            $(Box::new($text)),*
142        ])
143    };
144}