dfhack/library/include/Console.h

164 lines
4.8 KiB
C

2011-06-22 00:14:21 -06:00
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2012 Petr Mrázek (peterix@gmail.com)
2011-06-22 00:14:21 -06:00
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#pragma once
2011-12-31 04:48:42 -07:00
#include "Pragma.h"
#include "Export.h"
#include "ColorText.h"
#include <deque>
#include <fstream>
#include <assert.h>
#include <iostream>
#include <string>
namespace tthread
{
class mutex;
class recursive_mutex;
class condition_variable;
class thread;
}
2011-06-22 00:14:21 -06:00
namespace DFHack
{
class CommandHistory
{
public:
CommandHistory(std::size_t capacity = 100)
{
this->capacity = capacity;
}
bool load (const char * filename)
{
std::string reader;
std::ifstream infile(filename);
if(infile.bad())
return false;
std::string s;
while(std::getline(infile, s))
{
if(s.empty())
continue;
history.push_back(s);
}
return true;
}
bool save (const char * filename)
{
std::ofstream outfile (filename);
//fprintf(stderr,"Save: Initialized stream\n");
if(outfile.bad())
return false;
//fprintf(stderr,"Save: Iterating...\n");
for(auto iter = history.begin();iter < history.end(); iter++)
{
//fprintf(stderr,"Save: Dumping %s\n",(*iter).c_str());
outfile << *iter << std::endl;
//fprintf(stderr,"Save: Flushing\n");
outfile.flush();
}
//fprintf(stderr,"Save: Closing\n");
outfile.close();
//fprintf(stderr,"Save: Done\n");
return true;
}
/// add a command to the history
void add(const std::string& command)
{
// if current command = last in history -> do not add. Always add if history is empty.
if(!history.empty() && history.front() == command)
return;
history.push_front(command);
if(history.size() > capacity)
history.pop_back();
}
/// clear the command history
void clear()
{
history.clear();
}
/// get current history size
std::size_t size()
{
return history.size();
}
/// get pointer to a particular history item
std::string & operator[](std::size_t index)
{
assert(index < history.size());
return history[index];
}
void remove( void )
{
history.pop_front();
}
private:
std::size_t capacity;
std::deque <std::string> history;
};
class Private;
class DFHACK_EXPORT Console : public color_ostream
2011-06-22 00:14:21 -06:00
{
protected:
virtual void begin_batch();
virtual void add_text(color_value color, const std::string &text);
virtual void end_batch();
virtual void flush_proxy();
2011-06-22 00:14:21 -06:00
public:
///ctor, NOT thread-safe
2011-06-22 00:14:21 -06:00
Console();
///dtor, NOT thread-safe
2011-06-22 00:14:21 -06:00
~Console();
/// initialize the console. NOT thread-safe
2011-07-31 19:31:52 -06:00
bool init( bool sharing );
/// shutdown the console. NOT thread-safe
bool shutdown( void );
/// Clear the console, along with its scrollback
2011-06-22 00:14:21 -06:00
void clear();
/// Position cursor at x,y. 1,1 = top left corner
2011-06-22 00:14:21 -06:00
void gotoxy(int x, int y);
/// Enable or disable the caret/cursor
2011-06-22 00:14:21 -06:00
void cursor(bool enable = true);
/// Waits given number of milliseconds before continuing.
void msleep(unsigned int msec);
/// get the current number of columns
int get_columns(void);
/// get the current number of rows
int get_rows(void);
/// beep. maybe?
//void beep (void);
/// A simple line edit (raw mode)
int lineedit(const std::string& prompt, std::string& output, CommandHistory & history );
2011-11-04 02:08:29 -06:00
bool isInited (void) { return inited; };
bool is_console() { return true; }
private:
Private * d;
tthread::recursive_mutex * wlock;
2011-07-17 03:06:45 -06:00
bool inited;
2011-06-22 00:14:21 -06:00
};
}