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

Last change on this file since 13765 was 13731, checked in by tbretz, 12 years ago
Fixed a problem with the label interpretation; added a storage variable for the script name
File size: 4.3 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 static int fLabel;
30 static std::string fScript;
31
32 // Static member function which are used to adapt readline to ncurses
33 static int rl_ncurses_getc(FILE *);
34 static int rl_ncurses_startup();
35 static void rl_ncurses_redisplay();
36 static int rl_ncurses_event_hook();
37 static void rl_ncurses_completion_display(char **matches, int num, int max);
38 static char **rl_ncurses_completion_function(const char *text, int start, int end);
39 static char *CompleteImp(const char* text, int state);
40
41
42protected:
43 // The non static implementations of the callback funtions above
44 virtual int Getc(FILE *);
45 virtual void Startup();
46 virtual void EventHook();
47 virtual void Shutdown(const char *buf);
48 virtual void Redisplay();
49 virtual void CompletionDisplay(char **matches, int num, int max);
50
51 // Functions dealing with auto completion
52 virtual char *Complete(const char* text, int state);
53 virtual char **Completion(const char *text, int start, int end);
54
55 /// Pointer to a list of possible matched for auto-completion
56 const std::vector<std::string> *fCompletion;
57 void SetCompletion(const std::vector<std::string> *v) { fCompletion = v; }
58 char **Complete(const std::vector<std::string> &v, const char *text);
59
60 virtual void SetSection(int) { }
61
62public:
63 Readline(const char *prgname);
64 virtual ~Readline();
65
66 // Access to readline
67 void BindKeySequence(const char *seq, int (*func)(int, int));
68
69 static bool DumpVariables();
70 static bool DumpFunctions();
71 static bool DumpFunmap();
72 static bool DumpHistory();
73
74 virtual bool PrintGeneralHelp();
75 virtual bool PrintCommands();
76 virtual bool PrintKeyBindings();
77
78 // History functions
79 std::string GetName() const { return fName; }
80
81 void AddToHistory(const std::string &str, int skip=2);
82 static bool ClearHistory();
83 std::vector<const char*> GetHistory() const;
84
85 void SetMaxSize(int lines) { fMaxLines = lines; }
86
87 // Prompting
88 void UpdatePrompt(const std::string &prompt) const;
89 void UpdatePrompt() const { UpdatePrompt(GetUpdatePrompt()); }
90
91 virtual bool PreProcess(const std::string &str);
92 virtual bool Process(const std::string &str);
93 virtual std::string GetUpdatePrompt() const { return ""; }
94 virtual bool PromptEOF(std::string &str);
95 virtual std::string Prompt(const std::string &prompt);
96 virtual void Run(const char *prompt=0);
97 static void Stop();
98 virtual bool ExecuteShellCommand(const std::string &cmd);
99 int Execute(const std::string &fname, const std::map<std::string,std::string> &args=std::map<std::string,std::string>());
100 bool IsStopped() const;
101 void ProcessLine(const std::string &str);
102 static void SetLabel(int l) { fLabel = l; }
103 static int GetLabel() { return fLabel; }
104
105 static std::string GetScript() { return fScript; }
106
107 int GetLine() const { return fLine; }
108 virtual std::string GetLinePrompt() const;
109
110 // Helper
111 static char *Compare(const std::string &str, const std::string &txt);
112 static char **CompletionMatches(const char *text, char *(*func)(const char*, int));
113
114 // I/O Streams
115 static FILE *SetStreamOut(FILE *f);
116 static FILE *SetStreamIn(FILE *f);
117
118 // Other global readline variables
119 static std::string GetPrompt();
120 static std::string GetBuffer();
121 static int GetAbsCursor();
122 static int GetCursor();
123 static int GetBufferLength();
124 static int GetLineLength();
125
126 // Screen size
127 static void Resize();
128 static void Resize(int w, int h);
129 int GetCols() const;
130 int GetRows() const;
131
132 static Readline *Instance() { return This; }
133};
134
135#endif
Note: See TracBrowser for help on using the repository browser.