-
Notifications
You must be signed in to change notification settings - Fork 0
/
Show-ChildItem.ps1
148 lines (137 loc) · 4.26 KB
/
Show-ChildItem.ps1
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
function Show-ChildItem {
[cmdletbinding()]
param(
[switch]$Full
)
$ChildItem = [childitem]::new()
$ChildItem.write($Full)
}
class ChildItem
{
[object]$Host
<#
Recurse by depth?
GitHub?
FileChange?
Rights and Access?
FileCount?
#>
ChildItem() {
$this.Host = Get-Host
}
[object] Write([bool]$full)
{
switch ((Get-Location).Provider.Name) {
'FileSystem' {
return $this.writeFileSystem($full)
}
default {
return (Get-ChildItem -force)
}
}
return 0
}
[string] WriteFileSystem([bool]$full)
{
$Items = Get-ChildItem -force | Sort-Object -property `
@{
Expression = 'PSIsContainer'
Descending = $true
},
@{
Expression = 'Name'
Descending = $false
}
[console]::ForegroundColor = [consolecolor]::Gray
if ($full) {
[console]::write("`r Mode Creation LastWrite`n")
}
else {
[console]::write("`r Mode`n")
}
[console]::resetColor()
for ($i = 0; $i -lt $Items.Count; $i++) {
[console]::ForegroundColor = [consolecolor]::Black
[console]::BackgroundColor = [consolecolor]::DarkGray
[console]::write("$($Items[$i].Mode)-")
#CheckFileAccess
#ToDo
[console]::resetColor()
if ($full) {
[console]::write(" ")
[console]::ForegroundColor = [consolecolor]::Black
[console]::BackgroundColor = [consolecolor]::DarkGray
[console]::write("$($Items[$i].CreationTime.toString('ddMMMyy HH:mm:ss'))")
[console]::resetColor()
[console]::write(" ")
[console]::ForegroundColor = [consolecolor]::Black
[console]::BackgroundColor = [consolecolor]::DarkGray
[console]::write("$($Items[$i].LastWriteTime.toString('ddMMMyy HH:mm:ss'))")
[console]::resetColor()
}
if ($Items[$i].Attributes -match [system.io.fileattributes]::Directory) {
[console]::ForegroundColor = [consolecolor]::Gray
[console]::write(" $($Items[$i].Name)`n")
[console]::resetColor()
}
else {
[void]$this.writeSizeData($this.convertBytes($Items[$i].Length))
[console]::ForegroundColor = $this.getColor($Items[$i].Extension)
[console]::write("$($Items[$i].Name)`n")
[console]::resetColor()
}
}
if ((Get-Location).Path -match 'Microsoft\.PowerShell\.Core\\FileSystem::') {
return " $((Get-Location).Path.split(':')[2])`n"
}
else {
return " $((Get-Location).Path)`n"
}
}
[int] WriteSizeData([string]$string)
{
switch ($string.Length) {
2 { [console]::write(" $($string) ") }
3 { [console]::write(" $($string) ") }
4 { [console]::write(" $($string) ") }
5 { [console]::write(" $($string) ") }
6 { [console]::write(" $($string) ") }
7 { [console]::write(" $($string) ") }
8 { [console]::write("$($string) ") }
default {
[console]::ForegroundColor = [consolecolor]::Red
[console]::write(" SIZE_ERR ")
[console]::resetColor()
}
}
return 0
}
[string] ConvertBytes([int64]$length)
{
switch ($length) {
{$_ -ge 1TB} { return "$([math]::round(($($_) / 1TB), 2))T" }
{$_ -ge 1GB} { return "$([math]::round(($($_) / 1GB), 2))G" }
{$_ -ge 1MB} { return "$([math]::round(($($_) / 1MB), 2))M" }
{$_ -ge 1KB} { return "$([math]::round(($($_) / 1KB), 2))K" }
}
return "$([math]::round($length, 2))B"
}
[consolecolor] GetColor([string]$extension)
{
switch -regex ($extension) {
".(cs|csproj|sln|vbs)" { return [consolecolor]::Green }
".(msi|msu|msp|zip)" { return [consolecolor]::DarkGreen }
".(dll|reg|xml|xaml|cfg)" { return [consolecolor]::DarkCyan }
".(cmd|sh|exe|ini|dat|msc)" { return [consolecolor]::Yellow }
".(pdf|html|hta|docx|doc|md)" { return [consolecolor]::Magenta }
".(ps1|psm1|psd1|pson|ps1xml)" { return [consolecolor]::Cyan }
".(csv|json|log|toml|xlsx|xslt|yml|yaml)" { return [consolecolor]::Blue }
}
return $this.Host.UI.RawUI.ForegroundColor
}
[bool] Dispose()
{
$this.Host = $null
return $true
}
}