Skip to content

Commit

Permalink
added muh files
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewPla committed May 3, 2023
1 parent db435f4 commit 6478268
Show file tree
Hide file tree
Showing 14 changed files with 574 additions and 0 deletions.
19 changes: 19 additions & 0 deletions andrew-pla-cross-platform-tuis/1 - Basics/CrossPlatformTUIs.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# How to acquire? We need a couple .dlls to get started quick, install module
Install-Module Microsoft.PowerShell.ConsoleGuiTools
Install-Module TerminalGuiDesigner


# Load Assembly
Import-Module Microsoft.PowerShell.ConsoleGuiTools
$module = (Get-Module Microsoft.PowerShell.ConsoleGuiTools -List).ModuleBase
Add-Type -Path (Join-path $module Terminal.Gui.dll)

# Must initialize the top level application
[Terminal.Gui.Application]::Init()


# Use this code in VS Code to allow Ctrl + Q to pass to the terminal from
# https://github.com/microsoft/vscode/issues/108130
"terminal.integrated.commandsToSkipShell": [
"-workbench.action.quickOpenView"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
Install-Module Microsoft.PowerShell.ConsoleGuiTools

#region Quick tool to pick a new song to play
Get-ChildItem $Home\Music\ -Recurse -Filter *.mp3 |
Out-ConsoleGridView -OutputMode Single |
Invoke-Item
#endregion

#region Get some help, you probably need it
Get-Help about* |
Select-Object Name |
Out-ConsoleGridView -Title 'Plz get help' -OutputMode Single |
Get-Help
#endregion

#region filter processes to stop
Get-Process | Out-ConsoleGridView -PassThru | Stop-process -WhatIf
#endregion

#region f7 view history - Shift+f7 psreadlinehistory
function ocgv_history {
param(
[parameter(Mandatory = $true)]
[Boolean]
$global
)

$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
if ($global) {
# Global history
$history = [Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems().CommandLine
# reverse the items so most recent is on top
[array]::Reverse($history)
$selection = $history | Select-Object -Unique | Out-ConsoleGridView -OutputMode Single -Filter $line -Title "Global Command Line History"

}
else {
# Local history
$history = Get-History | Sort-Object -Descending -Property Id -Unique | Select-Object CommandLine -ExpandProperty CommandLine
$selection = $history | Out-ConsoleGridView -OutputMode Single -Filter $line -Title "Command Line History"
}

if ($selection) {
[Microsoft.PowerShell.PSConsoleReadLine]::DeleteLine()
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($selection)
if ($selection.StartsWith($line)) {
[Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($cursor)
}
else {
[Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($selection.Length)
}
}
}

# When F7 is pressed, show the local command line history in OCGV
$parameters = @{
Key = 'F7'
BriefDescription = 'Show Matching History'
LongDescription = 'Show Matching History using Out-ConsoleGridView'
ScriptBlock = {
ocgv_history -Global $false
}
}
Set-PSReadLineKeyHandler @parameters

# When Shift-F7 is pressed, show the local command line history in OCGV
$parameters = @{
Key = 'Shift-F7'
BriefDescription = 'Show Matching Global History'
LongDescription = 'Show Matching History for all PowerShell instances using Out-ConsoleGridView'
ScriptBlock = {
ocgv_history -Global $true
}
}
Set-PSReadLineKeyHandler @parameters

#endregion
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# https://blog.ironmansoftware.com/daily-powershell/powershell-out-tree-view/
Import-Module Microsoft.PowerShell.ConsoleGuiTools
$module = (Get-Module Microsoft.PowerShell.ConsoleGuiTools -List).ModuleBase
Add-Type -Path (Join-path $module Terminal.Gui.dll)
function Expand-Object {
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[object]$InputObject
)

Process {
if ($InputObject -eq $null) {
return
}
$InputObject | Get-Member -MemberType Properties | ForEach-Object {
try {
$Value = $InputObject.($_.Name)
$Node = [Terminal.Gui.Trees.TreeNode]::new("$($_.Name) = $Value")

if ($Value -ne $null) {
$Children = Expand-Object -InputObject $Value
foreach ($child in $Children) {
$Node.Children.Add($child)
}
}

$Node
}
catch {
Write-Host $_
}
}

}
}

function Out-TreeView {
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[object]$InputObject
)

Begin {
$Objects = @()
}

Process {
$Objects += $InputObject
}

End {
[Terminal.Gui.Application]::Init()
$Top = [Terminal.Gui.Application]::Top

$Win = [Terminal.Gui.Window]::new("Out-TreeView")
$Win.Height = [Terminal.Gui.Dim]::Fill()
$Win.Width = [Terminal.Gui.Dim]::Fill()

$TreeView = [Terminal.Gui.TreeView]::new()
$TreeView.Height = [Terminal.Gui.Dim]::Fill()
$TreeView.Width = [Terminal.Gui.Dim]::Fill()

foreach ($item in $Objects) {
$root = [Terminal.Gui.Trees.TreeNode]::new($item.GetType().Name)
$Children = Expand-Object $item
$Children | ForEach-Object {
$root.Children.Add($_)
}
$TreeView.AddObject($root)
}

$Win.Add($TreeView)

$Top.Add($Win)

[Terminal.Gui.Application]::Run()
[Terminal.Gui.Application]::Shutdown()
}
}
$Podcasts = Invoke-RestMethod 'https://feed.podbean.com/powershellpodcast/feed.xml'
$Podcasts | Out-TreeView
28 changes: 28 additions & 0 deletions andrew-pla-cross-platform-tuis/1 - Basics/starter.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using namespace Terminal.Gui

# We can get the required DLLs from
if (-not $(Get-Module Microsoft.PowerShell.ConsoleGuiTools -ListAvailable)) {
Install-Module Microsoft.PowerShell.ConsoleGuiTools
}

Import-Module Microsoft.PowerShell.ConsoleGuiTools
$module = (Get-Module Microsoft.PowerShell.ConsoleGuiTools -List).ModuleBase
Add-Type -Path (Join-path $module Terminal.Gui.dll)

# Initialize the application
[Application]::Init()

# Create the window to use
$Window = [Window]::New()
$Window.Title = "Window Title"

# put your code here
# put your code here



[Application]::Top.Add($Window)
[Application]::Run()

# This makes it so it actually closes
[Application]::Shutdown()
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using namespace Terminal.Gui

# We can get the required DLLs from
if (-not $(Get-Module Microsoft.PowerShell.ConsoleGuiTools -ListAvailable)) {
Install-Module Microsoft.PowerShell.ConsoleGuiTools
}

Import-Module Microsoft.PowerShell.ConsoleGuiTools
$module = (Get-Module Microsoft.PowerShell.ConsoleGuiTools -List).ModuleBase
Add-Type -Path (Join-path $module Terminal.Gui.dll)

# Initialize the application
[Application]::Init()

# Create the window to use
$Window = [Window]::New()
$Window.Title = "Window Title"
$Label = [Terminal.Gui.Label]::new()
$Label.Width = 10
$Label.Height = 1
$Window.Add($Label)

$Window.add_KeyPress({ param($arg) $Label.Text = "You Pressed $($arg.KeyEvent.Key.ToString())" })


[Application]::Top.Add($Window)
[Application]::Run()

# This makes it so it actually closes
[Application]::Shutdown()
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using namespace Terminal.Gui

# We can get the required DLLs from
if (-not $(Get-Module Microsoft.PowerShell.ConsoleGuiTools -ListAvailable)) {
Install-Module Microsoft.PowerShell.ConsoleGuiTools
}

Import-Module Microsoft.PowerShell.ConsoleGuiTools
$module = (Get-Module Microsoft.PowerShell.ConsoleGuiTools -List).ModuleBase
Add-Type -Path (Join-path $module Terminal.Gui.dll)

# Initialize the application
[Application]::Init()

# Create the window to use
$Window = [Window]::New()
$Window.Title = "Window Title"

$Label = [Terminal.Gui.Label]::new()
$Label.Text = "0"
$Label.Height = 1
$Label.Width = 20
$Window.Add($Label)

$Button = [Button]::new()
$Button.X = [Pos]::Right($Label)
$Button.Text = "Start Job"
$Button.add_Clicked({
Start-ThreadJob {
$bgLabel = $args[0]
write-host $bgLabel
1..100 | ForEach-Object {
$Item = $_
[Application]::MainLoop.Invoke({ $bgLabel.Text = $Item.ToString() })
Start-Sleep -Milliseconds 1000
}

} -ArgumentList $Label
})

$Window.Add($Button)

$Button2 = [Button]::new()
$Button2.X = [Pos]::Right($Button)
$Button2.Text = "Do I work?"
$Button2.add_Clicked({
[MessageBox]::Query("Still workin'", "")
})

$Window.Add($Button2)

[Application]::Top.Add($Window)
[Application]::Run()

# This makes it so it actually closes
[Application]::Shutdown()
29 changes: 29 additions & 0 deletions andrew-pla-cross-platform-tuis/2 - views and overview/Dialogs.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Getting Started
using namespace Terminal.Gui
# This lets us reference types by their short name and makes things easier to read

# We can get the required DLLs from
if (-not $(Get-Module Microsoft.PowerShell.ConsoleGuiTools -ListAvailable)) {
Install-Module Microsoft.PowerShell.ConsoleGuiTools
}

Import-Module Microsoft.PowerShell.ConsoleGuiTools
$module = (Get-Module Microsoft.PowerShell.ConsoleGuiTools -List).ModuleBase
Add-Type -Path (Join-path $module Terminal.Gui.dll)

# Initialize the application
[Application]::Init()

# Create the window to use
#$Window = [Window]::New()
#$Window.Title = "Pizza Question"


[MessageBox]::Query("Hello", "Ctrl + Q to close")


#[Application]::Top.Add($Window)
[Application]::Run()

# This makes it so it actually closes
[Application]::Shutdown()
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using namespace Terminal.Gui

# We can get the required DLLs from
if (-not $(Get-Module Microsoft.PowerShell.ConsoleGuiTools -ListAvailable)) {
Install-Module Microsoft.PowerShell.ConsoleGuiTools
}

Import-Module Microsoft.PowerShell.ConsoleGuiTools
$module = (Get-Module Microsoft.PowerShell.ConsoleGuiTools -List).ModuleBase
Add-Type -Path (Join-path $module Terminal.Gui.dll)

# Initialize the application
[Application]::Init()

# Create the window to use
$Window = [Window]::New()
$Window.Title = "Window Title"

$Dialog = [OpenDialog]::new("Open Powershell Script", "")
$Dialog.CanChooseDirectories = $false
$Dialog.CanChooseFiles = $true
$Dialog.AllowsMultipleSelection = $false

$Dialog.DirectoryPath = "$Home\Documents\PowerShell\Scripts"
$Dialog.AllowedFileTypes = @(".ps1")
[Application]::Run($Dialog)

# This makes it so it actually closes
[Application]::Shutdown()

# We must call the .tostring() method to work with this type
Write-Host "you selected $($dialog.FilePath.ToString())"
#Code "$($dialog.FilePath.ToString())"
Loading

0 comments on commit 6478268

Please sign in to comment.