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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
use std::fmt;
use std::io::{self, Write};
use std::ops::Deref;

use indextree::NodeId;
use termion::color::{Bg, Fg, Reset};

use fs::FileType;
use options::RenderOptions;
use tree::{PrefixPiece, Tree};

pub const MID_BRANCH: &str = "├──";
pub const END_BRANCH: &str = "└──";
pub const BLANK_INDENT: &str = "    ";
pub const BAR_INDENT: &str = "│   ";

pub const FOLD_MARK: &str = "*";
pub const RESTRICTED_MARK: &str = " [error opening dir]";
pub const LINK_MARK: &str = " -> ";

pub struct TreeRender<'a> {
    pub tree: &'a mut Tree,
    opts: RenderOptions,
}

impl<'a> fmt::Display for TreeRender<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "{}", self.tree.tree[self.tree.root].data.name)?;

        let mut l_ind = 1;
        while let Some(line) = &self.tree.lines.lines.get(l_ind) {
            writeln!(
                f,
                "{} {}{}",
                self.prefix_string(&line.prefix),
                self.tree.tree[line.node].data.name,
                self.suffix_for_node(line.node)
            )?;

            l_ind = line.next;
        }

        writeln!(f, "\n{}", self.tree.summary())?;

        Ok(())
    }
}

impl<'a> TreeRender<'a> {
    pub fn new(tree: &'a mut Tree, opts: RenderOptions) -> Self {
        Self { tree, opts }
    }

    pub fn focus_up(&mut self) {
        self.tree.focus_up();
    }

    pub fn focus_down(&mut self) {
        self.tree.focus_down();
    }

    pub fn focus_left(&mut self) {
        self.tree.focus_left();
    }

    pub fn focus_right(&mut self) {
        self.tree.focus_right();
    }

    pub fn toggle_focus_fold(&mut self) {
        self.tree.toggle_focus_fold();
    }

    fn prefix_string(&self, prefix: &Vec<PrefixPiece>) -> String {
        prefix.iter().fold(String::new(), |acc, pre| {
            acc + match pre {
                PrefixPiece::BarIndent => BAR_INDENT,
                PrefixPiece::BlankIndent => BLANK_INDENT,
                PrefixPiece::MidBranch => MID_BRANCH,
                PrefixPiece::EndBranch => END_BRANCH,
            }
        })
    }

    fn suffix_for_node(&self, node: NodeId) -> String {
        match &self.tree.tree[node].data.ft {
            FileType::File => String::new(),
            FileType::Dir => {
                if self.tree
                    .lines
                    .folded
                    .contains(&self.tree.lines.inds[&node])
                {
                    FOLD_MARK.to_owned()
                } else {
                    String::new()
                }
            }
            FileType::RestrictedDir => RESTRICTED_MARK.to_owned(),
            FileType::LinkTo(dest) => {
                let mut s = String::from(LINK_MARK);
                s.push_str(dest);
                s
            }
            FileType::Stdin => String::from("<stdin>"),
        }
    }

    /// Render at most n consecutive lines of the tree around the focused node.
    ///
    /// Lines are considered consecutive if they are adjacent in the
    /// doubly-linked list of lines in which a line's `next` and `prev`
    /// fields comprise the links.
    pub fn render_around_focus<W: Write>(
        &self,
        writer: &mut W,
        n: usize,
        width: usize,
    ) -> io::Result<()> {
        let y = self.tree.lines.inds[&self.tree.focused];
        let (mut start, end) = self.bounds_of_range_around_line(y, n, width);

        print!("{}", Fg(self.opts.fg_color.deref()));
        while start < end {
            let next = self.tree.lines.lines[start].next;
            let last = self.tree.lines.lines.get(next).is_none() || next >= end;

            self.render_line(writer, start, start == y, last)?;
            start = next
        }
        print!("{}", Fg(Reset));

        Ok(())
    }

    fn visual_lines_for_line(&self, l_ind: usize, width: usize) -> usize {
        let line = &self.tree.lines.lines[l_ind];
        let mut pl = line.prefix.len();
        if pl != 0 {
            pl += 1; // If not the root
        }
        pl += self.tree.tree[line.node].data.name.len();

        pl / width + 1
    }

