Script for automating a large assortment of AME related actions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
3.1 KiB

9 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Ameliorated.ConsoleUtils
  5. {
  6. public static class Extensions
  7. {
  8. public static bool IsEven(this int number)
  9. {
  10. return number % 2 == 0;
  11. }
  12. /// <summary>
  13. /// Checks if number is divisble by a divisor.
  14. /// <param name="divisor">Divisor.</param>
  15. /// <param name="returnFalseOnZeroOrNegative">Indicates whether to return false on numbers that are zero or below. Default is true.</param>
  16. /// </summary>
  17. public static bool IsDivisibleBy(this int number, int divisor, bool returnFalseOnZeroOrNegative = true)
  18. {
  19. if (returnFalseOnZeroOrNegative && number <= 0) return false;
  20. return number % divisor == 0;
  21. }
  22. public static int RoundToEven(this double number)
  23. {
  24. var rounded = (int)Math.Round(number);
  25. if (!rounded.IsEven()) return rounded - 1;
  26. return rounded;
  27. }
  28. public static int RoundToEven(this int number)
  29. {
  30. if (!number.IsEven()) return number - 1;
  31. return number;
  32. }
  33. public static int RoundToOdd(this double number)
  34. {
  35. if (!((int)Math.Ceiling(number)).IsEven()) return (int)Math.Ceiling(number);
  36. if (!((int)Math.Floor(number)).IsEven()) return (int)Math.Floor(number);
  37. return (int)Math.Truncate(number) - 1;
  38. }
  39. public static int RoundToOdd(this int number)
  40. {
  41. if (!number.IsEven()) return number - 1;
  42. return number;
  43. }
  44. public static string[] SplitByLine(this string text, StringSplitOptions options = StringSplitOptions.None)
  45. {
  46. return text.Split(new[]
  47. { "\r\n", "\n" }, options);
  48. }
  49. public static string LastLine(this string text, StringSplitOptions options = StringSplitOptions.None)
  50. {
  51. return text.SplitByLine().Last();
  52. }
  53. public static void ReplaceItem(this List<Menu.MenuItem> list, Menu.MenuItem oldItem, Menu.MenuItem newItem)
  54. {
  55. var index = list.IndexOf(oldItem);
  56. if (index == -1) throw new ArgumentException("Could not find item index.");
  57. list.RemoveAt(index);
  58. list.Insert(index, newItem);
  59. }
  60. public static Menu.MenuItem Clone(this Menu.MenuItem item)
  61. {
  62. return new Menu.MenuItem(item.PrimaryText, item.ReturnValue)
  63. {
  64. AddBetweenSpace = item.AddBetweenSpace,
  65. IsEnabled = item.IsEnabled,
  66. IsNextButton = item.IsNextButton,
  67. IsPreviousButton = item.IsPreviousButton,
  68. IsStatic = item.IsStatic,
  69. PrimaryTextBackground = item.PrimaryTextBackground,
  70. PrimaryTextForeground = item.PrimaryTextForeground,
  71. SecondaryText = item.SecondaryText,
  72. SecondaryTextBackground = item.SecondaryTextBackground,
  73. SecondaryTextForeground = item.SecondaryTextForeground,
  74. StretchSelection = item.StretchSelection,
  75. };
  76. }
  77. }
  78. }