aboutsummaryrefslogtreecommitdiff
path: root/src/output.rs
blob: fe3b14107fc46cbb677365e3376cd6153e5a634c (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
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
use crate::types::{DomainResult, DomainStatus, ErrorKind};
use colored::*;
use std::io::Write;
use std::path::PathBuf;

pub fn print_available_table(results: &[DomainResult], no_color: bool, no_unicode: bool) {
    let available: Vec<&DomainResult> = results.iter().filter(|r| r.is_available()).collect();

    if available.is_empty() {
        println!("No available domains found.");
        return;
    }

    let max_len = available.iter().map(|r| r.full.len()).max().unwrap_or(20);
    let width = max_len + 4; // padding

    let title = "Available Domains";
    let title_padded = format!("{:^width$}", title, width = width);

    if no_unicode {
        // ASCII box
        let border = format!("+{}+", "-".repeat(width));
        println!("{}", border);
        if no_color {
            println!("|{}|", title_padded);
        } else {
            println!("|{}|", title_padded.green());
        }
        println!("+{}+", "-".repeat(width));
        for r in &available {
            println!("| {:<pad$} |", r.full, pad = width - 2);
        }
        println!("{}", border);
    } else {
        // Unicode box
        let top = format!("┌{}┐", "─".repeat(width));
        let sep = format!("├{}┤", "─".repeat(width));
        let bot = format!("└{}┘", "─".repeat(width));
        println!("{}", top);
        if no_color {
            println!("│{}│", title_padded);
        } else {
            println!("│{}│", title_padded.green());
        }
        println!("{}", sep);
        for r in &available {
            println!("│ {:<pad$} │", r.full, pad = width - 2);
        }
        println!("{}", bot);
    }
}

pub fn print_full_table(results: &[DomainResult], no_color: bool, no_unicode: bool) {
    if results.is_empty() {
        println!("No results.");
        return;
    }

    // calc column widths
    let domain_w = results.iter().map(|r| r.full.len()).max().unwrap_or(10).max(7);
    let status_w = 10; // "registered" is the longest
    let note_w = results.iter().map(|r| r.note_str().len()).max().unwrap_or(4).max(4);

    let domain_col = domain_w + 2;
    let status_col = status_w + 2;
    let note_col = note_w + 2;

    if no_unicode {
        print_full_table_ascii(results, domain_col, status_col, note_col, no_color);
    } else {
        print_full_table_unicode(results, domain_col, status_col, note_col, no_color);
    }
}

fn print_full_table_unicode(
    results: &[DomainResult],
    dc: usize,
    sc: usize,
    nc: usize,
    no_color: bool,
) {
    let top = format!("┌{}┬{}┬{}┐", "─".repeat(dc), "─".repeat(sc), "─".repeat(nc));
    let sep = format!("├{}┼{}┼{}┤", "─".repeat(dc), "─".repeat(sc), "─".repeat(nc));
    let bot = format!("└{}┴{}┴{}┘", "─".repeat(dc), "─".repeat(sc), "─".repeat(nc));

    println!("{}", top);
    println!(
        "│{:^dc$}│{:^sc$}│{:^nc$}│",
        "Domains",
        "Status",
        "Note",
        dc = dc,
        sc = sc,
        nc = nc,
    );
    println!("{}", sep);

    for r in results {
        let domain_str = format!(" {:<width$} ", r.full, width = dc - 2);
        let status_str = format!(" {:<width$} ", r.status_str(), width = sc - 2);
        let note_str = format!(" {:<width$} ", r.note_str(), width = nc - 2);

        if no_color {
            println!("│{}│{}│{}│", domain_str, status_str, note_str);
        } else {
            let colored_domain = color_domain(&domain_str, &r.status);
            println!("│{}│{}│{}│", colored_domain, status_str, note_str);
        }
    }

    println!("{}", bot);
}

fn print_full_table_ascii(
    results: &[DomainResult],
    dc: usize,
    sc: usize,
    nc: usize,
    no_color: bool,
) {
    let border = format!("+{}+{}+{}+", "-".repeat(dc), "-".repeat(sc), "-".repeat(nc));

    println!("{}", border);
    println!(
        "|{:^dc$}|{:^sc$}|{:^nc$}|",
        "Domains",
        "Status",
        "Note",
        dc = dc,
        sc = sc,
        nc = nc,
    );
    println!("{}", border);

    for r in results {
        let domain_str = format!(" {:<width$} ", r.full, width = dc - 2);
        let status_str = format!(" {:<width$} ", r.status_str(), width = sc - 2);
        let note_str = format!(" {:<width$} ", r.note_str(), width = nc - 2);

        if no_color {
            println!("|{}|{}|{}|", domain_str, status_str, note_str);
        } else {
            let colored_domain = color_domain(&domain_str, &r.status);
            println!("|{}|{}|{}|", colored_domain, status_str, note_str);
        }
    }

    println!("{}", border);
}

fn color_domain(domain: &str, status: &DomainStatus) -> ColoredString {
    match status {
        DomainStatus::Available => domain.green(),
        DomainStatus::Registered { .. } => domain.red(),
        DomainStatus::Error { kind, .. } => match kind {
            ErrorKind::InvalidTld => domain.yellow(),
            _ => domain.blue(),
        },
    }
}

pub fn print_csv(results: &[DomainResult]) {
    println!("Domains, Status, Note");
    for r in results {
        println!("{}, {}, {}", r.full, r.status_str(), r.note_str());
    }
}

pub fn write_csv_file(results: &[DomainResult], path: &PathBuf) -> Result<(), String> {
    let mut file = std::fs::File::create(path)
        .map_err(|e| format!("Could not create CSV file: {}", e))?;
    writeln!(file, "Domains, Status, Note")
        .map_err(|e| format!("Write error: {}", e))?;
    for r in results {
        writeln!(file, "{}, {}, {}", r.full, r.status_str(), r.note_str())
            .map_err(|e| format!("Write error: {}", e))?;
    }
    Ok(())
}

pub fn print_errors(results: &[DomainResult], verbose: bool) {
    for r in results {
        if let DomainStatus::Error { kind, message } = &r.status {
            match kind {
                ErrorKind::InvalidTld => {
                    eprintln!("Error for {}, tld does not seem to exist", r.full);
                }
                _ => {
                    if verbose {
                        eprintln!("Error for {}, {} (raw: {})", r.full, message, message);
                    } else {
                        eprintln!(
                            "Error for {}, unknown error (enable verbose to see raw error)",
                            r.full
                        );
                    }
                }
            }
        }
    }
}

pub fn print_progress(current: usize, total: usize) {
    let percent = (current as f64 / total as f64 * 100.0) as u32;
    eprint!("\rParsing results : {}%", percent);
    if current == total {
        eprintln!("\rParsing results : Done   ");
    }
}