2011-06-22 00:14:21 -06:00
|
|
|
/*
|
|
|
|
https://github.com/peterix/dfhack
|
2012-09-29 20:03:37 -06:00
|
|
|
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"
|
2012-03-10 02:29:33 -07:00
|
|
|
#include "ColorText.h"
|
2011-08-13 06:42:09 -06:00
|
|
|
#include <deque>
|
|
|
|
#include <fstream>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
2011-07-26 21:59:09 -06:00
|
|
|
namespace tthread
|
|
|
|
{
|
|
|
|
class mutex;
|
2012-03-10 02:29:33 -07:00
|
|
|
class recursive_mutex;
|
2011-07-26 21:59:09 -06:00
|
|
|
class condition_variable;
|
|
|
|
class thread;
|
|
|
|
}
|
2011-06-22 00:14:21 -06:00
|
|
|
namespace DFHack
|
|
|
|
{
|
2011-08-13 06:42:09 -06:00
|
|
|
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);
|
2013-01-02 09:26:30 -07:00
|
|
|
//fprintf(stderr,"Save: Initialized stream\n");
|
2011-08-13 06:42:09 -06:00
|
|
|
if(outfile.bad())
|
|
|
|
return false;
|
2013-01-02 09:26:30 -07:00
|
|
|
//fprintf(stderr,"Save: Iterating...\n");
|
2011-08-13 06:42:09 -06:00
|
|
|
for(auto iter = history.begin();iter < history.end(); iter++)
|
|
|
|
{
|
2013-01-02 09:26:30 -07:00
|
|
|
//fprintf(stderr,"Save: Dumping %s\n",(*iter).c_str());
|
2011-08-13 06:42:09 -06:00
|
|
|
outfile << *iter << std::endl;
|
2013-01-02 09:26:30 -07:00
|
|
|
//fprintf(stderr,"Save: Flushing\n");
|
|
|
|
outfile.flush();
|
2011-08-13 06:42:09 -06:00
|
|
|
}
|
2013-01-02 09:26:30 -07:00
|
|
|
//fprintf(stderr,"Save: Closing\n");
|
2011-08-13 06:42:09 -06:00
|
|
|
outfile.close();
|
2013-01-02 09:26:30 -07:00
|
|
|
//fprintf(stderr,"Save: Done\n");
|
2011-08-13 06:42:09 -06:00
|
|
|
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;
|
|
|
|
};
|
2012-03-10 02:29:33 -07:00
|
|
|
|
2011-07-13 03:45:30 -06:00
|
|
|
class Private;
|
2012-03-10 02:29:33 -07:00
|
|
|
class DFHACK_EXPORT Console : public color_ostream
|
2011-06-22 00:14:21 -06:00
|
|
|
{
|
2012-03-10 02:29:33 -07: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:
|
2011-07-15 07:55:01 -06:00
|
|
|
///ctor, NOT thread-safe
|
2011-06-22 00:14:21 -06:00
|
|
|
Console();
|
2011-07-15 07:55:01 -06:00
|
|
|
///dtor, NOT thread-safe
|
2011-06-22 00:14:21 -06:00
|
|
|
~Console();
|
2011-07-15 07:55:01 -06:00
|
|
|
/// initialize the console. NOT thread-safe
|
2011-07-31 19:31:52 -06:00
|
|
|
bool init( bool sharing );
|
2011-07-15 07:55:01 -06:00
|
|
|
/// shutdown the console. NOT thread-safe
|
2011-07-13 03:45:30 -06:00
|
|
|
bool shutdown( void );
|
2011-07-15 07:55:01 -06:00
|
|
|
|
2011-06-24 21:35:29 -06:00
|
|
|
/// Clear the console, along with its scrollback
|
2011-06-22 00:14:21 -06:00
|
|
|
void clear();
|
2011-06-24 21:35:29 -06:00
|
|
|
/// Position cursor at x,y. 1,1 = top left corner
|
2011-06-22 00:14:21 -06:00
|
|
|
void gotoxy(int x, int y);
|
2011-06-24 21:35:29 -06:00
|
|
|
/// Enable or disable the caret/cursor
|
2011-06-22 00:14:21 -06:00
|
|
|
void cursor(bool enable = true);
|
2011-06-24 21:35:29 -06:00
|
|
|
/// Waits given number of milliseconds before continuing.
|
|
|
|
void msleep(unsigned int msec);
|
2011-07-13 03:45:30 -06:00
|
|
|
/// 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)
|
2011-08-13 06:42:09 -06:00
|
|
|
int lineedit(const std::string& prompt, std::string& output, CommandHistory & history );
|
2011-11-04 02:08:29 -06:00
|
|
|
bool isInited (void) { return inited; };
|
2012-03-10 02:29:33 -07:00
|
|
|
|
|
|
|
bool is_console() { return true; }
|
2011-07-15 07:55:01 -06:00
|
|
|
private:
|
2011-07-13 03:45:30 -06:00
|
|
|
Private * d;
|
2012-03-10 02:29:33 -07:00
|
|
|
tthread::recursive_mutex * wlock;
|
2011-07-17 03:06:45 -06:00
|
|
|
bool inited;
|
2011-06-22 00:14:21 -06:00
|
|
|
};
|
2011-08-13 06:42:09 -06:00
|
|
|
}
|