cache_command.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Text;
  5. using System.ConsoleApp;
  6. using System.ConsoleApp.Commands;
  7. namespace Speech.Commands {
  8. [ConsoleCommand("cache")]
  9. public class cache_command : BaseCommand {
  10. public static bool execute(string line, string command, NameValueCollection arg) {
  11. switch(arg["general"].ToLower()) {
  12. case "count":
  13. count(line, arg);
  14. break;
  15. case "clear":
  16. clear(line, arg);
  17. break;
  18. case "list":
  19. list(line, arg);
  20. break;
  21. default:
  22. return true;
  23. }
  24. return false;
  25. }
  26. public static string LineStartText() {
  27. return "Cache";
  28. }
  29. [ConsoleCommand("qqq")]
  30. [ConsoleCommand("exit")]
  31. [ConsoleCommand("quit")]
  32. public static bool exit() {
  33. return false;
  34. }
  35. [ConsoleCommand("count")]
  36. public static bool count(string line, NameValueCollection arg) {
  37. WriteLine("Items in cache: " + SpeechExecute.Cache.Count);
  38. return true;
  39. }
  40. [ConsoleCommand("clear")]
  41. public static bool clear(string line, NameValueCollection arg) {
  42. bool bRes = false;
  43. Input("Do you realy want to clear all cached items?", ref bRes);
  44. if (bRes) {
  45. SpeechExecute.Cache.Clear();
  46. }
  47. return true;
  48. }
  49. [ConsoleCommand("list")]
  50. public static bool list(string line, NameValueCollection arg) {
  51. BeginWrite();
  52. ConsoleColor cc = Console.ForegroundColor;
  53. int i = 0;
  54. Console.WriteLine("ID".PadRight(7) + " " + "Text".PadRight(60) + " " + "File".PadRight(40));
  55. Console.WriteLine(new string('=',113));
  56. foreach(SpeechCacheEntry e in SpeechExecute.Cache.Entry) {
  57. Console.ForegroundColor = ConsoleColor.Blue;
  58. Console.Write(i.ToString().PadLeft(7));
  59. Console.ForegroundColor = cc;
  60. if(e.Text.Length>60)
  61. Console.Write(" | " + e.Text.Substring(0, 57) + "...");
  62. else
  63. Console.Write(" | " + e.Text.PadRight(60));
  64. if (e.File.Length > 30)
  65. Console.Write(" | " + e.File.Substring(0, 40));
  66. else
  67. Console.Write(" | " + e.File.PadRight(40));
  68. Console.Write("\n");
  69. i++;
  70. }
  71. Console.ForegroundColor = cc;
  72. EndWrite();
  73. return true;
  74. }
  75. [ConsoleCommand("find")]
  76. public static bool find(string line, NameValueCollection arg) {
  77. BeginWrite();
  78. ConsoleColor cc = Console.ForegroundColor;
  79. int i = 0;
  80. Console.WriteLine("ID".PadRight(7) + " " + "Text".PadRight(60) + " " + "File".PadRight(40));
  81. Console.WriteLine(new string('=', 113));
  82. System.Text.RegularExpressions.Regex r;
  83. if(!String.IsNullOrEmpty(arg["regex"])) {
  84. r = new System.Text.RegularExpressions.Regex(arg["regex"]);
  85. } else if (!String.IsNullOrEmpty(arg["r"])) {
  86. r = new System.Text.RegularExpressions.Regex(arg["r"]);
  87. }
  88. foreach (SpeechCacheEntry e in SpeechExecute.Cache.Entry) {
  89. Console.ForegroundColor = ConsoleColor.Blue;
  90. Console.Write(i.ToString().PadLeft(7));
  91. Console.ForegroundColor = cc;
  92. if (e.Text.Length > 60)
  93. Console.Write(" | " + e.Text.Substring(0, 57) + "...");
  94. else
  95. Console.Write(" | " + e.Text.PadRight(60));
  96. if (e.File.Length > 30)
  97. Console.Write(" | " + e.File.Substring(0, 40));
  98. else
  99. Console.Write(" | " + e.File.PadRight(40));
  100. Console.Write("\n");
  101. i++;
  102. }
  103. Console.ForegroundColor = cc;
  104. EndWrite();
  105. return true;
  106. }
  107. }
  108. }