static void Main(string[] args) { // Parse and validate command line args. ArgValidator argValidator = new ArgValidator(); bool? validatedArgs = argValidator.Validate(args); if (validatedArgs != true) { // If there are parse errors display syntax help. if (validatedArgs == false) { ShowHelp(); } else { // User input a filename to save the data and the file already exists. // when prompted to overwrite the file, the user said no. Console.WriteLine("\n\n** Query cancelled.\n"); } Environment.Exit(0); } // Query the remote site. TweetDataClient tdc = new TweetDataClient(); IEnumerable <Tweet> queryResult = null; try { queryResult = tdc.GetItemsFromUrl(argValidator.Url, argValidator.StartDate, argValidator.EndDate, argValidator.Verbose); } catch { Console.WriteLine("\n**Query Error - Cannot query the remote host\n"); } int recsSaved = 0; if (queryResult != null && queryResult.Count() > 0 && !argValidator.NoList) { ShowPaginatedList(queryResult, argValidator); if (argValidator.Filename != String.Empty) { recsSaved = SaveToFile(argValidator.Filename, queryResult); } } // Show results summary. if (argValidator.PageSize == 0 || !string.IsNullOrEmpty(argValidator.Filename) || argValidator.NoList) { int recsFound = queryResult?.Count() ?? 0; Console.WriteLine("\n============================================================"); Console.WriteLine($"Query from: {argValidator.StartDate} to {argValidator.EndDate}"); Console.WriteLine($"Records Found: {recsFound}"); if (!string.IsNullOrEmpty(argValidator.Filename)) { Console.WriteLine($"Records Saved: {recsSaved}"); } Console.WriteLine("============================================================"); } }
private static void ShowPaginatedList(IEnumerable <Tweet> queryResult, ArgValidator argValidator) { List <Tweet> results = queryResult.ToList(); // Set start index for page. int startIndex = -1; int endIndex = 0; // Show page PageKey key = PageKey.Home; do { string keyNext = " next - >"; string keyPrvious = "< - previous "; bool updateScreen = false; switch (key) { // Jump to end. case PageKey.End: if (endIndex != results.Count) { endIndex = results.Count; startIndex = endIndex - argValidator.PageSize; if (startIndex < 0) { startIndex = 0; } keyNext = ""; updateScreen = true; } break; // Next page. case PageKey.Next: if (endIndex != results.Count) { if (startIndex + argValidator.PageSize < results.Count) { startIndex += argValidator.PageSize; endIndex += argValidator.PageSize; if (endIndex > results.Count) { keyNext = ""; } updateScreen = true; } } break; // Jump to beginning. case PageKey.Home: if (startIndex != 0) { Console.Clear(); startIndex = 0; endIndex = startIndex + ((argValidator.PageSize == 0) ? results.Count : argValidator.PageSize); updateScreen = true; keyPrvious = ""; } break; // Previou page. case PageKey.Previous: if (startIndex != 0) { startIndex -= argValidator.PageSize; if (startIndex <= 0) { startIndex = 0; keyPrvious = ""; } endIndex = startIndex + argValidator.PageSize; updateScreen = true; } break; // Show key help on invalid keystrokes. default: Console.WriteLine("*** Valid keys:"); Console.WriteLine("N-next P-previous HOME-begin END-end Q-quit\n"); updateScreen = false; break; } for (int i = startIndex; i < endIndex && i < results.Count() && updateScreen; i++) { Console.WriteLine($"{results[i].Id} {results[i].Stamp}\n{results[i].Text}"); Console.WriteLine("------------------------------------------------------------"); } // Display page number information if (argValidator.PageSize != 0) { if (updateScreen) { Console.WriteLine($"\nQuery from: {argValidator.StartDate} to {argValidator.EndDate}"); int endNum = (endIndex > results.Count) ? results.Count : endIndex; Console.WriteLine( $"Records {startIndex + 1} - {endNum} of {results.Count} {keyPrvious}{keyNext}\n"); } key = CheckKey(Console.ReadKey()); } else { key = PageKey.Quit; } } while (key != PageKey.Quit); }