tiny_tools/
macro.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#[macro_export]
#[cfg(feature = "debug")]
macro_rules! dprintln {
    ($($arg:tt)*) => (println!($($arg)*));
}

#[macro_export]
#[cfg(not(feature = "debug"))]
macro_rules! dprintln {
    ($($arg:tt)*) => {};
}

/// # verbose
#[macro_export]
macro_rules! verbose {
    ($verbose_flag:expr, $($arg:tt)*) => {
        if $verbose_flag {
        println!($($arg)*);
        }
    };
}

#[macro_export]
macro_rules! dev_debug {
($verbose_flag:expr, $($arg:tt)*) => {
    if $verbose_flag {
    println!("[{}:{}] {}", file!(), line!(), format!($($arg)*));
    }
};
}

/// Show an error to stderr in a similar style to GNU coreutils.
///
/// Takes a [`format!`]-like input and prints it to stderr. The output is
/// prepended with the current utility's name.
#[macro_export]
macro_rules! show_error(
    ($($args:tt)+) => ({
        eprint!("roxide: ");
        eprintln!($($args)+);
    })
);

// Prompt the user with a formatted string and returns `true` if they reply `'y'` or `'Y'`
//
// This macro functions accepts the same syntax as `format!`. The prompt is written to
// `stderr`. A space is also printed at the end for nice spacing between the prompt and
// the user input. Any input starting with `'y'` or `'Y'` is interpreted as `yes`.
//#[macro_export]
//macro_rules! prompt_yes(
//    ($($args:tt)+) => ({
//        use std::io::Write;
//        eprint!("roxide: ");
//        eprint!($($args)+);
//        eprint!(" ");
//    //    uucore::crash_if_err!(1, std::io::stderr().flush());
//        uucore::read_yes()
//    })
//);

/// Macro to prompt the user with a message and collect input.
/// Returns `true` if the input is "yes" or "y" (case-insensitive), otherwise `false`.
///
/// Example usage:
/// ```
/// if prompt_yes!("Do you want to continue? (yes/y):") {
///     println!("Continuing...");
/// } else {
///     println!("Exiting...");
/// }
/// ```
#[macro_export]
macro_rules! prompt_yes {
    ($($arg:tt)*) => {{
        use std::io::{self, Write};
        // Print the prompt and flush stdout
        print!("roxide: ");
        print!($($arg)*);
        print!(" "); // Add a space after the prompt
        io::stdout().flush().unwrap();

        // Read input from stdin
        let mut input = String::new();
        io::stdin().read_line(&mut input).unwrap();

        // Trim and check for "yes" or "y" (case-insensitive)
        matches!(input.trim().to_lowercase().as_str(), "yes" | "y")
    }};
}