libdiffsitter/
console_utils.rs

1//! Helper functions for dealing with the terminal
2
3use console::{set_colors_enabled, set_colors_enabled_stderr};
4use strum::{Display, EnumString};
5
6/// Whether the output to the terminal should be colored
7#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumString, Display)]
8#[strum(serialize_all = "snake_case")]
9#[derive(Default)]
10pub enum ColorOutputPolicy {
11    /// Automatically enable color if printing to a TTY, otherwise disable color
12    #[default]
13    Auto,
14    /// Force plaintext output
15    Off,
16    /// Force color output
17    On,
18}
19
20/// Set terminal color settings based on the output policy.
21pub fn set_term_colors(setting: ColorOutputPolicy) {
22    if setting == ColorOutputPolicy::Auto {
23        return;
24    }
25    let colors_enabled = match setting {
26        ColorOutputPolicy::On => true,
27        ColorOutputPolicy::Off => false,
28        ColorOutputPolicy::Auto => {
29            panic!("Color output policy is auto, this case should have been already handled")
30        }
31    };
32    set_colors_enabled(colors_enabled);
33    set_colors_enabled_stderr(colors_enabled);
34}