PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Overthere/Overthere/ConsoleSession.cs

https://bitbucket.org/floAr/personal
C# | 165 lines | 129 code | 36 blank | 0 comment | 16 complexity | f367785bd8c980973f09da9e176622ab MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using Overthere.Interfaces;
  6. using System.IO;
  7. using System.Text.RegularExpressions;
  8. namespace Overthere
  9. {
  10. public class ConsoleSession : IDisposable
  11. {
  12. public IConsole Console { get; private set; }
  13. public string Name { get; private set; }
  14. public ConsoleSession(string name, IConsole console)
  15. {
  16. this.Console = console;
  17. this.Name = name;
  18. this._commandHistory = new List<string>();
  19. }
  20. public void Dispose()
  21. {
  22. if (Console != null)
  23. {
  24. Console.Dispose();
  25. Console = null;
  26. GC.SuppressFinalize(this);
  27. }
  28. }
  29. #region History
  30. private List<string> _commandHistory;
  31. private int _historyIndex = 0;
  32. public ReadOnlyCollection<string> CommandHistory { get { return _commandHistory.AsReadOnly(); } }
  33. public string BackHistory()
  34. {
  35. if (_commandHistory.Count > 0)
  36. {
  37. if (_historyIndex > 0)
  38. {
  39. _historyIndex--;
  40. }
  41. return _commandHistory[_historyIndex];
  42. }
  43. return string.Empty;
  44. }
  45. public string ForwardHistory()
  46. {
  47. if (_historyIndex < _commandHistory.Count)
  48. {
  49. _historyIndex++;
  50. if (_historyIndex< _commandHistory.Count)
  51. {
  52. return _commandHistory[_historyIndex];
  53. }
  54. }
  55. return string.Empty;
  56. }
  57. public void AddHistory(string command)
  58. {
  59. _commandHistory.RemoveAll(s => s == command);
  60. _commandHistory.Add(command);
  61. _historyIndex = _commandHistory.Count;
  62. }
  63. #endregion History
  64. #region AutoComplete
  65. private List<string> _autoCompletes = null;
  66. private List<Token> _tokens = null;
  67. private Token _query = null;
  68. private int _currentAutoComplete = 0;
  69. private string _lastAutoComplete;
  70. private int _lastCaret;
  71. public string AutoComplete(string command, int caretIndex, bool up, out int newCaretIndex)
  72. {
  73. if (caretIndex != _lastCaret || command != _lastAutoComplete)
  74. {
  75. _tokens = Token.Split(command);
  76. _query = _tokens.Last(t => t.Start <= caretIndex);
  77. _autoCompletes = GetMatchingAutoCompletes(_query);
  78. _currentAutoComplete = -1;
  79. }
  80. if (_autoCompletes.Count > 0)
  81. {
  82. int increment = up ? -1 : 1;
  83. _currentAutoComplete += increment;
  84. if (_currentAutoComplete < 0)
  85. {
  86. _currentAutoComplete = _autoCompletes.Count - 1;
  87. }
  88. else if (_currentAutoComplete >= _autoCompletes.Count)
  89. {
  90. _currentAutoComplete = 0;
  91. }
  92. _query.Value = _autoCompletes[_currentAutoComplete];
  93. newCaretIndex = _query.Start + _query.Length;
  94. string result = Token.Join(_tokens);
  95. _lastAutoComplete = result;
  96. _lastCaret = newCaretIndex;
  97. return result;
  98. }
  99. newCaretIndex = caretIndex;
  100. return command;
  101. }
  102. private List<string> GetMatchingAutoCompletes(Token query)
  103. {
  104. string partialDirectory = query.Value;
  105. string fullDirectory = string.Empty;
  106. int slashIndex = partialDirectory.LastIndexOf('\\') + 1;
  107. if (slashIndex > 0)
  108. {
  109. fullDirectory = partialDirectory.Substring(0, slashIndex);
  110. partialDirectory = partialDirectory.Substring(slashIndex);
  111. }
  112. string currentDirectory = this.Console.GetCurrentDirectory();
  113. string combinedDirectory = Path.Combine(currentDirectory, fullDirectory);
  114. var directories = this.Console.GetDirectories(combinedDirectory);
  115. var files = this.Console.GetFiles(combinedDirectory);
  116. var items = (from d in directories
  117. select d.Name).Union(from f in files
  118. select f.Name);
  119. string pattern = Regex.Escape(partialDirectory);
  120. pattern = "^" + pattern.Replace("\\*", ".*");
  121. Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
  122. var results = from n in items
  123. where regex.IsMatch(n)
  124. select Path.Combine(fullDirectory, n);
  125. return results.ToList();
  126. }
  127. #endregion AutoComplete
  128. }
  129. }