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
//! Converts a [`flat tree`](http://docs.rs/flat-tree) to a string.
//!
//! ## Usage
//! ```rust,ignore
//! extern crate print_flat_tree;
//!
//! use print_flat_tree::fmt;
//!
//! let tree = vec!(0, 1, 2, 3, 7, 8, 9, 10);
//! print!("{}", fmt(tree));
//! ```
//!
//! Which outputs:
//!
//! ```text
//!  0──┐
//!     1──┐
//!  2──┘  │
//!        3──┐
//!           │
//!           │
//!           │
//!           7
//!  8──┐
//!     9
//! 10──┘
//! ```
//!
//! As can be seen from the above diagram `7` is the parent of `3`, and `3` is
//! the parent of `1` etc.
//!
//! ## See Also
//! - [mafintosh/print-flat-tree (JavaScript)](https://github.com/mafintosh/print-flat-tree)
//! - [flat-tree](https://docs.rs/flat-tree)

#![deny(warnings)]
#![cfg_attr(test, feature(plugin))]
#![cfg_attr(test, plugin(clippy))]

extern crate flat_tree;

use std::cmp;

const DOWN: char = '│';
const LEFT: char = '─';
const TURN_DOWN: char = '┐';
const TURN_UP: char = '┘';

/// Converts a `flat_tree` to a string.
pub fn fmt(tree: &Vec<usize>) -> String {
  // Fill a vec with bools, indicating if a value exists or not.
  let max = tree.iter().fold(0, |prev, curr| cmp::max(prev, *curr));
  let mut list = vec![false; max + 1];
  for &i in tree {
    list[i] = true;
  }

  let width = (list.len().to_string()).len() + 1;
  let last_block = list.len() - list.len() % 2;
  let _roots = flat_tree::full_roots(last_block as u64);

  let blank = format!("{:width$}", ' ', width = width);
  let mut matrix = vec![vec![blank.to_string(); max as usize]; list.len()];

  for i in 0..list.len() {
    if list[i] == false {
      continue;
    }
    let depth = flat_tree::depth(i as u64);
    let val = format!("{:width$}", i, width = width);
    matrix[i as usize][depth as usize] = val;

    if let Some(children) = flat_tree::children(i as u64) {
      add_path(&list, &mut matrix, children.0, i as u64, depth, 1);
      if (children.1 as usize) < list.len() {
        add_path(&list, &mut matrix, children.1, i as u64, depth, -1);
      }
    }
  }

  let mut flat_tree_str = String::from("");
  for arr in matrix {
    let partial = arr.join("") + "\n";
    flat_tree_str.push_str(partial.as_str());
  }

  flat_tree_str
}

fn add_path(
  list: &[bool],
  matrix: &mut Vec<Vec<String>>,
  child: u64,
  parent: u64,
  parent_depth: u64,
  dir: i32,
) -> () {
  if list[child as usize] == false {
    return;
  }

  let depth = flat_tree::depth(child);
  let width = (list.len().to_string()).len() + 1;
  let ptr = depth + 1;

  for i in ptr..parent_depth {
    matrix[child as usize][i as usize] = pad(LEFT, LEFT, width);
  }

  let turn_char = if dir < 0 { TURN_UP } else { TURN_DOWN };
  matrix[child as usize][ptr as usize] = pad(turn_char, LEFT, width);

  let mut _child: i32 = child as i32;
  loop {
    _child += dir;
    if _child == parent as i32 {
      break;
    };
    matrix[_child as usize][ptr as usize] = pad(DOWN, ' ', width);
  }
}

fn pad(str: char, pad_char: char, width: usize) -> String {
  let mut symbol = String::from("");
  for i in 0..width {
    if i == width - 1 {
      symbol.push(str);
    } else {
      symbol.push(pad_char);
    }
  }
  symbol
}