1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Linq ;
3
4
using XmrStakBootstrap . Common . Helper ;
4
5
5
6
namespace XmrStakBootstrap . Common . Menu
@@ -47,15 +48,23 @@ public void AddOption(string text, bool enabled, Func<T> action)
47
48
48
49
public T Execute ( )
49
50
{
50
- if ( _options . Count == 0 ) return ExecuteDefault ( ) ;
51
+ switch ( _options . Count )
52
+ {
53
+ case 0 :
54
+ return ExecuteDefault ( ) ;
55
+
56
+ case 1 :
57
+ return ExecuteAction ( _options . First ( ) . Action ) ;
51
58
52
- RenderOptions ( ) ;
53
- var input = GetInput ( ) ;
59
+ default :
60
+ RenderOptions ( ) ;
61
+ var input = GetInput ( ) ;
54
62
55
- if ( ! IsInputValid ( input ) ) return ExecuteDefault ( ) ;
63
+ if ( ! IsInputValid ( input ) ) return ExecuteDefault ( ) ;
56
64
57
- var option = _options [ input - 1 ] ;
58
- return option . IsEnabled ? ExecuteAction ( option . Action ) : ExecuteDefault ( ) ;
65
+ var option = _options [ input - 1 ] ;
66
+ return option . IsEnabled ? ExecuteAction ( option . Action ) : ExecuteDefault ( ) ;
67
+ }
59
68
}
60
69
61
70
private T ExecuteDefault ( )
@@ -73,12 +82,38 @@ private bool IsInputValid(int input)
73
82
return input >= 1 && input <= _options . Count ;
74
83
}
75
84
76
- private static int GetInput ( )
85
+ private int GetInput ( )
77
86
{
78
87
Console . Write ( @"Select: " ) ;
88
+
89
+ var knownOptions = _options . Select ( ( _ , i ) => ( i + 1 ) . ToString ( ) ) . ToList ( ) ;
90
+
91
+ ConsoleKeyInfo key ;
92
+ var input = "" ;
79
93
int index ;
80
- if ( ! int . TryParse ( Console . ReadLine ( ) , out index ) ) return - 1 ;
81
- return index ;
94
+ do
95
+ {
96
+ key = Console . ReadKey ( true ) ;
97
+ if ( key . Key == ConsoleKey . Backspace && input . Length > 0 )
98
+ {
99
+ input = input . Substring ( 0 , input . Length - 1 ) ;
100
+ // ReSharper disable once LocalizableElement
101
+ Console . Write ( "\b \b " ) ;
102
+ }
103
+ else if ( char . IsNumber ( key . KeyChar ) )
104
+ {
105
+ input += key . KeyChar ;
106
+ Console . Write ( key . KeyChar ) ;
107
+ }
108
+
109
+ if ( int . TryParse ( input , out index ) && IsInputValid ( index ) )
110
+ {
111
+ var stringInput = index . ToString ( ) ;
112
+ if ( knownOptions . Count ( x => x . StartsWith ( stringInput ) ) == 1 ) return index ;
113
+ }
114
+ }
115
+ while ( key . Key != ConsoleKey . Enter ) ;
116
+ return int . TryParse ( input , out index ) ? index : - 1 ;
82
117
}
83
118
84
119
private void RenderOptions ( )
0 commit comments