Powershell GUI fronted (WPF) to run categorized console scripts

⌈⌋ branch:  ClickyColoury


Check-in [891bc77e0e]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Slimmer popup for Ask-Gui/Read-Host. Add type:filechooser support for input boxes. Move widget/value extraction out of [Run] callback.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:891bc77e0e78b84a33d4ebc5f42d1b08bc00287c
User & Date: mario 2018-04-12 19:42:10
Context
2018-04-12
19:42
new PS-style icon check-in: 6b5ea13e6b user: mario tags: trunk
19:42
Slimmer popup for Ask-Gui/Read-Host. Add type:filechooser support for input boxes. Move widget/value extraction out of [Run] callback. check-in: 891bc77e0e user: mario tags: trunk
19:40
Adapt Extract-PluginMeta to: - finally recognize end of comment block - support Powershell <# ># wrap - and strip no more than three spaces after pound sign, which allows multi-line span vars:+config: {entries} - more stringent parsing of JSOL structure (curlies/quotes) check-in: eef03bc397 user: mario tags: trunk
Changes

Changes to modules/guimenu.psm1.

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
...
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
...
766
767
768
769
770
771
772

773
774
775

776
777
778
779
780
781
782

783
784

785
786
787









788
789
790
791
792
793
794
795
796
797
798
799
800
801
...
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843







844
845
846
847
848
849
850
...
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
....
1094
1095
1096
1097
1098
1099
1100

1101
1102
1103
1104
1105
1106
1107
$BaseDir = Split-Path -Parent $ModuleDir

#-- standard UI input widgets and handler names
$cfg.standard_fields = @{
    machine = "Computer"
    username = "Username"
    #bulkcsv = "BulkCSV"
    ticketnum = "ticketnum"
}
$cfg.standard_aliases = @{
    username = "^(?i)[$]?(AD[-\s]?)?(User|Account)(?:[-\s]?Name)?\s*[=:?]*\s*$"
    machine =  "^(?i)[$]?(Computer|Machine|PC|Host)([-\s]?Name)?\s*[=:?]*\s*$"
    #bulkcsv = "^(?i)[$]?(Bulk|bulk.?csv|bulkfn|list|CSV)" # obsolete
}
$cfg.standard_actions = @{   # Add-ButtonHooks
................................................................................
########################   everything below runs in main thread   ########################
##########################################################################################


#-- Variable list input
function Read-GuiExtraParams {
    Param(
       $meta = @{title="Extra Input";description="Additional variables";doc="...";version="0.0";vars=@(
          @{name="mail"; type="string"; description="email addr"; value="user@local"},
          @{name="write"; type="btn"; value="1"; description="delete all?" }
          @{name="bulkcsv"; type="text"; value="flag,user,group"; description="CSV input"}
          @{name="pick"; type="select"; select="a|b|c|d"; value="c"; description="Select test"}
       )},
       $height = 450, $CURRENT_STORE=$GUI.vars_previous,
       $vars = @{}, $btns = @(), $name2type = @{},
       $results = @{}, $proceed = $false, $SELECT_FN = "./data/combobox.{0}.txt"
    )
    if (!$meta.vars) { return }

    #-- input widgets
    $var_widgets = $meta.vars | ? { $_.name } | % {
         $PARAM = $_
         $NAME = $PARAM.name
................................................................................
             $VALUE = $GUI.vars[$matches[1]]
         }
         
         # widget/type variants
         $vars[$NAME] = switch -regex ($PARAM.type) {
            "bool" {
                W CheckBox @{Margin="10,0"; IsChecked=($VALUE -notmatch "^$|0|false|no")}

            }
            "text|csv|long" {
                W TextBox @{ Height=120; Width=280; Text=$VALUE+"`r`n"; AcceptsReturn=1 }

            }
            "select|combo" {
                if ($PARAM.select) { $ls = $PARAM.select.split("[,;|]") }
                elseif (Test-Path ($SELECT_FN -f $NAME)) { $ls = Get-Content ($SELECT_FN -f $NAME) }
                else { $ls = @("Null") }
                if (!$VALUE) { $VALUE = $ls[0] }
                W ComboBox @{Height=22; Width=260; IsEditable=$true; Text=$VALUE; ItemsSource=$ls }

            }
            "btn|button|action" {

                W Button @{Content=$PARAM.description; Add_Click={$results[$NAME] = $results._proceed = 1; $w.Close()}}
                #$btns += ...;continue;
            }









            "str|int|^$" { 
                W TextBox @{Height=20; Width=270; Text=$VALUE}
            }
        }
        W Label @{Content=$NAME; FontWeight="Bold"}
        $vars[$PARAM.name]
        W Label @{Content=$PARAM.description; FontSize=9; Margin="0,0,0,5"}
    }

    #-- window
    $w = (W (New-Object System.Windows.Window) @{
      Title=$meta.title;
      Width=350; Height=$height; Top=350; Left=600; TopMost=$true;
      Background="#eef7f7ff";
................................................................................
          (W Expander @{
            Expanded=$false; Header=$meta.description;
            Content=(
              W Label @{Content=" $($meta.api) // $($meta.type) // $($meta.category) // $($meta.version)`r`n$($meta.doc)"}
            )
          })
          (W ScrollViewer @{
            Height=($height-100); Padding="10,5";
            Content=(
              W StackPanel @{
                Add=$var_widgets
              }
            )
          }),
          (W DockPanel @{
            Add=@(
              (W Button @{Content="Cancel"; Width=70; Height=30; Dock="Right"; BackGround="Red";
                Add_Click={
                  $w.close()
                }
              }),
              (W Button @{Content="Run"; Width=150; Height=30; BackGround="Green";
                Add_Click={
                  $results._proceed = $true;
                  ForEach ($NAME in $vars.Keys) {
                      $results[$NAME] = $vars[$NAME].Text
                      if ($name2type[$NAME] -match "bool") { $results[$NAME] = $vars[$NAME].IsChecked }
                  }
                  $w.close()
                }
              })
            )
          })
        )}#=stackpanel
      )
    })#=window
    [void]$w.ShowDialog()

    #-- merge vars
    if ($results._proceed) {







        #$results|FL|Out-String|Write-Host
        $results.getEnumerator() | % { $CURRENT_STORE[$_.Name] = $_.Value }
        return $results
    }
}


