summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 0650cdf199e3bd63a628d4175a3562e65b08c3ae (plain)
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
#[macro_use]
extern crate error_chain;

mod align;
mod bat;
mod cli;
mod config;
mod delta;
mod draw;
mod edits;
mod env;
mod paint;
mod parse;
mod style;

use std::io::{self, BufRead, ErrorKind, Read, Write};
use std::process;

use ansi_term;
use atty;
use structopt::StructOpt;
use syntect::highlighting::{Color, FontStyle, Style};

use crate::bat::assets::{list_languages, HighlightingAssets};
use crate::bat::output::{OutputType, PagingMode};
use crate::delta::delta;

mod errors {
    error_chain! {
        foreign_links {
            Io(::std::io::Error);
            SyntectError(::syntect::LoadingError);
            ParseIntError(::std::num::ParseIntError);
        }
    }
}

fn main() -> std::io::Result<()> {
    let opt = cli::Opt::from_args();

    let assets = HighlightingAssets::new();

    if opt.list_languages {
        list_languages()?;
        process::exit(0);
    } else if opt.list_theme_names {
        list_theme_names()?;
        process::exit(0);
    } else if opt.list_themes {
        list_themes(&assets)?;
        process::exit(0);
    }

    let config = cli::process_command_line_arguments(&assets, &opt);

    if opt.show_background_colors {
        show_background_colors(&config);
        process::exit(0);
    }

    let mut output_type = OutputType::from_mode(PagingMode::QuitIfOneScreen, None).unwrap();
    let mut writer = output_type.handle().unwrap();

    if let Err(error) = delta(
        io::stdin().lock().lines().map(|l| l.unwrap()),
        &config,
        &assets,
        &mut writer,
    ) {
        match error.kind() {
            ErrorKind::BrokenPipe => process::exit(0),
            _ => eprintln!("{}", error),
        }
    };
    Ok(())
}

fn show_background_colors(config: &config::Config) {
    println!(
        "delta \
         --minus-color=\"{minus_color}\" \
         --minus-emph-color=\"{minus_emph_color}\" \
         --plus-color=\"{plus_color}\" \
         --plus-emph-color=\"{plus_emph_color}\"",
        minus_color = color_to_hex(config.minus_style_modifier.background.unwrap()),
        minus_emph_color = color_to_hex(config.minus_emph_style_modifier.background.unwrap()),
        plus_color = color_to_hex(config.plus_style_modifier.background.unwrap()),
        plus_emph_color = color_to_hex(config.plus_emph_style_modifier.background.unwrap()),
    )
}

fn color_to_hex(color: Color) -> String {
    let mut string = String::new();
    let style = Style {
        foreground: style::NO_COLOR,
        background: color,
        font_style: FontStyle::empty(),
    };
    paint::paint_text(
        &format!("#{:02x?}{:02x?}{:02x?}", color.r, color.g, color.b),
        style,
        &mut string,
    );
    string.push_str("\x1b[0m"); // reset
    string
}

fn list_themes(assets: &HighlightingAssets) -> std::io::Result<()> {
    let opt = cli::Opt::from_args();
    let mut input = String::new();
    if atty::is(atty::Stream::Stdin) {
        input = "\
diff --git a/example.rs b/example.rs
index f38589a..0f1bb83 100644
--- a/example.rs
+++ b/example.rs
@@ -1,5 +1,5 @@
-// Output the square of a number.
-fn print_square(num: f64) {
-    let result = f64::powf(num, 2.0);
-    println!(\"The square of {:.2} is {:.2}.\", num, result);
+// Output the cube of a number.
+fn print_cube(num: f64) {
+    let result = f64::powf(num, 3.0);
+    println!(\"The cube of {:.2} is {:.2}.\", num, result);
 }"
        .to_string()
    } else {
        io::stdin().read_to_string(&mut input)?;
    }

    let stdout = io::stdout();
    let mut stdout = stdout.lock();
    let style = ansi_term::Style::new().bold();

    for (theme, _) in assets.theme_set.themes.iter() {
        if opt.light && !style::is_light_theme(theme) || opt.dark && style::is_light_theme(theme) {
            continue;
        }

        writeln!(stdout, "\nTheme: {}\n", style.paint(theme))?;
        let new_opt = cli::Opt {
            theme: Some(theme.to_string()),
            ..opt.clone()
        };
        let config = cli::process_command_line_arguments(&assets, &new_opt);
        let mut output_type = OutputType::from_mode(PagingMode::QuitIfOneScreen, None).unwrap();
        let mut writer = output_type.handle().unwrap();

        if let Err(error) = delta(
            input.split('\n').map(String::from),
            &config,
            &assets,
            &mut writer,
        ) {
            match error.kind() {
                ErrorKind::BrokenPipe => process::exit(0),
                _ => eprintln!("{}", error),
            }
        };
    }
    Ok(())
}

pub fn list_theme_names() -> std::io::Result<()> {
    let assets = HighlightingAssets::new();
    let themes = &assets.theme_set.themes;
    let stdout = io::stdout();
    let mut stdout = stdout.lock();

    writeln!(stdout, "Light themes:")?;
    for (theme, _) in themes.iter() {
        if style::is_light_theme(theme) {
            writeln!(stdout, "    {}", theme)?;
        }
    }
    writeln!(stdout, "Dark themes:")?;
    for (theme, _) in themes.iter() {
        if !style::is_light_theme(theme) {
            writeln!(stdout, "    {}", theme)?;
        }
    }
    Ok(())
}