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