Skip to content

Commit a2e5f90

Browse files
committed
Command quick run autocomplete
1 parent 6df0c4a commit a2e5f90

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed

SmartImage/Commands.cs

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#region
22

33
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text.RegularExpressions;
47
using Neocmd;
58
using SmartImage.Engines.SauceNao;
69
using 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
}

SmartImage/Program.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.IO.Compression;
6+
using System.Linq;
67
using Neocmd;
78
using RapidSelenium;
89
using SmartImage.Engines.SauceNao;
@@ -26,7 +27,7 @@ public static class Program
2627

2728
// copy SmartImage.exe C:\Library /Y
2829
// copy SmartImage.exe C:\Users\Deci\Desktop /Y
29-
30+
3031
// Computer\HKEY_CLASSES_ROOT\*\shell\SmartImage
3132
// Computer\HKEY_CURRENT_USER\Software\SmartImage
3233

@@ -39,7 +40,7 @@ private static void Main(string[] args)
3940
{
4041
Commands.Setup();
4142
Config.Setup();
42-
43+
4344

4445
if (args == null || args.Length < 1) {
4546
CliOutput.WriteError("Image or command not specified!");
@@ -49,11 +50,30 @@ private static void Main(string[] args)
4950

5051
var arg = args[0];
5152

52-
if (arg == "test") {
53+
if (arg == "--test") {
5354
// ...
54-
55+
5556
return;
5657
}
58+
else if (arg == "--qr") {
59+
Console.Clear();
60+
61+
var commands = Commands.AllCommands.Select(c => c.Parameter).ToArray();
62+
63+
Console.WriteLine("Available commands:\n");
64+
65+
foreach (var c in commands)
66+
Console.WriteLine(c);
67+
68+
Console.WriteLine("\nEnter a command:");
69+
70+
var input = Commands.ReadHintedLine(commands, c => c);
71+
72+
Console.WriteLine($"\n>> {input}");
73+
74+
args = input.Split(' ');
75+
arg = args[0];
76+
}
5777

5878
// Run the command if one was parsed
5979
var cmd = CliOutput.ReadCommand(arg);

SmartImage/obj/Release/netcoreapp3.0/win10-x64/SmartImage.AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
[assembly: System.Reflection.AssemblyInformationalVersion("1.0.0")]
1414
[assembly: System.Reflection.AssemblyProduct("SmartImage")]
1515
[assembly: System.Reflection.AssemblyTitle("SmartImage")]
16-
[assembly: System.Reflection.AssemblyVersion("1.5.0.0")]
16+
[assembly: System.Reflection.AssemblyVersion("1.6.0.0")]

0 commit comments

Comments
 (0)