Skip to content

Commit

Permalink
Update example text
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaunLawrie committed Sep 2, 2024
1 parent cd451e6 commit 20dcaac
Show file tree
Hide file tree
Showing 71 changed files with 1,038 additions and 607 deletions.
28 changes: 14 additions & 14 deletions PwshSpectreConsole.Docs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ title: Invoke-SpectreLive

import Asciinema from '../../../../components/Asciinema.astro'
import invokespectreliveExample1 from '../../../../assets/examples/invokespectreliveExample1.cast?url'
import invokespectreliveExample2 from '../../../../assets/examples/invokespectreliveExample2.cast?url'

### Description

Starts live rendering for a given renderable. The script block is able to update the renderable in real-time and Spectre Console redraws every time the scriptblock calls $Context.refresh().
Starts live rendering for a given renderable. The script block is able to update the renderable in real-time and Spectre Console redraws every time the scriptblock calls `$Context.refresh()`.
See https://spectreconsole.net/live/live-display for more information.


Expand All @@ -26,7 +27,12 @@ See https://spectreconsole.net/live/live-display for more information.


### Examples
> EXAMPLE 1


**Example 1**
This is a live updating table example, the table will be updated every second with a new row.



```powershell
$data = @(
Expand All @@ -36,7 +42,7 @@ $data = @(
$table = Format-SpectreTable -Data $data
Invoke-SpectreLive -Data $table -ScriptBlock {
param (
$Context
[Spectre.Console.LiveDisplayContext] $Context
)
$Context.refresh()
for ($i = 0; $i -lt 5; $i++) {
Expand All @@ -58,6 +64,138 @@ Invoke-SpectreLive -Data $table -ScriptBlock {
/>


**Example 2**
This is a complex live updating nested layout example. It demonstrates how to create a file browser with a preview panel.
The root layout is constructed with a header and a content panel. The content panel is split into two columns: filelist and preview.
Invoke-SpectreLive is used to render the layout and update the content of each panel on every loop iteration until the escape key is pressed.



```powershell
$layout = New-SpectreLayout -Name "root" -Rows @(
# Row 1
(
New-SpectreLayout -Name "header" -MinimumSize 5 -Ratio 1 -Data ("empty")
),
# Row 2
(
New-SpectreLayout -Name "content" -Ratio 10 -Columns @(
(
New-SpectreLayout -Name "filelist" -Ratio 2 -Data "empty"
),
(
New-SpectreLayout -Name "preview" -Ratio 4 -Data "empty"
)
)
)
)
# Functions for rendering the content of each panel
function Get-TitlePanel {
return "File Browser - Spectre Live Demo [gray]$(Get-Date)[/]" | Format-SpectreAligned -HorizontalAlignment Center -VerticalAlignment Middle | Format-SpectrePanel -Expand
}
function Get-FileListPanel {
param (
$Files,
$SelectedFile
)
$fileList = $Files | ForEach-Object {
$name = $_.Name
if ($_.Name -eq $SelectedFile.Name) {
$name = "[Turquoise2]$($name)[/]"
}
return $name
} | Out-String
return Format-SpectrePanel -Header "[white]File List[/]" -Data $fileList.Trim() -Expand
}
function Get-PreviewPanel {
param (
$SelectedFile
)
$item = Get-Item -Path $SelectedFile.FullName
$result = ""
if ($item -is [System.IO.DirectoryInfo]) {
$result = "[grey]$($SelectedFile.Name) is a directory.[/]"
} else {
try {
$content = Get-Content -Path $item.FullName -Raw -ErrorAction Stop
$result = "[grey]$($content | Get-SpectreEscapedText)[/]"
} catch {
$result = "[red]Error reading file content: $($_.Exception.Message | Get-SpectreEscapedText)[/]"
}
}
return $result | Format-SpectrePanel -Header "[white]Preview[/]" -Expand
}
function Get-LastKeyPressed {
$lastKeyPressed = $null
while ([Console]::KeyAvailable) {
$lastKeyPressed = [Console]::ReadKey($true)
}
#return $lastKeyPressed
}
# Start live rendering the layout
# Type "↓", "↓", "↓" to navigate the file list, and press "Enter" to open a file in Notepad
Invoke-SpectreLive -Data $layout -ScriptBlock {
param (
[Spectre.Console.LiveDisplayContext] $Context
)
# State
$fileList = @(@{Name = ".."; Fullname = ".."}) + (Get-ChildItem)
$selectedFile = $fileList[0]
while ($true) {
# Handle input
$lastKeyPressed = Get-LastKeyPressed
if ($lastKeyPressed -ne $null) {
if ($lastKeyPressed.Key -eq "DownArrow") {
$selectedFile = $fileList[($fileList.IndexOf($selectedFile) + 1) % $fileList.Count]
} elseif ($lastKeyPressed.Key -eq "UpArrow") {
$selectedFile = $fileList[($fileList.IndexOf($selectedFile) - 1 + $fileList.Count) % $fileList.Count]
} elseif ($lastKeyPressed.Key -eq "Enter") {
if ($selectedFile -is [System.IO.DirectoryInfo] -or $selectedFile.Name -eq "..") {
$fileList = @(@{Name = ".."; Fullname = ".."}) + (Get-ChildItem -Path $selectedFile.FullName)
$selectedFile = $fileList[0]
} else {
notepad $selectedFile.FullName
return
}
} elseif ($lastKeyPressed.Key -eq "Escape") {
return
}
}
# Generate new data
$titlePanel = Get-TitlePanel
$fileListPanel = Get-FileListPanel -Files $fileList -SelectedFile $selectedFile
$previewPanel = Get-PreviewPanel -SelectedFile $selectedFile
# Update layout
$layout["header"].Update($titlePanel) | Out-Null
$layout["filelist"].Update($fileListPanel) | Out-Null
$layout["preview"].Update($previewPanel) | Out-Null
# Draw changes
$Context.Refresh()
Start-Sleep -Milliseconds 200
}
}
```
<Asciinema
src={invokespectreliveExample2}
settings={{
loop: false,
terminalLineHeight: 1.1,
theme: "spectre",
fit: "none",
terminalFontFamily: "'Cascadia Code', monospace"
}}
/>


---


Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
47FA5708E7A439C67F515B49C6B9C42515789387868C96BAE9D11840C488242A
305DED202A11D083F7813BE51174847406246001C3A48A3C1E1E850F0F0B6EA2
Original file line number Diff line number Diff line change
@@ -1 +1 @@
427A8A6495A8C26E9ADC05AF2E9067B44D1956F2E1985E8C0CF200C999B9A5E3
41CB2FB4B4843BDED8AE506CDDDA308597BD4CA9E6263413E52B4A217D5D8C5F
Original file line number Diff line number Diff line change
@@ -1 +1 @@
A970B00B3E68ABA93BD26E17A34FB979309718DCCCC36C6CA04E5D6056B0789C
583C9183188A0F3C9A372A6BF3DA81D1EECF5AD92A0BE65AFB25BEA266F49BD6
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4580B9355FDE3FCED723D8CB0C4CCC071B16C4217D2B7E5BBE1272BBEF5509C8
40A4A514A25ADC691D6191E7C7E24A5E9D6A49BC243703ABE46463D7789E0940
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3FCE80C98D8FC58C4AA504165EF2CBDA75D13D0EFF98BD37457B9F4789794F5C
E9495268CDA79E0B655829B712143D13D509DE96A410E71BD107E55775EF5F19
Original file line number Diff line number Diff line change
@@ -1 +1 @@
34B70D99A25CBA4B9C02B9182CFBD24C1562965184B54F90A5199E22687DAB85
39A6E2ED1C5A4D56F12D202609C52BAA9178BDDEC41E490D3C9E85DA3016E43F
Original file line number Diff line number Diff line change
@@ -1 +1 @@
701155B21122522FFFD98CD2F563E8BF53D2A9BFA0B1EF95FF0C05EEA0638D97
B2D0B0806BAF245A7C53D94D9B39E280F6C57D937DE961E79C9FD716BAF51514
Original file line number Diff line number Diff line change
@@ -1 +1 @@
66E190435F9F183A2A12BF728A9C168071749459ACC3AB60D83DC59DE009F2CC
A54F5CA0F1277464F1531F2969340FDB48E8E091A3163F12984B85931720EFAD
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BA0FA4D0BC482223E75875AF7D68C221AFB1D6254079690D5EA86E05358824CB
C98A5C2A366715974B01A09E274A17E5C58C1A3FAAED7EEF9B90CC808F4EBA71
Original file line number Diff line number Diff line change
@@ -1 +1 @@
296C1A03E8850632CEF3903A47308D83FF8B668309EE17C781DB38DEE50CA17A
FBEED7B0E607479EC3F7563F98D8DA765DFE4FBAB5CA447E0BD702255D9C182F
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5D94DD507C52366FB43A9797255BD7EE9D29AA10D7C84667825300C6C52638E5
E7AD75CB66D7486F2382355077377A77FF8CF29235B6AAF1AEA1E37BC63B4A4C
Original file line number Diff line number Diff line change
@@ -1 +1 @@
EB505088F1EE11BCC00B61FA4C8D6C7BD282374F6D0192354728ED02DFF651A3
BDF30DFB7608EAA1532CA23BD7B932E28983C3E68F8B8C4B55C269C5C725A1F4
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ The Write-SpectreRule function writes a Spectre horizontal-rule to the console w


### Examples
> EXAMPLE 1


**Example 1**
This example demonstrates how to write a rule to the console.



```powershell
Write-SpectreRule -Title "My Rule" -Alignment Center -Color Red
Write-SpectreRule -Title "My Rule" -Alignment Center -Color Yellow
```
<Asciinema
src={writespectreruleExample1}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
A0CD3421D86B7EA9CE4FF721F5572E4BB5778E48D4D3784EEE9C52E50B10FFE2
E9A7D2E3DBEF41CC69673ED5B2F5EC099D175A2B7BAFD049A12ED494BF134074
Original file line number Diff line number Diff line change
@@ -1 +1 @@
272C2BD3BD811C22678880F0782ED3CBC71C663515BD7F535D5E59D3AF3451B4
46B1CEC422EB961C0FAACF9327C9B252C6141A4FB932B9058096117AFEEFAE44
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1FDDD3E99CBD6598CAE04F6F843317F345B333762BF756799BAC460AC0A9FDCC
6CFA85F7F4D4B3F80309D058F8CF94297DBAFC63C7C07B3E2A030A68926BD4AD
Original file line number Diff line number Diff line change
@@ -1 +1 @@
A4EEF63DF98536F7516DB7F41D302C17242DFBAE8296F149FE0FC1A83CD99649
3F52F7288D3F3564A5B7C253A18D156A174C698F10A86E5BDCE40110B88237AC
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0DA98AE98DFA8E0D672192F4BBD4FAFA244817B1D46D6B04121C06F411852A9E
6FF5E99E08D903E6C84B4B28C2FFF6E9466EB303F90C99C0C14A1C6956A326BB
Original file line number Diff line number Diff line change
@@ -1 +1 @@
67A0FDD890CAC59DCF71CDF93D40D8CBC3103819236AAA28AA0463174CB640A3
D2D42C87B4B3236586F06CA957FD64111629A7B7B63BC79E4CEBE6FF2DAEA1A7
Loading

0 comments on commit 20dcaac

Please sign in to comment.