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
use item::*;
use print_config::*;
use style::*;

use std::io;

struct Indent {
    pub regular_prefix: String,
    pub child_prefix: String,
    pub last_regular_prefix: String,
    pub last_child_prefix: String,
}

impl Indent {
    pub fn from_config(config: &PrintConfig) -> Indent {
        Self::from_characters_and_padding(config.indent, config.padding, &config.characters)
    }

    #[allow(dead_code)]
    pub fn from_characters(indent_size: usize, characters: &IndentChars) -> Indent {
        Self::from_characters_and_padding(indent_size, 1, characters)
    }

    pub fn from_characters_and_padding(indent_size: usize, padding: usize, characters: &IndentChars) -> Indent {
        let m = 1 + padding;
        let n = if indent_size > m { indent_size - m } else { 0 };

        let right_pad = characters.right.repeat(n);
        let empty_pad = characters.empty.repeat(n);
        let item_pad = characters.empty.repeat(padding);

        Indent {
            regular_prefix: format!("{}{}{}", characters.down_and_right, right_pad, item_pad),
            child_prefix: format!("{}{}{}", characters.down, empty_pad, item_pad),
            last_regular_prefix: format!("{}{}{}", characters.turn_right, right_pad, item_pad),
            last_child_prefix: format!("{}{}{}", characters.empty, empty_pad, item_pad),
        }
    }
}

fn print_item<T: TreeItem, W: io::Write>(
    item: &T,
    f: &mut W,
    prefix: String,
    child_prefix: String,
    config: &PrintConfig,
    characters: &Indent,
    branch_style: &Style,
    leaf_style: &Style,
    level: u32,
) -> io::Result<()> {
    write!(f, "{}", branch_style.paint(prefix))?;
    item.write_self(f, leaf_style)?;
    writeln!(f, "")?;

    if level < config.depth {
        let children = item.children();
        if let Some((last_child, children)) = children.split_last() {
            let rp = child_prefix.clone() + &characters.regular_prefix;
            let cp = child_prefix.clone() + &characters.child_prefix;

            for c in children {
                print_item(
                    c,
                    f,
                    rp.clone(),
                    cp.clone(),
                    config,
                    characters,
                    branch_style,
                    leaf_style,
                    level + 1,
                )?;
            }

            let rp = child_prefix.clone() + &characters.last_regular_prefix;
            let cp = child_prefix.clone() + &characters.last_child_prefix;

            print_item(
                last_child,
                f,
                rp,
                cp,
                config,
                characters,
                branch_style,
                leaf_style,
                level + 1,
            )?;
        }
    }

    Ok(())
}

/// Print the tree `item` to standard output using default formatting
pub fn print_tree<T: TreeItem>(item: &T) -> io::Result<()> {
    print_tree_with(item, &PrintConfig::from_env())
}

/// Print the tree `item` to standard output using custom formatting
pub fn print_tree_with<T: TreeItem>(item: &T, config: &PrintConfig) -> io::Result<()> {
    let (branch_style, leaf_style) = if config.should_style_output(OutputKind::Stdout) {
        (config.branch.clone(), config.leaf.clone())
    } else {
        (Style::default(), Style::default())
    };

    let characters = Indent::from_config(config);
    let out = io::stdout();
    let mut handle = out.lock();
    print_item(
        item,
        &mut handle,
        "".to_string(),
        "".to_string(),
        config,
        &characters,
        &branch_style,
        &leaf_style,
        0,
    )
}

/// Write the tree `item` to writer `f` using default formatting
pub fn write_tree<T: TreeItem, W: io::Write>(item: &T, mut f: W) -> io::Result<()> {
    write_tree_with(item, &mut f, &PrintConfig::from_env())
}

/// Write the tree `item` to writer `f` using custom formatting
pub fn write_tree_with<T: TreeItem, W: io::Write>(item: &T, mut f: W, config: &PrintConfig) -> io::Result<()> {
    let (branch_style, leaf_style) = if config.should_style_output(OutputKind::Unknown) {
        (config.branch.clone(), config.leaf.clone())
    } else {
        (Style::default(), Style::default())
    };

    let characters = Indent::from_config(config);
    print_item(
        item,
        &mut f,
        "".to_string(),
        "".to_string(),
        config,
        &characters,
        &branch_style,
        &leaf_style,
        0,
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use print_config::PrintConfig;

    #[test]
    fn indent_from_characters() {
        let indent = Indent::from_characters(4, &UTF_CHARS.into());
        assert_eq!(indent.regular_prefix, "├── ");
        assert_eq!(indent.last_regular_prefix, "└── ");
        assert_eq!(indent.child_prefix, "│   ");
        assert_eq!(indent.last_child_prefix, "    ");
    }

    #[test]
    fn indent_from_characters_ascii() {
        let indent = Indent::from_characters(6, &ASCII_CHARS_TICK.into());
        assert_eq!(indent.regular_prefix, "|---- ");
        assert_eq!(indent.last_regular_prefix, "`---- ");
        assert_eq!(indent.child_prefix, "|     ");
        assert_eq!(indent.last_child_prefix, "      ");
    }

    #[test]
    fn indent_from_config() {
        let config = {
            let mut config = PrintConfig::default();
            config.indent = 3;
            config.characters = UTF_CHARS.into();
            config
        };
        let indent = Indent::from_config(&config);
        assert_eq!(indent.regular_prefix, "├─ ");
        assert_eq!(indent.last_regular_prefix, "└─ ");
        assert_eq!(indent.child_prefix, "│  ");
        assert_eq!(indent.last_child_prefix, "   ");
    }

    #[test]
    fn indent_from_characters_pad() {
        let indent = Indent::from_characters_and_padding(4, 0, &UTF_CHARS.into());
        assert_eq!(indent.regular_prefix, "├───");
        assert_eq!(indent.last_regular_prefix, "└───");
        assert_eq!(indent.child_prefix, "│   ");
        assert_eq!(indent.last_child_prefix, "    ");

        let indent = Indent::from_characters_and_padding(4, 2, &UTF_CHARS.into());
        assert_eq!(indent.regular_prefix, "├─  ");
        assert_eq!(indent.last_regular_prefix, "└─  ");
        assert_eq!(indent.child_prefix, "│   ");
        assert_eq!(indent.last_child_prefix, "    ");
    }
}