Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 36788239c3
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 112 lines (97 sloc) 3.331 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ShellMe.CommandLine
{
    public enum CommandStates
    {
        None = 0,
        LastCommandWasPrintet = 1
    }

    public class InMemoryCommandHistory : ICommandHistory
    {
        private readonly List<string> _commandHistory = new List<string>();
        private int _currentCommandIndex = 0;
        private Dictionary<ConsoleKey, Action> _matches;
        private IConsole _console;

        public InMemoryCommandHistory(IConsole console)
        {
            _console = console;
            _matches = new Dictionary<ConsoleKey, Action>
                           {
                               { ConsoleKey.UpArrow , () => GetPrevioseCommand() },
                               { ConsoleKey.DownArrow , () => GetNextCommand() }
                           };
        }

        public Dictionary<ConsoleKey, Action> Matches
        {
            get { return _matches; }
            set { _matches = value; }
        }

        public void SaveCommand(string command)
        {
            _commandHistory.Add(command);
        }

        public int GetCurrentCommandIndex()
        {
            return _currentCommandIndex;
        }

        public string GetLastCommand()
        {
            return _commandHistory[_commandHistory.Count - 1];
        }

        public string GetPrevioseCommand()
        {
            if(_currentCommandIndex < _commandHistory.Count)
                _currentCommandIndex++;
            else
            {
                _currentCommandIndex = _commandHistory.Count - 1;
            }
            return _commandHistory[_currentCommandIndex];
        }

        public string GetNextCommand()
        {
            if (_currentCommandIndex != 0)
                _currentCommandIndex--;
            else
                _currentCommandIndex = 0;

            return _commandHistory[_currentCommandIndex];
        }

        public string FindCommand(string searchstring)
        {
            return _commandHistory
                        .FirstOrDefault(cmd => cmd.Contains(searchstring));
        }

        public string GetCommand()
        {
            bool comandEnd = false;
            string buffer = string.Empty;
            int currentIndexOfLine = 0;

            while (!comandEnd)
            {
                var keyInfo = _console.Readkey();
                if (keyInfo.Key == ConsoleKey.Enter || keyInfo.KeyChar == '$')
                {
                    comandEnd = true;
                }
                else if (Matches.ContainsKey(keyInfo.Key))
                {
                    Action action = Matches[keyInfo.Key];
                    action.Invoke();
                    comandEnd = true;
                    buffer = CommandStates.LastCommandWasPrintet.ToString();
                }
                else
                {
                    buffer += keyInfo.KeyChar;
                    currentIndexOfLine++;
                }
            }
            if (buffer != CommandStates.LastCommandWasPrintet.ToString() && buffer != string.Empty)
                SaveCommand(buffer);
            return buffer;
        }

    }
}
Something went wrong with that request. Please try again.