@ -1,4 +1,7 @@
# include "lua_Console.h"
# include "lua_Console.h"
# include <sstream>
//TODO error management. Using lua error? or something other?
//TODO error management. Using lua error? or something other?
static DFHack : : color_ostream * GetConsolePtr ( lua : : state & st )
static DFHack : : color_ostream * GetConsolePtr ( lua : : state & st )
{
{
@ -9,22 +12,53 @@ static DFHack::color_ostream* GetConsolePtr(lua::state &st)
st . settop ( t ) ;
st . settop ( t ) ;
return c ;
return c ;
}
}
static std : : string lua_print_fmt ( lua_State * L )
{
/* Copied from lua source to fully replicate builtin print */
int n = lua_gettop ( L ) ; /* number of arguments */
lua_getglobal ( L , " tostring " ) ;
std : : stringstream ss ;
for ( int i = 1 ; i < = n ; i + + ) {
lua_pushvalue ( L , - 1 ) ; /* function to be called */
lua_pushvalue ( L , i ) ; /* value to print */
lua_call ( L , 1 , 1 ) ;
const char * s = lua_tostring ( L , - 1 ) ; /* get result */
if ( s = = NULL )
luaL_error ( L , " tostring must return a string to print " ) ;
if ( i > 1 )
ss < < ' \t ' ;
ss < < s ;
lua_pop ( L , 1 ) ; /* pop result */
}
return ss . str ( ) ;
}
static int lua_Console_print ( lua_State * S )
static int lua_Console_print ( lua_State * S )
{
{
lua : : state st ( S ) ;
lua : : state st ( S ) ;
int t = st . gettop ( ) ;
DFHack : : color_ostream * c = GetConsolePtr ( st ) ;
DFHack : : color_ostream * c = GetConsolePtr ( st ) ;
c - > print ( " %s " , lua_print_fmt ( S ) . c_str ( ) ) ;
c - > print ( " %s " , st . as < string > ( t ) . c_str ( ) ) ;
return 0 ;
return 0 ;
}
static int lua_Console_println ( lua_State * S )
{
lua : : state st ( S ) ;
DFHack : : color_ostream * c = GetConsolePtr ( st ) ;
c - > print ( " %s \n " , lua_print_fmt ( S ) . c_str ( ) ) ;
return 0 ;
}
}
static int lua_Console_printerr ( lua_State * S )
static int lua_Console_printerr ( lua_State * S )
{
{
lua : : state st ( S ) ;
lua : : state st ( S ) ;
int t = st . gettop ( ) ;
DFHack : : color_ostream * c = GetConsolePtr ( st ) ;
DFHack : : color_ostream * c = GetConsolePtr ( st ) ;
c - > printerr ( " %s " , lua_print_fmt ( S ) . c_str ( ) ) ;
c - > printerr ( " %s " , st . as < string > ( t ) . c_str ( ) ) ;
return 0 ;
return 0 ;
}
}
static int lua_Console_clear ( lua_State * S )
static int lua_Console_clear ( lua_State * S )
@ -123,6 +157,7 @@ static int lua_Console_lineedit(lua_State *S)
const luaL_Reg lua_console_func [ ] =
const luaL_Reg lua_console_func [ ] =
{
{
{ " print " , lua_Console_print } ,
{ " print " , lua_Console_print } ,
{ " println " , lua_Console_println } ,
{ " printerr " , lua_Console_printerr } ,
{ " printerr " , lua_Console_printerr } ,
{ " clear " , lua_Console_clear } ,
{ " clear " , lua_Console_clear } ,
{ " gotoxy " , lua_Console_gotoxy } ,
{ " gotoxy " , lua_Console_gotoxy } ,