Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

Commit

Permalink
Merge pull request #5 from SteveL-MSFT/TextUtils
Browse files Browse the repository at this point in the history
Initial checkin of TextUtility module
  • Loading branch information
SteveL-MSFT authored Nov 30, 2019
2 parents 9e496b6 + 6a9cef9 commit 1f50980
Show file tree
Hide file tree
Showing 12 changed files with 88,806 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# directories created during build
bin/
obj/
project.lock.json

# VSCode directories that are not at the repository root
/**/.vscode/
13 changes: 13 additions & 0 deletions Modules/Microsoft.PowerShell.TextUtility/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Microsoft.PowerShell.TextUtility

## Compare-Text

This cmdlet compares two text strings using [diff-match-patch](https://github.com/google/diff-match-patch).

`diff-match-patch` is under [Apache 2.0 License](https://github.com/google/diff-match-patch/blob/master/LICENSE)

## Test-Spelling

This cmdlet checks spelling against a word list derived from the [Project Gutenberg Webster's unabridged dictionary](http://www.gutenberg.org/ebooks/29765).

The dictionary file is under [Project Gutenberg License](https://www.gutenberg.org/wiki/Gutenberg:The_Project_Gutenberg_License)
13 changes: 13 additions & 0 deletions Modules/Microsoft.PowerShell.TextUtility/build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[CmdletBinding()]
param(
[switch] $Clean = $false
)

Push-Location $PSScriptRoot/src/code
if ($Clean) {
Remove-Item -Recurse -Path ./bin -Force
Remove-Item -Recurse -Path ./obj -Force
}

dotnet build
Pop-Location
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
<Configuration>
<ViewDefinitions>
<View>
<Name>Inline</Name>
<ViewSelectedBy>
<TypeName>Microsoft.PowerShell.TextUtility.CompareTextDiff</TypeName>
</ViewSelectedBy>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<ScriptBlock>
Set-StrictMode -Off

$deleteColor = "`e[1;9;31m"
$insertColor = "`e[0;1;32m"
$resetColor = "`e[0m"
$textBuilder = [System.Text.StringBuilder]::new()
$newline = [System.Environment]::NewLine

$addNewLine = $false
if ($_.LeftFile) {
$null = $textBuilder.Append("${deleteColor}File:${resetColor} $($_.LeftFile)${newline}")
$addNewLine = $true
}

if ($_.RightFile) {
$null = $textBuilder.Append("${insertColor}File:${resetColor} $($_.RightFile)${newline}")
$addNewLine = $true
}

if ($addNewLine) {
$null = $textBuilder.Append($newline)
}

foreach ($diff in $_.Diff) {
$text = $diff.text
switch ($diff.Operation)
{
"EQUAL" {
$null = $textBuilder.Append($text)
}

"DELETE" {
$null = $textBuilder.Append("${deleteColor}${text}${resetColor}")
}

"INSERT" {
$null = $textBuilder.Append("${insertColor}${text}${resetColor}")
}
}
}

$textBuilder.ToString()
</ScriptBlock>
</ExpressionBinding>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</View>
<View>
<Name>SideBySide</Name>
<ViewSelectedBy>
<TypeName>Microsoft.PowerShell.TextUtility.CompareTextDiff#SideBySide</TypeName>
</ViewSelectedBy>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<ScriptBlock>
Set-StrictMode -Off

$consoleWidth = 120
try {
$consoleWidth = [Console]::WindowWidth
}
catch {
# just use default if this fails because there isn't a real console
}

$deleteText = [System.Text.StringBuilder]::new()
$insertText = [System.Text.StringBuilder]::new()
$deleteColor = "`e[1;9;31m"
$insertColor = "`e[0;1;32m"
$resetColor = "`e[0m"
$ellipsis = "`u{2026}"
$newline = [System.Environment]::NewLine

foreach ($diff in $_.Diff) {
$text = $diff.text.Replace("`r","")

switch ($diff.operation)
{
"EQUAL" {
$text = $text.Replace("`n","`n${resetColor}")
$null = $deleteText.Append("${resetColor}${text}")
$null = $insertText.Append("${resetColor}${text}")
}

"DELETE" {
$text = $text.Replace("`n","`n${deleteColor}")
$null = $deleteText.Append("${deleteColor}${text}")
}

"INSERT" {
$text = $text.Replace("`n","`n${insertColor}")
$null = $insertText.Append("${insertColor}${text}")
}
}
}

function Remove-VT100([string] $text) {
$text.Replace($deleteColor,"").Replace($insertColor,"").Replace($resetColor,"")
}

$leftLines = $deleteText.ToString().Split("`n")
$rightLines = $insertText.ToString().Split("`n")
$lines = $leftLines.Count

if ($rightLines.Count -gt $lines) {
$lines = $rightLines.Count
}

$linesWidth = ($lines + 1).ToString().Length
# 5 chars for whitespace and pipe after line number and whitespace, pipe, whitespace separating left and right
[int]$columnWidth = ($consoleWidth - $linesWidth - 5) / 2

# find longest line and use that as the columnWidth if less than current
$longestWidth = 0
foreach ($line in $leftLines) {
$line = Remove-VT100 -text $line
if ($line.Length -gt $longestWidth) {
$longestWidth = $line.Length
}
}

if ($longestWidth -lt $columnWidth) {
$columnWidth = $longestWidth
}

$textOutput = [System.Text.StringBuilder]::new()

if ($_.LeftFile -or $_.RightFile) {
$prefix = "File: "
$null = $textOutput.Append("".PadLeft($linesWidth))
$null = $textOutput.Append("${resetColor} | ")
if ($_.LeftFile) {
$leftFile = $_.LeftFile
if ($leftFile.Length + $prefix.Length -gt $columnWidth) {
$leftFile = $ellipsis + $leftFile.Remove(0, $leftFile.Length - $columnWidth + 1 + $prefix.Length)
}
else {
$leftFile = $leftFile.PadRight($columnWidth - $prefix.Length)
}

$leftFile = "${deleteColor}${prefix}${resetColor}${leftFile}"
}
else {
$leftFile = "".PadLeft($columnWidth)
}

if ($_.RightFile) {
$rightFile = $_.RightFile
if ($rightFile.Length + $prefix.Length -gt $columnWidth) {
$rightFile = $ellipsis + $rightFile.Remove(0, $rightFile.Length - $columnWidth + 1 + $prefix.Length)
}

$rightFile = "${insertColor}${prefix}${resetColor}${rightFile}"
}

$null = $textOutput.Append($leftFile)
$null = $textOutput.Append("${resetColor} | ")
$null = $textOutput.Append($rightFile)
$null = $textOutput.Append($newline)
$null = $textOutput.Append("".PadLeft($linesWidth))
$null = $textOutput.Append("${resetColor} | ")
$null = $textOutput.Append("".PadLeft($columnWidth))
$null = $textOutput.Append(" |")
$null = $textOutput.Append($newline)
}

for ($i = 0; $i -lt $lines; $i++) {
if ($i -ge $leftLines.Count) {
$leftText = "".PadRight($columnWidth)
}
else {
$lineLength = (Remove-VT100 -text $leftLines[$i]).Length
if ($lineLength -gt $columnWidth) {
$leftText = $leftLines[$i].Substring(0, $columnWidth - 1 + ($leftLines[$i].Length - $lineLength)) + $ellipsis
}
else {
$leftText = $leftLines[$i] + "${resetColor}" + "".PadRight($columnWidth - $lineLength)
}
}

if ($i -ge $rightLines.Count) {
$rightText = ""
}
else {
$lineLength = (Remove-VT100 -text $rightLines[$i]).Length
if ($lineLength -gt $columnWidth) {
$rightText = $rightLines[$i].Substring(0, $columnWidth - 1 + ($rightLines[$i].Length - $lineLength)) + $ellipsis
}
else {
$rightText = $rightLines[$i]
}
}

$lineNumber = ($i + 1).ToString()
$null = $textOutput.Append($resetColor)
$null = $textOutput.Append($lineNumber.PadLeft($linesWidth - $lineNumber.Length))
$null = $textOutput.Append(" | ")
$null = $textOutput.Append($leftText)
$null = $textOutput.Append("${resetColor} | ")
$null = $textOutput.Append($rightText)
$null = $textOutput.Append($newline)
}

$null = $textOutput.Append("${resetColor}")
$textOutput.ToString()
</ScriptBlock>
</ExpressionBinding>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</View>
</ViewDefinitions>
</Configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

@{
# Script module or binary module file associated with this manifest.
RootModule = '.\Microsoft.PowerShell.TextUtility.dll'

# Version number of this module.
ModuleVersion = '1.0.0'

# Supported PSEditions
CompatiblePSEditions = @('Desktop', 'Core')

# ID used to uniquely identify this module
GUID = '5cb64356-cd04-4a18-90a4-fa4072126155'

# Author of this module
Author = 'Microsoft Corporation'

# Company or vendor of this module
CompanyName = 'Microsoft Corporation'

# Copyright statement for this module
Copyright = '(c) Microsoft Corporation. All rights reserved.'

# Description of the functionality provided by this module
Description = "This module contains cmdlets to help with manipulating or reading text."

# Minimum version of the PowerShell engine required by this module
PowerShellVersion = '5.1'

# Format files (.ps1xml) to be loaded when importing this module
FormatsToProcess = @('Microsoft.PowerShell.TextUtility.format.ps1xml')

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @(
'Compare-Text','ConvertFrom-Base64','ConvertTo-Base64'
)

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{

PSData = @{

# Tags applied to this module. These help with module discovery in online galleries.
# Tags = @()

# A URL to the license for this module.
LicenseUri = 'https://github.com/PowerShell/Modules/License.txt'

# A URL to the main website for this project.
ProjectUri = 'https://github.com/PowerShell/Modules'

# A URL to an icon representing this module.
# IconUri = ''

# ReleaseNotes of this module
# ReleaseNotes = ''

# Prerelease string of this module
# Prerelease = ''

# Flag to indicate whether the module requires explicit user acceptance for install/update/save
# RequireLicenseAcceptance = $false

# External dependent modules of this module
# ExternalModuleDependencies = @()
} # End of PSData hashtable

} # End of PrivateData hashtable

# HelpInfo URI of this module
# HelpInfoURI = ''

}
Loading

0 comments on commit 1f50980

Please sign in to comment.