window: store strings history in list instead of deque

This commit is contained in:
Andrzej Rybczak
2009-11-13 18:28:13 +01:00
parent 180d71ed28
commit 3365b066c3
2 changed files with 20 additions and 29 deletions

View File

@@ -155,7 +155,6 @@ Window::~Window()
{ {
delwin(itsWindow); delwin(itsWindow);
delwin(itsWinBorder); delwin(itsWinBorder);
delete itsHistory;
} }
void Window::SetColor(Color fg, Color bg) void Window::SetColor(Color fg, Color bg)
@@ -231,7 +230,7 @@ void Window::SetTitle(const std::string &new_title)
void Window::CreateHistory() void Window::CreateHistory()
{ {
if (!itsHistory) if (!itsHistory)
itsHistory = new std::deque<std::wstring>; itsHistory = new std::list<std::wstring>;
} }
void Window::DeleteHistory() void Window::DeleteHistory()
@@ -436,7 +435,7 @@ std::string Window::GetString(const std::string &base, size_t length, size_t wid
std::wstring wbase = ToWString(base); std::wstring wbase = ToWString(base);
std::wstring *tmp = &wbase; std::wstring *tmp = &wbase;
size_t history_offset = itsHistory && !encrypted ? itsHistory->size() : -1; std::list<std::wstring>::iterator history_it = itsHistory->end();
std::string tmp_in; std::string tmp_in;
wchar_t wc_in; wchar_t wc_in;
@@ -512,28 +511,22 @@ std::string Window::GetString(const std::string &base, size_t length, size_t wid
case KEY_MOUSE: case KEY_MOUSE:
break; break;
case KEY_UP: case KEY_UP:
if (itsHistory && !encrypted && history_offset > 0) if (itsHistory && !encrypted && history_it != itsHistory->begin())
{ {
do while (--history_it != itsHistory->begin())
tmp = &(*itsHistory)[--history_offset]; if (!history_it->empty())
while (tmp->empty() && history_offset); break;
tmp = &*history_it;
gotoend = 1; gotoend = 1;
} }
break; break;
case KEY_DOWN: case KEY_DOWN:
if (itsHistory && !encrypted && itsHistory->size() > 0) if (itsHistory && !encrypted && history_it != itsHistory->end())
{ {
if (history_offset < itsHistory->size()-1) while (++history_it != itsHistory->end())
{ if (!history_it->empty())
do break;
tmp = &(*itsHistory)[++history_offset]; tmp = &(history_it == itsHistory->end() ? wbase : *history_it);
while (tmp->empty() && history_offset < itsHistory->size()-1);
}
else if (history_offset == itsHistory->size()-1)
{
tmp = &wbase;
history_offset++;
}
gotoend = 1; gotoend = 1;
} }
break; break;
@@ -638,16 +631,14 @@ std::string Window::GetString(const std::string &base, size_t length, size_t wid
if (itsHistory && !encrypted) if (itsHistory && !encrypted)
{ {
size_t old_size = itsHistory->size(); if (history_it != itsHistory->end())
if (!tmp->empty() && (itsHistory->empty() || itsHistory->back() != *tmp))
itsHistory->push_back(*tmp);
if (old_size > 1 && history_offset < old_size)
{ {
std::deque<std::wstring>::iterator it = itsHistory->begin()+history_offset; itsHistory->push_back(*history_it);
wbase = *it; tmp = &itsHistory->back();
tmp = &wbase; itsHistory->erase(history_it);
itsHistory->erase(it);
} }
else
itsHistory->push_back(*tmp);
} }
return ToString(*tmp); return ToString(*tmp);

View File

@@ -31,7 +31,7 @@
#include "curses.h" #include "curses.h"
#include <deque> #include <list>
#include <stack> #include <stack>
#include <vector> #include <vector>
#include <string> #include <string>
@@ -580,7 +580,7 @@ namespace NCurses
FDCallbacks itsFDs; FDCallbacks itsFDs;
/// pointer to container used as history /// pointer to container used as history
std::deque<std::wstring> *itsHistory; std::list<std::wstring> *itsHistory;
/// counters for format flags /// counters for format flags
int itsBoldCounter; int itsBoldCounter;