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

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