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  UT_CmdLineReader(const UT_CmdLineReader &) = delete;
37  UT_CmdLineReader &operator=(const UT_CmdLineReader &) = delete;
38 
39  // readLine() will display an editable command line and place the result
40  // in a work buffer when the user presses enter. If stdin is not a tty,
41  // it will also behave properly. If the line could not be read because
42  // there is no more input available, false will be returned.
43  bool readLine(UT_WorkBuffer &line);
44 
45  // readPartOfLine() is similar to readLine(), except it will only read the
46  // next group of characters that are available in the input. If you want
47  // to continuously poll for other events while waiting for input (like
48  // hscript does), you should check for data on stdin and call this method
49  // when data is ready. false will be returned if stdin is no longer open.
50  // done_line will be set to true if an entire line was read, and only then
51  // will the work buffer be set. Note that if multiple lines are
52  // available in the input, they will all be read in, the first line
53  // will be placed in "line", done_line will be true, and hasUnhandledInput()
54  // will return true. Subsequent calls will first read from the unhandled
55  // input before going to the read input.
56  bool readPartOfLine(UT_WorkBuffer &line, bool &done_line);
57 
58  // Was there input as a result of the last read that hasn't been
59  // handled yet?
60  bool hasUnhandledInput() const { return myUnhandledInput.length() != 0; }
61 
62  // readLine() will disable line buffering when it is called. However,
63  // you may want to disable line buffering for the duration of your
64  // application so that you can check if data is available on stdin.
65  // (Otherwise, you would only know about data on stdin when a complete
66  // line is entered.)
67  void disableLineBuffering();
68  void reenableLineBuffering();
69  bool isLineBufferingEnabled() const;
70 
71  static UT_CmdLineReader *getInstance();
72 
73 private:
74  void construct();
75 
76  bool readAvailableInput(UT_WorkBuffer &buffer);
77  bool handleNextBufferedKey(bool &done_line);
78 
79  void insertChar(char ch);
80  void insertString(const char *str);
81  void removeCharBefore();
82  void removeCharAt();
83  void removeWordBefore();
84 
85  void updateDisplay(const UT_WorkBuffer &old_line, int old_cursor_pos,
86  const UT_WorkBuffer &new_line, int new_cursor_pos);
87  int tabLengthAtPosition(int tab_start_pos);
88  int *buildCharIndexToLengthArray(const UT_WorkBuffer &line);
89  void printExpandedChar(
90  char chr, int index, int *char_lengths, char replacement='\0');
91 
92  void moveUpThroughHistory();
93  void moveDownThroughHistory();
94 
95 
96  // Data:
97  bool myAllowCmdLineEditing;
98 
99  // The TerminalData opaque class avoids having to include termios.h
100  // in the header, which pollutes the global namespace.
101  class TerminalData;
102  TerminalData *myTerminalData;
103 
104  bool myLineBufferingIsDisabled;
105  bool myLineBufferingWasDisabled;
106  bool myIsReadingLine;
107  UT_WorkBuffer myLine;
108  UT_WorkBuffer myUnhandledInput;
109  int myCursorPos;
110  UT_StringArray myHistory;
111  int myHistoryIndex;
112  UT_TabCallback myTabCallback;
113  UT_TabCallback myDoubleTabCallback;
114  int myTabLength;
115  bool myJustReadTab;
116 
117  UT_String myEscapeKey;
118  UT_String myCtrlAKey;
119  UT_String myCtrlDKey;
120  UT_String myCtrlEKey;
121  UT_String myCtrlWKey;
122  UT_String myLeftArrowKey;
123  UT_String myRightArrowKey;
124  UT_String myUpArrowKey;
125  UT_String myDownArrowKey;
126  UT_String myBackspaceKey;
127  UT_String myDeleteKey;
128  UT_String myHomeKey;
129  UT_String myEndKey;
130 
131  static UT_CmdLineReader *theInstance;
132 };
133 
134 #endif
void
Definition: png.h:1083
#define UT_API
Definition: UT_API.h:14
bool hasUnhandledInput() const
Definition: core.h:760
void(* UT_TabCallback)(UT_WorkBuffer &, int &)
GLuint index
Definition: glcorearb.h:786