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