Skip to content

Instantly share code, notes, and snippets.

@la11111
Created October 20, 2012 05:01
Show Gist options
  • Save la11111/3922073 to your computer and use it in GitHub Desktop.
Save la11111/3922073 to your computer and use it in GitHub Desktop.
Write-HTML - Like Write-Host, but cooler
<html>
<head>
<title>my script output</title>
</head>
<body style="color:white; background:black">
<pre>
########################
# #
# my script output #
# #
########################
<span style="color:#FF0000">my line of output</span>
<span style="color:#00FF00"> my line John Stamos &amp; some stuff</span>
part 1 of line: <span style="color:#000000; background:#FFFFFF">part 2 of line in negative!</span>
</pre>
</body>
</html>
####
# EXAMPLE USAGE: somescript.ps1
###
$WriteHTMLDualOutput = $true
write-html -head -title "my script output"
write-html -fore red "my line of output"
write-html -fore green "`tmy line $var & some stuff"
write-html -nonewline "part 1 of line: "
write-html -fore black -back white "part 2 of line in negative!"
write-html -foot
<# in terminal: (must use imagination)
PS > .\somescript.ps1
########################
# #
# my script output #
# #
########################
my line of output
my line John Stamos & some stuff
part 1 of line: part 2 of line in negative!
PS > $WriteHTMLOutput > outfile.html
PS >
#>
# write-html
<#
Works just like write-host, except returns a line of html.
Intended so that you can output a script as HTML in the same format
that it would appear in a terminal without changing a bunch of stuff
in the script itself.
basically wraps text in a big <pre> block with some colorful frills.
Output is placed in the global variable $WriteHTMLOutput .
intended usage:
### somescript.ps1
write-html -head -title "my script output"
write-html -fore red "my line of output"
write-html -fore green "my line " + $var + " and some stuff"
write-html -nonewline "part 1 of line: "
write-html -fore black -back white "part 2 of line in negative!"
write-html -foot
###
> somescript.ps1; $WriteHTMLOutput > myhtmlfile.html
> firefox -new-tab .\myhtmlfile.html
#### other usefulness:
include this variable in your script to turn off/on
regular write-host style output :
$WriteHTMLToHost = $true (setting to false turns it off)
or:
$WriteHTMLDualOutput = $true
to write-host and write-HTML simultaneously.
caveat: can't do '"' $foo '"' ; have to do "$foo" or "$($foo.member)"
-head : write opening html doc stuff including title, body, etc.
required. in this context:
-backgroundcolor - sets default for document
-fore .. - sets default for document
-title - <title> tag
-tail - literally just "</pre></body></html>"
needed, however, or else your output will have too many newline's:
(-tail does '-join ""' on the output, so please add this at the bottom.)
otherwise: -fore/back/nonewline do what you'd think
title is ignored. -head or foot just override.
To override default HTML colors, create a variable in your profile.ps1 (or
declare one in your session) named $global:WriteHTMLColors , and copy the
hash from this file to that variable, with the desired modifications.
#>
param($Output,[string]$ForegroundColor = "",[string]$BackgroundColor = "",[switch]$NoNewline,[switch]$Header,[string]$Title = "",[switch]$Footer);
if ($global:WriteHTMLColors) {
$colors = $global:WriteHTMLColors
} else {
$colors = @{
"Black" = "#000000";
"DarkBlue" = "#00008B";
"DarkGreen" = "#006400";
"DarkCyan" = "#008B8B";
"DarkMagenta" = "#8B008B";
"DarkYellow" = "#BDB76B"; #html "DarkKhaki"
"DarkGray" = "#808080"; #html "Gray"
"Gray" = "#A9A9A9"; #html "DarkGray" ... wtf?
"Blue" = "#0000FF";
"Green" = "#00FF00"; #html "Lime"
"Cyan" = "#00FFFF";
"Red" = "#FF0000";
"Magenta" = "#FF00FF";
"Yellow" = "#FFFF00";
"White" = "#FFFFFF";
}
}
Function htmlify {
param($s)
$s = $s -replace "&", "&amp;"
$s = $s -replace ">", "&gt;"
$s = $s -replace "<", "&lt;"
$s = $s -replace """", "&quot;"
return $s
}
Function Make-StyleString {
param($color,$background,[switch]$ForceDefaults)
if (!($color -or $background)) {
if ($ForceDefaults) {
return " style=""color:$($colors.get_item('White')); background:$($colors.get_item('black'))"""
} else {
return ""
}
} else {
$style = ' style="'
if ($color) {
if ($colors.ContainsKey($color)) {
$style += "color:$($colors.get_item($color))"
} elseif ($color -match "^[a-fA-F0-9]{6}$") {
$style += "color:#$color"
} else {
write-host "skipping unknown foregroundcolor: $color"
$style += "color:white"
}
} else {
if ($ForceDefaults) {
$style += "color:white" #default foreground
}
}
if ($color -and $background) {
$style += "; "
}
if ($background) {
if ($colors.ContainsKey($background)) {
$style += "background:$($colors.get_item($background))"
} elseif ($background -match "^[a-fA-F0-9]{6}$") {
$style += "background:#$background"
} else {
write-host "skipping unknown backgroundcolor: $background"
$style += "background:black"
}
} else {
if ($ForceDefaults) {
$style += "; background:black" #default background
}
}
$style += '"'
}
return $style
}
### main :
if ($WriteHTMLToHost -or $WriteHTMLDualOutput) {
$OutString = "Write-host "
if ($Header) {
if ($Title) {
$t = $Title
} else {
$t = "Write-HTML Output"
}
$box = "##"
$blank = ""
foreach ($i in 1..$($t.length + 6)) {
$box += "#"
$blank += " "
}
$OutString += """$box`n#$blank#`n# $t #`n#$blank#`n$box"""
} elseif ($Footer) {
#pass
} else {
if ($NoNewline) {
$OutString += "-nonewline "
}
if (($ForegroundColor) -and ($colors.ContainsKey($ForegroundColor))) {
$OutString += "-ForegroundColor $ForegroundColor "
}
if (($BackgroundColor) -and ($colors.ContainsKey($BackgroundColor))) {
$OutString += "-BackgroundColor $BackgroundColor "
}
$OutString += """$Output"""
}
Invoke-Expression $OutString
}
if (!($WriteHTMLToHost) -or $WriteHTMLDualOutput) {
if ($Header -and !($Footer)) {
$global:WriteHTMLOutput = @()
$style = Make-StyleString $ForegroundColor $BackgroundColor -ForceDefaults
if ($Title -eq "") {
$t = "Write-HTML output"
} else {
$t = htmlify($Title)
}
$global:WriteHTMLOutput += "<html>`n<head>`n<title>$t</title>`n</head>`n<body$($style)>`n<pre>`n"
$box = "##"
$blank = ""
foreach ($i in 1..$($t.length + 6)) {
$box += "#"
$blank += " "
}
$global:WriteHTMLOutput += "$box`n#$blank#`n# $t #`n#$blank#`n$box`n"
} elseif ($Footer -and !($Header)) {
$global:WriteHTMLOutput += "</pre>`n</body>`n</html>"
$global:WriteHTMLOutput = $global:WriteHTMLOutput -join ""
} elseif ($Header -and $Footer) {
$global:WriteHTMLOutput = @()
$style = Make-StyleString $ForegroundColor $BackgroundColor -ForceDefaults
if ($Title -eq "") {
$t = "Write-HTML output"
} else {
$t = htmlify($Title)
}
$global:WriteHTMLOutput += "<html>`n<head>`n<title>$t</title>`n</head>`n<body$($style)>`n<pre>`n"
$box = "##"
$blank = ""
foreach ($i in 1..$($t.length + 6)) {
$box += "#"
$blank += " "
}
$global:WriteHTMLOutput += "$box`n#$blank#`n# $t #`n#$blank#`n$box`n"
$global:WriteHTMLOutput += "`n</pre>`n</body></html>"
$global:WriteHTMLOutput = $global:WriteHTMLOutput -join ""
} else {
$o = htmlify $Output
$style = Make-StyleString $ForegroundColor $BackgroundColor
if ($NoNewline) {
$nl = ""
} else {
$nl = "`n"
}
if ($o) {
if ($style) {
$global:WriteHTMLOutput += "<span$($style)>$o</span>$nl"
} else {
$global:WriteHTMLOutput += "$o$nl"
}
} else {
$global:WriteHTMLOutput += "$nl"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment