66
77namespace Sharprompt . Forms ;
88
9- internal class InputForm < T > : FormBase < T >
9+ internal class InputForm < T > : TextFormBase < T >
1010{
1111 public InputForm ( InputOptions < T > options )
1212 {
@@ -20,72 +20,6 @@ public InputForm(InputOptions<T> options)
2020 private readonly InputOptions < T > _options ;
2121 private readonly Optional < T > _defaultValue ;
2222
23- private readonly TextInputBuffer _textInputBuffer = new ( ) ;
24-
25- protected override bool TryGetResult ( [ NotNullWhen ( true ) ] out T ? result )
26- {
27- do
28- {
29- var keyInfo = ConsoleDriver . ReadKey ( ) ;
30- var controlPressed = keyInfo . Modifiers . HasFlag ( ConsoleModifiers . Control ) ;
31-
32- switch ( keyInfo . Key )
33- {
34- case ConsoleKey . Enter :
35- return HandleEnter ( out result ) ;
36- case ConsoleKey . LeftArrow when controlPressed && ! _textInputBuffer . IsStart :
37- _textInputBuffer . MoveToPreviousWord ( ) ;
38- break ;
39- case ConsoleKey . RightArrow when controlPressed && ! _textInputBuffer . IsEnd :
40- _textInputBuffer . MoveToNextWord ( ) ;
41- break ;
42- case ConsoleKey . LeftArrow when ! _textInputBuffer . IsStart :
43- _textInputBuffer . MoveBackward ( ) ;
44- break ;
45- case ConsoleKey . RightArrow when ! _textInputBuffer . IsEnd :
46- _textInputBuffer . MoveForward ( ) ;
47- break ;
48- case ConsoleKey . Home when ! _textInputBuffer . IsStart :
49- _textInputBuffer . MoveToStart ( ) ;
50- break ;
51- case ConsoleKey . End when ! _textInputBuffer . IsEnd :
52- _textInputBuffer . MoveToEnd ( ) ;
53- break ;
54- case ConsoleKey . Backspace when controlPressed && ! _textInputBuffer . IsStart :
55- _textInputBuffer . BackspaceWord ( ) ;
56- break ;
57- case ConsoleKey . Delete when controlPressed && ! _textInputBuffer . IsEnd :
58- _textInputBuffer . DeleteWord ( ) ;
59- break ;
60- case ConsoleKey . Backspace when ! _textInputBuffer . IsStart :
61- _textInputBuffer . Backspace ( ) ;
62- break ;
63- case ConsoleKey . Delete when ! _textInputBuffer . IsEnd :
64- _textInputBuffer . Delete ( ) ;
65- break ;
66- case ConsoleKey . LeftArrow :
67- case ConsoleKey . RightArrow :
68- case ConsoleKey . Home :
69- case ConsoleKey . End :
70- case ConsoleKey . Backspace :
71- case ConsoleKey . Delete :
72- ConsoleDriver . Beep ( ) ;
73- break ;
74- default :
75- if ( ! char . IsControl ( keyInfo . KeyChar ) )
76- {
77- _textInputBuffer . Insert ( keyInfo . KeyChar ) ;
78- }
79- break ;
80- }
81-
82- } while ( ConsoleDriver . KeyAvailable ) ;
83-
84- result = default ;
85-
86- return false ;
87- }
88-
8923 protected override void InputTemplate ( OffscreenBuffer offscreenBuffer )
9024 {
9125 offscreenBuffer . WritePrompt ( _options . Message ) ;
@@ -95,13 +29,13 @@ protected override void InputTemplate(OffscreenBuffer offscreenBuffer)
9529 offscreenBuffer . WriteHint ( $ "({ _defaultValue . Value } ) ") ;
9630 }
9731
98- if ( _textInputBuffer . Length == 0 && ! string . IsNullOrEmpty ( _options . Placeholder ) )
32+ if ( InputBuffer . Length == 0 && ! string . IsNullOrEmpty ( _options . Placeholder ) )
9933 {
10034 offscreenBuffer . PushCursor ( ) ;
10135 offscreenBuffer . WriteHint ( _options . Placeholder ) ;
10236 }
10337
104- offscreenBuffer . WriteInput ( _textInputBuffer ) ;
38+ offscreenBuffer . WriteInput ( InputBuffer ) ;
10539 }
10640
10741 protected override void FinishTemplate ( OffscreenBuffer offscreenBuffer , T result )
@@ -114,9 +48,9 @@ protected override void FinishTemplate(OffscreenBuffer offscreenBuffer, T result
11448 }
11549 }
11650
117- private bool HandleEnter ( [ NotNullWhen ( true ) ] out T ? result )
51+ protected override bool HandleEnter ( [ NotNullWhen ( true ) ] out T ? result )
11852 {
119- var input = _textInputBuffer . ToString ( ) ;
53+ var input = InputBuffer . ToString ( ) ;
12054
12155 try
12256 {
@@ -138,11 +72,7 @@ private bool HandleEnter([NotNullWhen(true)] out T? result)
13872 result = TypeHelper < T > . ConvertTo ( input ) ;
13973 }
14074
141- if ( TryValidate ( result , _options . Validators ) )
142- {
143- return true ;
144- }
145-
75+ return TryValidate ( result , _options . Validators ) ;
14676 }
14777 catch ( Exception ex )
14878 {
0 commit comments