PowerShell snippet: Get-Choice

You know when you want the user to make a choice, and you want him to type it out and get it right?

class Choice {    
    [string] $Key;
    [string] $Option;
    Choice($Key, $Option) {
        $this.Key = $Key
        $this.Option = $Option
    }
}
<# 
 .Synopsis
  Read userinput and validate against choices
 .Description
  Read userinput and validate against choices. Must match one of the keys. Not case sensitive.
 .Parameter Prompt
  Text to show when waiting for input
 .Parameter Choices
  List of Choice elements
 .Parameter NoHints
  Don't show list of hints for Choices
 .Example
  $MyChoices = @(
      [Choice]::new('yes', 'Let me continue'), 
      [Choice]::new('no', 'I want to stop')
  )
  $key = Get-Choice -Prompt "Do you want to continue?" -Choices $MyChoices
#>
function Get-Choice {
    #    [OutputType([string])]
    param (
        [string]$Prompt,
        [Choice[]]$Choices,
        [switch]$NoHints
    )
    $Hints = ""
    $Comma = ""
    $Opts = ""
    $Sep = ""
    $Choices | ForEach-Object {
        $Hints = $Hints + $Comma + '[' + $_.Key + '] ' + $_.Option
        $Comma = ', '
        $Opts = $Opts + $Sep + $_.Key 
        $Sep = '|'
    }
    if (-not $NoHints) {
        Write-Host $Hints
    }
    $pattern = $Opts.ToLower()
    $pattern = "¤¤$pattern¤¤" -replace "\|", "¤¤|¤¤"
    $ok = $false
    while (-not $ok) {
        [string]$userinput = (Read-Host -Prompt "$Prompt ($Opts)").ToLower()
        $userinput = ("¤¤$userinput¤¤" | Select-String -Pattern $pattern) -replace "¤¤" , ""
        $ok = ("$userinput" -ne "")
    }
    return $userinput
} # Get-Choice

PowerShell snippet: Show-TextFile

You know those times that you need to show a log file or some text file?

<# 
 .Synopsis
  Open NotePad with the specified filename
 .Description
  Open NotePad with the specified filename
 .Parameter FileName
  Name of text file to open
 .Parameter ScrollToEnd
  Scroll to the bottom of the opened file
 .Parameter Wait
  Wait for Notepad to close before continuing script
 .Parameter Kill 
  Kill Notepad after Timeout -Requires Wait
 .Parameter TimeOut 
  Timeout period in seconds - default = 300 - Requires Wait
 .Example
  Show-TextFile -FileName 'C:\Some path\folder\filename.txt' -ScrollToEnd -Wait -Kill -TimeOut 30 
#>
function Show-TextFile {
    param (
        [string]$FileName,
        [switch]$ScrollToEnd,
        [switch]$Wait,
        [switch]$Kill,
        [int]$TimeOut = 300
    )
    if (Test-Path -Path $FileName) {
        $proc = Start-Process -FilePath 'NotePad.exe' -ArgumentList "$FileName" -NoNewWindow -PassThru
        if ($ScrollToEnd) { 
            # Use VBScript to send Ctrl-End keystroke to Notepad
            $wshell = New-Object -ComObject wscript.shell
            $title = Split-Path -path $FileName -Leaf
            Start-Sleep -seconds 0.5
            $wshell.AppActivate($title) | Out-Null
            $wshell.SendKeys('^{END}') | Out-Null
        }
        if ($Wait) {
            Write-Host "To continue, close Notepad for '$FileName'"
            if (-not (Wait-Process -InputObject $proc -Timeout $TimeOut -ErrorAction SilentlyContinue)) {
                if (-not $Proc.HasExited) {
                    if ($Kill) {
                        Write-Host "Closed Notepad for $FileName"
                        Stop-Process -InputObject $proc -Force
                    } }
            }
        }
    }
    else {
        Write-Warning "Show-TextFile -FileName not found: $FileName"
    }
} # Show-TextFile

Embarcadero Launches LearnDelphi.org, a Delphi-Centric Learning Ecosystem, to Promote Delphi Education

LearnDelphi.org provides a platform for Delphi developers and educators, along with free licensing options and ways to get involved. Embarcadero (a division of Idera, Inc.), a provider of cross-platform app development productivity tools, today announced the launch of LearnDelphi.org, a learning ecosystem for Delphi developers, a teaching platform for Delphi educators, and a key destination for all things related to the Delphi Community.LearnDelphi.org is a collection of resources and free content for teaching and learning software development curated by and for Delphi developers. The site includes Delphi licensing options and resources such as ebooks, slideshows, and video tutorials, as well as ways to get involved. The content is compiled and supported by Embarcadero’s MVPs, community members, subject matter experts, and employees, and will be available in multiple languages over time. Potential contributors, educational liaisons, and educators are always welcome and encouraged to get involved.

Source: Embarcadero Launches LearnDelphi.org, a Delphi-Centric Learning Ecosystem, to Promote Delphi Education

Google Sheets does Translations

Although it usually pays off to pay for professional translation, this can be nice for creating an initial draft, at least.

Translations by =GOOGLETRANSLATE functions

=GOOGLETRANSLATE(<source text>, “from language”, “to language”) where languages are encoded as the ISO two-letter language codes. You can also use “auto” as a language.

There is also a =DETECTLANGUAGE function that can be used to identify the language used in a range of cells.

RAD Studio 10.3 Rio – Release 3 is out

10.3.3 is the last expected release before 10.4, planned for some point in first half of 2020, and bring us

  • 64-bit Android
  • iOS 13
  • MacOS Catalina (Delphi)
  • Enterprise Connectors (Enterprise and Architect SKUs)
  • Numerous fixes to VCL, RTL and IDE
  • 180+ user reported issues

What’s new: 10.3 Rio – Release 3 – RAD Studio
ISO: http://cc.embarcadero.com/item/30896
Web Installer: http://cc.embarcadero.com/item/30893

Dalija Prasnikar have some useful observations on her blog.