/taggy/CLI/Commands.cs

https://github.com/stolksdorf/taggy · C# · 296 lines · 13 code · 5 blank · 278 comment · 0 complexity · 1d50ca60c69d524d0b0135fab1c39bbc MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. namespace taggy
  7. {
  8. public static class Commands
  9. {
  10. public static int CurrentIndex = 0;
  11. public static Action<string,string> LastCommand;
  12. /*
  13. public static void help(string options, string parameter)
  14. {
  15. Console.WriteLine("List of Commands:");
  16. foreach (CommandTemplate cmd in Global.Commands)
  17. {
  18. Console.Write(" ");
  19. Console.WriteLine("{0} ({1}) : {2}",cmd.Name, cmd.Shortcut , cmd.Info);
  20. }
  21. }
  22. public static void search(string options, string parameter)
  23. {
  24. bool quiet = options.Contains('q');
  25. bool emptyGenre = options.Contains('g');
  26. bool emptyArtist = options.Contains('a');
  27. bool emptyTitle = options.Contains('t') ;
  28. bool displayList = options.Contains('d');
  29. bool noSearch = parameter == "";
  30. bool noOptions = (!emptyGenre && !emptyArtist && !emptyTitle);
  31. parameter = parameter.ToLower();
  32. int total = 0;
  33. List<Song> tempList = new List<Song>();
  34. Global.Queue.Clear();
  35. if (displayList) tempList = Global.Queue;
  36. else tempList = Global.Songs;
  37. for (int i = 0; i < tempList.Count; i++)
  38. {
  39. bool searchCheck = false;
  40. bool optionsCheck = false;
  41. if (tempList[i].Artist == null)
  42. {
  43. if (emptyArtist) optionsCheck = true;
  44. }
  45. else if (tempList[i].Artist.ToLower().Contains(parameter) || parameter == "")
  46. searchCheck = true;
  47. if (tempList[i].Title == null)
  48. {
  49. if (emptyTitle) optionsCheck = true;
  50. }
  51. else if (tempList[i].Title.ToLower().Contains(parameter) || parameter == "")
  52. searchCheck = true;
  53. if (tempList[i].Genre == null)
  54. {
  55. if (emptyGenre) optionsCheck = true;
  56. }
  57. else if (tempList[i].Genre.ToLower().Contains(parameter) || parameter == "")
  58. searchCheck = true;
  59. if (tempList[i].FileName.ToLower().Contains(parameter) || parameter == "")
  60. searchCheck = true;
  61. if ((noSearch || searchCheck) && (noOptions || optionsCheck))
  62. {
  63. if (!quiet) Console.WriteLine("{0}: {1}", total, tempList[i].FileName);
  64. Global.Queue.Add(tempList[i]);
  65. total++;
  66. }
  67. }
  68. Console.WriteLine();
  69. Console.WriteLine("{0} songs in queue.", total);
  70. }
  71. public static void info(string options, string parameter)
  72. {
  73. int result;
  74. if (int.TryParse(parameter, out result) || parameter == "")
  75. {
  76. if(parameter == "") result = CurrentIndex;
  77. if (result < Global.Queue.Count && result >= 0)
  78. {
  79. CurrentIndex = result;
  80. LastCommand = Commands.info;
  81. Song temp = Global.Queue[result];
  82. Console.WriteLine("Title: " + temp.Title);
  83. Console.WriteLine("Artist: " + temp.Artist);
  84. Console.WriteLine("Genre: " + temp.Genre);
  85. Console.WriteLine("Path: " + temp.Path);
  86. }else
  87. Console.WriteLine("Could not parse parameter: " + parameter);
  88. }
  89. else
  90. Console.WriteLine("Could not parse parameter: " + parameter);
  91. }
  92. public static void clear(string options, string parameter)
  93. {
  94. Console.Clear();
  95. }
  96. public static void play(string options, string parameter)
  97. {
  98. int result;
  99. if (int.TryParse(parameter, out result) || parameter == "")
  100. {
  101. if (parameter == "") result = CurrentIndex;
  102. CurrentIndex = result;
  103. //LastCommand = Commands.play;
  104. Console.WriteLine("Playing {0}.", Global.Queue[result].FileName);
  105. Global.Queue[result].Play();
  106. }
  107. else
  108. Console.WriteLine("Could not parse parameter: " + parameter);
  109. }
  110. public static void delete(string options, string parameter)
  111. {
  112. int result;
  113. if (int.TryParse(parameter, out result) || parameter == "")
  114. {
  115. if (parameter == "") result = CurrentIndex;
  116. CurrentIndex = result;
  117. Console.Write("Are you sure you want to delete " + Global.Queue[result].Path + " (y/n)? ");
  118. string ans = Console.ReadLine();
  119. if (ans.ToLower() == "y" || ans.ToLower() == "yes")
  120. {
  121. Global.Songs.Remove(Global.Queue[result]);
  122. Global.Queue.RemoveAt(result);
  123. File.Delete(Global.Queue[result].Path);
  124. Console.WriteLine("Deleted.");
  125. }
  126. }
  127. else
  128. Console.WriteLine("Could not parse parameter: " + parameter);
  129. }
  130. public static void edit(string options, string parameter)
  131. {
  132. bool noOptions = options == "";
  133. int index;
  134. string input;
  135. int result;
  136. if (int.TryParse(parameter, out result))
  137. CurrentIndex = result;
  138. index = CurrentIndex;
  139. if (noOptions || options.Contains('t'))
  140. {
  141. Console.Write("Title: ");
  142. input = Console.ReadLine();
  143. if (input != "")
  144. Global.Queue[index].Title = input;
  145. else
  146. {
  147. Console.CursorTop = Console.CursorTop - 1;
  148. Console.CursorLeft = 7;
  149. Console.WriteLine(Global.Queue[index].Title);
  150. }
  151. }
  152. if (noOptions || options.Contains('a'))
  153. {
  154. Console.Write("Artist: ");
  155. input = Console.ReadLine();
  156. if (input != "")
  157. Global.Queue[index].Artist = input;
  158. else
  159. {
  160. Console.CursorTop = Console.CursorTop - 1;
  161. Console.CursorLeft = 8;
  162. Console.WriteLine(Global.Queue[index].Artist);
  163. }
  164. }
  165. if (noOptions || options.Contains('g'))
  166. {
  167. Console.Write("Genre: ");
  168. input = Console.ReadLine();
  169. if (input != "")
  170. Global.Queue[index].Genre = input;
  171. else
  172. {
  173. Console.CursorTop = Console.CursorTop - 1;
  174. Console.CursorLeft = 7;
  175. Console.WriteLine(Global.Queue[index].Genre);
  176. }
  177. }
  178. Console.WriteLine();
  179. Console.WriteLine("Song Updated!");
  180. }
  181. public static void next(string options, string parameter)
  182. {
  183. CurrentIndex++;
  184. if (CurrentIndex >= Global.Queue.Count) CurrentIndex = 0;
  185. if (CurrentIndex < 0) CurrentIndex = 0;
  186. //LastCommand("",CurrentIndex.ToString());
  187. info("", CurrentIndex.ToString());
  188. }
  189. public static void rename(string options, string parameter)
  190. {
  191. //if(options.Contains("a"))
  192. int total = 0;
  193. Console.WriteLine("Renaming all songs in the queue");
  194. foreach (Song song in Global.Queue)
  195. {
  196. string newName = song.Artist + " - " + song.Title + ".mp3";
  197. if (newName != song.FileName && song.Title != "" && song.Artist != "")
  198. {
  199. string newPath = song.Path.Replace(song.FileName, newName);
  200. Console.WriteLine("{0} -> {1}", song.FileName, newName);
  201. System.IO.File.Move(song.Path, newPath);
  202. song.Path = newPath;
  203. song.FileName = newName;
  204. total++;
  205. }
  206. }
  207. Console.WriteLine();
  208. Console.WriteLine("{0} Songs renamed.", total);
  209. }
  210. /*
  211. public static void search(string options, string parameter)
  212. {
  213. bool quiet = false;
  214. Global.Queue.Clear();
  215. int total = 0;
  216. if (parameter != "")
  217. {
  218. foreach (Song song in Global.Songs)
  219. {
  220. bool check = false;
  221. if (song.Artist != null)
  222. if (song.Artist.ToLower().Contains(parameter))
  223. check = true;
  224. if (song.Genre != null)
  225. if (song.Genre.ToLower().Contains(parameter))
  226. check = true;
  227. if (song.Title != null)
  228. if (song.Title.ToLower().Contains(parameter))
  229. check = true;
  230. if (song.Path != null)
  231. if (song.Path.ToLower().Contains(parameter))
  232. check = true;
  233. if (check)
  234. {
  235. Global.Queue.Add(song);
  236. if (!quiet) Console.WriteLine("{0}: {1}", total, song.FileName);
  237. total++;
  238. }
  239. }
  240. Console.WriteLine();
  241. Console.WriteLine("{0} songs added to queue.", total);
  242. }
  243. }
  244. /*
  245. public static void temp(string options, string parameter)
  246. {
  247. }
  248. *
  249. * */
  250. }
  251. }