Skip to content

Commit

Permalink
Merge pull request #106 from StartAutomating/Posh-Updates
Browse files Browse the repository at this point in the history
Posh 0.1.2
  • Loading branch information
StartAutomating authored Aug 3, 2023
2 parents 696c00e + c2f325d commit 7b4266f
Show file tree
Hide file tree
Showing 35 changed files with 1,505 additions and 32 deletions.
176 changes: 174 additions & 2 deletions @.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region Splatter [ 0.5.5 ] : Simple Scripts to Supercharge Splatting (Install-Module Splatter, then Initialize-Splatter -Verb Get,Use,Find )
#region Splatter [ 0.5.5 ] : Simple Scripts to Supercharge Splatting (Install-Module Splatter, then Initialize-Splatter -Verb Get,Use,Find,Merge )

[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "", Justification="This Declares Variables for Other Scripts")]
param()
Expand Down Expand Up @@ -570,4 +570,176 @@ ${??@}=${FindSplat}=${fSplat}={
Write-Progress -Id $id -Completed -Activity 'Finding Splats' -Status 'Complete!'
}
}
#endregion Splatter [ 0.5.5 ] : Simple Scripts to Supercharge Splatting (Install-Module Splatter, then Initialize-Splatter -Verb Get,Use,Find )
${*@}=${MergeSplat}=${mSplat}={
<#
.Synopsis
Merges one or more splats
.Description
Merges one or more hashtables and property bags into one [ordered] hashtable.
Allows you to -Remove specific keys from any object
Allows you to -Include or -Exclude wildcards of keys (or patterns, with -RegularExpression)
Allows you to -Map additional values if a value if found.
.Link
Get-Splat
.Link
Use-Splat
.Example
@{a='b'}, @{c='d'} | Merge-Splat
.Example
[PSCustomOBject]@{a='b'}, @{c='d'} | Merge-Splat -Add @{e='f'} -Remove c
.Example
@{id=$pid} |
Use-Splat Get-Process |
Merge-Splat -Include Name
.Example
@{n=$(Get-Random) } |
Merge-Splat -Map @{
N = {
if (-not ($_ % 2)) { @{IsEven=$true;IsOdd=$false} }
else { @{IsEven=$false;IsOdd=$true}}
}
}
#>
[Alias('*@','mSplat')]
param(
# The splat
[Parameter(ValueFromPipeline)]
[Alias('InputObject')]
[PSObject[]]
$Splat,

# Splats or objects that will be added to the splat.
[Parameter(Position=0,ValueFromRemainingArguments)]
[Alias('With', 'W', 'A', '+')]
[PSObject[]]
$Add,

# The names of the keys to remove from the splat
[Alias('Delete','Drop', 'D','R', '-')]
[string[]]
$Remove,

# Patterns of names to include in the splat.
# If provided, only keys that match at least one -Include pattern will be kept.
# By default, these are wildcards, unlesss -RegularExpression is passed.
[Alias('E', 'EX')]
[string[]]
$Include,

# Patterns of names to exclude from the splat.
# By default, these are wildcards, unlesss -RegularExpression is passed.
[Alias('I', 'IN')]
[string[]]
$Exclude,

# If set, all patterns matched will be assumed to be RegularExpressions, not wildcards
[Alias('Regex','RX')]
[switch]
$RegularExpression,

# A map of new data to add. The key is the name of the original property.
# The value can be any a string, a hashtable, or a script block.
# If the value is a string, it will be treated as a key, and the original property will be copied to this key.
# If the value is a hashtable, it will add the values contained in Map.
# If the value is a ScriptBlock, it will combine the output of this script block with the splat.
[Alias('M','ReMap')]
[Collections.IDictionary]
$Map,

# If set, will keep existing values in a splat instead of adding it to a list of values.
[switch]
$Keep,

# If set, will replace existing values in a splat instead of adding it to a list of values.
[switch]
$Replace)


begin {
$accumulate = [Collections.ArrayList]::new()
$aSplat = {param($o, $i)
if ($i -is [Collections.IDictionary]) {
try { $o += $i }
catch {
foreach ($kv in $i.GetEnumerator()) {
$gotIt? = $o.Contains($kv.Key)
if ($gotIt? -and $Keep) { continue }
if ($gotIt? -and $Replace) {
$o[$kv.Key] = $kv.Value;continue
} elseif ($gotIt?) {
if ($o[$kv.Key] -isnot [Object[]]) {
$o[$kv.Key] = @($o[$kv.Key])
}
$o[$kv.Key] += $kv.Value
} else {
$o[$kv.Key] = $kv.Value
}
}

}
} else {
foreach ($prop in $i.psobject.properties) { $o[$prop.Name] = $prop.Value }
}
}
$imSplat = {
if (-not $accumulate.Count) { return }
$o = [Ordered]@{}
foreach ($in in $accumulate) {
. $aSplat $o $in
}
$ok = @($o.Keys)
:nextKey foreach ($k in $ok) {
if ($Map -and $map.$k) {
foreach ($mk in $map.$k) {
if ($mk -is [string]) { $o[$mk] = $o[$k] }
elseif ($mk -is [Collections.IDictionary]) {
. $aSplat $o $mk
}
elseif ($mk -is [ScriptBlock]) {
$_ = $o[$k]
$mkr = . $mk $_
foreach ($r in $mkr) { . $aSplat $o $r }
}
}
}

if ($Exclude) {
foreach ($ex in $Exclude) {
if (($RegularExpression -and ($k -match $ex)) -or
($k -like $ex)) {
$o.Remove($k)
continue nextKey
}
}
}

if ($include) {
foreach ($in in $include) {
if (($RegularExpression -and ($k -match $in)) -or
($k -like $in)) {
continue nextKey
}
}
$o.Remove($k)
}
}
foreach ($r in $Remove) { $o.Remove($r) }
$accumulate.Clear()
$o
}
}
process {
$isTheEndOfTheLine =
$MyInvocation.PipelinePosition -eq $MyInvocation.PipelineLength
if ($Splat) { $accumulate.AddRange($Splat) }
if ($Add) { $accumulate.AddRange($add) }
if (-not $isTheEndOfTheLine) {
. $imSplat
}
}

end {
. $imSplat
}
}
#endregion Splatter [ 0.5.5 ] : Simple Scripts to Supercharge Splatting (Install-Module Splatter, then Initialize-Splatter -Verb Get,Use,Find,Merge )
4 changes: 2 additions & 2 deletions Build/Posh.Splatter.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Push-Location (Split-Path $PSScriptRoot)
Initialize-Splatter -Verb Get, Use, Find -OutputPath .\@.ps1
Pop-Location
Initialize-Splatter -Verb Get, Use, Find, Merge -OutputPath .\@.ps1
Pop-Location
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
## Posh 0.1.2:

