Skip to content

Commit

Permalink
Merge pull request #13 from trackd/prerelease
Browse files Browse the repository at this point in the history
 adding some parameters to configure output
  • Loading branch information
ShaunLawrie authored Dec 8, 2023
2 parents 07b6c4f + 3619545 commit a9cb133
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 64 deletions.
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"powershell.codeFormatting.preset": "OTBS"
}
"powershell.codeFormatting.preset": "OTBS",
"editor.formatOnPaste": false,
"editor.formatOnSave": false,
"editor.formatOnType": false
}
2 changes: 1 addition & 1 deletion PwshSpectreConsole/PwshSpectreConsole.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'PwshSpectreConsole'

# Version number of this module.
ModuleVersion = '0.3.31'
ModuleVersion = '0.3.35'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
14 changes: 13 additions & 1 deletion PwshSpectreConsole/private/completions/Completers.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,16 @@ class SpectreConsoleTreeGuide : IValidateSetValuesGenerator {
$lookup = [TreeGuide] | Get-Member -Static -MemberType Properties | Select-Object -ExpandProperty Name
return $lookup
}
}
}
class SpectreConsoleWidth : IValidateSetValuesGenerator {
[String[]] GetValidValues() {
$lookup = 1..[console]::BufferWidth
return $lookup
}
}
class SpectreConsoleHeight : IValidateSetValuesGenerator {
[String[]] GetValidValues() {
$lookup = 1..[console]::BufferHeight
return $lookup
}
}
18 changes: 14 additions & 4 deletions PwshSpectreConsole/public/formatting/Format-SpectreBarChart.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using module "..\..\private\completions\Completers.psm1"

