Skip to content

Commit

Permalink
Fix write-host rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaunLawrie committed Sep 4, 2024
1 parent 4101c87 commit 5f30092
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 28 deletions.
13 changes: 6 additions & 7 deletions PwshSpectreConsole.Tests/writing/Write-SpectreHost.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,22 @@ Describe "Write-SpectreHost" {
$testMessage | Out-Null
Mock Write-SpectreHostInternalMarkup {
$Message | Should -Be $testMessage
[Spectre.Console.AnsiConsoleExtensions]::Markup($testConsole, $Message)
}
Mock Write-SpectreHostInternalMarkupLine {
$Message | Should -Be $testMessage
[Spectre.Console.AnsiConsoleExtensions]::MarkupLine($testConsole, $Message)
if (-not $NoNewline) {
$Message = $Message.TrimEnd() + "`n"
}
$testConsole.Write([Spectre.Console.Markup]::new($Message))
}
}

It "writes a message" {
Write-SpectreHost -Message $testMessage
Assert-MockCalled -CommandName "Write-SpectreHostInternalMarkupLine" -Times 1 -Exactly
Assert-MockCalled -CommandName "Write-SpectreHostInternalMarkup" -Times 1 -Exactly
$testConsole.Output.Split("`n").Count | Should -Be 2
}

It "accepts pipeline input" {
$testMessage | Write-SpectreHost
Assert-MockCalled -CommandName "Write-SpectreHostInternalMarkupLine" -Times 1 -Exactly
Assert-MockCalled -CommandName "Write-SpectreHostInternalMarkup" -Times 1 -Exactly
$testConsole.Output.Split("`n").Count | Should -Be 2
}

Expand Down
23 changes: 7 additions & 16 deletions PwshSpectreConsole/private/Write-SpectreHostInternal.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,20 @@ function Write-SpectreHostInternalMarkup {
[Parameter(Mandatory)]
[string] $Message,
[ValidateSet([SpectreConsoleJustify], ErrorMessage = "Value '{0}' is invalid. Try one of: {1}")]
[string]$Justify = "Left",
[string] $Justify = "Left",
[switch] $NoNewline,
[switch] $PassThru
)
$output = [Spectre.Console.Markup]::new($Message)
$output.Justification = [Spectre.Console.Justify]::$Justify
if ($PassThru) {
return $output

# Add a newline character to the end of the message if NoNewline is not specified
if (-not $NoNewline) {
$Message = $Message.TrimEnd() + "`n"
}
[Spectre.Console.AnsiConsole]::Write($output)
}

function Write-SpectreHostInternalMarkupLine {
param (
[Parameter(Mandatory)]
[string] $Message,
[ValidateSet([SpectreConsoleJustify], ErrorMessage = "Value '{0}' is invalid. Try one of: {1}")]
[string]$Justify = "Left",
[switch] $PassThru
)
$output = [Spectre.Console.Markup]::new($Message)
$output.Justification = [Spectre.Console.Justify]::$Justify
if ($PassThru) {
return $output
}
[Spectre.Console.AnsiConsole]::WriteLine($output)
[Spectre.Console.AnsiConsole]::Write($output)
}
6 changes: 1 addition & 5 deletions PwshSpectreConsole/public/writing/Write-SpectreHost.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,5 @@ function Write-SpectreHost {
[ValidateSet([SpectreConsoleJustify], ErrorMessage = "Value '{0}' is invalid. Try one of: {1}")]
[string]$Justify = "Left"
)
if ($NoNewline) {
return Write-SpectreHostInternalMarkup $Message -Justify $Justify -PassThru:$PassThru
} else {
return Write-SpectreHostInternalMarkupLine $Message -Justify $Justify -PassThru:$PassThru
}
return Write-SpectreHostInternalMarkup $Message -Justify $Justify -PassThru:$PassThru -NoNewline:$NoNewline
}

0 comments on commit 5f30092

Please sign in to comment.