From 9d19ca6d2294d4b8c5282f3df41bb6009160aba8 Mon Sep 17 00:00:00 2001 From: maxwes08 Date: Mon, 26 Jan 2026 10:35:50 +0100 Subject: [PATCH] searching --- Sortering/Program.cs | 165 ++++++++++++++++++++++++++++++++----------- 1 file changed, 123 insertions(+), 42 deletions(-) diff --git a/Sortering/Program.cs b/Sortering/Program.cs index d651102..4a85b41 100644 --- a/Sortering/Program.cs +++ b/Sortering/Program.cs @@ -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> 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); } - } } - private static void SortInsertion(string[] strings) + // autocompletion + + private static string ReadLineWithAutocomplete() { - Stopwatch sw = new Stopwatch(); - sw.Start(); + var buffer = new List(); + int cursor = 0; - Sort.InsertionSort(_ints); - - long timeElapsed = sw.ElapsedMilliseconds; - Console.WriteLine("Sorted array in " + timeElapsed.ToString() + "ms"); - } - - private static void SortBubble(string[] strings) - { - Stopwatch sw = new Stopwatch(); - sw.Start(); - - 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++) + while (true) { - Console.WriteLine(_ints[i]); + 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 MkArray(string[] strings) + private static void Autocomplete(List buffer, ref int cursor) { - if (strings.Length < 1) return; + var text = new string(buffer.ToArray()); + var parts = text.Split(' ', StringSplitOptions.RemoveEmptyEntries); - int count = int.Parse(strings[0]); + // Only autocomplete the command (first word) + if (parts.Length > 1) + return; + + string prefix = parts.Length == 0 ? "" : parts[0]; + + var matches = Commands.Keys + .Where(c => c.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) + .ToList(); + + if (matches.Count == 1) + { + buffer.Clear(); + buffer.AddRange(matches[0]); + cursor = buffer.Count; + RedrawLine(buffer, cursor); + } + } + + private static void RedrawLine(List buffer, int cursor) + { + Console.Write("\r> "); + Console.Write(new string(buffer.ToArray())); + Console.Write(" "); + Console.SetCursorPosition(2 + cursor, Console.CursorTop); + } + + // 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 "); + 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"); } -} \ No newline at end of file +}