summaryrefslogtreecommitdiff
path: root/library/Console-linux.cpp
diff options
context:
space:
mode:
authorAlexander Gavrilov2012-03-10 13:29:33 +0400
committerAlexander Gavrilov2012-03-10 13:29:33 +0400
commitb2737e2bed5f013a4dfbf6e19650ca60498a9afd (patch)
treee027655353f934c7c3ba444596ec66d4338aaf1d /library/Console-linux.cpp
parentc260aca3f1097c3d22c214c2c0c137efc84a7cf5 (diff)
downloaddfhack-b2737e2bed5f013a4dfbf6e19650ca60498a9afd.tar.gz
dfhack-b2737e2bed5f013a4dfbf6e19650ca60498a9afd.tar.bz2
dfhack-b2737e2bed5f013a4dfbf6e19650ca60498a9afd.tar.xz
Pull out a colored text output interface out of the Console class.
Diffstat (limited to 'library/Console-linux.cpp')
-rw-r--r--library/Console-linux.cpp186
1 files changed, 87 insertions, 99 deletions
diff --git a/library/Console-linux.cpp b/library/Console-linux.cpp
index f471d6a1..c732a017 100644
--- a/library/Console-linux.cpp
+++ b/library/Console-linux.cpp
@@ -126,13 +126,14 @@ const char * getANSIColor(const int c)
namespace DFHack
{
- class Private : public std::stringbuf
+ class Private
{
public:
Private()
{
dfout_C = NULL;
rawmode = false;
+ in_batch = false;
supported_terminal = false;
state = con_unclaimed;
};
@@ -164,68 +165,71 @@ namespace DFHack
return true;
}
}
- protected:
- int sync()
- {
- print(str().c_str());
- str(std::string()); // Clear the string buffer
- return 0;
- }
+
public:
- /// Print a formatted string, like printf
- int print(const char * format, ...)
+ void print(const char *data)
{
- va_list args;
- va_start( args, format );
- int ret = vprint( format, args );
- va_end( args );
- return ret;
+ fputs(data, dfout_C);
}
- int vprint(const char * format, va_list vl)
+
+ void print_text(color_ostream::color_value clr, const std::string &chunk)
{
- if(state == con_lineedit)
+ if(!in_batch && state == con_lineedit)
{
disable_raw();
fprintf(dfout_C,"\x1b[1G");
fprintf(dfout_C,"\x1b[0K");
- int ret = vfprintf( dfout_C, format, vl );
+
+ color(clr);
+ print(chunk.c_str());
+
+ reset_color();
enable_raw();
prompt_refresh();
- return ret;
}
- else return vfprintf( dfout_C, format, vl );
+ else
+ {
+ color(clr);
+ print(chunk.c_str());
+ }
}
- int vprinterr(const char * format, va_list vl)
+
+ void begin_batch()
{
- if(state == con_lineedit)
+ assert(!in_batch);
+
+ in_batch = true;
+
+ if (state == con_lineedit)
{
disable_raw();
- color(Console::COLOR_LIGHTRED);
fprintf(dfout_C,"\x1b[1G");
fprintf(dfout_C,"\x1b[0K");
- int ret = vfprintf( dfout_C, format, vl );
- reset_color();
- enable_raw();
- prompt_refresh();
- return ret;
}
- else
+ }
+
+ void end_batch()
+ {
+ assert(in_batch);
+
+ flush();
+
+ in_batch = false;
+
+ if (state == con_lineedit)
{
- color(Console::COLOR_LIGHTRED);
- int ret = vfprintf( dfout_C, format, vl );
reset_color();
- return ret;
+ enable_raw();
+ prompt_refresh();
}
}
- /// Print a formatted string, like printf, in red
- int printerr(const char * format, ...)
+
+ void flush()
{
- va_list args;
- va_start( args, format );
- int ret = vprinterr( format, args );
- va_end( args );
- return ret;
+ if (!rawmode)
+ fflush(dfout_C);
}
+
/// Clear the console, along with its scrollback
void clear()
{
@@ -243,7 +247,9 @@ namespace DFHack
/// Position cursor at x,y. 1,1 = top left corner
void gotoxy(int x, int y)
{
- print("\033[%d;%dH", y,x);
+ char tmp[64];
+ sprintf(tmp,"\033[%d;%dH", y,x);
+ print(tmp);
}
/// Set color (ANSI color number)
void color(Console::color_value index)
@@ -291,18 +297,19 @@ namespace DFHack
/// beep. maybe?
//void beep (void);
/// A simple line edit (raw mode)
- int lineedit(const std::string& prompt, std::string& output, mutex * lock, CommandHistory & ch)
+ int lineedit(const std::string& prompt, std::string& output, recursive_mutex * lock, CommandHistory & ch)
{
output.clear();
+ reset_color();
this->prompt = prompt;
if (!supported_terminal)
{
print(prompt.c_str());
fflush(dfout_C);
// FIXME: what do we do here???
- //SDL_mutexV(lock);
+ //SDL_recursive_mutexV(lock);
std::getline(std::cin, output);
- //SDL_mutexP(lock);
+ //SDL_recursive_mutexP(lock);
return output.size();
}
else
@@ -396,7 +403,7 @@ namespace DFHack
if (::write(STDIN_FILENO,seq,strlen(seq)) == -1) return;
}
- int prompt_loop(mutex * lock, CommandHistory & history)
+ int prompt_loop(recursive_mutex * lock, CommandHistory & history)
{
int fd = STDIN_FILENO;
size_t plen = prompt.size();
@@ -606,6 +613,7 @@ namespace DFHack
con_unclaimed,
con_lineedit
} state;
+ bool in_batch;
std::string prompt; // current prompt string
std::string raw_buffer; // current raw mode buffer
int raw_cursor; // cursor position in the buffer
@@ -615,12 +623,12 @@ namespace DFHack
};
}
-Console::Console():std::ostream(0), std::ios(0)
+Console::Console()
{
d = 0;
inited = false;
// we can't create the mutex at this time. the SDL functions aren't hooked yet.
- wlock = new mutex();
+ wlock = new recursive_mutex();
}
Console::~Console()
{
@@ -643,7 +651,6 @@ bool Console::init(bool sharing)
d = new Private();
// make our own weird streams so our IO isn't redirected
d->dfout_C = fopen("/dev/tty", "w");
- rdbuf(d);
std::cin.tie(this);
clear();
d->supported_terminal = !isUnsupportedTerm() && isatty(STDIN_FILENO);
@@ -660,7 +667,7 @@ bool Console::shutdown(void)
{
if(!d)
return true;
- lock_guard <mutex> g(*wlock);
+ lock_guard <recursive_mutex> g(*wlock);
if(d->rawmode)
d->disable_raw();
d->print("\n");
@@ -670,46 +677,41 @@ bool Console::shutdown(void)
return true;
}
-int Console::print( const char* format, ... )
+void Console::begin_batch()
{
- va_list args;
- lock_guard <mutex> g(*wlock);
- int ret;
- if(!inited) ret = -1;
- else
- {
- va_start( args, format );
- ret = d->vprint(format, args);
- va_end(args);
- }
- return ret;
+ color_ostream::begin_batch();
+
+ wlock->lock();
+
+ if (inited)
+ d->begin_batch();
}
-int Console::printerr( const char* format, ... )
+void Console::end_batch()
{
- va_list args;
- lock_guard <mutex> g(*wlock);
- int ret;
- // also mirror in error log
- if(!inited)
- {
- va_start( args, format );
- ret = vfprintf(stderr, format, args);
- va_end(args);
- }
- else
- {
- va_start( args, format );
- ret = d->vprinterr(format, args);
- vfprintf(stderr, format, args);
- va_end(args);
- }
- return ret;
+ if (inited)
+ d->end_batch();
+
+ wlock->unlock();
+}
+
+void Console::flush_proxy()
+{
+ lock_guard <recursive_mutex> g(*wlock);
+ if (inited)
+ d->flush();
+}
+
+void Console::add_text(color_value color, const std::string &text)
+{
+ lock_guard <recursive_mutex> g(*wlock);
+ if (inited)
+ d->print_text(color, text);
}
int Console::get_columns(void)
{
- lock_guard <mutex> g(*wlock);
+ lock_guard <recursive_mutex> g(*wlock);
int ret = -1;
if(inited)
ret = d->get_columns();
@@ -718,7 +720,7 @@ int Console::get_columns(void)
int Console::get_rows(void)
{
- lock_guard <mutex> g(*wlock);
+ lock_guard <recursive_mutex> g(*wlock);
int ret = -1;
if(inited)
ret = d->get_rows();
@@ -727,42 +729,28 @@ int Console::get_rows(void)
void Console::clear()
{
- lock_guard <mutex> g(*wlock);
+ lock_guard <recursive_mutex> g(*wlock);
if(inited)
d->clear();
}
void Console::gotoxy(int x, int y)
{
- lock_guard <mutex> g(*wlock);
+ lock_guard <recursive_mutex> g(*wlock);
if(inited)
d->gotoxy(x,y);
}
-void Console::color(color_value index)
-{
- lock_guard <mutex> g(*wlock);
- if(inited)
- d->color(index);
-}
-
-void Console::reset_color( void )
-{
- lock_guard <mutex> g(*wlock);
- if(inited)
- d->reset_color();
-}
-
void Console::cursor(bool enable)
{
- lock_guard <mutex> g(*wlock);
+ lock_guard <recursive_mutex> g(*wlock);
if(inited)
d->cursor(enable);
}
int Console::lineedit(const std::string & prompt, std::string & output, CommandHistory & ch)
{
- lock_guard <mutex> g(*wlock);
+ lock_guard <recursive_mutex> g(*wlock);
int ret = -2;
if(inited)
ret = d->lineedit(prompt,output,wlock,ch);