11#region
22
33using System ;
4+ using System . Collections . Generic ;
5+ using System . Linq ;
6+ using System . Text . RegularExpressions ;
47using Neocmd ;
58using SmartImage . Engines . SauceNao ;
69using SmartImage . Model ;
@@ -92,7 +95,7 @@ public static class Commands
9295 Parameter = "--add-to-path" ,
9396 Syntax = null ,
9497 Description = "Adds executable path to path environment variable" ,
95- Action = args =>
98+ Action = args =>
9699 {
97100 // done automatically for now
98101 Config . AddToPath ( ) ;
@@ -143,7 +146,7 @@ public static class Commands
143146 if ( args . Length >= 2 ) {
144147 auto = args [ 1 ] == "auto" ;
145148 }
146-
149+
147150 // todo
148151 var acc = SauceNao . CreateAccount ( auto ) ;
149152
@@ -176,5 +179,59 @@ public static void Setup()
176179 CliOutput . Commands . AddRange ( AllCommands ) ;
177180 CliOutput . Init ( Config . NAME , false ) ;
178181 }
182+
183+ // https://github.com/fjunqueira/hinter
184+ public static string ReadHintedLine < T , TResult > ( IEnumerable < T > hintSource , Func < T , TResult > hintField ,
185+ string inputRegex = ".*" ,
186+ ConsoleColor hintColor = ConsoleColor . DarkGray )
187+ {
188+ ConsoleKeyInfo input ;
189+
190+ var suggestion = string . Empty ;
191+ var userInput = string . Empty ;
192+ var readLine = string . Empty ;
193+
194+ while ( ConsoleKey . Enter != ( input = Console . ReadKey ( ) ) . Key ) {
195+ if ( input . Key == ConsoleKey . Backspace )
196+ userInput = userInput . Any ( ) ? userInput . Remove ( userInput . Length - 1 , 1 ) : string . Empty ;
197+
198+ else if ( input . Key == ConsoleKey . Tab )
199+ userInput = suggestion ?? userInput ;
200+
201+ else if ( input != null && Regex . IsMatch ( input . KeyChar . ToString ( ) , inputRegex ) )
202+ userInput += input . KeyChar ;
203+
204+ suggestion = hintSource . Select ( item => hintField ( item ) . ToString ( ) )
205+ . FirstOrDefault ( item => item . Length > userInput . Length &&
206+ item . Substring ( 0 , userInput . Length ) == userInput ) ;
207+
208+ readLine = suggestion == null ? userInput : suggestion ;
209+
210+ ClearCurrentConsoleLine ( ) ;
211+
212+ Console . Write ( userInput ) ;
213+
214+ var originalColor = Console . ForegroundColor ;
215+
216+ Console . ForegroundColor = hintColor ;
217+
218+ if ( userInput . Any ( ) )
219+ Console . Write ( readLine . Substring ( userInput . Length , readLine . Length - userInput . Length ) ) ;
220+
221+ Console . ForegroundColor = originalColor ;
222+ }
223+
224+ Console . WriteLine ( readLine ) ;
225+
226+ return userInput . Any ( ) ? readLine : string . Empty ;
227+ }
228+
229+ private static void ClearCurrentConsoleLine ( )
230+ {
231+ int currentLineCursor = Console . CursorTop ;
232+ Console . SetCursorPosition ( 0 , Console . CursorTop ) ;
233+ Console . Write ( new string ( ' ' , Console . WindowWidth ) ) ;
234+ Console . SetCursorPosition ( 0 , currentLineCursor ) ;
235+ }
179236 }
180237}
0 commit comments