Main+Readline: Do not leak history path std::string_view

This commit is contained in:
Riyyi
2023-03-25 22:36:02 +01:00
parent 9bbf238c34
commit aba70beeb3
3 changed files with 14 additions and 7 deletions
+12 -3
View File
@@ -9,6 +9,8 @@
#include <readline/history.h>
#include <readline/readline.h>
#include <readline/tilde.h>
#include <string>
#include <string_view>
#include "ruc/format/color.h"
@@ -18,7 +20,7 @@ namespace blaze {
Readline::Readline(bool pretty_print, std::string_view history_path)
: m_pretty_print(pretty_print)
, m_history_path(history_path)
, m_history_path(tilde_expand(history_path.data()))
{
if (!pretty_print) {
m_prompt = "user> ";
@@ -28,9 +30,16 @@ Readline::Readline(bool pretty_print, std::string_view history_path)
m_prompt += format(" \033[1m");
}
read_history(tilde_expand(history_path.data()));
read_history(m_history_path);
}
Readline::~Readline()
{
std::free(m_history_path);
}
// -----------------------------------------
bool Readline::get(std::string& output)
{
char* line = readline(m_prompt.c_str());
@@ -40,7 +49,7 @@ bool Readline::get(std::string& output)
// Add input to in-memory history
add_history(line);
append_history(1, m_history_path.data());
append_history(1, m_history_path);
output = line;
std::free(line);
+2 -2
View File
@@ -16,14 +16,14 @@ namespace blaze {
class Readline {
public:
Readline(bool pretty_print, std::string_view history_path);
virtual ~Readline() {}
virtual ~Readline();
bool get(std::string& output);
private:
bool m_pretty_print { false };
std::string m_prompt;
std::string_view m_history_path;
char* m_history_path;
};
} // namespace blaze
-2
View File
@@ -104,5 +104,3 @@ auto main(int argc, char* argv[]) -> int
return 0;
}
#endif
// - Add AST node printing support to ruc::format