    /// Find the bounds of the range of n consecutively renderable lines
    /// around a given line.
    ///
    /// Lines are considered consecutive if they follow each other in the
    /// doubly-linked list in which a line's `next` and `prev` fields comprise
    /// the edges.
    ///
    /// The range will include n/2 lines above and n/2 lines below the given line.
    /// If the given line is within n/2 lines of the top or bottom of the tree,
    /// the remaining space will be used on the other side.
    fn bounds_of_range_around_line(&self, line: usize, n: usize, width: usize) -> (usize, usize) {
        let space = n / 2;

        // Roll the start back n/2 spaces. If fewer, save the diff.
        let mut start = line;
        let mut start_diff = 0;
        let mut i = 0;
        while i < space {
            if let Some(prev) = self.tree.lines.lines[start].prev {
                i += self.visual_lines_for_line(start, width);
                start = prev;
            } else {
                start_diff = space - i;
                break;
            }
        }

        // Roll the end forward n/2 + start_diff spaces. If fewer, save the diff.
        let mut end = line;
        let mut end_diff = 0;
        let end_max = space + n % 2 + start_diff;
        let mut i = 0;
        while i < end_max {
            let next = self.tree.lines.lines[end].next;
            if let Some(_) = self.tree.lines.lines.get(next) {
                i += self.visual_lines_for_line(end, width);
                end = next;
            } else {
                end += 1;
                end_diff = end_max - i - 1;
                break;
            }
        }

        // Roll the start back at most an additional end_diff spaces.
        let mut i = 0;
        while i < end_diff {
            if let Some(prev) = self.tree.lines.lines[start].prev {
                i += self.visual_lines_for_line(start, width);
                start = prev;
            } else {
                break;
            }
        }

        (start, end)
    }

