File size: 2,368 Bytes
b1b3bae |
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 |
// Copyright (c) 2010 Joe Moorhouse
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PythonConsoleControl
{
/// <summary>
/// Stores the command line history for the PythonConsole.
/// </summary>
public class CommandLineHistory
{
List<string> lines = new List<string>();
int position;
public CommandLineHistory()
{
}
/// <summary>
/// Adds the command line to the history.
/// </summary>
public void Add(string line)
{
if (!String.IsNullOrEmpty(line))
{
int index = lines.Count - 1;
if (index >= 0)
{
if (lines[index] != line)
{
lines.Add(line);
}
}
else
{
lines.Add(line);
}
}
position = lines.Count;
}
/// <summary>
/// Gets the current command line. By default this will be the last command line entered.
/// </summary>
public string Current
{
get
{
if ((position >= 0) && (position < lines.Count))
{
return lines[position];
}
return null;
}
}
/// <summary>
/// Moves to the next command line.
/// </summary>
/// <returns>False if the current position is at the end of the command line history.</returns>
public bool MoveNext()
{
int nextPosition = position + 1;
if (nextPosition < lines.Count)
{
++position;
}
return nextPosition < lines.Count;
}
/// <summary>
/// Moves to the previous command line.
/// </summary>
/// <returns>False if the current position is at the start of the command line history.</returns>
public bool MovePrevious()
{
if (position >= 0)
{
if (position == 0)
{
return false;
}
--position;
}
return position >= 0;
}
}
}
|