Skip to content

Commit b6354ce

Browse files
committed
πŸ§‘β€πŸ’» Support importing character constants with aliases
1 parent adb4dd4 commit b6354ce

File tree

2 files changed

+55
-21
lines changed

2 files changed

+55
-21
lines changed

β€ŽImport-CharConstants.ps1β€Ž

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ Creates constants in the context of the current script for the named characters.
2424
[CmdletBinding()] Param(
2525
# The control code abbreviation, Unicode name, HTML entity, or GitHub name of the character to create a constant for.
2626
# "NL" will use the newline appropriate to the environment.
27-
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true,ValueFromRemainingArguments=$true)][string[]] $CharacterName,
27+
[Parameter(ParameterSetName='UseNames',Position=0,Mandatory=$true,ValueFromPipeline=$true,ValueFromRemainingArguments=$true)][string[]] $CharacterName,
28+
# A dictionary that maps character variable name aliases to control code abbreviations, Unicode names, HTML entities,
29+
# or GitHub names of characters.
30+
[Parameter(ParameterSetName='UseAliases',Mandatory=$true)][hashtable] $Alias,
2831
# The scope of the constant.
2932
[string] $Scope = 'Local',
3033
<#
@@ -33,13 +36,25 @@ for characters that support both a simple text presentation as well as a color e
3336
#>
3437
[switch] $AsEmoji
3538
)
36-
Begin {$level = Add-ScopeLevel.ps1 -Scope $Scope}
39+
Begin
40+
{
41+
$level = $Scope |Add-ScopeLevel.ps1 |Add-ScopeLevel.ps1
42+
43+
filter Add-CharacterConstant
44+
{
45+
[CmdletBinding()] Param(
46+
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][Alias('Key')][string] $Alias,
47+
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][Alias('Value')][string] $CharacterName
48+
)
49+
$char = $CharacterName -eq 'NL' ? [Environment]::NewLine : (Get-UnicodeByName.ps1 -Name $CharacterName -AsEmoji:$AsEmoji)
50+
Set-Variable -Name ($Alias.Trim(':')) -Value $char -Scope $level -Option Constant -Description $CharacterName
51+
}
52+
}
3753
Process
3854
{
39-
foreach($name in $CharacterName)
55+
switch($PSCmdlet.ParameterSetName)
4056
{
41-
$cname = $name -replace ':'
42-
$value = $name -ceq 'NL' ? [Environment]::NewLine : (Get-UnicodeByName.ps1 -Name $name -AsEmoji:$AsEmoji)
43-
Set-Variable -Name $cname -Value $value -Scope $level -Option Constant -Description $name
57+
UseNames {$CharacterName |Add-CharacterConstant}
58+
UseAliases {$Alias.GetEnumerator() |Add-CharacterConstant}
4459
}
4560
}

