-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsc.psm1
94 lines (89 loc) · 3.11 KB
/
lsc.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
82
83
84
85
86
87
88
89
90
91
92
93
94
function getColor ($item) {
if (isDirectory($item)) {
if (isHidden($item)) {
return "Gray"
}
return "DarkGreen"
}
if (isHidden($item)) {
return "DarkGray"
}
return "DarkBlue"
}
function isHidden ($item) {
return $item.Attributes.HasFlag([System.IO.FileAttributes]::Hidden)
}
function isDirectory ($item) {
return $item.Attributes.HasFlag([System.IO.FileAttributes]::Directory)
}
function isFile ($item) {
return -not (isDirectory($item))
}
function displayLength($ch) {
if ([int][char]$ch -ge 128) {
return 2
}
return 1
}
function printNameFormat ($item, $style) {
$maxNameLength = $style.maxSpacePerName - 1
$nameLength = $item.Name.Length
$restrictedIdx = 0
$restrictedLength = 0
$currentLength = 0
$restrictedShouldStop = $false
for ($idx=0;$idx -lt $item.Name.Length; $idx++) {
$chDisplayLength = displayLength($item.Name[$idx])
if (!$restrictedShouldStop) {
if (($restrictedLength+$chDisplayLength) -le ($maxNameLength-3)) {
$restrictedLength += $chDisplayLength
$restrictedIdx=$idx
}
else {$restrictedShouldStop = $true}
}
$currentLength += $chDisplayLength
if ($currentLength -gt $maxNameLength) {
return "{0,-$($restrictedIdx+1+$style.maxSpacePerName-$restrictedLength)}" -f `
($item.Name.SubString(0, $restrictedIdx+1) + "... ")
}
}
return "{0,-$($item.Name.Length+$style.maxSpacePerName-$currentLength)}" -f $item.Name
}
function printNames ($items, $style) {
$it = 0
foreach ($item in $items) {
$name = printNameFormat $item $style
Write-Host "$($name)" -ForegroundColor $(getColor $item) -NoNewLine
$it += 1
if (($it % $style.nNamePerRow) -eq 0) {Write-Host ""}
}
if (($it % $style.nNamePerRow) -ne 0) {Write-Host ""}
}
$printStyles = @{
"large"=@{"nNamePerRow"=8;"maxSpacePerName"=20};
"tightLarge"=@{"nNamePerRow"=8;"maxSpacePerName"=15};
"normal"=@{"nNamePerRow"=4;"maxSpacePerName"=20};
"wideNormal"=@{"nNamePerRow"=4;"maxSpacePerName"=40};
}
function lsc {
Param (
[string]$path = ".",
$style = $printStyles.normal,
[switch]$Force=$false
)
$items = if ($Force) {Get-ChildItem -Path $Path -Force} else {Get-ChildItem -Path $Path}
$hiddenDirectories = `
($items | foreach {if((isDirectory($_)) -and (isHidden($_))){,@($_)}else{}})
$normalDirectories = `
($items | foreach {if((isDirectory($_))-and -not (isHidden($_))){,@($_)}else{}})
$hiddenFiles = `
($items | foreach {if((isFile($_)) -and (isHidden($_))){,@($_)}else{}})
$normalfiles = `
($items | foreach {if((isFile($_)) -and -not (isHidden($_))){,@($_)}else{}})
printNames $hiddenDirectories $style
printNames $hiddenFiles $style
printNames $normalDirectories $style
printNames $normalfiles $style
}
Export-ModuleMember -Function "lsc"
Export-ModuleMember -Variable "printStyles"