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

74 lines
2.0 KiB
C++
Raw Normal View History

2021-01-23 16:10:29 -05:00
//////////////////////////////////////////////////////
//
// LuaConsole:
//
2021-02-02 16:29:07 -05:00
// Used to parse commands that are being interactively typed
// in by a user. The common usage pattern is:
2021-01-23 20:12:21 -05:00
//
// 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'.
2021-11-04 14:49:25 -04:00
// 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'.
2021-01-23 20:12:21 -05:00
//
// 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:
2021-01-23 20:12:21 -05:00
//
// lua <expr> - execute a lua expression
// syntax <message> - print a syntax error message
2021-02-02 16:29:07 -05:00
//
// 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.
2021-01-23 20:12:21 -05:00
//
2021-01-23 16:10:29 -05:00
//////////////////////////////////////////////////////
#ifndef LUACONSOLE_HPP
#define LUACONSOLE_HPP
#include <string>
#include "luastack.hpp"
class LuaConsole {
public:
2021-02-02 16:29:07 -05:00
using StringVec = std::vector<std::string>;
2021-01-23 16:10:29 -05:00
private:
lua_State *lua_state_;
2021-02-02 16:29:07 -05:00
std::string raw_input_;
2021-01-23 16:10:29 -05:00
int lines_;
2021-02-02 16:29:07 -05:00
StringVec words_;
2021-10-07 14:58:20 -04:00
std::string prompt_;
2021-01-23 16:10:29 -05:00
2021-11-04 14:49:25 -04:00
void clear_raw_input();
2021-02-02 16:29:07 -05:00
2021-01-23 16:10:29 -05:00
public:
LuaConsole();
~LuaConsole();
2021-11-04 14:49:25 -04:00
// Fetch the recommended prompt.
//
// You should update the prompt immediately after 'add'.
//
const std::string &get_prompt() { return prompt_; }
2021-10-07 14:58:20 -04:00
2021-11-04 14:49:25 -04:00
// Fetch the command to execute.
//
2021-11-04 14:49:25 -04:00
// 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.
//
2021-11-04 14:49:25 -04:00
StringVec get_command();
2021-02-02 16:29:07 -05:00
// Add a line of text that was just read from the console.
2021-11-04 14:49:25 -04:00
//
2021-01-23 16:10:29 -05:00
void add(std::string line);
2021-02-02 16:29:07 -05:00
2021-01-23 16:10:29 -05:00
};
2021-01-23 20:12:21 -05:00
#endif // LUACONSOLE_HPP