From d2137b7fc7e26f21468a34c22c0b84789532e245 Mon Sep 17 00:00:00 2001 From: KOGA Mitsuhiro Date: Sun, 22 Mar 2020 18:59:30 +0900 Subject: [PATCH 1/5] Fix wide character padding --- src/Get-ChildItemColor.psm1 | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Get-ChildItemColor.psm1 b/src/Get-ChildItemColor.psm1 index eedd29a..6eaddf4 100644 --- a/src/Get-ChildItemColor.psm1 +++ b/src/Get-ChildItemColor.psm1 @@ -57,8 +57,8 @@ Function Get-ChildItemColorFormatWide { $Items = Invoke-Expression $Expression - $lnStr = $Items | Select-Object Name | Sort-Object { "$_".Length } -Descending | Select-Object -First 1 - $len = $lnStr.Name.Length + $lnStr = $Items | Select-Object Name | Sort-Object { $Host.UI.RawUI.LengthInBufferCells("$_") } -Descending | Select-Object -First 1 + $len = $Host.UI.RawUI.LengthInBufferCells($lnStr.Name) $width = $Host.UI.RawUI.WindowSize.Width $cols = If ($len) {[math]::Floor(($width + 1) / ($len + 2))} Else {1} if (!$cols) {$cols = 1} @@ -106,12 +106,13 @@ Function Get-ChildItemColorFormatWide { # truncate the item name $toWrite = $Item.Name - If ($toWrite.length -gt $pad) { + If ($Host.UI.RawUI.LengthInBufferCells($toWrite) -gt $pad) { $toWrite = $toWrite.Substring(0, $pad - 3) + "..." } $Color = Get-FileColor $Item - Write-Host ("{0,-$pad}" -f $toWrite) -Fore $Color -NoNewLine:$nnl + $widePad = $pad - ($Host.UI.RawUI.LengthInBufferCells($toWrite) - $toWrite.Length) + Write-Host ("{0,-$widePad}" -f $toWrite) -Fore $Color -NoNewLine:$nnl If ($nnl) { Write-Host " " -NoNewLine From 9e0dca9972e11654e4e364b1ae3b3d32b93d1d33 Mon Sep 17 00:00:00 2001 From: KOGA Mitsuhiro Date: Tue, 24 Mar 2020 03:41:35 +0900 Subject: [PATCH 2/5] Fix error when omitting string --- src/Get-ChildItemColor.psm1 | 2 +- src/PSColorHelper.ps1 | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Get-ChildItemColor.psm1 b/src/Get-ChildItemColor.psm1 index 6eaddf4..c90bade 100644 --- a/src/Get-ChildItemColor.psm1 +++ b/src/Get-ChildItemColor.psm1 @@ -107,7 +107,7 @@ Function Get-ChildItemColorFormatWide { # truncate the item name $toWrite = $Item.Name If ($Host.UI.RawUI.LengthInBufferCells($toWrite) -gt $pad) { - $toWrite = $toWrite.Substring(0, $pad - 3) + "..." + $toWrite = (CutString $toWrite $pad) } $Color = Get-FileColor $Item diff --git a/src/PSColorHelper.ps1 b/src/PSColorHelper.ps1 index 51375bc..ef8239d 100644 --- a/src/PSColorHelper.ps1 +++ b/src/PSColorHelper.ps1 @@ -3,9 +3,17 @@ function CutString { param ([string]$Message, $length) - if ($Message.length -gt $length) + $len = 0 + $count = 0 + $max = $length - 3 + ForEach ($c in $Message.ToCharArray()) { - return $Message.SubString(0, $length-3) + '...' + $len += $Host.UI.RawUI.LengthInBufferCells($c) + if ($len -gt $max) + { + Return $Message.SubString(0, $count) + '...' + } + $count++ } Return $Message From b0b8311259a8df8c7107a83e1258dcb1fa9fc072 Mon Sep 17 00:00:00 2001 From: KOGA Mitsuhiro Date: Tue, 24 Mar 2020 03:47:07 +0900 Subject: [PATCH 3/5] Support pwsh v6 --- src/Get-ChildItemColor.psm1 | 10 ++++---- src/PSColorHelper.ps1 | 47 +++++++++++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/Get-ChildItemColor.psm1 b/src/Get-ChildItemColor.psm1 index c90bade..cd6d925 100644 --- a/src/Get-ChildItemColor.psm1 +++ b/src/Get-ChildItemColor.psm1 @@ -57,8 +57,8 @@ Function Get-ChildItemColorFormatWide { $Items = Invoke-Expression $Expression - $lnStr = $Items | Select-Object Name | Sort-Object { $Host.UI.RawUI.LengthInBufferCells("$_") } -Descending | Select-Object -First 1 - $len = $Host.UI.RawUI.LengthInBufferCells($lnStr.Name) + $lnStr = $Items | Select-Object Name | Sort-Object { LengthInBufferCells("$_") } -Descending | Select-Object -First 1 + $len = LengthInBufferCells($lnStr.Name) $width = $Host.UI.RawUI.WindowSize.Width $cols = If ($len) {[math]::Floor(($width + 1) / ($len + 2))} Else {1} if (!$cols) {$cols = 1} @@ -106,12 +106,14 @@ Function Get-ChildItemColorFormatWide { # truncate the item name $toWrite = $Item.Name - If ($Host.UI.RawUI.LengthInBufferCells($toWrite) -gt $pad) { + $itemLength = LengthInBufferCells($toWrite) + If ($itemLength -gt $pad) { $toWrite = (CutString $toWrite $pad) + $itemLength = LengthInBufferCells($toWrite) } $Color = Get-FileColor $Item - $widePad = $pad - ($Host.UI.RawUI.LengthInBufferCells($toWrite) - $toWrite.Length) + $widePad = $pad - ($itemLength - $toWrite.Length) Write-Host ("{0,-$widePad}" -f $toWrite) -Fore $Color -NoNewLine:$nnl If ($nnl) { diff --git a/src/PSColorHelper.ps1 b/src/PSColorHelper.ps1 index ef8239d..7b55074 100644 --- a/src/PSColorHelper.ps1 +++ b/src/PSColorHelper.ps1 @@ -8,13 +8,56 @@ function CutString $max = $length - 3 ForEach ($c in $Message.ToCharArray()) { - $len += $Host.UI.RawUI.LengthInBufferCells($c) + $len += LengthInBufferCell($c) if ($len -gt $max) { Return $Message.SubString(0, $count) + '...' } $count++ } - Return $Message } + +function LengthInBufferCells +{ + param ([string]$Str) + + $len = 0 + ForEach ($c in $Str.ToCharArray()) + { + $len += LengthInBufferCell($c) + } + Return $len +} + + +function LengthInBufferCell +{ + param ([char]$Char) + # The following is based on http://www.cl.cam.ac.uk/~mgk25/c/wcwidth.c + # which is derived from https://www.unicode.org/Public/UCD/latest/ucd/EastAsianWidth.txt + [bool]$isWide = $Char -ge 0x1100 -and + ($Char -le 0x115f -or # Hangul Jamo init. consonants + $Char -eq 0x2329 -or $Char -eq 0x232a -or + ([uint32]($Char - 0x2e80) -le (0xa4cf - 0x2e80) -and + $Char -ne 0x303f) -or # CJK ... Yi + ([uint32]($Char - 0xac00) -le (0xd7a3 - 0xac00)) -or # Hangul Syllables + ([uint32]($Char - 0xf900) -le (0xfaff - 0xf900)) -or # CJK Compatibility Ideographs + ([uint32]($Char - 0xfe10) -le (0xfe19 - 0xfe10)) -or # Vertical forms + ([uint32]($Char - 0xfe30) -le (0xfe6f - 0xfe30)) -or # CJK Compatibility Forms + ([uint32]($Char - 0xff00) -le (0xff60 - 0xff00)) -or # Fullwidth Forms + ([uint32]($Char - 0xffe0) -le (0xffe6 - 0xffe0))) + + # We can ignore these ranges because .Net strings use surrogate pairs + # for this range and we do not handle surrogage pairs. + # ($Char -ge 0x20000 -and $Char -le 0x2fffd) -or + # ($Char -ge 0x30000 -and $Char -le 0x3fffd) + if ($isWide) + { + Return 2 + } + else + { + Return 1 + } +} From 26b0895ebb2661b26015d8e190da4bf8ee57f6cd Mon Sep 17 00:00:00 2001 From: Joon Ro Date: Mon, 23 Mar 2020 16:08:52 -0700 Subject: [PATCH 4/5] Update PSModulePath for PowerShell 6 and later --- README.org | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.org b/README.org index 749652f..8672f72 100644 --- a/README.org +++ b/README.org @@ -42,9 +42,9 @@ you need =-AllowClobber= option so =Get-ChildItemColor= may override the existing command =Out-Default=. ** Install from GitHub After cloning the repo, you can put files in =/src= folder into -=Get-ChildItemColor= folder under your PowerShell Module folder -(=$ENV:UserProfile\Documents\WindowsPowerShell\Modules=). The =master= branch -always contains the latest release version. +=Get-ChildItemColor= folder under your =PSModulePath= +(e.g., =$ENV:UserProfile\Documents\PowerShell\Modules= for PowerShell 6 and +later). The =master= branch always contains the latest release version. * Usage When you import the module: From 48971ac6643a0e117e1219e616cb1bdf76afae9c Mon Sep 17 00:00:00 2001 From: Joon Ro Date: Mon, 23 Mar 2020 16:09:03 -0700 Subject: [PATCH 5/5] Update v2.2.0 information --- README.org | 2 ++ src/Get-ChildItemColor.psd1 | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.org b/README.org index 8672f72..1785402 100644 --- a/README.org +++ b/README.org @@ -100,6 +100,8 @@ $Global:GetChildItemColorVerticalSpace = 1 * Authors - [[http://github.com/joonro][Joon Ro]]. * Changelog +** v2.2.0 +- Fix #27, Display issue with Chinese. (Thanks to [[https://github.com/shiena][shiena]]) ** v2.1.1 - BUGFIX: Print directory names correctly when =-Recurse= option is used ** v2.1.0 diff --git a/src/Get-ChildItemColor.psd1 b/src/Get-ChildItemColor.psd1 index debf3f9..04a098c 100644 --- a/src/Get-ChildItemColor.psd1 +++ b/src/Get-ChildItemColor.psd1 @@ -10,7 +10,7 @@ RootModule = 'Get-ChildItemColor.psm1' # Version number of this module. -ModuleVersion = '2.1.1' +ModuleVersion = '2.2.0' # Supported PSEditions # CompatiblePSEditions = @()