-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadLine.cpp
43 lines (39 loc) · 861 Bytes
/
ReadLine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <string>
#include <cstdlib>
#include "ReadLine.hpp"
#include <unistd.h>
using namespace std;
namespace{
char *_readline(const char *s){
return readline(s);
}
}
string ReadLine::history_file;
void ReadLine::init(string history_name, bool home){
using_history();
history_file = history_name;
if(home)history_file = string(getenv("HOME")) + "/" + history_file;
read_history(history_file.c_str());
ReadLine::eof = false;
}
string ReadLine::readline(string prompt){
char *res = _readline(prompt.c_str());
if(res){
add_history(res);
string r(res);
free(res);
return r;
}else{
ReadLine::eof = true;
return "";
}
}
bool ReadLine::is_eof(){
return eof;
}
void ReadLine::finalize(){
write_history(history_file.c_str());
}