source: trunk/FACT++/src/Readline.h@ 15040

Last change on this file since 15040 was 14984, checked in by tbretz, 12 years ago
Update the prompt in regular intervals even if GetUpdatePrompt returns nothing; added ::Static* functions which allow to run a simplified version of readline
File size: 5.0 KB
Line 
1#ifndef FACT_Readline
2#define FACT_Readline
3
4#include <map>
5#include <string>
6#include <vector>
7#include <fstream>
8
9class Readline
10{
11public:
12 static bool RedirectionWrapper(std::ostream &out, bool (*function)());
13
14protected:
15 /// A pointer to the unique instance of Readline to let the static
16 /// functions used as callback for readline call member function
17 /// with an object. This makes overwriting easier.
18 static Readline *This;
19
20private:
21 std::string fName; /// Filename for the history file compiled in the constructor
22 int fMaxLines; /// Maximum number of lines in the history file
23
24 std::ofstream fCommandLog;
25
26 std::string fLastLine; /// Last line adde to history
27
28 int fLine;
29 int fSection;
30 int fLabel;
31 static int fScriptDepth;
32 static bool fStopScript;
33
34 static std::string fExternalInput;
35
36 /// Static member function which are used to adapt readline to ncurses
37 static int rl_ncurses_getc(FILE *);
38 static int rl_ncurses_startup();
39 static void rl_ncurses_redisplay();
40 static int rl_ncurses_event_hook();
41 static void rl_ncurses_completion_display(char **matches, int num, int max);
42 static char **rl_ncurses_completion_function(const char *text, int start, int end);
43 static char *CompleteImp(const char* text, int state);
44
45
46protected:
47 static std::string fScript;
48
49 /// The non static implementations of the callback funtions above
50 virtual int Getc(FILE *);
51 virtual void Startup();
52 virtual void EventHook();
53 virtual void Shutdown(const char *buf);
54 virtual void Redisplay();
55 virtual void CompletionDisplay(char **matches, int num, int max);
56
57 /// Functions dealing with auto completion
58 virtual char *Complete(const char* text, int state);
59 virtual char **Completion(const char *text, int start, int end);
60
61 /// Pointer to a list of possible matched for auto-completion
62 const std::vector<std::string> *fCompletion;
63 void SetCompletion(const std::vector<std::string> *v) { fCompletion = v; }
64 char **Complete(const std::vector<std::string> &v, const char *text);
65
66 ///
67 virtual void SetSection(int) { }
68 virtual void PrintReadlineError(const std::string &str);
69
70public:
71 Readline(const char *prgname);
72 virtual ~Readline();
73
74 // Access to readline
75 void BindKeySequence(const char *seq, int (*func)(int, int));
76
77 static bool DumpVariables();
78 static bool DumpFunctions();
79 static bool DumpFunmap();
80 static bool DumpHistory();
81
82 virtual bool PrintGeneralHelp();
83 virtual bool PrintCommands();
84 virtual bool PrintKeyBindings();
85
86 // History functions
87 std::string GetName() const { return fName; }
88
89 void AddToHistory(const std::string &str, int skip=2);
90 static bool ClearHistory();
91 std::vector<const char*> GetHistory() const;
92
93 void SetMaxSize(int lines) { fMaxLines = lines; }
94
95 // Prompting
96 void UpdatePrompt(const std::string &prompt) const;
97 void UpdatePrompt() const { UpdatePrompt(GetUpdatePrompt()); }
98
99 virtual bool PreProcess(const std::string &str);
100 virtual bool Process(const std::string &str);
101 virtual std::string GetUpdatePrompt() const { return ""; }
102 virtual bool PromptEOF(std::string &str);
103 virtual std::string Prompt(const std::string &prompt);
104 virtual void Run(const char *prompt=0);
105 static void Stop();
106 virtual bool ExecuteShellCommand(const std::string &cmd);
107 int Execute(const std::string &fname, const std::map<std::string,std::string> &args=std::map<std::string,std::string>());
108 bool IsStopped() const;
109 void ProcessLine(const std::string &str);
110 void SetLabel(int l) { fLabel = l; }
111 static void StopScript() { fStopScript = true; }
112 static bool IsScriptStopped() { return fStopScript; }
113 static int GetScriptDepth() { return fScriptDepth; }
114 static void SetExternalInput(const std::string &inp) { fExternalInput = inp; }
115
116 static std::string GetScript() { return fScript; }
117 static std::string GetExternalInput() { return fExternalInput; }
118
119 int GetLine() const { return fLine; }
120 virtual std::string GetLinePrompt() const;
121
122 // Helper
123 static char *Compare(const std::string &str, const std::string &txt);
124 static char **CompletionMatches(const char *text, char *(*func)(const char*, int));
125
126 // I/O Streams
127 static FILE *SetStreamOut(FILE *f);
128 static FILE *SetStreamIn(FILE *f);
129
130 // Other global readline variables
131 static std::string GetPrompt();
132 static std::string GetBuffer();
133 static int GetAbsCursor();
134 static int GetCursor();
135 static int GetBufferLength();
136 static int GetLineLength();
137
138 // Screen size
139 static void Resize();
140 static void Resize(int w, int h);
141 int GetCols() const;
142 int GetRows() const;
143
144 static Readline *Instance() { return This; }
145
146 static void StaticPushHistory(const std::string &fname);
147 static std::string StaticPrompt(const std::string &prompt);
148 static void StaticPopHistory(const std::string &fname);
149};
150
151#endif
Note: See TracBrowser for help on using the repository browser.