Sub.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace TodoList
  7. {
  8. [Command(
  9. Name: "sub",
  10. ShortDescription: "Make a todo task a child of another task.",
  11. ErrorText: "",
  12. LongHelpText: "Tasks exist in a tree. Use this command to move tasks from one parent to another."
  13. )]
  14. internal class Sub : ICommand
  15. {
  16. [SwitchDocumentation("The ID of the task to move.")]
  17. [DefaultSwitch(0)] public UInt32 id = 0;
  18. [DefaultSwitch(1)] public UInt32 parent = 0;
  19. [SwitchDocumentation("Path to task file.")]
  20. public string file = "todo.txt";
  21. public void Invoke(Dictionary<String, Object> PipedArguments)
  22. {
  23. if (String.IsNullOrEmpty(file))
  24. {
  25. Console.WriteLine("No file specified. How did you manage that? It defaults to todo.txt");
  26. return;
  27. }
  28. if (id == 0)
  29. {
  30. Console.WriteLine("You need to specify the entry you're editing.");
  31. return;
  32. }
  33. if (id == parent)
  34. {
  35. Console.WriteLine("Not going to work.");
  36. return;
  37. }
  38. var list = EntryList.LoadFile(file, true);
  39. var entry = list.Root.EnumerateParentChildPairs().FirstOrDefault(e => e.Child.ID == id);
  40. if (entry.Parent == null || entry.Child == null)
  41. {
  42. Console.WriteLine("Could not find entry with ID{0}.", id);
  43. return;
  44. }
  45. var newParent = list.Root.FindChildWithID(parent);
  46. if (newParent == null)
  47. {
  48. Console.WriteLine("Could not find entry with ID{0}.", id);
  49. return;
  50. }
  51. if (entry.Child.FindChildWithID(parent) != null)
  52. {
  53. Console.WriteLine("That would create a circular reference.");
  54. return;
  55. }
  56. entry.Parent.Children.Remove(entry.Child);
  57. newParent.Children.Add(entry.Child);
  58. EntryList.SaveFile(file, list);
  59. Presentation.OutputEntry(newParent, new StatusMatcher { Status = "-" }, 0);
  60. }
  61. }
  62. }