/Overthere/Overthere/ConsoleSession.cs
C# | 165 lines | 129 code | 36 blank | 0 comment | 16 complexity | f367785bd8c980973f09da9e176622ab MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using Overthere.Interfaces;
- using System.IO;
- using System.Text.RegularExpressions;
-
- namespace Overthere
- {
- public class ConsoleSession : IDisposable
- {
- public IConsole Console { get; private set; }
- public string Name { get; private set; }
-
- public ConsoleSession(string name, IConsole console)
- {
- this.Console = console;
- this.Name = name;
- this._commandHistory = new List<string>();
- }
-
- public void Dispose()
- {
- if (Console != null)
- {
- Console.Dispose();
- Console = null;
-
- GC.SuppressFinalize(this);
- }
- }
-
- #region History
-
- private List<string> _commandHistory;
- private int _historyIndex = 0;
- public ReadOnlyCollection<string> CommandHistory { get { return _commandHistory.AsReadOnly(); } }
-
- public string BackHistory()
- {
- if (_commandHistory.Count > 0)
- {
- if (_historyIndex > 0)
- {
- _historyIndex--;
- }
-
- return _commandHistory[_historyIndex];
- }
-
- return string.Empty;
- }
-
- public string ForwardHistory()
- {
- if (_historyIndex < _commandHistory.Count)
- {
- _historyIndex++;
-
- if (_historyIndex< _commandHistory.Count)
- {
- return _commandHistory[_historyIndex];
- }
- }
-
- return string.Empty;
- }
-
- public void AddHistory(string command)
- {
- _commandHistory.RemoveAll(s => s == command);
- _commandHistory.Add(command);
- _historyIndex = _commandHistory.Count;
- }
-
- #endregion History
-
- #region AutoComplete
-
- private List<string> _autoCompletes = null;
- private List<Token> _tokens = null;
- private Token _query = null;
- private int _currentAutoComplete = 0;
-
- private string _lastAutoComplete;
- private int _lastCaret;
-
- public string AutoComplete(string command, int caretIndex, bool up, out int newCaretIndex)
- {
- if (caretIndex != _lastCaret || command != _lastAutoComplete)
- {
- _tokens = Token.Split(command);
-
- _query = _tokens.Last(t => t.Start <= caretIndex);
-
- _autoCompletes = GetMatchingAutoCompletes(_query);
- _currentAutoComplete = -1;
- }
-
- if (_autoCompletes.Count > 0)
- {
- int increment = up ? -1 : 1;
- _currentAutoComplete += increment;
- if (_currentAutoComplete < 0)
- {
- _currentAutoComplete = _autoCompletes.Count - 1;
- }
- else if (_currentAutoComplete >= _autoCompletes.Count)
- {
- _currentAutoComplete = 0;
- }
-
- _query.Value = _autoCompletes[_currentAutoComplete];
-
- newCaretIndex = _query.Start + _query.Length;
- string result = Token.Join(_tokens);
-
- _lastAutoComplete = result;
- _lastCaret = newCaretIndex;
-
- return result;
- }
-
- newCaretIndex = caretIndex;
- return command;
- }
-
- private List<string> GetMatchingAutoCompletes(Token query)
- {
- string partialDirectory = query.Value;
-
- string fullDirectory = string.Empty;
- int slashIndex = partialDirectory.LastIndexOf('\\') + 1;
- if (slashIndex > 0)
- {
- fullDirectory = partialDirectory.Substring(0, slashIndex);
- partialDirectory = partialDirectory.Substring(slashIndex);
- }
-
- string currentDirectory = this.Console.GetCurrentDirectory();
-
- string combinedDirectory = Path.Combine(currentDirectory, fullDirectory);
-
- var directories = this.Console.GetDirectories(combinedDirectory);
- var files = this.Console.GetFiles(combinedDirectory);
-
- var items = (from d in directories
- select d.Name).Union(from f in files
- select f.Name);
-
- string pattern = Regex.Escape(partialDirectory);
- pattern = "^" + pattern.Replace("\\*", ".*");
- Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
-
- var results = from n in items
- where regex.IsMatch(n)
- select Path.Combine(fullDirectory, n);
-
- return results.ToList();
- }
-
- #endregion AutoComplete
- }
- }