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

74 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 and call 'clear'.
//
// The 'words' returned by the luaconsole can be empty, meaning
// that there's no command to execute. If the words are nonempty,
// then the first word is the command. There are two hardwired
// commands:
//
// lua <expr> - execute a lua expression
// syntax <message> - print a syntax error message
//
// If the first word is anything else, it means the user typed
// that word on the command line. It is up to you to interpret
// such commands.
//
//////////////////////////////////////////////////////
#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