1 | #ifndef FACT_Readline
|
---|
2 | #define FACT_Readline
|
---|
3 |
|
---|
4 | #include <string>
|
---|
5 | #include <vector>
|
---|
6 |
|
---|
7 | class Readline
|
---|
8 | {
|
---|
9 | public:
|
---|
10 | static bool RedirectionWrapper(std::ostream &out, bool (*function)());
|
---|
11 |
|
---|
12 | protected:
|
---|
13 | /// A pointer to the unique instance of Readline to let the static
|
---|
14 | /// functions used as callback for readline call member function
|
---|
15 | /// with an object. This makes overwriting easier.
|
---|
16 | static Readline *This;
|
---|
17 |
|
---|
18 | private:
|
---|
19 | std::string fName; /// Filename for the history file compiled in the constructor
|
---|
20 | int fMaxLines; /// Maximum number of lines in the history file
|
---|
21 |
|
---|
22 | std::string fLastLine; /// Last line adde to history
|
---|
23 |
|
---|
24 | int fLine;
|
---|
25 |
|
---|
26 | // Static member function which are used to adapt readline to ncurses
|
---|
27 | static int rl_ncurses_getc(FILE *);
|
---|
28 | static int rl_ncurses_startup();
|
---|
29 | static void rl_ncurses_redisplay();
|
---|
30 | static int rl_ncurses_event_hook();
|
---|
31 | static void rl_ncurses_completion_display(char **matches, int num, int max);
|
---|
32 | static char **rl_ncurses_completion_function(const char *text, int start, int end);
|
---|
33 | static char *CompleteImp(const char* text, int state);
|
---|
34 |
|
---|
35 |
|
---|
36 | protected:
|
---|
37 | // The non static implementations of the callback funtions above
|
---|
38 | virtual int Getc(FILE *);
|
---|
39 | virtual void Startup();
|
---|
40 | virtual void EventHook();
|
---|
41 | virtual void Shutdown(const char *buf);
|
---|
42 | virtual void Redisplay();
|
---|
43 | virtual void CompletionDisplay(char **matches, int num, int max);
|
---|
44 |
|
---|
45 | // Functions dealing with auto completion
|
---|
46 | virtual char *Complete(const char* text, int state);
|
---|
47 | virtual char **Completion(const char *text, int start, int end);
|
---|
48 |
|
---|
49 | /// Pointer to a list of possible matched for auto-completion
|
---|
50 | const std::vector<std::string> *fCompletion;
|
---|
51 | void SetCompletion(const std::vector<std::string> *v) { fCompletion = v; }
|
---|
52 | char **Complete(const std::vector<std::string> &v, const char *text);
|
---|
53 |
|
---|
54 | public:
|
---|
55 | Readline(const char *prgname);
|
---|
56 | ~Readline();
|
---|
57 |
|
---|
58 | // Access to readline
|
---|
59 | void BindKeySequence(const char *seq, int (*func)(int, int));
|
---|
60 |
|
---|
61 | static bool DumpVariables();
|
---|
62 | static bool DumpFunctions();
|
---|
63 | static bool DumpFunmap();
|
---|
64 | static bool DumpHistory();
|
---|
65 |
|
---|
66 | virtual bool PrintGeneralHelp();
|
---|
67 | virtual bool PrintCommands();
|
---|
68 | virtual bool PrintKeyBindings();
|
---|
69 |
|
---|
70 | // History functions
|
---|
71 | std::string GetName() const { return fName; }
|
---|
72 |
|
---|
73 | void AddToHistory(const std::string &str, int skip=2);
|
---|
74 | static bool ClearHistory();
|
---|
75 | std::vector<const char*> GetHistory() const;
|
---|
76 |
|
---|
77 | void SetMaxSize(int lines) { fMaxLines = lines; }
|
---|
78 |
|
---|
79 | // Prompting
|
---|
80 | void UpdatePrompt(const std::string &prompt) const;
|
---|
81 | void UpdatePrompt() const { UpdatePrompt(GetUpdatePrompt()); }
|
---|
82 |
|
---|
83 | virtual bool Process(const std::string &str);
|
---|
84 | virtual std::string GetUpdatePrompt() const { return ""; }
|
---|
85 | virtual bool PromptEOF(std::string &str);
|
---|
86 | virtual std::string Prompt(const std::string &prompt);
|
---|
87 | virtual void Run(const char *prompt=0);
|
---|
88 | static void Stop();
|
---|
89 | int Execute(const std::string &fname);
|
---|
90 |
|
---|
91 | int GetLine() const { return fLine; }
|
---|
92 | virtual std::string GetLinePrompt() const;
|
---|
93 |
|
---|
94 | // Helper
|
---|
95 | static char *Compare(const std::string &str, const std::string &txt);
|
---|
96 | static char **CompletionMatches(const char *text, char *(*func)(const char*, int));
|
---|
97 |
|
---|
98 | // I/O Streams
|
---|
99 | static FILE *SetStreamOut(FILE *f);
|
---|
100 | static FILE *SetStreamIn(FILE *f);
|
---|
101 |
|
---|
102 | // Other global readline variables
|
---|
103 | static std::string GetPrompt();
|
---|
104 | static std::string GetBuffer();
|
---|
105 | static int GetAbsCursor();
|
---|
106 | static int GetCursor();
|
---|
107 | static int GetBufferLength();
|
---|
108 | static int GetLineLength();
|
---|
109 |
|
---|
110 | // Screen size
|
---|
111 | static void Resize();
|
---|
112 | static void Resize(int w, int h);
|
---|
113 | int GetCols() const;
|
---|
114 | int GetRows() const;
|
---|
115 |
|
---|
116 | };
|
---|
117 |
|
---|
118 | #endif
|
---|