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

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