AppendAutocomplete.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Terminal.Gui {
  6. /// <summary>
  7. /// Autocomplete for a <see cref="TextField"/> which shows suggestions within the box.
  8. /// Displayed suggestions can be completed using the tab key.
  9. /// </summary>
  10. public class AppendAutocomplete : AutocompleteBase {
  11. private TextField textField;
  12. /// <inheritdoc/>
  13. public override View HostControl { get => textField; set => textField = (TextField)value; }
  14. /// <summary>
  15. /// The color used for rendering the appended text. Note that only
  16. /// <see cref="ColorScheme.Normal"/> is used and then only <see cref="Attribute.Foreground"/>
  17. /// (Background comes from <see cref="HostControl"/>).
  18. /// </summary>
  19. public override ColorScheme ColorScheme { get; set; }
  20. /// <summary>
  21. /// Creates a new instance of the <see cref="AppendAutocomplete"/> class.
  22. /// </summary>
  23. public AppendAutocomplete (TextField textField)
  24. {
  25. this.textField = textField;
  26. SelectionKey = KeyCode.Tab;
  27. ColorScheme = new ColorScheme {
  28. Normal = new Attribute (Color.DarkGray, Color.Black),
  29. Focus = new Attribute (Color.DarkGray, Color.Black),
  30. HotNormal = new Attribute (Color.DarkGray, Color.Black),
  31. HotFocus = new Attribute (Color.DarkGray, Color.Black),
  32. Disabled = new Attribute (Color.DarkGray, Color.Black),
  33. };
  34. }
  35. /// <inheritdoc/>
  36. public override void ClearSuggestions ()
  37. {
  38. base.ClearSuggestions ();
  39. textField.SetNeedsDisplay ();
  40. }
  41. /// <inheritdoc/>
  42. public override bool MouseEvent (MouseEvent me, bool fromHost = false)
  43. {
  44. return false;
  45. }
  46. /// <inheritdoc/>
  47. public override bool ProcessKey (Key a)
  48. {
  49. var key = a.KeyCode;
  50. if (key == SelectionKey) {
  51. return this.AcceptSelectionIfAny ();
  52. } else
  53. if (key == KeyCode.CursorUp) {
  54. return this.CycleSuggestion (1);
  55. } else
  56. if (key == KeyCode.CursorDown) {
  57. return this.CycleSuggestion (-1);
  58. } else if (key == CloseKey && Suggestions.Any ()) {
  59. ClearSuggestions ();
  60. _suspendSuggestions = true;
  61. return true;
  62. }
  63. if (char.IsLetterOrDigit ((char)a)) {
  64. _suspendSuggestions = false;
  65. }
  66. return false;
  67. }
  68. bool _suspendSuggestions = false;
  69. /// <inheritdoc/>
  70. public override void GenerateSuggestions (AutocompleteContext context)
  71. {
  72. if (_suspendSuggestions) {
  73. return;
  74. }
  75. base.GenerateSuggestions (context);
  76. }
  77. /// <summary>
  78. /// Renders the current suggestion into the <see cref="TextField"/>
  79. /// </summary>
  80. public override void RenderOverlay (Point renderAt)
  81. {
  82. if (!this.MakingSuggestion ()) {
  83. return;
  84. }
  85. // draw it like its selected even though its not
  86. Application.Driver.SetAttribute (new Attribute (ColorScheme.Normal.Foreground, textField.ColorScheme.Focus.Background));
  87. textField.Move (textField.Text.Length, 0);
  88. var suggestion = this.Suggestions.ElementAt (this.SelectedIdx);
  89. var fragment = suggestion.Replacement.Substring (suggestion.Remove);
  90. int spaceAvailable = textField.Bounds.Width - textField.Text.GetColumns ();
  91. int spaceRequired = fragment.EnumerateRunes ().Sum (c => c.GetColumns ());
  92. if (spaceAvailable < spaceRequired) {
  93. fragment = new string (
  94. fragment.TakeWhile (c => (spaceAvailable -= ((Rune)c).GetColumns ()) >= 0)
  95. .ToArray ()
  96. );
  97. }
  98. Application.Driver.AddStr (fragment);
  99. }
  100. /// <summary>
  101. /// Accepts the current autocomplete suggestion displaying in the text box.
  102. /// Returns true if a valid suggestion was being rendered and acceptable or
  103. /// false if no suggestion was showing.
  104. /// </summary>
  105. /// <returns></returns>
  106. internal bool AcceptSelectionIfAny ()
  107. {
  108. if (this.MakingSuggestion ()) {
  109. var insert = this.Suggestions.ElementAt (this.SelectedIdx);
  110. var newText = textField.Text;
  111. newText = newText.Substring (0, newText.Length - insert.Remove);
  112. newText += insert.Replacement;
  113. textField.Text = newText;
  114. this.textField.MoveEnd ();
  115. this.ClearSuggestions ();
  116. return true;
  117. }
  118. return false;
  119. }
  120. internal void SetTextTo (FileSystemInfo fileSystemInfo)
  121. {
  122. var newText = fileSystemInfo.FullName;
  123. if (fileSystemInfo is DirectoryInfo) {
  124. newText += System.IO.Path.DirectorySeparatorChar;
  125. }
  126. textField.Text = newText;
  127. textField.MoveEnd ();
  128. }
  129. /// <summary>
  130. /// Returns true if there is a suggestion that can be made and the control
  131. /// is in a state where user would expect to see auto-complete (i.e. focused and
  132. /// cursor in right place).
  133. /// </summary>
  134. /// <returns></returns>
  135. private bool MakingSuggestion ()
  136. {
  137. return Suggestions.Any () && this.SelectedIdx != -1 && textField.HasFocus && textField.CursorIsAtEnd ();
  138. }
  139. private bool CycleSuggestion (int direction)
  140. {
  141. if (this.Suggestions.Count <= 1) {
  142. return false;
  143. }
  144. this.SelectedIdx = (this.SelectedIdx + direction) % this.Suggestions.Count;
  145. if (this.SelectedIdx < 0) {
  146. this.SelectedIdx = this.Suggestions.Count () - 1;
  147. }
  148. textField.SetNeedsDisplay ();
  149. return true;
  150. }
  151. }
  152. }