ConsoleLog.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
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
<#
.SYNOPSIS
Transcribes the console log into HTML, plain text, JSON, or object.

.DESCRIPTION
Get-ConsoleLog captures the buffer of the console PowerShell is currently
running and converts it into HTML, plain text, JSON, or object.

.PARAMETER IncludeThisLine
This switch determines whether or not to capture the line the cursor is at
when the command is invoked. By default, this line is not captured since it
is almost always empty when invoked interactively. If we run
    PS C:\> Write-Host 1; Get-ConsoleLog -IncludeThisLine
interactively, the result is
```
PS C:\> Write-Host 1; Get-ConsoleLog
1

```
Note the extra blank line after "1".

.PARAMETER TrailingSpace
Determines how trailing space characters are handled, can be one of
    KeepAll | IgnoreAll | IgnoreDefaultColors
and `IgnoreDefaultColors` is the default.

`KeepAll` simply keeps all space characters, so each line is exactly 80
characters wide if all characters are basic Latin and the console is also
80 characters wide.

`IgnoreAll` simply removes all trailing space characters. Note that some
trailing space characters may have a different background than the other
parts of the line. They are removed regardless of such. Consequently,
the outcome might not accurately reflect the actual on-screen rendering.

`IgnoreDefaultColors` is the default, and it removes all trailing space
characters of the same color as the background of the line. This is what
people usually expect. However, this might come as a surprise when the
format is `Text`, where the default behavior is still `IgnoreDefaultColors`.
This means it is possible that a line contains trailing space.

.PARAMETER Format
Determines the format of the output, can be one of
    Html | Text | Json | Object
and `Html` is the default.

`Html` format also considers CSS incorporation. See also `Css` parameter.
The result is always one single string.

`Text` is the plain text format. See `TrailingSpace` for possible surprise.
By default, each line is sent down the pipeline. See also `Concat` parameter.

`Json` is the JSON serialization of the object result. See also `Compress`.
The result directly comes from `ConvertTo-Json`.

`Object` gives one single `ConsoleLog` object.

.PARAMETER Css
Determines whether and how CSS is incorporated, can be one of
    Embed | Link | None
and `None` is the default. It is ignored if `Format` is not `Html`.

.PARAMETER Concat
This switch determines whether the plain text capture is returned as one
single string. It is ignored if `Format` is not `Text`.

.PARAMETER Compress
This switch determines whether the JSON output is minified. It is ignored
if `Format` is not `Json`.

.LINK
https://github.com/GeeLaw/PowerShellThingies/blob/master/modules/ConsoleLog/README.md#get-consolelog

#>

