Renderers/FileRenderer.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

function Write-FileHeader
{
    param ([Parameter(Mandatory=$True,Position=1)] $item)

    if($item -is [System.IO.DirectoryInfo])
    {
        $itemPath = $item.Parent.ToString()
        
        # GetRootedPath will cause an exception if $itemPath is a root directory
        try
        {
            # If running on .net framework, the above statement returns a relative path
            # remove the item name from the full name to get the full parent path
            if ([System.IO.Path]::GetPathRoot($itemPath) -eq "" )
            {
                $replaceName = $item.Name;
                $replaceName = "\" + $replaceName

                $itemPath = $item.FullName.Replace($replaceName, "" )
            }
        }   
        catch
        {
            $replaceName = $item.Name;

            $itemPath = $item.FullName.Replace($replaceName, "" )
        }
    }
    else
    {
        $itemPath = $item.Directory.ToString()
    }

    if (($script:currentDirectory -eq "") -or ($script:currentDirectory -ne $itemPath))
    {
        Write-Host
        $script:currentDirectory = $itemPath

        # Do we have a foregorund color configured?
        if ($global:PoshColor.DirectoryForeground)
        {
            Write-Host "Directory: " -NoNewline

            # Use console colors?
            if ($global:PoshColor.UseConsoleColors)
            {
                Write-Host $script:currentDirectory -ForegroundColor $global:PoshColor.DirectoryForeground
            }
            else {
                Write-HostColor $script:currentDirectory $global:PoshColor.DirectoryForeground
            }
        }
        else {
            Write-Host "Directory: $script:currentDirectory"
        }
        Write-Host
        Write-Host "Mode LastWriteTime Length Name"
        Write-Host "---- ------------- ------ ----"
    }
}

function Write-File
{
    param ([Parameter(Mandatory=$True,Position=1)] $item)

    Write-FileHeader $item

    $isDirectory = $item.GetType() -eq [System.IO.DirectoryInfo]

    $isHidden = $item.Attributes.HasFlag([System.IO.FileAttributes]::Hidden)
    $isSystem = $item.Attributes.HasFlag([System.IO.FileAttributes]::System)
    $isEncrypted = $item.Attributes.HasFlag([System.IO.FileAttributes]::Encrypted)
    $isCompressed = $item.Attributes.HasFlag([System.IO.FileAttributes]::Compressed)

    $outputColor = "White"

    $length = ""

    if ($isDirectory -eq $false)
    {
        $length = $item.Length
    }

    foreach($filterKey in $global:PoshColor.File.Keys)
    {
        $match = $true;

        $filter = $global:PoshColor.File.Item($filterKey)

        if (($filter.Directory -eq $true) -and $isDirectory -eq $false)
        {
            $match = $false
        }

        if (($filter.Hidden -eq $true) -and $isHidden -eq $false)
        {
            $match = $false
        }

        if ($filter.Compressed -eq $true -and $isCompressed -eq $false)
        {
            $match = $false
        }

        if ($filter.Encrypted -eq $true -and $isEncrypted -eq $false)
        {
            $match = $false
        }

        if ($filter.System -eq $true -and $isSystem -eq $false)
        {
            $match = $false
        }

        if ($match -and $filter.Pattern)
        {
            $regex_opts = ([System.Text.RegularExpressions.RegexOptions]::IgnoreCase)

            $regex = New-Object System.Text.RegularExpressions.Regex($filter.Pattern, $regex_opts)

            if ($regex.IsMatch($item.Name) -eq $false)
            {
                $match = $false
            }
        }

        if ($match -eq $true)
        {
            $outputColor = $filter.Color

            $backgroundColor = $filter.BackgroundColor

            break
        }
    }

    $lastWriteDate = $item.LastWriteTime.ToString("d")
    $lastWriteTime = $item.LastWriteTime.ToString("t")
    $lastWrite = [String]::Format("{0,10} {1,8}", $lastWriteDate, $lastWriteTime)

    $outputText = [System.String]::Format("{0,-7} {1,25} {2,10} {3}", $item.mode, $lastWrite, $length, $item.name)

    Write-HostColor $outputText $outputColor -Background $backgroundColor

    return $true
}