ArgValidators.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 enum GradientDirection
  109. {
  110. Horizontal,
  111. Vertical,
  112. Diagonal,
  113. Radial
  114. }
  115. public static class GradientDirectionParser
  116. {
  117. public static GradientDirection Parse (string arg)
  118. {
  119. return arg.ToLower () switch
  120. {
  121. "horizontal" => GradientDirection.Horizontal,
  122. "vertical" => GradientDirection.Vertical,
  123. "diagonal" => GradientDirection.Diagonal,
  124. "radial" => GradientDirection.Radial,
  125. _ => throw new ArgumentException ($"invalid gradient direction: '{arg}' is not a valid gradient direction. Choices are diagonal, horizontal, vertical, or radial."),
  126. };
  127. }
  128. }
  129. public static class ColorArg
  130. {
  131. public static Color Parse (string arg)
  132. {
  133. if (int.TryParse (arg, out int xtermValue) && xtermValue >= 0 && xtermValue <= 255)
  134. {
  135. return new Color (xtermValue);
  136. }
  137. else if (arg.Length == 6 && int.TryParse (arg, NumberStyles.HexNumber, null, out int _))
  138. {
  139. return new Color (arg);
  140. }
  141. else
  142. {
  143. throw new ArgumentException ($"invalid color value: '{arg}' is not a valid XTerm or RGB color. Must be in range 0-255 or 000000-FFFFFF.");
  144. }
  145. }
  146. }
  147. public static class Symbol
  148. {
  149. public static string Parse (string arg)
  150. {
  151. if (arg.Length == 1 && IsAsciiOrUtf8 (arg))
  152. {
  153. return arg;
  154. }
  155. else
  156. {
  157. throw new ArgumentException ($"invalid symbol: '{arg}' is not a valid symbol. Must be a single ASCII/UTF-8 character.");
  158. }
  159. }
  160. private static bool IsAsciiOrUtf8 (string s)
  161. {
  162. try
  163. {
  164. Encoding.ASCII.GetBytes (s);
  165. }
  166. catch (EncoderFallbackException)
  167. {
  168. try
  169. {
  170. Encoding.UTF8.GetBytes (s);
  171. }
  172. catch (EncoderFallbackException)
  173. {
  174. return false;
  175. }
  176. }
  177. return true;
  178. }
  179. }
  180. public static class CanvasDimension
  181. {
  182. public static int Parse (string arg)
  183. {
  184. if (int.TryParse (arg, out int value) && value >= -1)
  185. {
  186. return value;
  187. }
  188. else
  189. {
  190. throw new ArgumentException ($"invalid value: '{arg}' is not >= -1.");
  191. }
  192. }
  193. }
  194. public static class TerminalDimensions
  195. {
  196. public static (int, int) Parse (string arg)
  197. {
  198. var parts = arg.Split (' ');
  199. if (parts.Length == 2 && int.TryParse (parts [0], out int width) && int.TryParse (parts [1], out int height) && width >= 0 && height >= 0)
  200. {
  201. return (width, height);
  202. }
  203. else
  204. {
  205. throw new ArgumentException ($"invalid terminal dimensions: '{arg}' is not a valid terminal dimension. Must be >= 0.");
  206. }
  207. }
  208. }
  209. public static class Ease
  210. {
  211. private static readonly Dictionary<string, EasingFunction> easingFuncMap = new ()
  212. {
  213. {"linear", Easing.Linear},
  214. {"in_sine", Easing.InSine},
  215. {"out_sine", Easing.OutSine},
  216. {"in_out_sine", Easing.InOutSine},
  217. {"in_quad", Easing.InQuad},
  218. {"out_quad", Easing.OutQuad},
  219. {"in_out_quad", Easing.InOutQuad},
  220. {"in_cubic", Easing.InCubic},
  221. {"out_cubic", Easing.OutCubic},
  222. {"in_out_cubic", Easing.InOutCubic},
  223. {"in_quart", Easing.InQuart},
  224. {"out_quart", Easing.OutQuart},
  225. {"in_out_quart", Easing.InOutQuart},
  226. {"in_quint", Easing.InQuint},
  227. {"out_quint", Easing.OutQuint},
  228. {"in_out_quint", Easing.InOutQuint},
  229. {"in_expo", Easing.InExpo},
  230. {"out_expo", Easing.OutExpo},
  231. {"in_out_expo", Easing.InOutExpo},
  232. {"in_circ", Easing.InCirc},
  233. {"out_circ", Easing.OutCirc},
  234. {"in_out_circ", Easing.InOutCirc},
  235. {"in_back", Easing.InBack},
  236. {"out_back", Easing.OutBack},
  237. {"in_out_back", Easing.InOutBack},
  238. {"in_elastic", Easing.InElastic},
  239. {"out_elastic", Easing.OutElastic},
  240. {"in_out_elastic", Easing.InOutElastic},
  241. {"in_bounce", Easing.InBounce},
  242. {"out_bounce", Easing.OutBounce},
  243. {"in_out_bounce", Easing.InOutBounce},
  244. };
  245. public static EasingFunction Parse (string arg)
  246. {
  247. if (easingFuncMap.TryGetValue (arg.ToLower (), out var easingFunc))
  248. {
  249. return easingFunc;
  250. }
  251. else
  252. {
  253. throw new ArgumentException ($"invalid ease value: '{arg}' is not a valid ease.");
  254. }
  255. }
  256. }