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

Last change on this file since 11131 was 11131, checked in by tbretz, 13 years ago
Put everything into the history, but only successfull commands into the file history.
File size: 3.9 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
57 // Wrapper for some process code
58 void ProcessLine(const std::string &str);
59
60public:
61 Readline(const char *prgname);
62 ~Readline();
63
64 // Access to readline
65 void BindKeySequence(const char *seq, int (*func)(int, int));
66
67 static bool DumpVariables();
68 static bool DumpFunctions();
69 static bool DumpFunmap();
70 static bool DumpHistory();
71
72 virtual bool PrintGeneralHelp();
73 virtual bool PrintCommands();
74 virtual bool PrintKeyBindings();
75
76 // History functions
77 std::string GetName() const { return fName; }
78
79 void AddToHistory(const std::string &str, int skip=2);
80 static bool ClearHistory();
81 std::vector<const char*> GetHistory() const;
82
83 void SetMaxSize(int lines) { fMaxLines = lines; }
84
85 // Prompting
86 void UpdatePrompt(const std::string &prompt) const;
87 void UpdatePrompt() const { UpdatePrompt(GetUpdatePrompt()); }
88
89 virtual bool Process(const std::string &str);
90 virtual std::string GetUpdatePrompt() const { return ""; }
91 virtual bool PromptEOF(std::string &str);
92 virtual std::string Prompt(const std::string &prompt);
93 virtual void Run(const char *prompt=0);
94 static void Stop();
95 int Execute(const std::string &fname);
96 bool IsStopped() const;
97
98 int GetLine() const { return fLine; }
99 virtual std::string GetLinePrompt() const;
100
101 // Helper
102 static char *Compare(const std::string &str, const std::string &txt);
103 static char **CompletionMatches(const char *text, char *(*func)(const char*, int));
104
105 // I/O Streams
106 static FILE *SetStreamOut(FILE *f);
107 static FILE *SetStreamIn(FILE *f);
108
109 // Other global readline variables
110 static std::string GetPrompt();
111 static std::string GetBuffer();
112 static int GetAbsCursor();
113 static int GetCursor();
114 static int GetBufferLength();
115 static int GetLineLength();
116
117 // Screen size
118 static void Resize();
119 static void Resize(int w, int h);
120 int GetCols() const;
121 int GetRows() const;
122
123};
124
125#endif
Note: See TracBrowser for help on using the repository browser.