Skip to content

Jaykul/TerminalBlocks

Repository files navigation

TerminalBlocks

TerminalBlocks are basically ScriptBlocks with some rendering information for virtual terminals.

When you convert a TerminalBlock to string, it invokes the script and applies foreground and background colors, positioning, alignment, etc.

Right now, I am focusing on prompts, and I have written a handful of functions that output terminal blocks for prompts. The generic one is New-TerminalBlock which literally just takes a ScriptBlock and all the properties of a TerminalBlock and combines them. The rest actually involve some code-generation...

Important: if you're using TerminalBlocks in your prompt, you should use the Initialize-Prompt and Exit-Prompt functions, or set the two "Last" properties at the top of your prompt, and restore them at the end, like this:

    # Initialize-Prompt
    [PoshCode.TerminalBlock]::LastSuccess = $global:?
    [PoshCode.TerminalBlock]::LastExitCode = $global:LASTEXITCODE

    ...

    # Exit-Prompt
    $global:LASTEXITCODE = [PoshCode.TerminalBlock]::LastExitCode

Aspect-oriented function generation

In this module, I have added a Generators/ModuleBuildExtensions, which I intend to ship in ModuleBuilder eventually -- it adds a Merge-Aspect function I can call in my build script, and I use that to combine the functions in the /Generators folder with the functions in the /public folder.

The bottom line is that the build process takes the Show- functions and combines them with Generators/NewTerminalBlock to create a new function where the end block of the show function is the scriptblock in the TerminalBlock. Along the way, you get all the parameters from NewTerminalBlock as common parameters, and you get error handling and tracing from Generators/TracingAndErrorHandling

To see it in action, you need to run ./build.ps1 and then compare the files in source/public with the functions in the output TerminalBlocks.psm1 module.

Currently available blocks:

  • Show-AzureContext - Shows the current Azure context (i.e. (Get-AzContext).Name).
  • Show-DockerContext - shows the current Docker context (i.e. docker context show).
  • Show-KubeContext - shows the current Kubernetes context (i.e. kubectl config current-context).
  • Show-Date - Shows the current date/time.
  • Show-ElapsedTime - shows how long the last command took to run.
  • Show-ErrorCount - shows how many new errors occurred since the last time this block was run.
  • Show-HistoryId - shows the history ID of the next command to run -- you can tab complete that command later by using #nn{Tab}
  • Show-LastExitCode - shows the last exit code when the last native command failed.
  • Show-Path - shows the current working directory. There are a lot of options for shortening the path!
  • Show-PoshGitStatus - shows your git status (using posh-git module).

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published