Lib/git_helper.psm1

<#
    .SYNOPSIS
        Git Helper
 
    .DESCRIPTION
        Git Helper
 
    .NOTES
        Author : hillesheim@n-dimensions.de
        Version : 2.5
        DateLastChanged : 2023-09-16
#>
 

Write-Host "Load module 'git_helper' ... "; 

#region begin DECLARATION
    
    #region begin TYPES
        
    #endregion

    #region begin USER_VARIABLES
        
    #endregion

    #region begin SYSTEM_VARIABLES
        
    #endregion

    #endregion

#region begin FUNCTIONS
    
Function Enter-Selection {
    Param(
        $Enum, 
        [Int[]] $Default = @(), 
        [String] $Title = "PLEASE SELECT"
    )
    
    $rtrVal = @(); 
    [Int[]] $selectedNumbers0 = @(); 
    
    $PresentUi = {
        Param(
            $Enum, 
            [Int[]] $Default = @()
        )

        [Int[]] $selectedNumbers1 = @(); 
        
        # Compute ui
        [String] $ui = "`n<header_line> `n<title> `n<header_line> `n`n<list_of_choices> <explanation>"; 
        [String] $headerLine = "*" * 70; 
        # [String] $prompt = "PLEASE SELECT";
        
        [String] $listOfChoices = ""; 
        [Int] $enumLength = [System.Enum]::GetValues($Enum).Length; 
        For ( $i = 1; $i -lt $enumLength; $i++ ){
            $listOfChoices += "[{0}] {1}" -f $i.ToString(), $Enum.GetEnumName($i) + "`n"; 
        }

        [String] $explanation = ""; 
        $explanation += "`n- Input a single number "; 
        $explanation += "or `n- Input a list of numbers separated by ',' "; 
        If($Default.Length -gt 0){
            $explanation += "or `n- Press ENTER for default ({0})" -f ($Default -join ",");
        }
        
        $ui = $ui.Replace("<header_line>", $headerLine); 
        $ui = $ui.Replace("<title>", $Title); 
        $ui = $ui.Replace("<list_of_choices>", $listOfChoices); 
        $ui = $ui.Replace("<explanation>", $explanation); 
        
        $userChoice = Read-Host -Prompt $ui; 
        
        # If null, apply default
        If ( ($userChoice -eq "") -and ($Default.Length -gt 0) ){
            $selectedNumbers1 = $Default; 
        }

        # If invalid, apply 0
        If ( ($userChoice -eq "") -and ($Default.Length -eq 0) ){
            $selectedNumbers1 = @(0); 
        }
    
        # Check input; try parse
        Try {
            If ( $userChoice.Length -gt 0){
                [String[]] $stringParts = $userChoice.Split(","); 
            }

            ForEach ($item in $stringParts) {
                $item = $item.Trim(); 
                [Int] $number = -1; 
                
                # Check type
                [Boolean] $success = [Int]::TryParse($item, [ref] $number); 
                If (-not $success){
                    $number = 0; 
                }

                # Check range
                If(-not ($number -ge 1 -and $number -lt $enumLength )) {
                    $number = 0; 
                } 

                # Check duplicate
                If(-not ($selectedNumbers1 -contains $number )){
                    $selectedNumbers1 += $number; 
                }

            }
        } Catch {} 

        # Return
        Write-Output $selectedNumbers1; 
    } 
    
    # Run
    [Int[]] $selectedNumbers0 = $PresentUi.Invoke(@($Enum,$Default)); 
       
    # Re-run
    If($selectedNumbers0 -contains 0){
        $rerun = Read-Host ( "Your selection contained {0}. Re-run (y|n). Press ENTER for 'n'?" -f [Enum]::ToObject($Enum, 0))    
        If($rerun -eq "" -or $null -eq $rerun -or $rerun.ToLower() -eq "y"){
            [Int[]] $selectedNumbers0 = $PresentUi.Invoke(@($Enum,$Default)); 
        }
    }

    $selectedNumbers0 `
        | ForEach-Object {
            $selection = [Enum]::ToObject($Enum, $_); 
            $rtrVal += $Enum::$selection; 
        }
    
    Write-Output $rtrVal;    
}

Function Open-ScriptEditor { 
    Param(
        [String] $Filepath 
    )

    [String] $fileName = (Get-Item -Path $Filepath).Name; 
    
    Write-Host ("Edit script '{0}' ... " -f $fileName);  
    notepad "$Filepath";     
    
}

#endregion