17
17
using System . Windows . Input ;
18
18
using System . Windows . Media ;
19
19
using System . Windows . Threading ;
20
+ using PoshCode . Interop ;
21
+ using PoshCode . Properties ;
20
22
21
23
namespace PoshCode . Controls
22
24
{
@@ -653,16 +655,26 @@ public string Title
653
655
set { SetValue ( TitleProperty , value ) ; }
654
656
}
655
657
656
-
657
- public Dictionary < string , PSObject > Prompt ( string caption , string message , Collection < FieldDescription > descriptions )
658
+ public event EventHandler < PromptForObjectEventArgs > PromptForObject ;
659
+ public Dictionary < string , PSObject > OnPromptForObject ( PromptForObjectEventArgs e )
658
660
{
659
- if ( ! string . IsNullOrEmpty ( caption ) )
660
- Write ( caption + "\n " ) ;
661
- if ( ! string . IsNullOrEmpty ( message ) )
662
- Write ( message + "\n " ) ;
661
+
662
+ EventHandler < PromptForObjectEventArgs > handler = PromptForObject ;
663
+ if ( handler != null )
664
+ {
665
+ handler ( this , e ) ;
666
+ return e . Results ;
667
+ }
668
+
669
+
670
+
671
+ if ( ! string . IsNullOrEmpty ( e . Caption ) )
672
+ Write ( e . Caption + "\n " ) ;
673
+ if ( ! string . IsNullOrEmpty ( e . Message ) )
674
+ Write ( e . Message + "\n " ) ;
663
675
664
676
var results = new Dictionary < string , PSObject > ( ) ;
665
- foreach ( var fd in descriptions )
677
+ foreach ( var fd in e . Descriptions )
666
678
{
667
679
Type type = Type . GetType ( fd . ParameterAssemblyFullName ) ;
668
680
@@ -675,7 +687,7 @@ public Dictionary<string, PSObject> Prompt(string caption, string message, Colle
675
687
int count = 0 ;
676
688
do
677
689
{
678
- PSObject single = GetSingle ( caption , message , string . Format ( "{0 }[{1}]" , prompt , count ++ ) ,
690
+ PSObject single = GetSingle ( e . Caption , e . Message , $ " { prompt } [{ count ++ } ]" ,
679
691
fd . HelpMessage , fd . DefaultValue , type ) ;
680
692
if ( single == null ) break ;
681
693
@@ -690,7 +702,7 @@ public Dictionary<string, PSObject> Prompt(string caption, string message, Colle
690
702
}
691
703
else
692
704
{
693
- results [ fd . Name ] = GetSingle ( caption , message , prompt , fd . HelpMessage , fd . DefaultValue , type ) ;
705
+ results [ fd . Name ] = GetSingle ( e . Caption , e . Message , prompt , fd . HelpMessage , fd . DefaultValue , type ) ;
694
706
}
695
707
696
708
}
@@ -700,7 +712,14 @@ private PSObject GetSingle(string caption, string message, string prompt, string
700
712
{
701
713
if ( null != type && type == typeof ( PSCredential ) )
702
714
{
703
- return PSObject . AsPSObject ( PromptForCredentialInline ( caption , message , String . Empty , prompt ) ) ;
715
+ if ( Settings . Default . UseCredentialUI )
716
+ {
717
+ return PSObject . AsPSObject ( CredentialUI . Prompt ( caption , message , psDefault . ToString ( ) , string . Empty ) ) ;
718
+ }
719
+ else
720
+ {
721
+ return PSObject . AsPSObject ( PromptForCredentialInline ( caption , message , psDefault . ToString ( ) , prompt ) ) ;
722
+ }
704
723
}
705
724
706
725
while ( true )
@@ -765,14 +784,24 @@ private PSObject GetSingle(string caption, string message, string prompt, string
765
784
}
766
785
}
767
786
768
- public int PromptForChoice ( string caption , string message , Collection < ChoiceDescription > choices , int defaultChoice )
787
+
788
+ // public delegate void PromptForChoiceHandler(object sender, PromptForChoiceEventArgs e);
789
+ public event EventHandler < PromptForChoiceEventArgs > PromptForChoice ;
790
+ public int OnPromptForChoice ( PromptForChoiceEventArgs e )
769
791
{
792
+ EventHandler < PromptForChoiceEventArgs > handler = PromptForChoice ;
793
+ if ( handler != null )
794
+ {
795
+ handler ( this , e ) ;
796
+ return e . SelectedIndex ;
797
+ }
798
+
770
799
// Write the caption and message strings in Blue.
771
- Write ( ConsoleColor . Blue , ConsoleColor . Black , caption + "\n " + message + "\n " ) ;
800
+ Write ( ConsoleColor . Blue , ConsoleColor . Black , e . Caption + "\n " + e . Message + "\n " ) ;
772
801
773
802
// Convert the choice collection into something that's a little easier to work with
774
803
// See the BuildHotkeysAndPlainLabels method for details.
775
- var promptData = BuildHotkeysAndPlainLabels ( choices , true ) ;
804
+ var promptData = BuildHotkeysAndPlainLabels ( e . Choices , true ) ;
776
805
777
806
778
807
// Loop reading prompts until a match is made, the default is
@@ -781,9 +810,9 @@ public int PromptForChoice(string caption, string message, Collection<ChoiceDesc
781
810
{
782
811
783
812
// Format the overall choice prompt string to display...
784
- for ( int element = 0 ; element < promptData . GetLength ( 1 ) ; element ++ )
813
+ for ( var element = 0 ; element < promptData . GetLength ( 1 ) ; element ++ )
785
814
{
786
- if ( element == defaultChoice )
815
+ if ( element == e . SelectedIndex )
787
816
{
788
817
Write ( ConsoleBrushes . VerboseForeground , ConsoleBrushes . VerboseBackground ,
789
818
$ "[{ promptData [ 0 , element ] } ] { promptData [ 1 , element ] } ") ;
@@ -793,27 +822,27 @@ public int PromptForChoice(string caption, string message, Collection<ChoiceDesc
793
822
Write ( null , null , $ "[{ promptData [ 0 , element ] } ] { promptData [ 1 , element ] } ") ;
794
823
}
795
824
}
796
- Write ( null , null , $ "(default is \" { promptData [ 0 , defaultChoice ] } \" ):") ;
825
+ Write ( null , null , $ "(default is \" { promptData [ 0 , e . SelectedIndex ] } \" ):") ;
797
826
798
827
string data = ReadLine ( ) . Trim ( ) . ToUpper ( ) ;
799
828
800
829
// If the choice string was empty, use the default selection.
801
830
if ( data . Length == 0 )
802
- return defaultChoice ;
831
+ return e . SelectedIndex ;
803
832
804
833
// See if the selection matched and return the
805
834
// corresponding index if it did...
806
- for ( int i = 0 ; i < choices . Count ; i ++ )
835
+ for ( var i = 0 ; i < e . Choices . Count ; i ++ )
807
836
{
808
837
if ( promptData [ 0 , i ] [ 0 ] == data [ 0 ] )
809
838
return i ;
810
839
}
811
840
812
841
// If they picked the very last thing in the list, they want help
813
- if ( promptData . GetLength ( 1 ) > choices . Count && promptData [ 0 , choices . Count ] == data )
842
+ if ( promptData . GetLength ( 1 ) > e . Choices . Count && promptData [ 0 , e . Choices . Count ] == data )
814
843
{
815
844
// Show help
816
- foreach ( var choice in choices )
845
+ foreach ( var choice in e . Choices )
817
846
{
818
847
Write ( $ "{ choice . Label . Replace ( "&" , "" ) } - { choice . HelpMessage } \n ") ;
819
848
}
@@ -848,7 +877,8 @@ public PSCredential PromptForCredentialInline(string caption, string message, st
848
877
do
849
878
{
850
879
fields = new Collection < FieldDescription > ( new [ ] { user } ) ;
851
- var login = Prompt ( caption , message , fields ) ;
880
+ var username = new PromptForObjectEventArgs ( caption , message , fields ) ;
881
+ var login = OnPromptForObject ( username ) ;
852
882
userName = login [ "User" ] . BaseObject as string ;
853
883
} while ( userName != null && userName . Length == 0 ) ;
854
884
}
@@ -870,7 +900,8 @@ public PSCredential PromptForCredentialInline(string caption, string message, st
870
900
pass . IsMandatory = true ;
871
901
872
902
fields = new Collection < FieldDescription > ( new [ ] { pass } ) ;
873
- var password = Prompt ( String . Empty , String . Empty , fields ) ;
903
+ var pwd = new PromptForObjectEventArgs ( string . Empty , string . Empty , fields ) ;
904
+ var password = OnPromptForObject ( pwd ) ;
874
905
875
906
// TODO: I'm not sure what to do with the PSCredentialUIOptions options, because PowerShell.exe ignores them
876
907
return new PSCredential ( userName , ( SecureString ) password [ "Password" ] . BaseObject ) ;
0 commit comments