HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_CmdLineReader.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_CmdLineReader.h
7  *
8  * COMMENTS:
9  * This class acts as a replacement to UT_Workbuffer::getline(cin).
10  * It supports command-line editing with arrow keys and it keeps
11  * track of a command-line history accessible with the up and down
12  * arrow keys.
13  */
14 
15 #ifndef __UT_CmdLineReader_h__
16 #define __UT_CmdLineReader_h__
17 
18 #include "UT_API.h"
19 #include "UT_WorkBuffer.h"
20 #include "UT_StringArray.h"
21 
22 typedef void (*UT_TabCallback)(UT_WorkBuffer &, int &);
23 
25 {
26 public:
27  // If a function is supplied to the constructor, it will be called when
28  // tab is pressed. The function is passed the current contents of the
29  // line and the current cursor position. It should change both these
30  // values.
32  UT_TabCallback tab_callback=NULL,
33  UT_TabCallback double_tab_callback=NULL);
35 
36  // readLine() will display an editable command line and place the result
37  // in a work buffer when the user presses enter. If stdin is not a tty,
38  // it will also behave properly. If the line could not be read because
39  // there is no more input available, false will be returned.
40  bool readLine(UT_WorkBuffer &line);
41 
42  // readPartOfLine() is similar to readLine(), except it will only read the
43  // next group of characters that are available in the input. If you want
44  // to continuously poll for other events while waiting for input (like
45  // hscript does), you should check for data on stdin and call this method
46  // when data is ready. false will be returned if stdin is no longer open.
47  // done_line will be set to true if an entire line was read, and only then
48  // will the work buffer be set. Note that if multiple lines are
49  // available in the input, they will all be read in, the first line
50  // will be placed in "line", done_line will be true, and hasUnhandledInput()
51  // will return true. Subsequent calls will first read from the unhandled
52  // input before going to the read input.
53  bool readPartOfLine(UT_WorkBuffer &line, bool &done_line);
54 
55  // Was there input as a result of the last read that hasn't been
56  // handled yet?
57  bool hasUnhandledInput() const { return myUnhandledInput.length() != 0; }
58 
59  // readLine() will disable line buffering when it is called. However,
60  // you may want to disable line buffering for the duration of your
61  // application so that you can check if data is available on stdin.
62  // (Otherwise, you would only know about data on stdin when a complete
63  // line is entered.)
64  void disableLineBuffering();
65  void reenableLineBuffering();
66  bool isLineBufferingEnabled() const;
67 
68  static UT_CmdLineReader *getInstance();
69 
70 private:
71  void construct();
72 
73  bool readAvailableInput(UT_WorkBuffer &buffer);
74  bool handleNextBufferedKey(bool &done_line);
75 
76  void insertChar(char ch);
77  void insertString(const char *str);
78  void removeCharBefore();
79  void removeCharAt();
80  void removeWordBefore();
81 
82  void updateDisplay(const UT_WorkBuffer &old_line, int old_cursor_pos,
83  const UT_WorkBuffer &new_line, int new_cursor_pos);
84  int tabLengthAtPosition(int tab_start_pos);
85  int *buildCharIndexToLengthArray(const UT_WorkBuffer &line);
86  void printExpandedChar(
87  char chr, int index, int *char_lengths, char replacement='\0');
88 
89  void moveUpThroughHistory();
90  void moveDownThroughHistory();
91 
92 
93  // Data:
94  bool myAllowCmdLineEditing;
95 
96  // The TerminalData opaque class avoids having to include termios.h
97  // in the header, which pollutes the global namespace.
98  class TerminalData;
99  TerminalData *myTerminalData;
100 
101  bool myLineBufferingIsDisabled;
102  bool myLineBufferingWasDisabled;
103  bool myIsReadingLine;
104  UT_WorkBuffer myLine;
105  UT_WorkBuffer myUnhandledInput;
106  int myCursorPos;
107  UT_StringArray myHistory;
108  int myHistoryIndex;
109  UT_TabCallback myTabCallback;
110  UT_TabCallback myDoubleTabCallback;
111  int myTabLength;
112  bool myJustReadTab;
113 
114  UT_String myEscapeKey;
115  UT_String myCtrlAKey;
116  UT_String myCtrlDKey;
117  UT_String myCtrlEKey;
118  UT_String myCtrlWKey;
119  UT_String myLeftArrowKey;
120  UT_String myRightArrowKey;
121  UT_String myUpArrowKey;
122  UT_String myDownArrowKey;
123  UT_String myBackspaceKey;
124  UT_String myDeleteKey;
125  UT_String myHomeKey;
126  UT_String myEndKey;
127 
128  static UT_CmdLineReader *theInstance;
129 };
130 
131 #endif
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
#define UT_API
Definition: UT_API.h:13
GLuint buffer
Definition: glcorearb.h:659
bool hasUnhandledInput() const
void(* UT_TabCallback)(UT_WorkBuffer &, int &)
GLuint index
Definition: glcorearb.h:785