IHasDescription.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. using System.Text;
  3. namespace Animancer
  4. {
  5. /// <summary>An object which can give a detailed description of itself.</summary>
  6. /// https://kybernetik.com.au/animancer/api/Animancer/IHasDescription
  7. ///
  8. public interface IHasDescription
  9. {
  10. /************************************************************************************************************************/
  11. /// <summary>Appends a detailed descrption of the current details of this object.</summary>
  12. /// <remarks>
  13. /// <see cref="AnimancerUtilities.GetDescription"/> calls this method with a pooled
  14. /// <see cref="StringBuilder"/>.
  15. /// </remarks>
  16. void AppendDescription(StringBuilder text, string separator = "\n");
  17. /************************************************************************************************************************/
  18. }
  19. /// https://kybernetik.com.au/animancer/api/Animancer/AnimancerUtilities
  20. ///
  21. public static partial class AnimancerUtilities
  22. {
  23. /************************************************************************************************************************/
  24. /// <summary>
  25. /// Calls <see cref="IHasDescription.AppendDescription"/> with a pooled <see cref="StringBuilder"/>.
  26. /// </summary>
  27. public static string GetDescription(
  28. this IHasDescription hasDescription,
  29. string separator = "\n")
  30. {
  31. if (hasDescription == null)
  32. return "Null";
  33. var text = StringBuilderPool.Instance.Acquire();
  34. hasDescription.AppendDescription(text, separator);
  35. return text.ReleaseToString();
  36. }
  37. /************************************************************************************************************************/
  38. /// <summary>
  39. /// Appends "Null" if `maybeHasDescription` is null. Otherwise calls
  40. /// <see cref="IHasDescription.AppendDescription"/>.
  41. /// </summary>
  42. public static StringBuilder AppendDescription<T>(
  43. this StringBuilder text,
  44. T maybeHasDescription,
  45. string separator = "\n",
  46. bool fullNodeDescription = false)
  47. => maybeHasDescription is IHasDescription hasDescription
  48. ? text.AppendDescription(hasDescription, separator, fullNodeDescription)
  49. : text.Append(ToStringOrNull(maybeHasDescription));
  50. /// <summary>
  51. /// Appends "Null" if `hasDescription` is null. Otherwise calls
  52. /// <see cref="IHasDescription.AppendDescription"/>.
  53. /// </summary>
  54. public static StringBuilder AppendDescription(
  55. this StringBuilder text,
  56. IHasDescription hasDescription,
  57. string separator = "\n",
  58. bool fullNodeDescription = false)
  59. {
  60. if (hasDescription == null)
  61. return text.Append("Null");
  62. if (!fullNodeDescription && hasDescription is AnimancerNode node)
  63. return text.Append(node.GetPath());
  64. hasDescription.AppendDescription(text, separator);
  65. return text;
  66. }
  67. /************************************************************************************************************************/
  68. /// <summary>Appends <c>{prefix}{name}: {value}</c>.</summary>
  69. public static StringBuilder AppendField<T>(
  70. this StringBuilder text,
  71. string prefix,
  72. string name,
  73. T value,
  74. string separator = "\n",
  75. bool fullNodeDescription = false)
  76. => text
  77. .Append(prefix)
  78. .Append(name)
  79. .Append(": ")
  80. .AppendDescription(value, separator, fullNodeDescription);
  81. /************************************************************************************************************************/
  82. /// <summary>Does the `text` start with a new line character?</summary>
  83. public static bool StartsWithNewLine(this string text)
  84. {
  85. if (text == null || text.Length == 0)
  86. return false;
  87. var start = text[0];
  88. return start == '\n' || start == '\r';
  89. }
  90. /************************************************************************************************************************/
  91. }
  92. }