* New Demos
* Posh.demo.ps1 (#95)
* Posh.Prompt.demo.ps1 (#96)
* Posh Profile Customization (#97)
* New formatters:
* Adding Formatting for Match (#100)
* Adding Variable Formatting (#102)
* Extending Modules
* Adding PSModuleInfo.Test(s) (#104)
* Prompt Customization:
* Posh.Prompt.Undo() (#103)
* Profile Customization:
* Posh.Profiles.RemoveModule (#105)
* Posh.Profiles.New() (#101)
* Posh.Profile.ImportModule (#93)
* Posh.Profile.Matches (#99)
* Posh.Profile.Replace (#92)
* Posh.Profile.get_File (#76)

---

## Posh 0.1.1:

* Posh can be sponsored (please show your support) (#77)
Expand Down
31 changes: 31 additions & 0 deletions Demos/Posh.Profile.demo.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#1. Customizing Profiles

# Profiles are a useful part of PowerShell.

# They run when PowerShell starts.

# Posh makes profiles easy to explore and modify

$Posh.Profile

# Let's pipe it into Get-Member and see what it can do:

$Posh.Profile | Get-Member

# We can get all the current profile file, by using

$Posh.Profile.File

# We can also get all current profile files, by using

$Posh.Profile.Files

# If we want to import a module in our profile, we can use

$Posh.Profile.ImportModule("Posh")

# If we want to add arbitrary code to our profile, we can use:

$Posh.Profile.Add({
"Welcome to PowerShell!" | Out-Host
})
56 changes: 56 additions & 0 deletions Demos/Posh.Prompt.demo.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# 1. Making Prompts more Posh

# `Prompt` is a PowerShell function that is called just before PowerShell asks for input.

# It outputs the text that goes before a prompt.

# Modifying the PowerShell prompt can be incredibly useful.

# This is why Posh makes modifying the prompt easy.

#2. Replacing parts of the prompt

# We can see information about the current prompt by running:

$Posh.Prompt

# Let's use Get-Member to see what we can do:

$Posh.Prompt | Get-Member

# Now let's modify the prompt to replace the username with astericks

$Posh.Prompt.Replace(
$(if ($env:User) { $env:User } else { $env:USERNAME }),
'*****'
)

#.ShowPrompt

# Now let's replace the start of the prompt.

# We can provide a string, scriptblock, or emoji number.

$Posh.Prompt.Replace("PS ", 0x2AA1)

# Looks nice. Let's replace the end of the prompt.

$Posh.Prompt.Replace(">", 0x2AA2)

# For extra fun, let's add some content to the end of the prompt

$Posh.Prompt.Append({(Get-Date).ToShortTimeString()})

# Hmm, that isn't quite distintive enough. Let's undo that step.

$Posh.Prompt.Pop()

# Now let's add a "better" end to the prompt:

$Posh.Prompt.Append({
"[$((Get-Date).ToShortTimeString())]"
})

# Posh provides the fundamental building blocks to modify your prompt.

# May it make your shell more sleek.
Loading

0 comments on commit 7b4266f

Please sign in to comment.