function Format-SpectreBarChart {
<#
.SYNOPSIS
Expand All @@ -15,6 +17,9 @@ function Format-SpectreBarChart {
.PARAMETER Width
The width of the chart in characters.
.PARAMETER HideValues
Hides the values from being displayed on the chart.
.EXAMPLE
# This example displays a bar chart with the title "Fruit Sales" and a width of 50 characters.
$data = @(
Expand All @@ -31,21 +36,26 @@ function Format-SpectreBarChart {
$data += New-SpectreChartItem -Label "Apples" -Value 10 -Color [Spectre.Console.Color]::Green
$data += New-SpectreChartItem -Label "Oranges" -Value 5 -Color "Orange"
$data += New-SpectreChartItem -Label "Bananas" -Value 2.2 -Color "#FFFF00"
Format-SpectreBarChart -Data $data -Title "Fruit Sales" -Width 50
#>
[Reflection.AssemblyMetadata("title", "Format-SpectreBarChart")]
param (
[Parameter(ValueFromPipeline, Mandatory)]
[array] $Data,
$Title,
$Width = $Host.UI.RawUI.Width
[String]$Title,
[ValidateSet([SpectreConsoleWidth],ErrorMessage = "Value '{0}' is invalid. Cannot exceed console width.")]
[int]$Width = [console]::BufferWidth,
[switch]$HideValues
)
begin {
$barChart = [Spectre.Console.BarChart]::new()
if($Title) {
$barChart.Label = $Title
}
if ($HideValues) {
$barChart.ShowValues = $false
}
$barChart.Width = $Width
}
process {
Expand All @@ -60,4 +70,4 @@ function Format-SpectreBarChart {
end {
Write-AnsiConsole $barChart
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using module "..\..\private\completions\Completers.psm1"

function Format-SpectreBreakdownChart {
<#
.SYNOPSIS
Expand All @@ -12,6 +14,12 @@ function Format-SpectreBreakdownChart {
.PARAMETER Width
The width of the chart. Defaults to the width of the console.
.PARAMETER HideTags
Hides the tags on the chart.
.PARAMETER HideTagValues
Hides the tag values on the chart.
.EXAMPLE
# This example displays a breakdown chart with the title "Fruit Sales" and a width of 50 characters.
$data = @(
Expand All @@ -28,18 +36,27 @@ function Format-SpectreBreakdownChart {
$data += New-SpectreChartItem -Label "Apples" -Value 10 -Color [Spectre.Console.Color]::Green
$data += New-SpectreChartItem -Label "Oranges" -Value 5 -Color "Orange"
$data += New-SpectreChartItem -Label "Bananas" -Value 2.2 -Color "#FFFF00"
Format-SpectreBreakdownChart -Data $data -Width 50
#>
[Reflection.AssemblyMetadata("title", "Format-SpectreBreakdownChart")]
param (
[Parameter(ValueFromPipeline, Mandatory)]
[array] $Data,
$Width = $Host.UI.RawUI.Width
[ValidateSet([SpectreConsoleWidth],ErrorMessage = "Value '{0}' is invalid. Cannot exceed console width.")]
[int]$Width = [console]::BufferWidth,
[switch]$HideTags,
[Switch]$HideTagValues
)
begin {
$chart = [Spectre.Console.BreakdownChart]::new()
$chart.Width = $Width
if ($HideTags) {
$chart.ShowTags = $false
}
if ($HideTagValues) {
$chart.ShowTagValues = $false
}
}
process {
if($Data -is [array]) {
Expand All @@ -53,4 +70,4 @@ function Format-SpectreBreakdownChart {
end {
Write-AnsiConsole $chart
}
}
}
20 changes: 18 additions & 2 deletions PwshSpectreConsole/public/formatting/Format-SpectrePanel.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ function Format-SpectrePanel {
.PARAMETER Color
The color of the panel border.
.PARAMETER Width
The width of the panel.
.PARAMETER Height
The height of the panel.
.EXAMPLE
# This example displays a panel with the title "My Panel", a rounded border, and a red border color.
Format-SpectrePanel -Data "Hello, world!" -Title "My Panel" -Border "Rounded" -Color "Red"
Expand All @@ -37,14 +43,24 @@ function Format-SpectrePanel {
[switch] $Expand,
[ValidateSpectreColor()]
[ArgumentCompletionsSpectreColors()]
[string] $Color = $script:AccentColor.ToMarkup()
[string] $Color = $script:AccentColor.ToMarkup(),
[ValidateSet([SpectreConsoleWidth],ErrorMessage = "Value '{0}' is invalid. Cannot exceed console width.")]
[int]$Width,
[ValidateSet([SpectreConsoleHeight],ErrorMessage = "Value '{0}' is invalid. Cannot exceed console height.")]
[int]$Height
)
$panel = [Spectre.Console.Panel]::new($Data)
if ($Title) {
$panel.Header = [Spectre.Console.PanelHeader]::new($Title)
}
if ($width) {
$panel.Width = $Width
}
if ($height) {
$panel.Height = $Height
}
$panel.Expand = $Expand
$panel.Border = [Spectre.Console.BoxBorder]::$Border
$panel.BorderStyle = [Spectre.Console.Style]::new(($Color | Convert-ToSpectreColor))
Write-AnsiConsole $panel
}
}
43 changes: 32 additions & 11 deletions PwshSpectreConsole/public/formatting/Format-SpectreTable.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,28 @@ function Format-SpectreTable {
<#
.SYNOPSIS
Formats an array of objects into a Spectre Console table.
.DESCRIPTION
This function takes an array of objects and formats them into a table using the Spectre Console library. The table can be customized with a border style and color.
.PARAMETER Data
The array of objects to be formatted into a table.
.PARAMETER Border
The border style of the table. Default is "Double".
.PARAMETER Color
The color of the table border. Default is the accent color of the script.
.PARAMETER Width
The width of the table.
.PARAMETER HideHeaders
Hides the headers of the table.
.PARAMETER Title
The title of the table.
.EXAMPLE
# This example formats an array of objects into a table with a double border and the accent color of the script.
$data = @(
Expand All @@ -33,31 +42,43 @@ function Format-SpectreTable {
[string] $Border = "Double",
[ValidateSpectreColor()]
[ArgumentCompletionsSpectreColors()]
[string] $Color = $script:AccentColor.ToMarkup()
[string] $Color = $script:AccentColor.ToMarkup(),
[ValidateSet([SpectreConsoleWidth],ErrorMessage = "Value '{0}' is invalid. Cannot exceed console width.")]
[int]$Width,
[switch]$HideHeaders,
[String]$Title
)
begin {
$table = [Spectre.Console.Table]::new()
$table.Border = [Spectre.Console.TableBorder]::$Border
$table.BorderStyle = [Spectre.Console.Style]::new(($Color | Convert-ToSpectreColor))
$headerProcessed = $false
if ($Width) {
$table.Width = $Width
}
if ($HideHeaders) {
$table.ShowHeaders = $false
}
if ($Title) {
$table.Title = [Spectre.Console.TableTitle]::new($Title, [Spectre.Console.Style]::new(($Color | Convert-ToSpectreColor)))
}
}
process {
if(!$headerProcessed) {
$Data[0].psobject.Properties.Name | Foreach-Object {
$table.AddColumn($_) | Out-Null
}

$headerProcessed = $true
}
$Data | Foreach-Object {
$row = @()
$_.psobject.Properties | ForEach-Object {
$row = $_.psobject.Properties | ForEach-Object {
$cell = $_.Value
if ($null -eq $cell) {
$row += [Spectre.Console.Text]::new("")
[Spectre.Console.Text]::new("")
}
else {
$row += [Spectre.Console.Text]::new($cell.ToString())
[Spectre.Console.Text]::new($cell.ToString())
}
}
$table = [Spectre.Console.TableExtensions]::AddRow($table, [Spectre.Console.Text[]]$row)
Expand All @@ -66,4 +87,4 @@ function Format-SpectreTable {
end {
Write-AnsiConsole $table
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function Invoke-SpectreCommandWithStatus {
.PARAMETER Spinner
The type of spinner to display. Valid values are "dots", "dots2", "dots3", "dots4", "dots5", "dots6", "dots7", "dots8", "dots9", "dots10", "dots11", "dots12", "line", "line2", "pipe", "simpleDots", "simpleDotsScrolling", "star", "star2", "flip", "hamburger", "growVertical", "growHorizontal", "balloon", "balloon2", "noise", "bounce", "boxBounce", "boxBounce2", "triangle", "arc", "circle", "squareCorners", "circleQuarters", "circleHalves", "squish", "toggle", "toggle2", "toggle3", "toggle4", "toggle5", "toggle6", "toggle7", "toggle8", "toggle9", "toggle10", "toggle11", "toggle12", "toggle13", "arrow", "arrow2", "arrow3", "bouncingBar", "bouncingBall", "smiley", "monkey", "hearts", "clock", "earth", "moon", "runner", "pong", "shark", "dqpb", "weather", "christmas", "grenade", "point", "layer", "betaWave", "pulse", "noise2", "gradient", "christmasTree", "santa", "box", "simpleDotsDown", "ballotBox", "checkbox", "radioButton", "spinner", "lineSpinner", "lineSpinner2", "pipeSpinner", "simpleDotsSpinner", "ballSpinner", "balloonSpinner", "noiseSpinner", "bouncingBarSpinner", "smileySpinner", "monkeySpinner", "heartsSpinner", "clockSpinner", "earthSpinner", "moonSpinner", "auto", "random".
.PARAMETER Title
The title to display above the spinner.
Expand All @@ -36,8 +36,11 @@ function Invoke-SpectreCommandWithStatus {
[ArgumentCompletionsSpectreColors()]
[string] $Color = $script:AccentColor.ToMarkup()
)
Start-AnsiConsoleStatus -Title $Title `
-Spinner ([Spectre.Console.Spinner+Known]::$Spinner) `
-SpinnerStyle ([Spectre.Console.Style]::new(($Color | Convert-ToSpectreColor))) `
-ScriptBlock $ScriptBlock
}
$splat = @{
Title = $Title
Spinner = [Spectre.Console.Spinner+Known]::$Spinner
SpinnerStyle = [Spectre.Console.Style]::new(($Color | Convert-ToSpectreColor))
ScriptBlock = $ScriptBlock
}
Start-AnsiConsoleStatus @splat
}
20 changes: 10 additions & 10 deletions PwshSpectreConsole/public/prompts/Read-SpectreMultiSelection.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function Read-SpectreMultiSelection {
[string] $Color = $script:AccentColor.ToMarkup(),
[int] $PageSize = 5
)
$prompt = [Spectre.Console.MultiSelectionPrompt[string]]::new()
$Spectreprompt = [Spectre.Console.MultiSelectionPrompt[string]]::new()

$choiceLabels = $Choices
if($ChoiceLabelProperty) {
Expand All @@ -50,18 +50,18 @@ function Read-SpectreMultiSelection {
exit 2
}

$prompt = [Spectre.Console.MultiSelectionPromptExtensions]::AddChoices($prompt, [string[]]$choiceLabels)
$prompt.Title = $Title
$prompt.PageSize = $PageSize
$prompt.WrapAround = $true
$prompt.HighlightStyle = [Spectre.Console.Style]::new(($Color | Convert-ToSpectreColor))
$prompt.InstructionsText = "[$($script:DefaultValueColor.ToMarkup())](Press [$($script:AccentColor.ToMarkup())]space[/] to toggle a choice and press [$($script:AccentColor.ToMarkup())]<enter>[/] to submit your answer)[/]"
$prompt.MoreChoicesText = "[$($script:DefaultValueColor.ToMarkup())](Move up and down to reveal more choices)[/]"
$selected = Invoke-SpectrePromptAsync -Prompt $prompt
$Spectreprompt = [Spectre.Console.MultiSelectionPromptExtensions]::AddChoices($Spectreprompt, [string[]]$choiceLabels)
$Spectreprompt.Title = $Title
$Spectreprompt.PageSize = $PageSize
$Spectreprompt.WrapAround = $true
$Spectreprompt.HighlightStyle = [Spectre.Console.Style]::new(($Color | Convert-ToSpectreColor))
$Spectreprompt.InstructionsText = "[$($script:DefaultValueColor.ToMarkup())](Press [$($script:AccentColor.ToMarkup())]space[/] to toggle a choice and press [$($script:AccentColor.ToMarkup())]<enter>[/] to submit your answer)[/]"
$Spectreprompt.MoreChoicesText = "[$($script:DefaultValueColor.ToMarkup())](Move up and down to reveal more choices)[/]"
$selected = Invoke-SpectrePromptAsync -Prompt $Spectreprompt

if($ChoiceLabelProperty) {
$selected = $Choices | Where-Object -Property $ChoiceLabelProperty -Eq $selected
}

return $selected
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function Read-SpectreMultiSelectionGrouped {
[string] $Color = $script:AccentColor.ToMarkup(),
[int] $PageSize = 10
)
$prompt = [Spectre.Console.MultiSelectionPrompt[string]]::new()
$Spectreprompt = [Spectre.Console.MultiSelectionPrompt[string]]::new()

$choiceLabels = $Choices.Choices
if($ChoiceLabelProperty) {
Expand All @@ -72,20 +72,20 @@ function Read-SpectreMultiSelectionGrouped {
if($ChoiceLabelProperty) {
$choiceLabels = $Choices | Select-Object -ExpandProperty $ChoiceLabelProperty
}
$prompt = [Spectre.Console.MultiSelectionPromptExtensions]::AddChoiceGroup($prompt, $group.Name, [string[]]$choiceLabels)
$Spectreprompt = [Spectre.Console.MultiSelectionPromptExtensions]::AddChoiceGroup($Spectreprompt, $group.Name, [string[]]$choiceLabels)
}

$prompt.Title = $Title
$prompt.PageSize = $PageSize
$prompt.WrapAround = $true
$prompt.HighlightStyle = [Spectre.Console.Style]::new(($Color | Convert-ToSpectreColor))
$prompt.InstructionsText = "[$($script:DefaultValueColor.ToMarkup())](Press [$($script:AccentColor.ToMarkup())]space[/] to toggle a choice and press [$($script:AccentColor.ToMarkup())]<enter>[/] to submit your answer)[/]"
$prompt.MoreChoicesText = "[$($script:DefaultValueColor.ToMarkup())](Move up and down to reveal more choices)[/]"
$selected = Invoke-SpectrePromptAsync -Prompt $prompt
$Spectreprompt.Title = $Title
$Spectreprompt.PageSize = $PageSize
$Spectreprompt.WrapAround = $true
$Spectreprompt.HighlightStyle = [Spectre.Console.Style]::new(($Color | Convert-ToSpectreColor))
$Spectreprompt.InstructionsText = "[$($script:DefaultValueColor.ToMarkup())](Press [$($script:AccentColor.ToMarkup())]space[/] to toggle a choice and press [$($script:AccentColor.ToMarkup())]<enter>[/] to submit your answer)[/]"
$Spectreprompt.MoreChoicesText = "[$($script:DefaultValueColor.ToMarkup())](Move up and down to reveal more choices)[/]"
$selected = Invoke-SpectrePromptAsync -Prompt $Spectreprompt

if($ChoiceLabelProperty) {
$selected = $Choices | Where-Object -Property $ChoiceLabelProperty -Eq $selected
}

return $selected
}
}
Loading

0 comments on commit a9cb133

Please sign in to comment.