-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreePlus.psm1
More file actions
269 lines (212 loc) · 7.44 KB
/
TreePlus.psm1
File metadata and controls
269 lines (212 loc) · 7.44 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
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
function Show-Tree {
<#
.SYNOPSIS
Displays a tree-like structure of folders and optionally files.
.EXAMPLE
Show-Tree -Path "C:\Projects" -ShowFiles -MarkdownOutput -OutputFile "tree.md"
Generates a markdown file with the directory tree for the C:\Projects folder including files.
.DESCRIPTION
Recursively lists folder structures using ASCII-style connectors, with optional emojis, file filters,
extension-based color themes, and support for clipboard or Markdown output.
.PARAMETER Path
The root path to start displaying the tree.
.PARAMETER Depth
Maximum tree depth to display. Default is unlimited.
.PARAMETER ShowFiles
Include files in the output tree.
.PARAMETER IncludeExtensions
Only include files with these extensions (e.g. ".ps1", ".txt").
.PARAMETER ExcludeFolders
Folder names to exclude from output.
.PARAMETER IncludeHidden
Include hidden files and folders.
.PARAMETER ToClipboard
Copy tree output to clipboard.
.PARAMETER DarkTheme
Use colors optimized for dark terminals.
.PARAMETER OutputFile
Path to save the output (plain or markdown depending on -MarkdownOutput).
.PARAMETER PlainAscii
Disable emojis and display tree using plain ASCII.
.PARAMETER MarkdownOutput
Format the output as a markdown list (suitable for GitHub).
.PARAMETER ShowFileSizes
Display file sizes next to file names (except in Markdown mode).
.PARAMETER Favorite
Name of a preset configuration with commonly used parameter combinations.
#>
[CmdletBinding()]
[Alias("tree", "lstree")]
param (
[Parameter(Position = 0)]
[string]$Path = ".",
[int]$Depth = [int]::MaxValue,
[switch]$ShowFiles,
[string[]]$IncludeExtensions = @(),
[string[]]$ExcludeFolders = @(),
[switch]$IncludeHidden,
[switch]$ToClipboard,
[switch]$DarkTheme,
[string]$OutputFile,
[switch]$PlainAscii,
[switch]$MarkdownOutput,
[switch]$ShowFileSizes,
[string]$Favorite
)
# Favorite profiles
$Favorites = @{
"markdown-dev" = @{
ShowFiles = $true
MarkdownOutput = $true
ShowFileSizes = $true
Depth = 5
OutputFile = "tree.md"
}
"clipboard-dark" = @{
ShowFiles = $true
ToClipboard = $true
DarkTheme = $true
PlainAscii = $true
}
}
if ($Favorite) {
if ($Favorites.ContainsKey($Favorite)) {
$preset = $Favorites[$Favorite]
foreach ($key in $preset.Keys) {
if (-not $PSBoundParameters.ContainsKey($key)) {
Set-Variable -Name $key -Value $preset[$key] -Scope Local
}
}
}
else {
Write-Warning "Unknown favorite '$Favorite'. Available: $($Favorites.Keys -join ', ')"
}
}
if (-not (Test-Path $Path)) {
Write-Error "The specified path '$Path' does not exist."
return
}
$global:OutputLines = @()
$useEmojis = -not $PlainAscii -and -not $MarkdownOutput
$emojiFolder = "`u{1F4C1}" # 📁
$emojiFile = "`u{1F4C4}" # 📄
$folderColor = if ($DarkTheme) { "Yellow" } else { "DarkYellow" }
$defaultFileColor = if ($DarkTheme) { "Cyan" } else { "DarkCyan" }
$fileColorMap = @{
'.ps1' = 'Green'
'.txt' = 'Gray'
'.json' = 'Magenta'
'.csv' = 'White'
'.log' = 'DarkGray'
'.xml' = 'DarkCyan'
'.md' = 'Cyan'
}
if (-not $MarkdownOutput -and -not $PlainAscii) {
Write-Host "Legend: " -NoNewline
Write-Host " Folders" -ForegroundColor $folderColor -NoNewline
Write-Host ", " -NoNewline
Write-Host " Files" -ForegroundColor $defaultFileColor
}
function Get-Tree {
param (
[string]$BasePath,
[string]$Indent = "",
[int]$Level = 0
)
if ($Level -ge $Depth) { return }
Write-Progress -Activity "Building Tree" -Status "Scanning $BasePath" -PercentComplete (($Level / $Depth) * 100)
$items = Get-ChildItem -Path $BasePath -Force | Sort-Object -Property PSIsContainer, Name
$count = $items.Count
for ($i = 0; $i -lt $count; $i++) {
$item = $items[$i]
$isLast = ($i -eq $count - 1)
if (-not $IncludeHidden -and $item.Attributes -match "Hidden") {
continue
}
$connector = if ($MarkdownOutput) {
"- "
}
elseif ($isLast) {
"+--"
}
else {
"|--"
}
$newIndent = if ($MarkdownOutput) {
$Indent + " "
}
elseif ($isLast) {
$Indent + " "
}
else {
$Indent + "| "
}
if ($item.PSIsContainer) {
if ($ExcludeFolders -contains $item.Name) { continue }
$emoji = if ($useEmojis) { "$emojiFolder " } else { "" }
$line = "$Indent$connector$emoji$($item.Name)"
$global:OutputLines += $line
if (-not $MarkdownOutput) {
Write-Host $line -ForegroundColor $folderColor
}
Get-Tree -BasePath $item.FullName -Indent $newIndent -Level ($Level + 1)
}
elseif ($ShowFiles) {
if ($IncludeExtensions.Count -gt 0 -and ($IncludeExtensions -notcontains $item.Extension)) {
continue
}
$emoji = if ($useEmojis) { "$emojiFile " } else { "" }
$size = [math]::Round($item.Length / 1024, 1)
$ext = $item.Extension.ToLower()
$color = $fileColorMap[$ext]
if (-not $color) { $color = $defaultFileColor }
$sizeText = if ($ShowFileSizes -and -not $MarkdownOutput) {
" [$size KB]"
}
else {
""
}
$line = "$Indent$connector$emoji$($item.Name)$sizeText"
$global:OutputLines += $line
if (-not $MarkdownOutput) {
Write-Host $line -ForegroundColor $color
}
}
}
}
$global:OutputLines = @()
if ($MarkdownOutput) {
$header = "# Directory Tree: $Path"
$global:OutputLines += $header
}
else {
Write-Host $Path -ForegroundColor Green
}
Get-Tree -BasePath $Path
if ($OutputFile) {
try {
$outPath = $OutputFile
if ($MarkdownOutput -and (-not $OutputFile.EndsWith(".md"))) {
$outPath = [IO.Path]::ChangeExtension($OutputFile, "md")
}
$global:OutputLines | Out-File -FilePath $outPath -Encoding utf8
Write-Host "`nTree exported to '$outPath'" -ForegroundColor Green
if (Test-Path $outPath) {
Start-Process notepad.exe $outPath
}
}
catch {
Write-Error "Failed to write output file: $_"
}
}
if ($ToClipboard) {
try {
$global:OutputLines -join "`r`n" | Set-Clipboard
Write-Host "Tree copied to clipboard" -ForegroundColor Green
}
catch {
Write-Error "Failed to copy to clipboard: $_"
}
}
}
Export-ModuleMember -Function Show-Tree