-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasciitree.js
More file actions
137 lines (110 loc) · 3.7 KB
/
Copy pathasciitree.js
File metadata and controls
137 lines (110 loc) · 3.7 KB
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
/**
* (c) 2014 Anton Medvedev
*
* SELECT_________________
* / \ \
* .___ FROM JOIN
* / \ | / \
* a city_name people address ON
* |
* =___________
* / \
* .____ .
* / \ / \
* p address_id a id
*
*/
export default function drawTree(tree, _getTitle, _getNodes) {
var whitespace = ' ',
output = [];
var getTitle = _getTitle || function (node) {
return (typeof node === 'string') ? node : node[0];
};
var getNodes = _getNodes || function (node) {
return (typeof node === 'string') ? null : node[1];
};
var checkEmpty = function (onLine) {
if (output[onLine] == undefined) {
output[onLine] = '';
}
};
var repeat = function (string, times) {
var output = '';
for (var i = 0; i < times; i++) {
output += string;
}
return output;
};
var findPadding = function (string, onLine, position, _margin) {
checkEmpty(onLine);
var padding = 0,
margin = _margin != undefined ? _margin : 2,
length = output[onLine].length;
if (position < 0) {
padding = -position;
position = 0;
}
if (length >= position) {
padding += length - position + margin;
}
return padding;
};
var insert = function (string, onLine, position) {
checkEmpty(onLine);
var length = output[onLine].length;
if (position < 0) {
throw "Trying to insert \"" + string + "\" at negative position(" + position + ").";
}
if (position < length) {
throw "Trying to insert \"" + string + "\" at position(" + position + ") less then length(" + length + ").";
}
output[onLine] += repeat(whitespace, position - length) + string;
};
var drawNode = function (tree, onLine, position) {
var padding = 0,
foundedPadding = 0,
nodePadding = 0,
node, title,
offset = 0,
currentTitle = getTitle(tree),
nodes = getNodes(tree);
if (nodes) {
var at = position;
if (nodes.length == 1) {
node = nodes[0];
title = getTitle(node);
var halfOfCurrentTitle = Math.floor(currentTitle.length / 2);
offset = Math.floor(title.length / 2) - halfOfCurrentTitle;
foundedPadding = findPadding(title, onLine + 2, position - offset);
nodePadding = drawNode(node, onLine + 2, position - offset + foundedPadding);
insert('|', onLine + 1, position + halfOfCurrentTitle + foundedPadding + nodePadding);
padding = foundedPadding + nodePadding;
} else {
for (var i = 0; i < nodes.length; i++) {
node = nodes[i];
title = getTitle(node);
if (i == 0) {
offset = title.length == 1 ? 2 : Math.floor(title.length / 2) + 1;
foundedPadding = findPadding(title, onLine + 2, position - offset);
nodePadding = drawNode(node, onLine + 2, position - offset + foundedPadding);
insert('/', onLine + 1, position - 1 + foundedPadding + nodePadding);
insert(repeat(whitespace, currentTitle.length), onLine + 1, position + foundedPadding + nodePadding);
padding = foundedPadding + nodePadding;
at += padding + currentTitle.length;
} else {
offset = title.length == 1 ? -1 : Math.floor(title.length / 2) - 1;
foundedPadding = findPadding(title, onLine + 2, at - offset);
nodePadding = drawNode(node, onLine + 2, at - offset + foundedPadding);
at += foundedPadding + nodePadding;
insert('\\', onLine + 1, at);
currentTitle += repeat('_', foundedPadding + nodePadding);
}
}
}
}
insert(currentTitle, onLine, position + padding);
return padding;
};
drawNode(tree, 0, 0);
return output.join("\n");
}