searching
This commit is contained in:
@@ -5,7 +5,6 @@ internal class Program
|
||||
{
|
||||
private static int[] _ints;
|
||||
private static bool _running = true;
|
||||
|
||||
private static Random _random = new Random();
|
||||
|
||||
private static Dictionary<string, Action<string[]>> Commands =
|
||||
@@ -15,6 +14,7 @@ internal class Program
|
||||
{ "bubblesort", SortBubble },
|
||||
{ "mkarray", MkArray },
|
||||
{ "cat", Cat },
|
||||
{ "exit", _ => _running = false }
|
||||
};
|
||||
|
||||
private static void Main(string[] args)
|
||||
@@ -22,7 +22,7 @@ internal class Program
|
||||
while (_running)
|
||||
{
|
||||
Console.Write("> ");
|
||||
var input = Console.ReadLine();
|
||||
var input = ReadLineWithAutocomplete();
|
||||
if (string.IsNullOrWhiteSpace(input)) continue;
|
||||
|
||||
var parts = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||||
@@ -33,60 +33,141 @@ internal class Program
|
||||
action(cmdArgs);
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Unknown command, commands available are:");
|
||||
foreach (var kvp in Commands)
|
||||
{
|
||||
Console.WriteLine(kvp.Key);
|
||||
}
|
||||
Console.WriteLine("Unknown command. Available:");
|
||||
foreach (var c in Commands.Keys)
|
||||
Console.WriteLine(" - " + c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// autocompletion
|
||||
|
||||
private static string ReadLineWithAutocomplete()
|
||||
{
|
||||
var buffer = new List<char>();
|
||||
int cursor = 0;
|
||||
|
||||
while (true)
|
||||
{
|
||||
var key = Console.ReadKey(intercept: true);
|
||||
|
||||
if (key.Key == ConsoleKey.Enter)
|
||||
{
|
||||
Console.WriteLine();
|
||||
return new string(buffer.ToArray());
|
||||
}
|
||||
|
||||
if (key.Key == ConsoleKey.Backspace && cursor > 0)
|
||||
{
|
||||
buffer.RemoveAt(cursor - 1);
|
||||
cursor--;
|
||||
RedrawLine(buffer, cursor);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (key.Key == ConsoleKey.Tab)
|
||||
{
|
||||
Autocomplete(buffer, ref cursor);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!char.IsControl(key.KeyChar))
|
||||
{
|
||||
buffer.Insert(cursor, key.KeyChar);
|
||||
cursor++;
|
||||
RedrawLine(buffer, cursor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void SortInsertion(string[] strings)
|
||||
private static void Autocomplete(List<char> buffer, ref int cursor)
|
||||
{
|
||||
Stopwatch sw = new Stopwatch();
|
||||
sw.Start();
|
||||
var text = new string(buffer.ToArray());
|
||||
var parts = text.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
Sort.InsertionSort(_ints);
|
||||
// Only autocomplete the command (first word)
|
||||
if (parts.Length > 1)
|
||||
return;
|
||||
|
||||
long timeElapsed = sw.ElapsedMilliseconds;
|
||||
Console.WriteLine("Sorted array in " + timeElapsed.ToString() + "ms");
|
||||
}
|
||||
string prefix = parts.Length == 0 ? "" : parts[0];
|
||||
|
||||
private static void SortBubble(string[] strings)
|
||||
{
|
||||
Stopwatch sw = new Stopwatch();
|
||||
sw.Start();
|
||||
var matches = Commands.Keys
|
||||
.Where(c => c.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
|
||||
.ToList();
|
||||
|
||||
Sort.BubbleSort(_ints);
|
||||
|
||||
long timeElapsed = sw.ElapsedMilliseconds;
|
||||
Console.WriteLine("Sorted array in " + timeElapsed.ToString() + "ms");
|
||||
}
|
||||
|
||||
private static void Cat(string[] strings)
|
||||
{
|
||||
for (int i = 0; i < _ints.Length; i++)
|
||||
if (matches.Count == 1)
|
||||
{
|
||||
Console.WriteLine(_ints[i]);
|
||||
buffer.Clear();
|
||||
buffer.AddRange(matches[0]);
|
||||
cursor = buffer.Count;
|
||||
RedrawLine(buffer, cursor);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void MkArray(string[] strings)
|
||||
private static void RedrawLine(List<char> buffer, int cursor)
|
||||
{
|
||||
if (strings.Length < 1) return;
|
||||
Console.Write("\r> ");
|
||||
Console.Write(new string(buffer.ToArray()));
|
||||
Console.Write(" ");
|
||||
Console.SetCursorPosition(2 + cursor, Console.CursorTop);
|
||||
}
|
||||
|
||||
int count = int.Parse(strings[0]);
|
||||
// commands
|
||||
|
||||
private static void SortInsertion(string[] args)
|
||||
{
|
||||
if (_ints == null)
|
||||
{
|
||||
Console.WriteLine("No array. Use mkarray first.");
|
||||
return;
|
||||
}
|
||||
|
||||
var sw = Stopwatch.StartNew();
|
||||
Sort.InsertionSort(_ints);
|
||||
sw.Stop();
|
||||
|
||||
Console.WriteLine($"Sorted array in {sw.ElapsedMilliseconds} ms");
|
||||
}
|
||||
|
||||
private static void SortBubble(string[] args)
|
||||
{
|
||||
if (_ints == null)
|
||||
{
|
||||
Console.WriteLine("No array. Use mkarray first.");
|
||||
return;
|
||||
}
|
||||
|
||||
var sw = Stopwatch.StartNew();
|
||||
Sort.BubbleSort(_ints);
|
||||
sw.Stop();
|
||||
|
||||
Console.WriteLine($"Sorted array in {sw.ElapsedMilliseconds} ms");
|
||||
}
|
||||
|
||||
private static void Cat(string[] args)
|
||||
{
|
||||
if (_ints == null)
|
||||
{
|
||||
Console.WriteLine("No array.");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < _ints.Length; i++)
|
||||
Console.WriteLine(_ints[i]);
|
||||
}
|
||||
|
||||
private static void MkArray(string[] args)
|
||||
{
|
||||
if (args.Length < 1 || !int.TryParse(args[0], out int count) || count <= 0)
|
||||
{
|
||||
Console.WriteLine("Usage: mkarray <size>");
|
||||
return;
|
||||
}
|
||||
|
||||
_ints = new int[count];
|
||||
|
||||
for (int i=0; i< count; i++)
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
_ints[i] = _random.Next(count);
|
||||
}
|
||||
|
||||
Console.WriteLine("Created array with " + strings[0] + " indices");
|
||||
Console.WriteLine($"Created array with {count} elements");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user