Skip to content

Commit

Permalink
Merge branch 'release/v2.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
joonro committed Mar 23, 2020
2 parents 2d4a2b1 + 48971ac commit 0c1744f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 12 deletions.
8 changes: 5 additions & 3 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Get-ChildItemColor.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
RootModule = 'Get-ChildItemColor.psm1'

# Version number of this module.
ModuleVersion = '2.1.1'
ModuleVersion = '2.2.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
13 changes: 8 additions & 5 deletions src/Get-ChildItemColor.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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 { 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}
Expand Down Expand Up @@ -106,12 +106,15 @@ Function Get-ChildItemColorFormatWide {

# truncate the item name
$toWrite = $Item.Name
If ($toWrite.length -gt $pad) {
$toWrite = $toWrite.Substring(0, $pad - 3) + "..."
$itemLength = LengthInBufferCells($toWrite)
If ($itemLength -gt $pad) {
$toWrite = (CutString $toWrite $pad)
$itemLength = LengthInBufferCells($toWrite)
}

$Color = Get-FileColor $Item
Write-Host ("{0,-$pad}" -f $toWrite) -Fore $Color -NoNewLine:$nnl
$widePad = $pad - ($itemLength - $toWrite.Length)
Write-Host ("{0,-$widePad}" -f $toWrite) -Fore $Color -NoNewLine:$nnl

If ($nnl) {
Write-Host " " -NoNewLine
Expand Down
57 changes: 54 additions & 3 deletions src/PSColorHelper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,61 @@ 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 += 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
}
}

0 comments on commit 0c1744f

Please sign in to comment.