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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use core::sync::atomic::{AtomicBool, Ordering as AtomicOrdering};

#[cfg(feature = "custom-default-colors")]
use atomic_ref::AtomicRef;

use crate::{AnsiColorScheme, DEFAULT_DEFAULT_COLOR_SCHEME};

static SHOULD_COLORIZE: AtomicBool = AtomicBool::new(false);

#[cfg(feature = "custom-default-colors")]
#[cfg_attr(docsrs, doc(cfg(feature = "custom-default-colors")))]
static DEFAULT_COLOR_SCHEME: AtomicRef<'_, AnsiColorScheme> = AtomicRef::new(None);

/// Enables or disables ANSI colorization.
///
/// Note that this function does not check whether the terminal supports
/// 16-ANSI-color mode or not and doesn't check `NO_COLOR` or `FORCE_COLOR`
/// environment variables. If you need to enable colorization only if supported
/// by the terminal, and you don't need any custom colorization enable
/// conditions, please use [`enable_colors_if_supported`].
///
/// # Examples
///
///
/// ```rust
/// use unwind_context::unwind_context;
///
/// fn func(foo: u32, bar: &str) {
///     let _ctx = unwind_context!(fn(foo, bar));
///     // ...
/// }
/// # /*
/// fn main() {
/// # */
///     unwind_context::set_colors_enabled(true);
/// #   test();
///     // ...
///     func(123, "abc");
///     // ...
/// # /*
/// }
///
/// # */
/// # /*
/// #[test]
/// # */
/// fn test() {
///     unwind_context::set_colors_enabled(true);
///     // ...
///     func(234, "bcd");
///     // ...
/// }
/// ```
#[inline]
pub fn set_colors_enabled(enabled: bool) {
    SHOULD_COLORIZE.store(enabled, AtomicOrdering::Relaxed);
}

#[doc(hidden)]
#[deprecated(since = "0.2.0", note = "renamed to `set_colors_enabled`.")]
pub use set_colors_enabled as set_ansi_colors_enabled;

/// Returns `true` if ANSI colors were enabled before.
///
/// By default colorization is disabled.
///
/// # Examples
///
/// ```rust
/// if unwind_context::are_colors_enabled() {
///     eprintln!("colorization is enabled");
/// } else {
///     eprintln!("colorization is disabled");
/// }
/// ```
#[inline]
pub fn are_colors_enabled() -> bool {
    SHOULD_COLORIZE.load(AtomicOrdering::Relaxed)
}

#[doc(hidden)]
#[deprecated(since = "0.2.0", note = "renamed to `are_colors_enabled`.")]
pub use are_colors_enabled as are_ansi_colors_enabled;

#[cfg(feature = "detect-color-support")]
#[cfg_attr(docsrs, doc(cfg(feature = "detect-color-support")))]
/// Enables ANSI colors if supported by the terminal for stderr stream for all
/// threads.
///
/// It checks for a basic colors support. By default, it enables 16-ANSI-color
/// colorization if the colors have not changed.
///
/// This function uses [`supports-color`] crate to detect color support.
/// [`supports-color`] crate takes the `NO_COLOR` and `FORCE_COLOR` environment
/// variables into account as well.
///
/// [`unwind_context`]: crate::unwind_context
/// [`debug_unwind_context`]: crate::debug_unwind_context
///
/// # Examples
///
/// ```rust
/// use unwind_context::unwind_context;
///
/// fn func(foo: u32, bar: &str) {
///     let _ctx = unwind_context!(fn(foo, bar));
///     // ...
/// }
/// # /*
/// fn main() {
/// # */
///     unwind_context::enable_colors_if_supported();
/// #   test();
///     // ...
///     func(123, "abc");
///     // ...
/// # /*
/// }
///
/// # */
/// # /*
/// #[test]
/// # */
/// fn test() {
///     unwind_context::enable_colors_if_supported();
///     // ...
///     func(234, "bcd");
///     // ...
/// }
/// ```
///
/// [`supports-color`]: https://crates.io/crates/supports-color
#[inline]
pub fn enable_colors_if_supported() {
    use supports_color::Stream;
    if supports_color::on(Stream::Stderr).is_some() {
        set_colors_enabled(true);
    }
}

#[cfg(feature = "detect-color-support")]
#[doc(hidden)]
#[deprecated(since = "0.2.0", note = "renamed to `enable_colors_if_supported`.")]
pub use enable_colors_if_supported as enable_ansi_colors_if_supported;

