ps/Modules/Cole.PowerShell.Developer/Public/Get-HackerText.ps1
2023-05-30 22:51:22 -07:00

151 lines
5.0 KiB
PowerShell

function Get-HackerText {
param (
[string]$Textline,
[switch]$Print,
[switch]$Clipboard
)
$words = @()
$currentQuoteDelimiter = ''
$currentWord = @()
$inQuotedString = $false
$lastCharWildcard = $false
foreach ($char in $Textline -split '') {
$quoteDelimiter = ($char -eq '"' -or $char -eq "'")
if ($lastCharWildcard) {
$currentWord += $char
$lastCharWildcard = $false
} elseif ($char -eq '`') {
$lastCharWildcard = $true
$currentWord += $char
} elseif ($quoteDelimiter -and -not $inQuotedString) {
# begin quoted string
$inQuotedString = $true
$currentQuoteDelimiter = $char
$currentWord += $char
} elseif ($quoteDelimiter -and $inQuotedString -and $quoteDelimiter -eq $currentQuoteDelimiter) {
# end quoted string
$inQuotedString = $false
$currentQuoteDelimiter = ''
$currentWord += $char
$words += ($currentWord -join '')
$currentWord = @()
} elseif ($char -eq ' ') {
if ($currentWord.Count -gt 0) {
$words += ($currentWord -join '')
}
$currentWord = @()
} else {
$currentWord += $char
}
}
if ($currentWord.Count -gt 0) {
$words += ($currentWord -join '')
}
$actions = @()
foreach ($word in $words) {
if ($word[0] -in ('"',"'")) {
# a human would just type 6 letters, not paste it
if ($word.Trim().Length -lt 8) {
$actions += @{ Type = " $word" }
} else {
# word is a quoted string, use Paste action
$actions += @{ Type = " $($word[0])" }
$actions += @{ Paste = ($word[1..($word.Length - 2)] -join '') }
$actions += @{ Type = "$($word[-1]) " }
}
} else {
$hyphenSplit = $word -split '-'
if ($hyphenSplit.Count -eq 2) {
if ($hyphenSplit[1].Length -gt 3) {
$type = $hyphenSplit[0]
if ([string]::IsNullOrWhiteSpace($type)) {
$type = ' '
}
# special case for special children
if ($word -eq '-ProfileName') {
$actions += @{ Type = " -Pro" }
$actions += @{ Tab = 'file' }
$actions += @{ TabChange = 'Location' }
$actions += @{ Tab = 'Name' }
} else {
$tabSplit = ($hyphenSplit[1][0..2] -join '')
$actions += @{ Type = "$type-$tabSplit" }
$actions += @{ Tab = $hyphenSplit[1].Substring(3) }
}
} else {
$actions += @{ Type = " $word" }
}
} else {
if ($word[0] -eq '$' -and $word.Length -gt 6) {
$actions += @{ Type = " $(($word[0..3] -join ''))" }
$actions += @{ Tab = $word.Substring(4) }
} else {
$actions += @{ Type = " $word" }
}
}
}
}
$reparsedActions = @()
$reparsingCounter = 0
while ($true) {
$actionsLength = $actions.Count
for ($i = 0; $i -lt $actionsLength; $i++) {
$action = $actions[$i]
if ($i -lt ($actionsLength - 1)) {
if ($action.Keys[0] -eq $actions[$i + 1].Keys[0]) {
# same key, combine the values
$key = $action.Keys[0]
$value1 = $action.Values[0]
$value2 = $actions[$i + 1].Values[0]
$value = "$value1$value2" -replace ' ',' '
$reparsedActions += @{ "$key" = "$value" }
$i += 1
} else {
$reparsedActions += $action
}
} else {
$reparsedActions += $action
}
}
if ($reparsedActions.Count -eq $actions.Count -or $reparsingCounter -gt 5) {
break
} else {
$actions = $reparsedActions.Clone()
$reparsedActions = @()
$reparsingCounter += 1
}
}
if ($Print -or $Clipboard) {
$output = @()
$output += "Invoke-ScriptedActions -Actions @("
foreach ($action in $actions) {
$key = $action.Keys[0]
$value = $action.Values[0]
$stringDelimiter = "'"
if ($value.IndexOf("'") -gt -1) {
$stringDelimiter = '"'
} else {
$value = $value -replace '`',''
}
$output += " @{ $key = $stringDelimiter$value$stringDelimiter }"
}
$output += ")"
if ($Clipboard) {
$output | Set-Clipboard
} else {
return $output
}
} else {
return $actions
}
}