β€ŽShow-Status.ps1β€Ž

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
.SYNOPSIS
33
Displays requested system status values using powerline font characters.
44
5+
.LINK
6+
https://www.nerdfonts.com/cheat-sheet
7+
58
.LINK
69
Get-Unicode.ps1
710
@@ -20,7 +23,7 @@ Show-Status.ps1 UserName HomeDirectory -Separator ' * '
2023
# The format to serialize the date as.
2124
[Parameter(Position=0,Mandatory=$true,ValueFromRemainingArguments=$true)]
2225
[ValidateSet('AdminIndicator','ComputerName','DotNetVersion','DriveUsage','ExplorerUser','GitUser','HomeDirectory',
23-
'OSVersion','PowerShellCommand','PowerShellVersion','Uptime','UserName')]
26+
'OSVersion','PowerShellCommand','PowerShellVersion','Updates','Uptime','UserName')]
2427
[string[]] $Status,
2528
# The separator to use between formatted dates.
2629
[string] $Separator = " $(Get-Unicode.ps1 0x2022) ",
@@ -31,21 +34,30 @@ Show-Status.ps1 UserName HomeDirectory -Separator ' * '
3134
)
3235
Begin
3336
{
34-
Import-CharConstants.ps1 'BUST IN SILHOUETTE' 'HOUSE BUILDING' 'SQUARED ID' 'PERSONAL COMPUTER' `
35-
'POWER SYMBOL' 'NEW MOON SYMBOL' 'HARD DISK' 'POLICE CARS REVOLVING LIGHT' -AsEmoji
37+
Import-CharConstants.ps1 -Alias @{
38+
USER = 'BUST IN SILHOUETTE'
39+
HOUSE = 'HOUSE BUILDING'
40+
COMPUTER = 'PERSONAL COMPUTER'
41+
REDCIRCLE = 'LARGE RED CIRCLE'
42+
BLUECIRCLE = 'LARGE BLUE CIRCLE'
43+
POWER = 'POWER SYMBOL'
44+
HARDDISK = 'HARD DISK'
45+
OVERLAP = 'OVERLAP'
46+
'UP!' = 'SQUARED UP WITH EXCLAMATION MARK'
47+
} -AsEmoji
3648
filter Format-Status
3749
{
3850
[CmdletBinding()][OutputType([string])] Param(
3951
[Parameter(ValueFromPipeline=$true)][string] $Status
4052
)
4153
switch($Status)
4254
{
43-
AdminIndicator { (Test-Administrator.ps1) ? ${POLICE CARS REVOLVING LIGHT} : '' }
44-
ComputerName {"${PERSONAL COMPUTER}$([Environment]::MachineName)"}
45-
DotNetVersion {"$(Get-Unicode.ps1 0xE77F).NET $([Environment]::Version)"}
55+
AdminIndicator { (Test-Administrator.ps1) ? $REDCIRCLE : $BLUECIRCLE }
56+
ComputerName {"$COMPUTER $([Environment]::MachineName)"}
57+
DotNetVersion {"$(Get-Unicode.ps1 0xE77F) .NET $([Environment]::Version)"}
4658
DriveUsage
4759
{
48-
${HARD DISK} + ' ' + ((Get-PSDrive -PSProvider FileSystem |
60+
$HARDDISK + ' ' + ((Get-PSDrive -PSProvider FileSystem |Where-Object {$null -ne $_.Free} |
4961
ForEach-Object {"$(Get-Unicode.ps1 (0x1F311 + [math]::Round((5*$_.Used)/($_.Used+$_.Free)) ))$($_.Name)"}) -join ' ')
5062
}
5163
ExplorerUser
@@ -55,22 +67,29 @@ Begin
5567
ForEach-Object {$_.Get()} |
5668
ForEach-Object {$_.GetOwner()} |
5769
Select-Object -ExpandProperty User
58-
"${SQUARED ID} $euser"
70+
"$OVERLAP $euser"
5971
}
60-
GitUser {"$(Get-Unicode.ps1 0xF1D2)$(git config user.name) <$(git config user.email)>"}
61-
HomeDirectory {"${HOUSE BUILDING}$HOME"}
72+
GitUser {"$(Get-Unicode.ps1 0xF1D2) $(git config user.name) <$(git config user.email)>"}
73+
HomeDirectory {"$HOUSE $HOME"}
6274
OSVersion
6375
{
6476
$icon =
6577
if($IsWindows) {Get-Unicode.ps1 0xF17A}
6678
elseif($IsLinux) {Get-Unicode.ps1 0xF17C}
6779
elseif($IsMacOS) {Get-Unicode.ps1 0xF179}
68-
"$icon$([Environment]::OSVersion.VersionString) $($PSVersionTable.OS)"
80+
"$icon $([Environment]::OSVersion.VersionString) $($PSVersionTable.OS)"
81+
}
82+
PowerShellCommand {"$(Get-Unicode.ps1 0xE86C) $([Environment]::ProcessPath) $([Environment]::CommandLine)"}
83+
PowerShellVersion {"$(Get-Unicode.ps1 0xE86C) PS $($PSVersionTable.PSVersion) $($PSVersionTable.PSEdition)"}
84+
Updates
85+
{
86+
${UP!} + ' ' + (@(
87+
((Get-Command choco -ErrorAction Ignore) -and $(choco outdated -r)) ? 'choco' : $null
88+
((Get-Command winget -ErrorAction Ignore) -and $(winget list --upgrade-available)) ? 'winget' : $null
89+
) |Where-Object {$_}) -join ' '
6990
}
70-
PowerShellCommand {"$(Get-Unicode.ps1 0xE86C)$([Environment]::ProcessPath) $([Environment]::CommandLine)"}
71-
PowerShellVersion {"$(Get-Unicode.ps1 0xE86C)PS $($PSVersionTable.PSVersion) $($PSVersionTable.PSEdition)"}
72-
Uptime {"${POWER SYMBOL} $(Get-Uptime)"}
73-
UserName {"${BUST IN SILHOUETTE}$env:USERNAME"}
91+
Uptime {"$POWER$(Get-Uptime)"}
92+
UserName {"$USER $env:USERNAME"}
7493
default {$_}
7594
}
7695
}

0 commit comments

Comments
Β (0)