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
use crate::config::Config;
use nu_protocol::{Primitive, UntaggedValue, Value};
use nu_source::{AnchorLocation, Tag};
use std::path::Path;

#[derive(Default)]
pub struct TextView;

impl TextView {
    pub fn new() -> TextView {
        TextView
    }
}

fn get_file_path(source: AnchorLocation) -> Option<String> {
    match source {
        AnchorLocation::File(file) => {
            let path = Path::new(&file);

            Some(path.to_string_lossy().to_string())
        }
        AnchorLocation::Url(url) => url::Url::parse(&url).ok().and_then(|url| {
            url.path_segments().and_then(|mut segments| {
                segments
                    .next_back()
                    .map(|segment| Path::new(segment).to_string_lossy().to_string())
            })
        }),
        //FIXME: this probably isn't correct
        AnchorLocation::Source(_source) => None,
    }
}

#[allow(clippy::cognitive_complexity)]
pub fn view_text_value(value: &Value) {
    let config = nu_data::config::config(Tag::unknown())
        .ok()
        .and_then(|config| config.get("textview").map(Config::from))
        .unwrap_or_default();

    if let UntaggedValue::Primitive(Primitive::String(ref s)) = &value.value {
        let mut printer = bat::PrettyPrinter::new();

        printer
            .term_width(config.term_width)
            .tab_width(Some(config.tab_width as usize))
            .colored_output(config.colored_output)
            .true_color(config.true_color)
            .header(config.header)
            .line_numbers(config.line_numbers)
            .grid(config.grid)
            .vcs_modification_markers(config.vcs_modification_markers)
            .snip(config.snip)
            .wrapping_mode(config.wrapping_mode)
            .use_italics(config.use_italics)
            .paging_mode(config.paging_mode)
            .pager(&config.pager)
            .line_ranges(config.line_ranges)
            .highlight_range(
                config.highlight_range_from as usize,
                config.highlight_range_to as usize,
            )
            .theme(&config.theme);

        match value.anchor().and_then(get_file_path) {
            Some(file_path) => printer.input(bat::Input::from_bytes(s.as_bytes()).name(file_path)),
            None => printer.input_from_bytes(s.as_bytes()),
        };

        printer.print().expect("Error with bat PrettyPrint");
    }
}