Mittwoch, 4. November 2009

Reload-ISE - part II

I have extended my function, so that it now handles all Code Page numbers, like OEM 850, OEM 437 or whatever is listed in http://msdn.microsoft.com/en-us/library/system.text.encodinginfo.aspx. I even tried to make the header more standard.

This file is written in a special way, which I call ISEpackCX.
It can use ISEpack's Add-IseMenu to extend the Add-ons menu. If you wish, you can do without this and take care of the menu by your own.

Add the following lines to your profile

import-module isepack            
#. <your path>\Reload-ISE.ps1 -ISEpackCX


and save the following as Reload-ISE.ps1.

Happy ISE-extending

Bernd


          
[cmdletbinding()]
param(
# use this switch, when you have ISEpack installed and want a default menu added
# import-module isepack
# . -ISEpackCX
[switch]$ISEpackCX
)


function Reload-ISE ()
{
<# .Synopsis Reload file asuming given encoding .Description Reload the file in the current editor asuming given encoding, the file is not saved automatically. You can try other encondings on the unmodified file. .Example Reload-ISE utf8 Reloads the file asuming utf8 encoding. This is useful when the file has no BOM .Example Reload-ISE Prompts for a codepage number and reloads the file using it .LINK Script posted to: http://pauerschell.blogspot.com by Bernd Kriszio (twitter: bernd_k) .Parameter encoding encodings known to Get-Content: Unknown, String, Unicode, Byte, BigEndianUnicode, UTF8, UTF7, Ascii for a list of valid codepages see http://msdn.microsoft.com/en-us/library/system.text.encodinginfo.aspx .NOTES use -verbose to show the current encoding of the reloaded file #>
[cmdletbinding()]
param(
# encoding to use for reloading
[Parameter(Position = 0, Mandatory=$True)]
$encoding
)
$path = $psise.CurrentFile
$fullpath = $path.FullPath

if ( ("String", "UTF8", "UTF7", "Ascii", "Unicode", "BigEndianUnicode") -contains $encoding)
{
$text = Get-Content -Encoding $encoding $fullpath -ReadCount 0 -totalcount -1
$text = $text -join "`r`n"
}
else
{
$encoding = [int]$encoding
$bytes = get-content $fullpath -encoding byte
$encoding_obj = [System.Text.Encoding]::GetEncoding( $encoding )
$text = $encoding_obj.GetString($bytes)
}

$loadToNewEditor = $False
if ($loadToNewEditor )
{
$count = $psise.CurrentPowerShellTab.Files.count
$psIse.CurrentPowerShellTab.Files.Add()
$Newfile = $psIse.CurrentPowerShellTab.Files[$count]
$Newfile.Editor.Text = $text
}
else
{
$psise.CurrentFile.Editor.Text = $text
}
$psISE.CurrentFile.Encoding | Write-Verbose
}


if ($ISEpackCX)
{
# activate only those you need
# this is not stupid SQL-Management-Studio with no selection or select from all posible encodings
Add-IseMenu -name 'Reload' @{
" Codepage..." = { Reload-ISE }
"Ascii" = { Reload-ISE Ascii }
"BigEndianUnicode" = { Reload-ISE BigEndianUnicode }
"String" = { Reload-ISE String }
"Unicode" = { Reload-ISE Unicode }
"UTF7" = { Reload-ISE UTF7 }
"UTF8" = { Reload-ISE UTF8 }
}
}

Keine Kommentare:

Kommentar veröffentlichen