Make luaconsole even easier to use.
This commit is contained in:
@@ -10,28 +10,20 @@ static bool is_single_letter(const std::string &s) {
|
||||
|
||||
LuaConsole::LuaConsole() {
|
||||
lua_state_ = luaL_newstate();
|
||||
clear();
|
||||
clear_raw_input();
|
||||
}
|
||||
|
||||
LuaConsole::~LuaConsole() {
|
||||
lua_close(lua_state_);
|
||||
}
|
||||
|
||||
void LuaConsole::clear() {
|
||||
raw_input_ = "";
|
||||
lines_ = 0;
|
||||
words_.clear();
|
||||
prompt_ = "> ";
|
||||
}
|
||||
|
||||
|
||||
void LuaConsole::split_words() {
|
||||
words_.clear();
|
||||
static LuaConsole::StringVec split_words(const std::string &raw) {
|
||||
LuaConsole::StringVec result;
|
||||
std::string acc;
|
||||
for (char c : raw_input_) {
|
||||
for (char c : raw) {
|
||||
if ((c == ' ')||(c == '\n')||(c == '\r')) {
|
||||
if (!acc.empty()) {
|
||||
words_.push_back(acc);
|
||||
result.push_back(acc);
|
||||
acc = "";
|
||||
}
|
||||
} else {
|
||||
@@ -39,16 +31,23 @@ void LuaConsole::split_words() {
|
||||
}
|
||||
}
|
||||
if (!acc.empty()) {
|
||||
words_.push_back(acc);
|
||||
result.push_back(acc);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string LuaConsole::get_prompt() {
|
||||
std::string result = prompt_;
|
||||
prompt_ = "";
|
||||
LuaConsole::StringVec LuaConsole::get_command() {
|
||||
StringVec result = words_;
|
||||
words_.clear();
|
||||
return result;
|
||||
}
|
||||
|
||||
void LuaConsole::clear_raw_input() {
|
||||
raw_input_ = "";
|
||||
lines_ = 0;
|
||||
prompt_ = "> ";
|
||||
}
|
||||
|
||||
void LuaConsole::add(std::string line) {
|
||||
for (int i = 0; i < int(line.size()); i++) {
|
||||
if (line[i] == '\n') line[i] = ' ';
|
||||
@@ -56,18 +55,19 @@ void LuaConsole::add(std::string line) {
|
||||
raw_input_ += line;
|
||||
raw_input_ += '\n';
|
||||
lines_ += 1;
|
||||
prompt_ = ">> ";
|
||||
|
||||
// Try to interpret it as a special command.
|
||||
if (lines_ == 1) {
|
||||
split_words();
|
||||
if (words_.size() >= 1) {
|
||||
if (is_single_letter(words_[0]) || (util::validinteger(words_[0]))) {
|
||||
// We have a valid parsed command. Return it.
|
||||
StringVec words = split_words(raw_input_);
|
||||
if (words.size() >= 1) {
|
||||
if (is_single_letter(words[0]) || (util::validinteger(words[0]))) {
|
||||
words_ = words;
|
||||
clear_raw_input();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
words_.clear();
|
||||
|
||||
// Translate lua expression with leading '=' to 'return'
|
||||
std::string partial;
|
||||
@@ -90,14 +90,13 @@ void LuaConsole::add(std::string line) {
|
||||
if (strstr(msg, eof) != tp) {
|
||||
words_.push_back("syntax");
|
||||
words_.push_back(msg);
|
||||
clear_raw_input();
|
||||
}
|
||||
} else {
|
||||
words_.push_back("lua");
|
||||
words_.push_back(partial);
|
||||
clear_raw_input();
|
||||
}
|
||||
lua_settop(lua_state_, top);
|
||||
if (words_.empty()) {
|
||||
prompt_ = ">> ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user