question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Minimal PowerShell Sample

See original GitHub issue
using namespace Terminal.Gui

# Load the Terminal.Gui assembly and its dependencies (assumed to be in the
# the same directory).
# NOTE: `using assmembly <path>` seemingly only works with full, literal paths
# as of PowerShell Core 7.1.0-preview.7.
# The assumption here is that all relevant DLLs are stored in subfolder
# assemblies/bin/Release/*/publish of the script directory, as shown in 
#   https://stackoverflow.com/a/50004706/45375
#Add-Type -Path $PSScriptRoot/assemblies/bin/Release/*/publish/Terminal.Gui.dll

# Initialize the "GUI".
# Note: This must come before creating windows and controls.
[Application]::Init()

$win = [Window] @{
  Title = 'Hello World'
}

$edt = [TextField] @{
    X = [Pos]::Center()
    Y = [Pos]::Center()
    Width = 20
    Text = 'This text will be returned'
}
$win.Add($edt)

$btn = [Button] @{
  X = [Pos]::Center()
  Y = [Pos]::Center() + 1
  Text = 'Quit'
}
$win.Add($btn)
[Application]::Top.Add($win)

# Attach an event handler to the button.
# Note: Register-ObjectEvent -Action is NOT an option, because
# the [Application]::Run() method used to display the window is blocking.
$btn.add_Clicked({
  # Close the modal window.
  # This call is also necessary to stop printing garbage in response to mouse
  # movements later.
  [Application]::RequestStop()
})

# Show the window (takes over the whole screen). 
# Note: This is a blocking call.
[Application]::Run()

# As of 1.0.0-pre.4, at least on macOS, the following two statements
# are necessary on in order for the terminal to behave properly again.
[Application]::Shutdown() # Clears the screen too; required for being able to rerun the application in the same session.
#tput reset # To make PSReadLine work properly again, notably up- and down-arrow.
$edt.Text.ToString()

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:25

github_iconTop GitHub Comments

2reactions
ca0abinarycommented, Feb 7, 2023

I wrote a slight modification of your example which installs the ConsoleGuiTools module to help make life easier.

using namespace Terminal.Gui

if (!$(Get-Module Microsoft.PowerShell.ConsoleGuiTools)) {
  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)

[Application]::Init()

$topLevel = [TopLevel]@{}

$topLevel.Add([StatusBar]@{ 
  Visible = $true
  Items = @(
    [StatusItem]::new([int][Key]("CtrlMask") -bor [int][Key]("Q"), '~CTRL-Q~ Quit', {
      [Application]::RequestStop()
    })
  )
})

$dialog = [Dialog]@{
  X = [Pos]::Center()
  Y = [Pos]::Center()
  Title = 'Dialog'
}

$text = [TextField]@{
  X = [Pos]::Center()
  Y = [Pos]::Center()
  Width = 20
  Text = 'Howdy!'
}
$dialog.Add($text)

$quit = [Button]@{
  X = [Pos]::Center()
  Y = [Pos]::Center() + 1
  Text = 'Quit'
}
$quit.add_Clicked({ [Application]::RequestStop() })
$dialog.Add($quit)

$topLevel.Add($dialog)

[Application]::Run($topLevel)
[Application]::Shutdown()

Write-Output "Got ""$($text.Text.ToString())"" from user input"

edit 1: I struggled with status bar items and the Key class being not CLS compliant. Updated this example to show my workaround.

2reactions
ca0abinarycommented, Feb 7, 2023

Thanks for a really nice example! There are others out there, but I like how cleanly this one is implemented.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Minimal PowerShell Sample #945 - gui-cs/Terminal.Gui
PowerShell script : broken in both Terminal.app and iTerm.app, though with different symptoms: Terminal.app: broken (doesn't seem to be aware of ...
Read more >
Develop Minimal Footprint with PowerShell and Features ...
This post presents a sample process and operations for developing a minimal server footprint by using Features on Demand with Windows ...
Read more >
minimal-windows-server.json 1.0.133
{{ template_dir }}/scripts/Windows/create_appveyor_user.ps1" ], "environment_vars": [ ... {{ template_dir }}/iso/minimal-windows-server.iso" ] ...
Read more >
Using Experimental Features in PowerShell
The following examples use the TestExe.exe tool. You can build TestExe from the source code. See TestExe in the PowerShell source repository.
Read more >
How to Write a PowerShell Module Manifest
For an example of a default module manifest, see the Sample module ... Type: Version, <empty string>, Minimum version of the PowerShell ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found