................................................................................
    }
    else {
        #-- undefined/non-Param() input request / stray Read-Host calls
        $results = Read-GuiExtraParams @{
            title="Input"; description="Read-Host"; doc="stray Read-Host call from script";
            api="clicky"; type="io"; category="extra"; version="1.1";
            vars=@(@{name=$name; type="str"; description="$name"; value=""})
        }
        if (!$results) { throw "❎ Cancelled at Read-Host request." }
        return $results[$name]
    }
}

#-- Parameter/varname aliasing
function Get-VarsAlias {
................................................................................
        #-- build gui
        Write-Splash "Create WPF-Window.." 40
        $GUI.w = WPF-Window
        Write-Splash "Add-ButtonHooks.." 10
        Add-ButtonHooks
        Write-Splash "Add-GuiMenu.." 15
        Add-GuiMenu $menu


        #-- execute `type:init` files right away
        $menu | ? { $_.fn -and ($_.type -eq "init-gui") } | % {
            Write-Splash "Init-Gui plugins.. $($_.title)" 25
            . $_.fn
        }
        Write-Splash "Go." 1000







<







 







|







|







 







>



>







>


>
|
<
|
>
>
>
>
>
>
>
>
>
|





|







 







|









|
<
<

|
<
|
<
<
<
<
<
<








|

>
>
>
>
>
>
>







 







|







 







>







58
59
60
61
62
63
64

65
66
67
68
69
70
71
...
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
...
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788

789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
...
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832


833
834

835






836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
...
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
....
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
$BaseDir = Split-Path -Parent $ModuleDir

#-- standard UI input widgets and handler names
$cfg.standard_fields = @{
    machine = "Computer"
    username = "Username"
    #bulkcsv = "BulkCSV"

}
$cfg.standard_aliases = @{
    username = "^(?i)[$]?(AD[-\s]?)?(User|Account)(?:[-\s]?Name)?\s*[=:?]*\s*$"
    machine =  "^(?i)[$]?(Computer|Machine|PC|Host)([-\s]?Name)?\s*[=:?]*\s*$"
    #bulkcsv = "^(?i)[$]?(Bulk|bulk.?csv|bulkfn|list|CSV)" # obsolete
}
$cfg.standard_actions = @{   # Add-ButtonHooks
................................................................................
########################   everything below runs in main thread   ########################
##########################################################################################


#-- Variable list input
function Read-GuiExtraParams {
    Param(
       $meta = @{title="INPUT";description="EXAMPLE";doc="META";version="0.0";vars=@(
          @{name="mail"; type="string"; description="email addr"; value="user@local"},
          @{name="write"; type="btn"; value="1"; description="delete all?" }
          @{name="bulkcsv"; type="text"; value="flag,user,group"; description="CSV input"}
          @{name="pick"; type="select"; select="a|b|c|d"; value="c"; description="Select test"}
       )},
       $height = 450, $CURRENT_STORE=$GUI.vars_previous,
       $vars = @{}, $btns = @(), $name2type = @{},
       $results = @{}, $SELECT_FN = "./data/combobox.{0}.txt"
    )
    if (!$meta.vars) { return }

    #-- input widgets
    $var_widgets = $meta.vars | ? { $_.name } | % {
         $PARAM = $_
         $NAME = $PARAM.name
................................................................................
             $VALUE = $GUI.vars[$matches[1]]
         }
         
         # widget/type variants
         $vars[$NAME] = switch -regex ($PARAM.type) {
            "bool" {
                W CheckBox @{Margin="10,0"; IsChecked=($VALUE -notmatch "^$|0|false|no")}
                break
            }
            "text|csv|long" {
                W TextBox @{ Height=120; Width=280; Text=$VALUE+"`r`n"; AcceptsReturn=1 }
                break
            }
            "select|combo" {
                if ($PARAM.select) { $ls = $PARAM.select.split("[,;|]") }
                elseif (Test-Path ($SELECT_FN -f $NAME)) { $ls = Get-Content ($SELECT_FN -f $NAME) }
                else { $ls = @("Null") }
                if (!$VALUE) { $VALUE = $ls[0] }
                W ComboBox @{Height=22; Width=260; IsEditable=$true; Text=$VALUE; ItemsSource=$ls }
                break
            }
            "btn|button|action" {
                W Button @{Content="▶ "+$PARAM.description; Width=125; Height=26; HorizontalAlignment="Left"; Background="#cc77ff77"; Add_Click={
                    $results[$NAME] = $results._proceed = 1; $w.Close()

                }}
                break
            }
            "file|choose" {
                W Button @{Content="📂 "+$PARAM.description; Background="#eeffee77"; Add_Click={
                    $f = WF OpenFileDialog @{CheckFileExists=$false; Filter="Text (*.txt)|*.txt|CSV (*.csv)|*.csv|JSON (*.json)|*.json|XML (*.xml)|*.xml|Powershell (*.ps1)|*.ps1|All Files (*.*)|*.*"; SupportMultiDottedExtensions=$true; Title="Script '$NAME' file"}
                    if ($f.ShowDialog() -and $f.FileName) { $this.Content = $f.FileName }
                }}
                break
            }
            "str|int|^$|.*" { 
                W TextBox @{Height=20; Width=270; Text=$VALUE}
            }
        }
        W Label @{Content=$NAME; FontWeight="Bold"}
        $vars[$PARAM.name]
        W TextBlock @{Text=$PARAM.description; TextWrapping="WrapWithOverflow"; FontSize=9; Margin="0,0,0,5"}
    }

    #-- window
    $w = (W (New-Object System.Windows.Window) @{
      Title=$meta.title;
      Width=350; Height=$height; Top=350; Left=600; TopMost=$true;
      Background="#eef7f7ff";
................................................................................
          (W Expander @{
            Expanded=$false; Header=$meta.description;
            Content=(
              W Label @{Content=" $($meta.api) // $($meta.type) // $($meta.category) // $($meta.version)`r`n$($meta.doc)"}
            )
          })
          (W ScrollViewer @{
            Height=($height-100); Padding="10,5"; HorizontalAlignment="Left";
            Content=(
              W StackPanel @{
                Add=$var_widgets
              }
            )
          }),
          (W DockPanel @{
            Add=@(
              (W Button @{Content="Cancel"; Width=70; Height=30; Dock="Right"; BackGround="Red";
                Add_Click={ $w.close() }


              }),
              (W Button @{Content="Run"; Width=150; Height=30; BackGround="Green";

                Add_Click={ $results._proceed = $true; $w.close() }






              })
            )
          })
        )}#=stackpanel
      )
    })#=window
    [void]$w.ShowDialog()

    #-- fetch vars
    if ($results._proceed) {
        ForEach ($NAME in $vars.Keys) {
            $results[$NAME] = switch -regex ($name2type[$NAME]) {
                "bool"     { [int32]($vars[$NAME].IsChecked); break }
                "file|cho" { $vars[$NAME].Content; break }
                "str|.*"   { $vars[$NAME].Text; break }
            }
        }
        #$results|FL|Out-String|Write-Host
        $results.getEnumerator() | % { $CURRENT_STORE[$_.Name] = $_.Value }
        return $results
    }
}


................................................................................
    }
    else {
        #-- undefined/non-Param() input request / stray Read-Host calls
        $results = Read-GuiExtraParams @{
            title="Input"; description="Read-Host"; doc="stray Read-Host call from script";
            api="clicky"; type="io"; category="extra"; version="1.1";
            vars=@(@{name=$name; type="str"; description="$name"; value=""})
        } -Height 200
        if (!$results) { throw "❎ Cancelled at Read-Host request." }
        return $results[$name]
    }
}

#-- Parameter/varname aliasing
function Get-VarsAlias {
................................................................................
        #-- build gui
        Write-Splash "Create WPF-Window.." 40
        $GUI.w = WPF-Window
        Write-Splash "Add-ButtonHooks.." 10
        Add-ButtonHooks
        Write-Splash "Add-GuiMenu.." 15
        Add-GuiMenu $menu
        #$GUI.w.Add_Loaded({Add-GuiMenu $menu})  # "speed" mode (late init would be faster, but halts window draw, might make sense with timer pulsing)

        #-- execute `type:init` files right away
        $menu | ? { $_.fn -and ($_.type -eq "init-gui") } | % {
            Write-Splash "Init-Gui plugins.. $($_.title)" 25
            . $_.fn
        }
        Write-Splash "Go." 1000