Skip to content

Instantly share code, notes, and snippets.

@Jaykul
Created July 2, 2022 00:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jaykul/2e00ae7fd087058c4f12f1ec66cbd0af to your computer and use it in GitHub Desktop.
Save Jaykul/2e00ae7fd087058c4f12f1ec66cbd0af to your computer and use it in GitHub Desktop.
function Get-Date {
<#
.SYNOPSIS
Get the time span elapsed during the execution of command (by default the previous command)
.DESCRIPTION
Calls Get-History to return a single command and returns the difference between the Start and End execution time
#>
[OutputType([string])]
[CmdletBinding(DefaultParameterSetName = "SimpleFormat")]
param(
# A DateTime format string such as "h\:mm\:ss". Defaults to "T"
[Parameter(ParameterSetName = 'SimpleFormat')]
[string]$Format = "T"
)
Get-Date -Format $Format
}
function Show-Date {
<#
.SYNOPSIS
Get the time span elapsed during the execution of command (by default the previous command)
.DESCRIPTION
Calls Get-History to return a single command and returns the difference between the Start and End execution time
#>
[OutputType([string])]
[CmdletBinding(DefaultParameterSetName = "SimpleFormat")]
param(
[Parameter(ParameterSetName='SimpleFormat')]
[string]
${Format}, [PoshCode.TerminalPosition]
${Position},
[PoshCode.BlockAlignment]
${Alignment},
[Alias('Prepend')]
[string]
${Prefix},
[Alias('Suffix','Append')]
[string]
${Postfix},
[string]
${Separator},
[PoshCode.BlockCaps]
${Caps},
[Alias('ForegroundColor','Fg','DFg')]
[AllowNull()]
[PoshCode.Pansies.RgbColor]
${DefaultForegroundColor},
[Alias('BackgroundColor','Bg','DBg')]
[AllowNull()]
[PoshCode.Pansies.RgbColor]
${DefaultBackgroundColor},
[Alias('AdminFg','AFg')]
[AllowNull()]
[PoshCode.Pansies.RgbColor]
${AdminForegroundColor},
[Alias('AdminBg','ABg')]
[AllowNull()]
[PoshCode.Pansies.RgbColor]
${AdminBackgroundColor},
[Alias('ErrorFg','EFg')]
[AllowNull()]
[PoshCode.Pansies.RgbColor]
${ErrorForegroundColor},
[Alias('ErrorBg','EBg')]
[AllowNull()]
[PoshCode.Pansies.RgbColor]
${ErrorBackgroundColor}
)
$PSBoundParameters['Content'] = { # Show-Date
Get-Date -Format $Format
}.GetNewClosure()
# toss all the parameters that came from the command
foreach ($name in 'Format','Verbose','Debug','ErrorAction','WarningAction','InformationAction','ErrorVariable','WarningVariable','InformationVariable','OutVariable','OutBuffer','PipelineVariable') {
$null = $PSBoundParameters.Remove($name)
}
[PoshCode.TerminalBlock]$PSBoundParameters
}
function New-TerminalBlock {
<#
.Synopsis
Create PoshCode.TerminalBlock with variable background colors
.Description
Allows changing the foreground and background colors based on elevation or success.
Tests elevation fist, and then whether the last command was successful, so if you pass separate colors for each, the Elevated*Color will be used when PowerShell is running as administrator and there is no error. The Error*Color will be used whenever there's an error, whether it's elevated or not.
.Example
New-TerminalBlock { Show-ElapsedTime } -ForegroundColor White -BackgroundColor DarkBlue -ErrorBackground DarkRed -ElevatedForegroundColor Yellow
This example shows the time elapsed executing the last command in White on a DarkBlue background, but switches the text to yellow if elevated, and the background to red on error.
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '', Justification = 'New is not state changing!')]
[OutputType([PoshCode.TerminalBlock])]
[CmdletBinding(DefaultParameterSetName = "Content")]
[Alias("TerminalBlock", "Block")]
param(
# The text, object, or scriptblock to show as output
[AllowNull()][EmptyStringAsNull()]
[Parameter(Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName = "Content")] # , Mandatory=$true
[Alias("InputObject")]
$Content,
[PoshCode.TerminalPosition]$Position,
[PoshCode.BlockAlignment]$Alignment,
[Alias("Prepend")]
[String]$Prefix,
[Alias("Suffix", "Append")]
[String]$Postfix,
# The separator character(s) are used between blocks of output by this scriptblock
# Pass two characters: the first for normal (Left aligned) blocks, the second for right-aligned blocks
[ArgumentCompleter({
[System.Collections.Generic.List[System.Management.Automation.CompletionResult]]::new(
[System.Management.Automation.CompletionResult[]]@(
# The Consolas-friendly block characters ▌and▐ and ╲ followed by all the extended Terminal cahracters
@([string[]][char[]]@(@(0xe0b0..0xe0d4) + @(0x2588..0x259b) + @(0x256d..0x2572))).ForEach({
[System.Management.Automation.CompletionResult]::new("'$_'", $_, "ParameterValue", $_) })
))
})]
[String]$Separator,
# The cap character(s) are used on the ends of blocks of output
# Pass two characters: the first for normal (Left aligned) blocks, the second for right-aligned blocks
[ArgumentCompleter({
[System.Collections.Generic.List[System.Management.Automation.CompletionResult]]::new(
[System.Management.Automation.CompletionResult[]]@(
# The Consolas-friendly block characters ▌and▐ and ╲ followed by all the extended Terminal cahracters
@([string[]][char[]]@(@(0xe0b0..0xe0d4) + @(0x2588..0x259b) + @(0x256d..0x2572))).ForEach({
[System.Management.Automation.CompletionResult]::new("'$_'", $_, "ParameterValue", $_) })
))
})]
[PoshCode.BlockCaps]$Caps,
# The foreground color to use when the last command succeeded
[Alias("ForegroundColor", "Fg", "DFg")]
[AllowNull()][EmptyStringAsNull()]
[PoshCode.Pansies.RgbColor]$DefaultForegroundColor,
# The background color to use when the last command succeeded
[Alias("BackgroundColor", "Bg", "DBg")]
[AllowNull()][EmptyStringAsNull()]
[PoshCode.Pansies.RgbColor]$DefaultBackgroundColor,
# The foreground color to use when the process is elevated (running as administrator)
[Alias("AdminFg","AFg")]
[AllowNull()][EmptyStringAsNull()]
[PoshCode.Pansies.RgbColor]$AdminForegroundColor,
# The background color to use when the process is elevated (running as administrator)
[Alias("AdminBg","ABg")]
[AllowNull()][EmptyStringAsNull()]
[PoshCode.Pansies.RgbColor]$AdminBackgroundColor,
# The foreground color to use when the last command failed
[Alias("ErrorFg", "EFg")]
[AllowNull()][EmptyStringAsNull()]
[PoshCode.Pansies.RgbColor]$ErrorForegroundColor,
# The background color to use when the last command failed
[Alias("ErrorBg", "EBg")]
[AllowNull()][EmptyStringAsNull()]
[PoshCode.Pansies.RgbColor]$ErrorBackgroundColor
)
process {
# Strip common parameters if they're on here (so we can use -Verbose)
foreach($name in [System.Management.Automation.PSCmdlet]::CommonParameters) {
$null = $PSBoundParameters.Remove($name)
}
[PoshCode.TerminalBlock]$PSBoundParameters
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment