-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPSBufferToHtml.ps1
More file actions
143 lines (120 loc) · 4.99 KB
/
PSBufferToHtml.ps1
File metadata and controls
143 lines (120 loc) · 4.99 KB
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
<#
.SYNOPSIS
This is a Powershell script to dump console buffer as html to file.
.DESCRIPTION
This Powershell script will iterate over the current console buffer and
output it as html preserving colors.
.PARAMETER FilePath
Specifies the path to the output file.
.PARAMETER Encoding
Specifies the type of character encoding used in the file. Valid values are "Unicode", "UTF7", "UTF8", "UTF32", "ASCII", "BigEndianUnicode", default is UTF8
.PARAMETER Last
Specifies the rows to output from the end of the buffer
.PARAMETER SkipLast
Skips last buffer row in output
#>
Param(
[Parameter(Mandatory=$true)]
[ValidateScript({ ![String]::IsNullOrWhiteSpace($_) })]
[string] $FilePath,
[ValidateSet("Unicode", "UTF7", "UTF8", "UTF32", "ASCII", "BigEndianUnicode")]
[string] $Encoding = "UTF8",
[int] $Last = 0,
[switch]$SkipLast
)
Function ToHex ([System.ConsoleColor] $color)
{
switch($color)
{
"Black" { "#000000" }
"DarkBlue" { "#012456" }
"DarkGreen" { "#005711" }
"DarkCyan" { "#007680" }
"DarkRed" { "#6F0711" }
"DarkMagenta" { "#6F0780" }
"DarkYellow" { "#888F11" }
"Gray" { "#878E98" }
"DarkGray" { "#666D77" }
"Blue" { "#0000FF" }
"Green" { "#00FF00" }
"Cyan" { "#00FFFF" }
"Red" { "#FF0000" }
"Magenta" { "#FF00FF" }
"Yellow" { "#FFFF00" }
"White" { "#FFFFFF" }
default { throw "Unknown color: $color" }
}
}
if ($host.Name -ne "ConsoleHost")
{
throw "Console host $($host.Name) not supported";
}
$state = @{
Width = $host.ui.rawui.BufferSize.Width
Height = if($SkipLast.IsPresent) { $host.ui.rawui.CursorPosition.Y - 1 } else { $host.ui.rawui.CursorPosition.Y }
Rect = (new-object System.Management.Automation.Host.Rectangle 0,0, ($host.ui.rawui.BufferSize.Width-1), $host.ui.rawui.CursorPosition.Y)
DefaultBackgroundColor = [System.ConsoleColor]::DarkMagenta
DefaultForegroundColor = [System.ConsoleColor]::White
}
$buffer = $host.ui.rawui.GetBufferContents($state.Rect)
$currentForegroundColor = $state.DefaultForegroundColor
$currentBackgroundColor = $state.DefaultBackgroundColor
$outputBuilder = new-object System.Text.StringBuilder
$inSpan = $false;
[void]$outputBuilder.AppendLine("<body>")
[void]$outputBuilder.Append("<pre style=`"display: inline-block;padding:2px;border:2px solid black;font-size:10px;font-family: Consolas, 'Lucida Console', Monaco, monospace;color:$(ToHex([System.ConsoleColor]::White));background-color:$(ToHex([System.ConsoleColor]::DarkBlue))`">")
$firstRow = 0
if($Last -gt 0 -and ($state.Height -$Last) -gt 0)
{
$firstRow = $state.Height - $Last;
}
for ($row=$firstRow; $row -lt $state.Height; $row++)
{
for($col = 0; $col -lt $state.Width; $col++)
{
$cell = $buffer[$row, $col]
if ($currentForegroundColor -ne $cell.ForegroundColor -or $currentBackgroundColor -ne $cell.BackgroundColor)
{
$currentForegroundColor = $cell.ForegroundColor
$currentBackgroundColor = $cell.BackgroundColor
if ($inSpan)
{
[void]$outputBuilder.Append("</span>")
$inSpan = $false
}
if (($currentForegroundColor -ne $state.DefaultForegroundColor -and $currentForegroundColor -ne [System.ConsoleColor]::DarkYellow) -or $currentBackgroundColor -ne $state.DefaultBackgroundColor)
{
[void]$outputBuilder.Append("<span style=`"")
if ($currentForegroundColor -ne $state.DefaultForegroundColor -and $currentForegroundColor -ne [System.ConsoleColor]::DarkYellow)
{
[void]$outputBuilder.Append("color:$(ToHex($currentForegroundColor))")
}
if ($currentBackgroundColor -ne $state.DefaultBackgroundColor)
{
[void]$outputBuilder.Append(";background-color:$(ToHex($currentBackgroundColor))")
}
[void]$outputBuilder.Append("`">")
$inSpan = $true
}
}
switch($cell.Character)
{
"<" { [void]$outputBuilder.Append("<") }
">" { [void]$outputBuilder.Append(">") }
default { [void]$outputBuilder.Append($cell.Character) }
}
}
for ($index = $outputBuilder.Length-1; $outputBuilder.Length -gt 0 -and [String]::IsNullOrWhiteSpace($outputBuilder[$index]); $index--)
{
[void]$outputBuilder.Remove($index, 1);
}
if ($inSpan)
{
[void]$outputBuilder.Append("</span>")
$inSpan = $false;
}
[void]$outputBuilder.AppendLine()
}
[void]$outputBuilder.AppendLine("</pre>")
[void]$outputBuilder.AppendLine("</body>")
$outputBuilder.ToString()|Out-File -FilePath $FilePath -Encoding $Encoding