    /// Render a single line of the tree.
    ///
    /// Uses \r\n as a line ending since when terminal is in raw mode \n
    /// alone does not move the cursor back to the beginning of the line.
    fn render_line<W: Write>(
        &self,
        writer: &mut W,
        ind: usize,
        focus: bool,
        last: bool,
    ) -> io::Result<()> {
        let line = &self.tree.lines.lines[ind];
        let ending = if last { "" } else { "\r\n" };

        if focus {
            write!(
                writer,
                "{}{}{}{}{}{}{}",
                self.prefix_string(&line.prefix),
                if line.prefix.is_empty() { "" } else { " " },
                Bg(self.opts.bg_color.deref()),
                self.tree.tree[line.node].data.name,
                self.suffix_for_node(line.node),
                Bg(Reset),
                ending,
            )
        } else {
            write!(
                writer,
                "{}{}{}{}{}",
                self.prefix_string(&line.prefix),
                if line.prefix.is_empty() { "" } else { " " },
                self.tree.tree[line.node].data.name,
                self.suffix_for_node(line.node),
                if last { "" } else { "\r\n" }
            )
        }?;

        if last {
            writer.flush()
        } else {
            Ok(())
        }
    }
}

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

    use std::path::PathBuf;

    fn test_dir(dir: &str) -> PathBuf {
        PathBuf::new().join("resources/test").join(dir)
    }

    fn abs_test_dir(dir: &str) -> PathBuf {
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(test_dir(dir))
    }

    fn draw_to_string(dir: &PathBuf) -> String {
        let mut t = Tree::new_from_dir(dir);
        format!("{}", TreeRender::new(&mut t, RenderOptions::new()))
    }

    #[test]
    fn test_draw_abs_path() {
        let dir = abs_test_dir("simple");

        let exp = format!(
            "{}\n{} {}\n{} {}\n\n{}\n",
            dir.display(),
            MID_BRANCH,
            "myfile",
            END_BRANCH,
            "myotherfile",
            "0 directories, 2 files",
        );

        assert_eq!(exp, draw_to_string(&dir));
    }

    #[test]
    fn test_draw_rel_path() {
        let dir = test_dir("simple");

        let exp = format!(
            "{}\n{} {}\n{} {}\n\n{}\n",
            dir.display(),
            MID_BRANCH,
            "myfile",
            END_BRANCH,
            "myotherfile",
            "0 directories, 2 files",
        );

        assert_eq!(exp, draw_to_string(&dir));
    }

    #[test]
    fn test_draw_dir() {
        let dir = test_dir("one_dir");

        let exp = format!(
            "{}\n{} {}\n{}{} {}\n{} {}\n\n{}\n",
            dir.display(),
            MID_BRANCH,
            "mydir",
            BAR_INDENT,
            END_BRANCH,
            "myfile",
            END_BRANCH,
            "myotherfile",
            "1 directory, 2 files",
        );

        assert_eq!(exp, draw_to_string(&dir));
    }

    #[test]
    fn test_draw_link() {
        let dir = test_dir("link");

        let exp = format!(
            "{}\n{} {}\n{} {}\n\n{}\n",
            dir.display(),
            MID_BRANCH,
            "dest -> source",
            END_BRANCH,
            "source",
            "0 directories, 2 files",
        );

        assert_eq!(exp, draw_to_string(&dir));
    }

    #[test]
    fn test_focus() {
        let mut t = Tree::new_from_dir(&test_dir(""));
        assert_eq!("link", t.focused().name);
        t.focus_up();
        assert_eq!("link", t.focused().name);

        t.focus_right();
        assert_eq!("one_dir", t.focused().name);
        t.focus_down();
        assert_eq!("mydir", t.focused().name);

        t.focus_left();
        assert_eq!("mydir", t.focused().name);
        t.focus_right();
        assert_eq!("myotherfile", t.focused().name);

        t.focus_up();
        assert_eq!("one_dir", t.focused().name);
    }

    #[test]
    fn test_fold() {
        let mut t = Tree::new_from_dir(&test_dir(""));
        t.focus_right();
        t.focus_down();
        t.toggle_focus_fold();

        let exp = format!(
            "{}\n{} {}\n{}{} {}\n{}{} {}\n{} {}\n{}{} {}\n{}{} {}\n{} {}\n{}{} {}\n{}{} {}\n\n{}\n",
            "resources/test",
            MID_BRANCH,
            "link",
            BAR_INDENT,
            MID_BRANCH,
            "dest -> source",
            BAR_INDENT,
            END_BRANCH,
            "source",
            MID_BRANCH,
            "one_dir",
            BAR_INDENT,
            MID_BRANCH,
            "mydir*",
            BAR_INDENT,
            END_BRANCH,
            "myotherfile",
            END_BRANCH,
            "simple",
            BLANK_INDENT,
            MID_BRANCH,
            "myfile",
            BLANK_INDENT,
            END_BRANCH,
            "myotherfile",
            "4 directories, 6 files",
        );
        let actual = format!("{}", TreeRender::new(&mut t, RenderOptions::new()));

        assert_eq!(exp, actual);

        t.focus_up();
        t.focus_right();
        t.toggle_focus_fold();

        let actual = format!("{}", TreeRender::new(&mut t, RenderOptions::new()));
        let exp_pre = format!(
            "{}\n{} {}\n{}{} {}\n{}{} {}\n{} {}\n{}{} {}\n{}{} {}\n{} {}\n\n{}\n",
            "resources/test",
            MID_BRANCH,
            "link",
            BAR_INDENT,
            MID_BRANCH,
            "dest -> source",
            BAR_INDENT,
            END_BRANCH,
            "source",
            MID_BRANCH,
            "one_dir",
            BAR_INDENT,
            MID_BRANCH,
            "mydir*",
            BAR_INDENT,
            END_BRANCH,
            "myotherfile",
            END_BRANCH,
            "simple*",
            "4 directories, 6 files",
        );

        assert_eq!(exp_pre, actual);

        t.focus_left();
        t.toggle_focus_fold();

        let actual = format!("{}", TreeRender::new(&mut t, RenderOptions::new()));
        let exp = format!(
            "{}\n{} {}\n{}{} {}\n{}{} {}\n{} {}\n{} {}\n\n{}\n",
            "resources/test",
            MID_BRANCH,
            "link",
            BAR_INDENT,
            MID_BRANCH,
            "dest -> source",
            BAR_INDENT,
            END_BRANCH,
            "source",
            MID_BRANCH,
            "one_dir*",
            END_BRANCH,
            "simple*",
            "4 directories, 6 files",
        );

        assert_eq!(exp, actual);

        t.toggle_focus_fold();

        let actual = format!("{}", TreeRender::new(&mut t, RenderOptions::new()));
        assert_eq!(exp_pre, actual);
    }
}