#[cfg(feature = "custom-default-colors")]
#[cfg_attr(docsrs, doc(cfg(feature = "custom-default-colors")))]
/// Sets default ANSI color scheme for all threads.
///
/// This function uses [`atomic_ref`] crate to modify a static `AtomicRef` with
/// a default ANSI color scheme.
///
/// # Examples
///
/// ```rust
/// unwind_context::set_default_color_scheme(&unwind_context::AnsiColorScheme {
///     default: "\u{1b}[0m",
///     location: "\u{1b}[31m",
///     fn_keyword: "\u{1b}[32m",
///     func_name: "\u{1b}[33m",
///     func_braces: "\u{1b}[34m",
///     value_braces: "\u{1b}[35m",
///     ident: "\u{1b}[36m",
///     item: "\u{1b}[37m",
///     boolean: "\u{1b}[91m",
///     number: "\u{1b}[92m",
///     quoted: "\u{1b}[93m",
///     escaped: "\u{1b}[94m",
/// });
/// ```
///
/// [`atomic_ref`]: https://crates.io/crates/atomic_ref
#[inline]
pub fn set_default_color_scheme(color_scheme: &'static AnsiColorScheme) {
    DEFAULT_COLOR_SCHEME.store(Some(color_scheme), AtomicOrdering::Release);
}

#[cfg(feature = "custom-default-colors")]
#[doc(hidden)]
#[deprecated(since = "0.2.0", note = "renamed to `set_default_color_scheme`.")]
pub use set_default_color_scheme as set_ansi_color_scheme;

/// Returns the currently set default ANSI color scheme.
///
/// # Examples
///
/// ```rust
/// if unwind_context::are_colors_enabled() {
///     eprintln!("colorization is enabled");
/// } else {
///     eprintln!("colorization is disabled");
/// }
///
/// let color_scheme = unwind_context::get_default_color_scheme();
/// eprintln!("color scheme: {:?}", color_scheme);
/// ```
#[inline]
#[must_use]
pub fn get_default_color_scheme() -> &'static AnsiColorScheme {
    get_default_ansi_color_scheme_impl()
}

#[doc(hidden)]
#[deprecated(since = "0.2.0", note = "renamed to `get_default_color_scheme`.")]
pub use get_default_color_scheme as get_ansi_color_scheme;

#[cfg(feature = "custom-default-colors")]
#[inline]
fn get_default_ansi_color_scheme_impl() -> &'static AnsiColorScheme {
    DEFAULT_COLOR_SCHEME
        .load(AtomicOrdering::Acquire)
        .unwrap_or(&DEFAULT_DEFAULT_COLOR_SCHEME)
}

#[cfg(not(feature = "custom-default-colors"))]
#[inline]
fn get_default_ansi_color_scheme_impl() -> &'static AnsiColorScheme {
    &DEFAULT_DEFAULT_COLOR_SCHEME
}

/// Returns current ANSI color scheme if ANSI colors were enabled, `None`
/// otherwise.
///
/// # Examples
///
/// ```rust
/// if let Some(color_scheme) = unwind_context::get_default_color_scheme_if_enabled() {
///     eprintln!(
///         "colorization is enabled with the following color scheme: {:?}",
///         color_scheme
///     );
/// } else {
///     eprintln!("colorization is disabled");
/// }
/// ```
#[inline]
#[must_use]
pub fn get_default_color_scheme_if_enabled() -> Option<&'static AnsiColorScheme> {
    are_colors_enabled().then(get_default_color_scheme)
}

#[doc(hidden)]
#[deprecated(
    since = "0.2.0",
    note = "renamed to `get_default_color_scheme_if_enabled`."
)]
pub use get_default_color_scheme_if_enabled as get_ansi_color_scheme_if_colors_enabled;

#[cfg(all(test, feature = "std"))]
mod tests {
    #[cfg(all(feature = "std", feature = "detect-color-support"))]
    use crate::enable_colors_if_supported;
    use crate::test_common::{SERIAL_TEST, TEST_COLOR_SCHEME};
    use crate::test_util::FixedBufWriter;
    use crate::{
        are_colors_enabled, set_colors_enabled, unwind_context_with_fmt, StdPanicDetector,
    };
    #[cfg(feature = "custom-default-colors")]
    use crate::{set_default_color_scheme, DEFAULT_DEFAULT_COLOR_SCHEME};

