ArgValidators.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using Terminal.Gui.TextEffects;
  7. using Color = Terminal.Gui.TextEffects.Color;
  8. public static class PositiveInt
  9. {
  10. public static int Parse (string arg)
  11. {
  12. if (int.TryParse (arg, out int value) && value > 0)
  13. {
  14. return value;
  15. }
  16. else
  17. {
  18. throw new ArgumentException ($"invalid value: '{arg}' is not > 0.");
  19. }
  20. }
  21. }
  22. public static class NonNegativeInt
  23. {
  24. public static int Parse (string arg)
  25. {
  26. if (int.TryParse (arg, out int value) && value >= 0)
  27. {
  28. return value;
  29. }
  30. else
  31. {
  32. throw new ArgumentException ($"invalid value: '{arg}' Argument must be int >= 0.");
  33. }
  34. }
  35. }
  36. public static class IntRange
  37. {
  38. public static (int, int) Parse (string arg)
  39. {
  40. var parts = arg.Split ('-');
  41. if (parts.Length == 2 && int.TryParse (parts [0], out int start) && int.TryParse (parts [1], out int end) && start > 0 && start <= end)
  42. {
  43. return (start, end);
  44. }
  45. else
  46. {
  47. throw new ArgumentException ($"invalid range: '{arg}' is not a valid range. Must be start-end. Ex: 1-10");
  48. }
  49. }
  50. }
  51. public static class PositiveFloat
  52. {
  53. public static float Parse (string arg)
  54. {
  55. if (float.TryParse (arg, out float value) && value > 0)
  56. {
  57. return value;
  58. }
  59. else
  60. {
  61. throw new ArgumentException ($"invalid value: '{arg}' is not a valid value. Argument must be a float > 0.");
  62. }
  63. }
  64. }
  65. public static class NonNegativeFloat
  66. {
  67. public static float Parse (string arg)
  68. {
  69. if (float.TryParse (arg, out float value) && value >= 0)
  70. {
  71. return value;
  72. }
  73. else
  74. {
  75. throw new ArgumentException ($"invalid argument value: '{arg}' is out of range. Must be float >= 0.");
  76. }
  77. }
  78. }
  79. public static class PositiveFloatRange
  80. {
  81. public static (float, float) Parse (string arg)
  82. {
  83. var parts = arg.Split ('-');
  84. if (parts.Length == 2 && float.TryParse (parts [0], out float start) && float.TryParse (parts [1], out float end) && start > 0 && start <= end)
  85. {
  86. return (start, end);
  87. }
  88. else
  89. {
  90. throw new ArgumentException ($"invalid range: '{arg}' is not a valid range. Must be start-end. Ex: 0.1-1.0");
  91. }
  92. }
  93. }
  94. public static class Ratio
  95. {
  96. public static float Parse (string arg)
  97. {
  98. if (float.TryParse (arg, out float value) && value >= 0 && value <= 1)
  99. {
  100. return value;
  101. }
  102. else
  103. {
  104. throw new ArgumentException ($"invalid value: '{arg}' is not a float >= 0 and <= 1. Example: 0.5");
  105. }
  106. }
  107. }
  108. public static class GradientDirectionParser
  109. {
  110. public static Gradient.Direction Parse (string arg)
  111. {
  112. return arg.ToLower () switch
  113. {
  114. "horizontal" => Gradient.Direction.Horizontal,
  115. "vertical" => Gradient.Direction.Vertical,
  116. "diagonal" => Gradient.Direction.Diagonal,
  117. "radial" => Gradient.Direction.Radial,
  118. _ => throw new ArgumentException ($"invalid gradient direction: '{arg}' is not a valid gradient direction. Choices are diagonal, horizontal, vertical, or radial."),
  119. };
  120. }
  121. }
  122. public static class ColorArg
  123. {
  124. public static Color Parse (string arg)
  125. {
  126. if (int.TryParse (arg, out int xtermValue) && xtermValue >= 0 && xtermValue <= 255)
  127. {
  128. return new Color (xtermValue);
  129. }
  130. else if (arg.Length == 6 && int.TryParse (arg, NumberStyles.HexNumber, null, out int _))
  131. {
  132. return new Color (arg);
  133. }
  134. else
  135. {
  136. throw new ArgumentException ($"invalid color value: '{arg}' is not a valid XTerm or RGB color. Must be in range 0-255 or 000000-FFFFFF.");
  137. }
  138. }
  139. }
  140. public static class Symbol
  141. {
  142. public static string Parse (string arg)
  143. {
  144. if (arg.Length == 1 && IsAsciiOrUtf8 (arg))
  145. {
  146. return arg;
  147. }
  148. else
  149. {
  150. throw new ArgumentException ($"invalid symbol: '{arg}' is not a valid symbol. Must be a single ASCII/UTF-8 character.");
  151. }
  152. }
  153. private static bool IsAsciiOrUtf8 (string s)
  154. {
  155. try
  156. {
  157. Encoding.ASCII.GetBytes (s);
  158. }
  159. catch (EncoderFallbackException)
  160. {
  161. try
  162. {
  163. Encoding.UTF8.GetBytes (s);
  164. }
  165. catch (EncoderFallbackException)
  166. {
  167. return false;
  168. }
  169. }
  170. return true;
  171. }
  172. }
  173. public static class CanvasDimension
  174. {
  175. public static int Parse (string arg)
  176. {
  177. if (int.TryParse (arg, out int value) && value >= -1)
  178. {
  179. return value;
  180. }
  181. else
  182. {
  183. throw new ArgumentException ($"invalid value: '{arg}' is not >= -1.");
  184. }
  185. }
  186. }
  187. public static class TerminalDimensions
  188. {
  189. public static (int, int) Parse (string arg)
  190. {
  191. var parts = arg.Split (' ');
  192. if (parts.Length == 2 && int.TryParse (parts [0], out int width) && int.TryParse (parts [1], out int height) && width >= 0 && height >= 0)
  193. {
  194. return (width, height);
  195. }
  196. else
  197. {
  198. throw new ArgumentException ($"invalid terminal dimensions: '{arg}' is not a valid terminal dimension. Must be >= 0.");
  199. }
  200. }
  201. }
  202. public static class Ease
  203. {
  204. private static readonly Dictionary<string, EasingFunction> easingFuncMap = new ()
  205. {
  206. {"linear", Easing.Linear},
  207. {"in_sine", Easing.InSine},
  208. {"out_sine", Easing.OutSine},
  209. {"in_out_sine", Easing.InOutSine},
  210. {"in_quad", Easing.InQuad},
  211. {"out_quad", Easing.OutQuad},
  212. {"in_out_quad", Easing.InOutQuad},
  213. {"in_cubic", Easing.InCubic},
  214. {"out_cubic", Easing.OutCubic},
  215. {"in_out_cubic", Easing.InOutCubic},
  216. {"in_quart", Easing.InQuart},
  217. {"out_quart", Easing.OutQuart},
  218. {"in_out_quart", Easing.InOutQuart},
  219. {"in_quint", Easing.InQuint},
  220. {"out_quint", Easing.OutQuint},
  221. {"in_out_quint", Easing.InOutQuint},
  222. {"in_expo", Easing.InExpo},
  223. {"out_expo", Easing.OutExpo},
  224. {"in_out_expo", Easing.InOutExpo},
  225. {"in_circ", Easing.InCirc},
  226. {"out_circ", Easing.OutCirc},
  227. {"in_out_circ", Easing.InOutCirc},
  228. {"in_back", Easing.InBack},
  229. {"out_back", Easing.OutBack},
  230. {"in_out_back", Easing.InOutBack},
  231. {"in_elastic", Easing.InElastic},
  232. {"out_elastic", Easing.OutElastic},
  233. {"in_out_elastic", Easing.InOutElastic},
  234. {"in_bounce", Easing.InBounce},
  235. {"out_bounce", Easing.OutBounce},
  236. {"in_out_bounce", Easing.InOutBounce},
  237. };
  238. public static EasingFunction Parse (string arg)
  239. {
  240. if (easingFuncMap.TryGetValue (arg.ToLower (), out var easingFunc))
  241. {
  242. return easingFunc;
  243. }
  244. else
  245. {
  246. throw new ArgumentException ($"invalid ease value: '{arg}' is not a valid ease.");
  247. }
  248. }
  249. }