This repository has been archived by the owner on Nov 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
powerls.psm1
81 lines (64 loc) · 2.21 KB
/
powerls.psm1
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
<#
.Synopsis
Powershell unix-like ls
Written by Jesse Jurman (JRJurman)
.Description
A colorful ls
.Parameter Redirect
The first month to display.
.Example
# List the current directory
PowerLS
.Example
# List the parent directory
PowerLS ../
#>
function PowerLS {
param(
[string]$redirect = "."
)
write-host "" # add newline at top
# get the console buffersize
$buffer = Get-Host
$bufferwidth = $buffer.ui.rawui.buffersize.width
# get all the files and folders
$childs = Get-ChildItem $redirect
# get the longest string and get the length
$lnStr = $childs | select-object Name | sort-object { "$_".length } -descending | select-object -first 1
$len = $lnStr.name.length
# keep track of how long our line is so far
$count = 0
# extra space to give some breather space
$breather = 4
# for every element, print the line
foreach ($e in $childs) {
$newName = $e.name + (" "*($len - $e.name.length+$breather))
$count += $newName.length
# determine color we should be printing
# Blue for folders, Green for files, and Gray for hidden files
if (($newName -match "^\..*$") -and (Test-Path ($redirect + "\" + $e) -pathtype container)) { #hidden folders
$newName = $e.name + "\" + (" "*($len - $e.name.length+$breather - 1))
write-host $newName -nonewline -foregroundcolor darkcyan
}
elseif (Test-Path ($redirect + "\" + $e) -pathtype container) { #normal folders
$newName = $e.name + "\" + (" "*($len - $e.name.length+$breather - 1))
write-host $newName -nonewline -foregroundcolor cyan
}
elseif ($newName -match "^\..*$") { #hidden files
write-host $newName -nonewline -foregroundcolor darkgray
}
elseif ($newName -match "\.[^\.]*") { #normal files
write-host $newName -nonewline -foregroundcolor darkyellow
}
else { #others...
write-host $newName -nonewline -foregroundcolor gray
}
if ( $count -ge ($bufferwidth - ($len+$breather)) ) {
write-host ""
$count = 0
}
}
write-host "" # add newline at bottom
write-host "" # add newline at bottom
}
export-modulemember -function PowerLS