Donnerstag, 12. November 2009

Reload-ISE - Part III

I added some code to save using different encodings and to show the encoding of the current file.

I thought default for new files was utf8. Surprize it is BigEndianUnicode. But as long as it uses a BOM-Mark i will not complain. (Otherwise read my comments. ;-) )

Play and learn.

# use this switch, when you have ISEpack installed and want a default menu added
# import-module isepack
# . -ISEpackCX

function Reload-ISE ()
Reload file asuming given encoding
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.
Reload-ISE utf8
Reloads the file asuming utf8 encoding. This is useful when the file
has no BOM
Prompts for a codepage number and reloads the file using it
Script posted to:
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
use -verbose to show the current encoding of the reloaded file
and RMFC read my f... comments

# encoding to use for reloading
[Parameter(Position = 0, Mandatory=$True)]
$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"
$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
$Newfile = $psIse.CurrentPowerShellTab.Files[$count]
$Newfile.Editor.Text = $text
$psise.CurrentFile.Editor.Text = $text
$psISE.CurrentFile.Encoding | Write-Verbose

if ($ISEpackCX)
# I'm beginning to dislike Add-IseMenu because I can't control the order of the items, I'm using blanks as workaround. Dirty old...
Add-IseMenu -name 'Reload/Save' @{
" Show encoding" = {$psISE.CurrentFile.Encoding| Select BodyName, IsSingleByte, EncodingName, CodePage| fl}
"Reload Codepage..." = { Reload-ISE } # in most cases you can't save back in this encoding, only export
"Reload Ascii" = { Reload-ISE Ascii }
"Reload BigEndianUnicode" = { Reload-ISE BigEndianUnicode } # this seems to be the default for new files in ISE
"Reload OEM850" = { Reload-ISE 850 } # you can't save back in this encoding, only export
"Reload String" = { Reload-ISE String } # seems to be the same as unicode
"Reload Unicode" = { Reload-ISE Unicode }
"Reload utf7" = { Reload-ISE UTF7 }
"Reload utf8" = { Reload-ISE UTF8 }
# To save means use an encoding, which ISE can read back again
"Save File ASCII" = {$psISE.CurrentFile.Save([Text.Encoding]::ascii)} # if you use this, you are insulting most Europeans
"Save File ANSI" = {$psISE.CurrentFile.Save([Text.Encoding]::default)} # my personal favorite ;-)
# I hope the following write BOM's. Unicode without BOM is more evil than OEM437 or OEM850
# I sentence everyone who uses unicode without BOM to 10 years VB3 coding
# (without PowerShell and on Vista to make it a real punishment ;-) )
"Save File utf7" = {$psISE.CurrentFile.Save([Text.Encoding]::utf7)}
"Save File utf8" = {$psISE.CurrentFile.Save([Text.Encoding]::utf8)}
"Save File unicode" = {$psISE.CurrentFile.Save([Text.Encoding]::unicode)}
"Save File BigEndianUnicode" = {$psISE.CurrentFile.Save([Text.Encoding]::BigEndianUnicode)} # as far as I know, that's ISE's default for new files
# Todo: Export in arbritary encodings into another file

Keine Kommentare:

Kommentar veröffentlichen