private static void AddPathNavigationTests(ICollection <ConsoleMenuItem> items) { items.Add(new ConsoleMenuItem <string>("PathNavigation", _ => { var currentPath = Environment.CurrentDirectory; Describe("Navigate 2 folders up"); var twoFoldersUp = PowerConsole.PathSelector(currentPath); if (twoFoldersUp != new DirectoryInfo(currentPath).Parent !.Parent !.FullName) { Describe("Test failed"); return; } Describe("Navigate to the root. Use Drive navigation also"); var root = PowerConsole.PathSelector(currentPath); var rootDir = new DirectoryInfo(currentPath).Root; if (root != rootDir.FullName) { Describe("Test failed"); return; } Describe("Test successful"); }, null));
private static Crawler CreateCrawler(CrawlContext context) { var crawler = new Crawler(context.Sequence); crawler.PauseInterval = context.Pause; if (context.QueryType == QueryType.Name) { ExtraSetupForNameBasedCrawler(crawler, context); } crawler.AfterCrawl += (sender, e) => { var progress = e.Progress; var message = string.Format(GetMessageFormat(context.MaximumTry), progress.Current, progress.Total, context.CancelRate, progress.CurrentKeyword, progress.Message); if (e.Error == null) { PowerConsole.Info(message); } else { PowerConsole.Error(message); } }; return crawler; }
private void Awake() { var context = new DefaultContext(); context.CommandContext.RegisterCommand <AddNumberCommand>(); context.CommandContext.RegisterCommand <SubtractNumberCommand>(); context.CommandContext.RegisterCommand <MultiplyNumberCommand>(); context.CommandContext.RegisterCommand <DivideNumberCommand>(); _console = new PowerConsole(this, context); }
private static void AddNullableNumbersTests(ICollection <ConsoleMenuItem> items) { items.Add(new ConsoleMenuItem <string>("NullableNumbers", _ => { Console.WriteLine(); //number var five = PowerConsole.EnsureNullableIntInput("Input 5 here"); if (five != 5) { Describe("Test failed"); return; } //null var notANumber = PowerConsole.EnsureNullableIntInput("Fail with input 'aaa' here, then input nothing"); if (notANumber is not null) { Describe("Test failed"); return; } //range var eight = PowerConsole.EnsureNullableIntInputInRange("Input 8 here", 5, 8); if (eight != 8) { Describe("Test failed"); return; } //outside range var six = PowerConsole.EnsureNullableIntInputInRange("Fail with 9 first, then use 6", 5, 8); if (six != 6) { Describe("Test failed"); return; } //null var notANumber2 = PowerConsole.EnsureNullableIntInputInRange("Fail with 'aaa' here, then input nothing", 5, 10); if (notANumber2 is not null) { Describe("Test failed"); return; } Console.WriteLine(); Describe("Test successful", false); }, null)); }
private static void AddPositionTests(ICollection <ConsoleMenuItem> items) { items.Add(new ConsoleMenuItem <string>("ClearLine", async _ => { Console.Clear(); var words = new[] { "Some", " text", " should", " appear", " and", " disappear", " like", " magic." }; foreach (var word in words) { Console.Write(word); await Task.Delay(1000); } PowerConsole.ClearCurrentLine(); Describe("See only this... nice!!!"); }, null)); }
private static void ExtraSetupForNameBasedCrawler(Crawler crawler, CrawlContext context) { string recordFilePath = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "AllCheckinRandomNameRecord-" + context.SequenceType + ".txt"); IDictionary<string, bool> processedKeywords = new Dictionary<string, bool>(); if (File.Exists(recordFilePath)) { using (StreamReader reader = new StreamReader(recordFilePath)) { string line = null; while ((line = reader.ReadLine()) != null) { processedKeywords[line] = true; } } } Console.WriteLine("{0} processed keywords found!", processedKeywords.Count); crawler.BeforeCrawl += (sender, e) => { var keyword = e.Progress.CurrentKeyword; if (processedKeywords.ContainsKey(keyword)) { context.CancelStatQueue.Enqueue(true); e.Cancel = true; var progress = e.Progress; PowerConsole.WarningF( GetMessageFormat(context.MaximumTry), progress.Current, progress.Total, context.CancelRate, progress.CurrentKeyword, progress.Message); } else { context.CancelStatQueue.Enqueue(false); processedKeywords[keyword] = true; } }; crawler.AfterCrawl += (sender, e) => { if (e.Error == null) { File.AppendAllText(recordFilePath, e.Progress.CurrentKeyword + Environment.NewLine); } }; }
private static void AddWindowTests(ICollection <ConsoleMenuItem> items) { if (OperatingSystem.IsWindows()) { items.Add(new ConsoleMenuItem <string>("Hiding", async _ => { if (!OperatingSystem.IsWindows()) { return; } Describe("Console should hide for 2 seconds"); PowerConsole.HideWindow(); await Task.Delay(2000); PowerConsole.ShowWindow(); }, null)); } }
private static void AddDecisionTests(ICollection <ConsoleMenuItem> items) { items.Add(new ConsoleMenuItem <string>("Decision", _ => { var trueResult = PowerConsole.EnsureDecision("Select Yes here."); if (!trueResult) { Describe("Test failed"); return; } var falseResult = PowerConsole.EnsureDecision("Select No here."); if (falseResult) { Describe("Test failed"); return; } Describe("Test successful"); }, null)); }
private static void AddNumbersTests(ICollection <ConsoleMenuItem> items) { items.Add(new ConsoleMenuItem <string>("Numbers", _ => { Console.WriteLine(); var answer = PowerConsole.EnsureIntInput("Input 42 here"); if (answer != 42) { Describe("Test failed"); return; } var seven = PowerConsole.EnsureIntInputInRange("Numbers 5 - 10 are working. Try outside outside as well, but use 7 finally", 5, 10); if (seven != 7) { Describe("Test failed"); return; } Console.WriteLine(); Describe("Test successful", false); }, null)); }
private static void AddTextTests(ICollection <ConsoleMenuItem> items) { items.Add(new ConsoleMenuItem <string>("Text", _ => { Console.WriteLine(); var aaa = PowerConsole.EnsureTextInput("Input 'aaa' here"); if (aaa != "aaa") { Describe("Test failed"); return; } var aa = PowerConsole.EnsureTextInput("Fail with 'a' here, then input 'aa'", 2); if (aa != "aa") { Describe("Test failed"); return; } Console.WriteLine(); Describe("Test successful", false); }, null)); }
private static void AddSelectionTests(ICollection <ConsoleMenuItem> items) { items.Add(new ConsoleMenuItem <string>("Selection", _ => { var persons = new[] { "Bob", "Alice", "Tim" }; var alice = PowerConsole.EnsureSelection("Select Alice here", persons, x => x); if (alice != "Alice") { Describe("Test failed"); return; } var tim = PowerConsole.AbortableSelection("Select Tim here", persons, x => x); if (tim != "Tim") { Describe("Test failed"); return; } var nobody = PowerConsole.AbortableSelection("Select Abort here", persons, x => x); if (nobody is not null) { Describe("Test failed"); return; } Describe("Test successful"); }, null)); }
static void Main(string[] args) { PowerConsole.BeepOnMessageStatus = MessageStatus.Error; PowerConsole.WriteLine("Hello", MessageStatus.Info); PowerConsole.ReadLine <bool>("Enter bool: ", ConsoleColor.Yellow); }