    #[test]
    fn test_set_ansi_colors_enabled() {
        let _guard = SERIAL_TEST.lock().unwrap();

        let mut buffer = [0; 128];
        let foo = 123;
        let bar = "BAR";

        assert!(!are_colors_enabled());

        // Colors are disabled by default.
        let mut writer = FixedBufWriter::new(&mut buffer);
        let mut ctx = unwind_context_with_fmt!(
            (foo, bar),
            writer = &mut writer,
            panic_detector = StdPanicDetector
        );
        ctx.print();
        drop(ctx);
        assert!(writer
            .into_str()
            .starts_with("foo: 123, bar: \"BAR\"\n    at "));

        // Colors are used if local color scheme if specified.
        let mut writer = FixedBufWriter::new(&mut buffer);
        let mut ctx = unwind_context_with_fmt!(
            (foo, bar),
            writer = &mut writer,
            panic_detector = StdPanicDetector,
            color_scheme = Some(&TEST_COLOR_SCHEME)
        );
        ctx.print();
        drop(ctx);
        assert!(writer
            .into_str()
            .starts_with("foo: {NUM}123{DEF}, bar: {QUOT}\"BAR\"{DEF}\n    at {LOC}"));

        set_colors_enabled(true);
        assert!(are_colors_enabled());

        // The default color scheme is used if colors are enabled globally.
        let mut writer = FixedBufWriter::new(&mut buffer);
        let mut ctx = unwind_context_with_fmt!(
            (foo, bar),
            writer = &mut writer,
            panic_detector = StdPanicDetector,
        );
        ctx.print();
        drop(ctx);
        assert!(writer.into_str().starts_with(
            "foo: \u{1b}[0;96m123\u{1b}[0m, bar: \u{1b}[0;32m\"BAR\"\u{1b}[0m\n    at \u{1b}[94m"
        ));

        // The local color scheme overrides the global one is used if specified.
        let mut writer = FixedBufWriter::new(&mut buffer);
        assert!(are_colors_enabled());
        let mut ctx = unwind_context_with_fmt!(
            (foo, bar),
            writer = &mut writer,
            panic_detector = StdPanicDetector,
            color_scheme = Some(&TEST_COLOR_SCHEME)
        );
        ctx.print();
        drop(ctx);
        assert!(writer
            .into_str()
            .starts_with("foo: {NUM}123{DEF}, bar: {QUOT}\"BAR\"{DEF}\n    at {LOC}"));

        set_colors_enabled(false);
        assert!(!are_colors_enabled());

        // When colors are disabled, it no longer uses any color scheme.
        let mut writer = FixedBufWriter::new(&mut buffer);
        let mut ctx = unwind_context_with_fmt!(
            (foo, bar),
            writer = &mut writer,
            panic_detector = StdPanicDetector
        );
        ctx.print();
        drop(ctx);
        assert!(writer
            .into_str()
            .starts_with("foo: 123, bar: \"BAR\"\n    at "));
    }

    #[cfg(all(feature = "std", feature = "detect-color-support"))]
    #[test]
    fn test_enable_ansi_colors_if_supported() {
        let _guard = SERIAL_TEST.lock().unwrap();

        assert!(!are_colors_enabled());

        std::env::remove_var("FORCE_COLOR");
        std::env::remove_var("NO_COLOR");
        std::env::set_var("IGNORE_IS_TERMINAL", "true");
        std::env::set_var("TERM", "dumb");
        enable_colors_if_supported();
        assert!(!are_colors_enabled());

        std::env::set_var("TERM", "xterm-256color");
        std::env::set_var("COLORTERM", "truecolor");
        enable_colors_if_supported();
        assert!(are_colors_enabled());
        set_colors_enabled(false);

        std::env::set_var("NO_COLOR", "true");
        enable_colors_if_supported();
        assert!(!are_colors_enabled());

        std::env::remove_var("NO_COLOR");
        std::env::set_var("FORCE_COLOR", "true");
        enable_colors_if_supported();
        assert!(are_colors_enabled());
        set_colors_enabled(false);

        set_colors_enabled(false);
        assert!(!are_colors_enabled());
    }

    #[cfg(feature = "custom-default-colors")]
    #[test]
    fn test_set_default_ansi_color_scheme() {
        let _guard = SERIAL_TEST.lock().unwrap();

        let mut buffer = [0; 128];
        let foo = 123;
        let bar = "BAR";

        set_colors_enabled(true);
        assert!(are_colors_enabled());

        // The default color scheme is used if colors are enabled globally.
        let mut writer = FixedBufWriter::new(&mut buffer);
        let mut ctx = unwind_context_with_fmt!(
            (foo, bar),
            writer = &mut writer,
            panic_detector = StdPanicDetector,
        );
        ctx.print();
        drop(ctx);
        assert!(writer.into_str().starts_with(concat!(
            "foo: \u{1b}[0;96m123",
            "\u{1b}[0m, bar: \u{1b}[0;32m\"BAR\"",
            "\u{1b}[0m\n    at \u{1b}[94m"
        )));

        set_default_color_scheme(&TEST_COLOR_SCHEME);

        // The default color scheme can be changed.
        let mut writer = FixedBufWriter::new(&mut buffer);
        assert!(are_colors_enabled());
        let mut ctx = unwind_context_with_fmt!(
            (foo, bar),
            writer = &mut writer,
            panic_detector = StdPanicDetector,
        );
        ctx.print();
        drop(ctx);
        assert!(writer
            .into_str()
            .starts_with("foo: {NUM}123{DEF}, bar: {QUOT}\"BAR\"{DEF}\n    at {LOC}"));

        set_default_color_scheme(&DEFAULT_DEFAULT_COLOR_SCHEME);

        // The default color scheme can be changed.
        let mut writer = FixedBufWriter::new(&mut buffer);
        assert!(are_colors_enabled());
        let mut ctx = unwind_context_with_fmt!(
            (foo, bar),
            writer = &mut writer,
            panic_detector = StdPanicDetector,
        );
        ctx.print();
        drop(ctx);
        assert!(writer.into_str().starts_with(concat!(
            "foo: \u{1b}[0;96m123",
            "\u{1b}[0m, bar: \u{1b}[0;32m\"BAR\"",
            "\u{1b}[0m\n    at \u{1b}[94m"
        )));

        set_colors_enabled(false);
        assert!(!are_colors_enabled());
    }
}