code-page.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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
Set-StrictMode -Version Latest Set-Variable -Name all -Option Constant -Value "All" Set-Variable -Name identifier -Option Constant -Value "Identifier" Set-Variable -Name name -Option Constant -Value "Name" Set-Variable -Name information -Option Constant -Value "Information" Set-Variable -Name def -Option Constant -Value "Default" Set-Variable -Name exact -Option Constant -Value "Exact" Set-Variable -Name fuzzy -Option Constant -Value "Fuzzy" function Find-CodePageIdentifierExact { param ( [Parameter(Mandatory)][String]$Search, [Parameter(Mandatory)][Object[]]$CodePages ) foreach ($cp in $CodePages) { if ($cp.Identifier -ceq $Search) { $cp return } } $null return } function Find-CodePageIdentifierFuzzy { param ( [Parameter(Mandatory)][String]$Search, [Parameter(Mandatory)][Object[]]$CodePages ) $candidates = @() foreach ($cp in $CodePages) { if ($cp.Identifier -like "*$Search*") { $candidates += $cp } } $candidates return } function Find-CodePageNameExact { param ( [Parameter(Mandatory)][String]$Search, [Parameter(Mandatory)][Object[]]$CodePages ) foreach ($cp in $CodePages) { if ($cp.'.NET Name' -ceq $Search) { $cp return } } $null return } function Find-CodePageNameFuzzy { param ( [Parameter(Mandatory)][String]$Search, [Parameter(Mandatory)][Object[]]$CodePages ) $candidates = @() foreach ($cp in $CodePages) { if ($cp.'.NET Name' -like "*$Search*") { $candidates += $cp } } $candidates return } function Find-CodePageInfomationExact { param ( [Parameter(Mandatory)][String]$Search, [Parameter(Mandatory)][Object[]]$CodePages ) foreach ($cp in $CodePages) { if ($cp.'Additional information' -ceq $Search) { $cp return } } $null return } function Find-CodePageInfomationFuzzy { param ( [Parameter(Mandatory)][String]$Search, [Parameter(Mandatory)][Object[]]$CodePages ) $candidates = @() foreach ($cp in $CodePages) { if ($cp.'Additional information' -like "*$Search*") { $candidates += $cp } } $candidates return } function Add-Spaces { param ( [Parameter(Mandatory)][AllowEmptyString()][AllowNull()][String]$Message, [Parameter(Mandatory)][Int]$Width, [Parameter(Mandatory)][ValidateSet("Left", "Right")][String]$Side ) $l = if ($null -eq $Message) { 0 } else { $Message.Length } $n = $Width - $l switch ($Side) { "Left" { "$(' ' * $n)$Message" } "Right" { "$Message$(' ' * $n)" } } } function Invoke-Chcp { param ( [Parameter(Mandatory)][PSCustomObject]$CodePage, [Bool]$DryRun = $false ) if ($DryRun) { Write-Host "chcp $($CodePage.Identifier)" return } chcp $CodePage.Identifier } # Export # .SYNOPSIS # Set code page with its name. function Set-CodePage() { [CmdletBinding()] param ( [Parameter(Mandatory)][String]$Search, [ValidateSet("All", "Identifier", "Name", "Information")][String]$Target = $all, [ValidateSet("Default", "Exact", "Fuzzy")][String]$Way = $def, [Switch]$DryRun = $false ) $ErrorActionPreference = 'Stop' $codePages = Import-Csv "$($MyInvocation.MyCommand.Module.ModuleBase)\code-page.csv" $targets = switch ($Target) { $all { @($identifier, $name, $information) } Default { @($Target)} } $candidates = @() foreach ($t in $targets) { switch ($t) { $identifier { switch ($Way) { $fuzzy { $cps = Find-CodePageIdentifierFuzzy $Search $codePages $candidates += $cps } Default { $cp = Find-CodePageIdentifierExact $Search $codePages if ($null -ne $cp) { Invoke-Chcp -CodePage $cp -DryRun $DryRun return } } } } $name { switch ($Way) { $exact { $cp = Find-CodePageNameExact $Search $codePages if ($null -ne $cp) { Invoke-Chcp -CodePage $cp -DryRun $DryRun return } } Default { $cps = Find-CodePageNameFuzzy $Search $codePages $candidates += $cps } } } $information { switch ($Way) { $exact { $cp = Find-CodePageInfomationExact $Search $codePages if ($null -ne $cp) { Invoke-Chcp -CodePage $cp -DryRun $DryRun return } } Default { $cps = Find-CodePageInfomationFuzzy $Search $codePages $candidates += $cps } } } } } $candidates = $candidates | ForEach-Object { [PSCustomObject]@{ id = [Int]::Parse($_.Identifier); original = $_ } } | Sort-Object -Property id | Get-Unique -AsString | ForEach-Object { $_.original } if ($null -eq $candidates) { Write-Warning "no code page found" return } $max = $candidates.Length - 1 (0 .. $max) | ForEach-Object { $id = $candidates[$_].Identifier $name = $candidates[$_].'.NET Name' $info = $candidates[$_].'Additional information' Write-Host "$(Add-Spaces -Message $_ -Width 3 -Side Left): $(Add-Spaces -Message $id -Width 5 -Side Left) $(Add-Spaces -Message $name -Width 23 -Side Right) $info" } $choice = Read-Host "Enter the number which you select $(if (0 -eq $max) { '[0]' } else { "[0-$max]" })" Invoke-Chcp -CodePage $candidates[$choice] -DryRun $DryRun } # .SYNOPSIS # Lists code pages. function Show-CodePages() { [CmdletBinding()] param () $ErrorActionPreference = 'Stop' Import-Csv "$($MyInvocation.MyCommand.Module.ModuleBase)\code-page.csv" } Export-ModuleMember ` -Function ` Set-CodePage, ` Show-CodePages |