Function Get-ConsoleLog
{
    [CmdletBinding(HelpURI='https://github.com/GeeLaw/PowerShellThingies/blob/master/modules/ConsoleLog/README.md#get-consolelog', PositionalBinding=$False)]
    Param
    (
        [switch]$IncludeThisLine,
        [ValidateSet('KeepAll', 'IgnoreAll', 'IgnoreDefaultColors')]
        [string]$TrailingSpace = 'IgnoreDefaultColors',
        [ValidateSet('Html', 'Text', 'Json', 'Object')]
        [string]$Format = 'Html',
        [ValidateSet('Embed', 'Link', 'None')]
        [string]$Css = 'None',
        [switch]$Concat,
        [switch]$Compress
    )
    Process
    {
        If ($Format -eq 'Html')
        {
            $local:html = '';
            If ($Css -eq 'Embed')
            {
                $html = '<style type="text/css">' + [GeeLaw.ConsoleCapture.Helper]::StylesContent + "</style>`n";
            }
            ElseIf ($Css -eq 'Link')
            {
                $html = '<link href="file:///' + [GeeLaw.ConsoleCapture.Helper]::StylesPath.Replace('\', '/').Replace('&', '&amp;').Replace("'", '&#39;') + "`" rel=`"stylesheet`" />`n";
            }
            $html += [GeeLaw.ConsoleCapture.HtmlCapturer]::Capture($Host, $IncludeThisLine, $TrailingSpace);
            Return $html;
        }
        If ($Format -eq 'Text')
        {
            $local:text = [GeeLaw.ConsoleCapture.TextCapturer]::Capture($Host,
                $IncludeThisLine, $TrailingSpace);
            If ($Concat) { $text -join "`n" }
            Else { $text | Write-Output }
        }
        Else
        {
            $local:obj = [GeeLaw.ConsoleCapture.Capturer]::Capture($Host, $IncludeThisLine, $TrailingSpace);
            If ($Format -eq 'Object')
            {
                Return $obj;
            }
            ConvertTo-Json -InputObject $obj -Depth 9 -Compress:$Compress;
        }
    }
}

<#
.SYNOPSIS
Transcribes the console log into HTML and displays it.

.DESCRIPTION
Show-ConsoleLog captures the buffer of the console PowerShell is currently
running, converts it into HTML, and displays it in the default browser.

.PARAMETER IncludeThisLine
This switch determines whether or not to capture the line the cursor is at
when the command is invoked. By default, this line is not captured since it
is almost always empty when invoked interactively. If we run
    PS C:\> Write-Host 1; Get-ConsoleLog -IncludeThisLine
interactively, the result is
```
PS C:\> Write-Host 1; Get-ConsoleLog
1

```
Note the extra blank line after "1".

.PARAMETER TrailingSpace
Determines how trailing space characters are handled, can be one of
    KeepAll | IgnoreAll | IgnoreDefaultColors
and `IgnoreDefaultColors` is the default.

`KeepAll` simply keeps all space characters, so each line is exactly 80
characters wide if all characters are basic Latin and the console is also
80 characters wide.

`IgnoreAll` simply removes all trailing space characters. Note that some
trailing space characters may have a different background than the other
parts of the line. They are removed regardless of such. Consequently,
the outcome might not accurately reflect the actual on-screen rendering.

`IgnoreDefaultColors` is the default, and it removes all trailing space
characters of the same color as the background of the line. This is what
people usually expect. However, this might come as a surprise when the
format is `Text`, where the default behavior is still `IgnoreDefaultColors`.
This means it is possible that a line contains trailing space.

.PARAMETER Interactive
This switch determines whether the result should be interactive. By default,
the output tries to recover the exact rendering of the console history, which
is like a vector-graphical screenshot and is non-interactive. If this switch
is on, an alternative view arranging the captured `ConsoleLog` object as a
tree is used.

.LINK
https://github.com/GeeLaw/PowerShellThingies/blob/master/modules/ConsoleLog/README.md#show-consolelog

#>

Function Show-ConsoleLog
{
    [CmdletBinding(HelpURI='https://github.com/GeeLaw/PowerShellThingies/blob/master/modules/ConsoleLog/README.md#show-consolelog')]
    Param
    (
        [switch]$IncludeThisLine,
        [ValidateSet('KeepAll', 'IgnoreAll', 'IgnoreDefaultColors')]
        [string]$TrailingSpace = 'IgnoreDefaultColors',
        [switch]$Interactive
    )
    Process
    {
        $local:tempFile = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), [System.Guid]::NewGuid().ToString('n') + '.html');
        $local:html = '';
        If (-not $Interactive)
        {
            $html = "<!doctype html>`n<html><meta charset=`"utf-8`" />`n<meta name=`"viewport`" content=`"width=device-width, initial-scale=1`" />`n<title>Console Log (Show-ConsoleLog)</title>`n" + (Get-ConsoleLog -IncludeThisLine:$IncludeThisLine -TrailingSpace $TrailingSpace -Format 'Html' -Css 'Link') + "</html>`n";
        }
        Else
        {
            $html = [GeeLaw.ConsoleCapture.Helper]::GetInteractiveHtml(
                $Host, $IncludeThisLine, $TrailingSpace);
        }
        [System.IO.File]::WriteAllText($tempFile, $html);
        Invoke-Item $tempFile;
    }
}