Skip to content

Commit

Permalink
Command quick run autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
Decimation committed May 5, 2020
1 parent 6df0c4a commit a2e5f90
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 7 deletions.
61 changes: 59 additions & 2 deletions SmartImage/Commands.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Neocmd;
using SmartImage.Engines.SauceNao;
using SmartImage.Model;
Expand Down Expand Up @@ -92,7 +95,7 @@ public static class Commands
Parameter = "--add-to-path",
Syntax = null,
Description = "Adds executable path to path environment variable",
Action = args =>
Action = args =>
{
// done automatically for now
Config.AddToPath();
Expand Down Expand Up @@ -143,7 +146,7 @@ public static class Commands
if (args.Length >= 2) {
auto = args[1] == "auto";
}
// todo
var acc = SauceNao.CreateAccount(auto);
Expand Down Expand Up @@ -176,5 +179,59 @@ public static void Setup()
CliOutput.Commands.AddRange(AllCommands);
CliOutput.Init(Config.NAME, false);
}

// https://github.com/fjunqueira/hinter
public static string ReadHintedLine<T, TResult>(IEnumerable<T> hintSource, Func<T, TResult> hintField,
string inputRegex = ".*",
ConsoleColor hintColor = ConsoleColor.DarkGray)
{
ConsoleKeyInfo input;

var suggestion = string.Empty;
var userInput = string.Empty;
var readLine = string.Empty;

while (ConsoleKey.Enter != (input = Console.ReadKey()).Key) {
if (input.Key == ConsoleKey.Backspace)
userInput = userInput.Any() ? userInput.Remove(userInput.Length - 1, 1) : string.Empty;

else if (input.Key == ConsoleKey.Tab)
userInput = suggestion ?? userInput;

else if (input != null && Regex.IsMatch(input.KeyChar.ToString(), inputRegex))
userInput += input.KeyChar;

suggestion = hintSource.Select(item => hintField(item).ToString())
.FirstOrDefault(item => item.Length > userInput.Length &&
item.Substring(0, userInput.Length) == userInput);

readLine = suggestion == null ? userInput : suggestion;

ClearCurrentConsoleLine();

Console.Write(userInput);

var originalColor = Console.ForegroundColor;

Console.ForegroundColor = hintColor;

if (userInput.Any())
Console.Write(readLine.Substring(userInput.Length, readLine.Length - userInput.Length));

Console.ForegroundColor = originalColor;
}

Console.WriteLine(readLine);

return userInput.Any() ? readLine : string.Empty;
}

private static void ClearCurrentConsoleLine()
{
int currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, currentLineCursor);
}
}
}
28 changes: 24 additions & 4 deletions SmartImage/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using Neocmd;
using RapidSelenium;
using SmartImage.Engines.SauceNao;
Expand All @@ -26,7 +27,7 @@ public static class Program

// copy SmartImage.exe C:\Library /Y
// copy SmartImage.exe C:\Users\Deci\Desktop /Y

// Computer\HKEY_CLASSES_ROOT\*\shell\SmartImage
// Computer\HKEY_CURRENT_USER\Software\SmartImage

Expand All @@ -39,7 +40,7 @@ private static void Main(string[] args)
{
Commands.Setup();
Config.Setup();


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

var arg = args[0];

if (arg == "test") {
if (arg == "--test") {
// ...

return;
}
else if (arg == "--qr") {
Console.Clear();

var commands = Commands.AllCommands.Select(c => c.Parameter).ToArray();

Console.WriteLine("Available commands:\n");

foreach (var c in commands)
Console.WriteLine(c);

Console.WriteLine("\nEnter a command:");

var input = Commands.ReadHintedLine(commands, c => c);

Console.WriteLine($"\n>> {input}");

args = input.Split(' ');
arg = args[0];
}

// Run the command if one was parsed
var cmd = CliOutput.ReadCommand(arg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
[assembly: System.Reflection.AssemblyInformationalVersion("1.0.0")]
[assembly: System.Reflection.AssemblyProduct("SmartImage")]
[assembly: System.Reflection.AssemblyTitle("SmartImage")]
[assembly: System.Reflection.AssemblyVersion("1.5.0.0")]
[assembly: System.Reflection.AssemblyVersion("1.6.0.0")]

0 comments on commit a2e5f90

Please sign in to comment.