The console is now awesome when you use C++ streams too.

develop
Petr Mrázek 2011-07-16 00:15:20 +02:00
parent b85f196dc4
commit 9eea07fb66
3 changed files with 33 additions and 14 deletions

@ -122,17 +122,28 @@ const char * getANSIColor(const int c)
namespace DFHack namespace DFHack
{ {
class Private class Private : public std::stringbuf
{ {
public: public:
Private() Private()
{ {
dfout_C = NULL; dfout_C = NULL;
stream_o = NULL;
rawmode = false; rawmode = false;
supported_terminal = false; supported_terminal = false;
state = con_unclaimed; state = con_unclaimed;
}; };
virtual ~Private()
{
//sync();
}
protected:
int sync()
{
print(str().c_str());
str(std::string()); // Clear the string buffer
return 0;
}
public:
/// Print a formatted string, like printf /// Print a formatted string, like printf
int print(const char * format, ...) int print(const char * format, ...)
{ {
@ -563,7 +574,6 @@ namespace DFHack
return raw_buffer.size(); return raw_buffer.size();
} }
FILE * dfout_C; FILE * dfout_C;
duthomhas::stdiobuf * stream_o;
std::deque <std::string> history; std::deque <std::string> history;
bool supported_terminal; bool supported_terminal;
// state variables // state variables
@ -598,9 +608,8 @@ bool Console::init(void)
d = new Private(); d = new Private();
// make our own weird streams so our IO isn't redirected // make our own weird streams so our IO isn't redirected
d->dfout_C = fopen("/dev/tty", "w"); d->dfout_C = fopen("/dev/tty", "w");
d->stream_o = new duthomhas::stdiobuf(d->dfout_C);
d->wlock = SDL_CreateMutex(); d->wlock = SDL_CreateMutex();
rdbuf(d->stream_o); rdbuf(d);
std::cin.tie(this); std::cin.tie(this);
clear(); clear();
d->supported_terminal = !isUnsupportedTerm() && isatty(STDIN_FILENO); d->supported_terminal = !isUnsupportedTerm() && isatty(STDIN_FILENO);

@ -64,13 +64,12 @@ using namespace DFHack;
namespace DFHack namespace DFHack
{ {
class Private class Private : public std::stringbuf
{ {
public: public:
Private() Private() : basic_stringbuf<char>::basic_stringbuf()
{ {
dfout_C = 0; dfout_C = 0;
stream_o = 0;
rawmode = 0; rawmode = 0;
console_in = 0; console_in = 0;
console_out = 0; console_out = 0;
@ -79,6 +78,19 @@ namespace DFHack
state = con_unclaimed; state = con_unclaimed;
raw_cursor = 0; raw_cursor = 0;
}; };
virtual ~Private()
{
//sync();
}
protected:
int sync()
{
print (str().c_str());
// Clear the string buffer
str(std::string());
return 0;
}
public:
/// Print a formatted string, like printf /// Print a formatted string, like printf
int print(const char * format, ...) int print(const char * format, ...)
{ {
@ -371,7 +383,6 @@ namespace DFHack
} }
FILE * dfout_C; FILE * dfout_C;
duthomhas::stdiobuf * stream_o;
int rawmode; /* for atexit() function to check if restore is needed*/ int rawmode; /* for atexit() function to check if restore is needed*/
std::deque <std::string> history; std::deque <std::string> history;
@ -415,6 +426,7 @@ bool Console::init(void)
// Allocate a console! // Allocate a console!
AllocConsole(); AllocConsole();
d->ConsoleWindow = GetConsoleWindow(); d->ConsoleWindow = GetConsoleWindow();
d->wlock = SDL_CreateMutex();
HMENU hm = GetSystemMenu(d->ConsoleWindow,false); HMENU hm = GetSystemMenu(d->ConsoleWindow,false);
DeleteMenu(hm, SC_CLOSE, MF_BYCOMMAND); DeleteMenu(hm, SC_CLOSE, MF_BYCOMMAND);
@ -444,10 +456,8 @@ bool Console::init(void)
std::ios::sync_with_stdio(); std::ios::sync_with_stdio();
// make our own weird streams so our IO isn't redirected // make our own weird streams so our IO isn't redirected
d->stream_o = new duthomhas::stdiobuf(d->dfout_C); rdbuf(d);
rdbuf(d->stream_o);
std::cin.tie(this); std::cin.tie(this);
d->wlock = SDL_CreateMutex();
clear(); clear();
return true; return true;
} }

@ -129,7 +129,7 @@ DFhackCExport command_result kittens (Core * c, vector <string> & parameters)
while (kit != 0) while (kit != 0)
{ {
con.gotoxy(1,1+index); con.gotoxy(1,1+index);
con << kit; con << kit << std::endl;
index++; index++;
kit = nyan[index]; kit = nyan[index];
} }