-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQTreeNodeExample.kt
More file actions
116 lines (89 loc) · 3.62 KB
/
Copy pathQTreeNodeExample.kt
File metadata and controls
116 lines (89 loc) · 3.62 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
/*
* Copyright 2023. nyabkun
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package tree
import nyab.util.QLazyTreeNode
import nyab.util.QSearchAlgo
import nyab.util.QShColor
import nyab.util.QTreeNode
import nyab.util.QTreeStyle
import nyab.util.add
import nyab.util.descendantsList
import nyab.util.fillTree
import nyab.util.tree
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.io.path.isDirectory
import kotlin.io.path.name
import kotlin.streams.asSequence
fun main() {
intTree()
println()
textTree()
println()
fileTree()
}
fun intTree() {
// First, you have to create the root node.
val root = QTreeNode(0)
val node1 = root add 1
val node2 = root add 2
val node3 = node2 add 3
val node4 = node2 add 4
val node5 = node4 add 5
val node6 = node4 add 6
val node7 = node2 add 7
val unicodeTree = root.tree(color = QShColor.Green, style = QTreeStyle.UNICODE)
println(unicodeTree)
val asciiTree = root.tree(color = QShColor.Blue, style = QTreeStyle.ASCII)
println(asciiTree)
println()
val depthFirstResult = root.descendantsList(QSearchAlgo.DepthFirst).toString()
println("DepthFirst : $depthFirstResult") // [0, 1, 2, 3, 4, 5, 6, 7]
val breadthFirstResult = root.descendantsList(QSearchAlgo.BreadthFirst).toString()
println("BreadthFirst : $breadthFirstResult") // [0, 1, 2, 3, 4, 7, 5, 6]
}
fun textTree() {
// node can store anything
val rootA = QTreeNode("A")
val nodeB = rootA add "B"
val nodeC = nodeB add "C"
val nodeD = nodeB add "D"
val nodeE = nodeD add "E"
val nodeF = nodeE add "F"
val nodeG = nodeC add "G"
val textTree = rootA.tree(color = QShColor.Cyan, style = QTreeStyle.UNICODE)
println(textTree)
}
fun fileTree() {
// You can implement QLazyNode for more complicated situations.
class QFileNode(override val value: Path) : QLazyTreeNode<Path> {
override fun hasChildNodesToFill(): Boolean {
return value.isDirectory()
}
override fun fillChildNodes(): List<QFileNode> {
return Files.walk(value, 1).asSequence().filter {
it != value
}.sortedBy {
it.name
}.sortedBy {
!it.isDirectory()
}.map {
QFileNode(it)
}.toList()
}
override fun toTreeNodeString(): String {
return if (value.isDirectory()) "📂 " + value.name else value.name
}
}
val rootDir = Paths.get("src-split").toAbsolutePath()
val fileTree = QFileNode(rootDir).fillTree(maxDepth = 3).tree()
println(fileTree)
}