Files
integration/luprex/core/cpp/luaconsole.hpp

76 lines
2.0 KiB
C++

//////////////////////////////////////////////////////
//
// LuaConsole:
//
// Used to parse commands that are being interactively typed
// in by a user. The common usage pattern is:
//
// 1. Print the prompt suggested by 'get_prompt'.
// 2. Read a line of text from stdin.
// 3. Add the line to the LuaConsole using 'add'.
// 4. Get the command words using 'get_command'.
// 5. If the command is empty, do nothing.
// 6. If the command is nonempty, execute it.
//
// The LuaConsole expects you to type one of three things:
//
// * Valid Lua Code.
// * Valid Lua Code preceded by '=' as shorthand for 'return'
// * A slash-command.
//
// The 'get_command' returned by the luaconsole can be one of
// the following:
//
// empty vector - still collecting input: do nothing.
// lua <expr> - found a lua command: execute it as lua.
// syntax <words> - detected a syntax error: print the error.
// word word ... - read a slash-command: execute the command.
//
//////////////////////////////////////////////////////
#ifndef LUACONSOLE_HPP
#define LUACONSOLE_HPP
#include <string>
#include "luastack.hpp"
class LuaConsole {
public:
using StringVec = std::vector<std::string>;
private:
lua_State *lua_state_;
std::string raw_input_;
int lines_;
StringVec words_;
std::string prompt_;
void clear_raw_input();
public:
LuaConsole();
~LuaConsole();
// Fetch the recommended prompt.
//
// You should update the prompt immediately after 'add'.
//
const std::string &get_prompt() { return prompt_; }
// Fetch the command to execute.
//
// You should fetch the command after calling 'add'.
// Returns the empty vector if there is no command.
// If there is a command, the first word is the command word.
// See the file comment for certain built-in command words.
//
StringVec get_command();
// Add a line of text that was just read from the console.
//
void add(std::string line);
};
#endif // LUACONSOLE_HPP