TextRegexProvider.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #nullable enable
  2. using System.Text.RegularExpressions;
  3. namespace Terminal.Gui.Views;
  4. /// <summary>Regex Provider for TextValidateField.</summary>
  5. public class TextRegexProvider : ITextValidateProvider
  6. {
  7. private List<Rune> _pattern = null!;
  8. private Regex _regex = null!;
  9. private List<Rune> _text = null!;
  10. /// <summary>Empty Constructor.</summary>
  11. public TextRegexProvider (string pattern) { Pattern = pattern; }
  12. /// <summary>Regex pattern property.</summary>
  13. public string Pattern
  14. {
  15. get => StringExtensions.ToString (_pattern);
  16. set
  17. {
  18. _pattern = value.ToRuneList ();
  19. CompileMask ();
  20. SetupText ();
  21. }
  22. }
  23. /// <summary>When true, validates with the regex pattern on each input, preventing the input if it's not valid.</summary>
  24. public bool ValidateOnInput { get; set; } = true;
  25. /// <inheritdoc/>
  26. public event EventHandler<EventArgs<string>> TextChanged = null!;
  27. /// <inheritdoc/>
  28. public string Text
  29. {
  30. get => StringExtensions.ToString (_text);
  31. set
  32. {
  33. _text = (value != string.Empty ? value.ToRuneList () : null)!;
  34. SetupText ();
  35. }
  36. }
  37. /// <inheritdoc/>
  38. public string DisplayText => Text;
  39. /// <inheritdoc/>
  40. public bool IsValid => Validate (_text);
  41. /// <inheritdoc/>
  42. public bool Fixed => false;
  43. /// <inheritdoc/>
  44. public int Cursor (int pos)
  45. {
  46. if (pos < 0)
  47. {
  48. return CursorStart ();
  49. }
  50. if (pos >= _text.Count)
  51. {
  52. return CursorEnd ();
  53. }
  54. return pos;
  55. }
  56. /// <inheritdoc/>
  57. public int CursorStart () { return 0; }
  58. /// <inheritdoc/>
  59. public int CursorEnd () { return _text.Count; }
  60. /// <inheritdoc/>
  61. public int CursorLeft (int pos)
  62. {
  63. if (pos > 0)
  64. {
  65. return pos - 1;
  66. }
  67. return pos;
  68. }
  69. /// <inheritdoc/>
  70. public int CursorRight (int pos)
  71. {
  72. if (pos < _text.Count)
  73. {
  74. return pos + 1;
  75. }
  76. return pos;
  77. }
  78. /// <inheritdoc/>
  79. public bool Delete (int pos)
  80. {
  81. if (_text.Count > 0 && pos < _text.Count)
  82. {
  83. string oldValue = Text;
  84. _text.RemoveAt (pos);
  85. OnTextChanged (new (in oldValue));
  86. }
  87. return true;
  88. }
  89. /// <inheritdoc/>
  90. public bool InsertAt (char ch, int pos)
  91. {
  92. List<Rune> aux = _text.ToList ();
  93. aux.Insert (pos, (Rune)ch);
  94. if (Validate (aux) || ValidateOnInput == false)
  95. {
  96. string oldValue = Text;
  97. _text.Insert (pos, (Rune)ch);
  98. OnTextChanged (new (in oldValue));
  99. return true;
  100. }
  101. return false;
  102. }
  103. /// <inheritdoc/>
  104. public void OnTextChanged (EventArgs<string> args) { TextChanged?.Invoke (this, args); }
  105. /// <summary>Compiles the regex pattern for validation./></summary>
  106. private void CompileMask () { _regex = new (StringExtensions.ToString (_pattern), RegexOptions.Compiled); }
  107. private void SetupText ()
  108. {
  109. if (_text is { } && IsValid)
  110. {
  111. return;
  112. }
  113. _text = new ();
  114. }
  115. private bool Validate (List<Rune> text)
  116. {
  117. Match match = _regex.Match (StringExtensions.ToString (text));
  118. return match.Success;
  119. }
  120. }