blob: 33e203be193151641ba1a99a38c3f0e1bbb2f3fa [file] [log] [blame]
Peter Kriensde0b31b2010-05-20 15:18:18 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.felix.service.terminal;
20
21import java.io.*;
22
23/**
24 * Terminal.
25 *
26 * The Terminal interface describes a minimal terminal that can easily be mapped
27 * to command line editing tools.
28 *
29 * A Terminal is associated with an Input Stream and an Output Stream. The Input
30 * Stream represents the keyboard and the Output Stream the screen.
31 *
32 * A terminal does not block the input, each character is returned as it is
33 * typed, no buffering or line editing takes place, characters are also not
34 * echoed. However, the Input Stream is not restricted to bytes only, it can
35 * also return translated key strokes. Integers from 1000 are used for those.
36 * Not all keys have to be supported by an implementation.
37 *
38 * A number of functions is provided to move the cursor and erase
39 * characters/lines/screens. Any text outputed to the Output Stream is
40 * immediately added to the cursor position, which is then moved forwards. The
41 * control characters (LF,CR,TAB,BS) perform their normal actions. However lines
42 * do not wrap. Text typed that is longer than the window will not be visible,
43 * it is the responsibility of the sender to ensure this does not happen.
44 *
45 * A screen is considered to be {@link #height()} lines that each have
46 * {@link #width()} characters. For cursor positioning, the screen is assumed to
47 * be starting at 0,0 and increases its position from left to right and from top
48 * to bottom. Positioning outside the screen bounds is undefined.
49 */
50public interface Terminal {
51 /**
52 * Cursor up key
53 */
54 int CURSOR_UP = 1000;
55 /**
56 * Cursor down key.
57 */
58 int CURSOR_DOWN = 1001;
59 /**
60 * Cursors forward key. Usually right.
61 */
62 int CURSOR_FORWARD = 1002;
63
64 /**
65 * Cursors backward key. Usually left.
66 */
67 int CURSOR_BACKWARD = 1003;
68
69 /**
70 * Page up key
71 */
72 int PAGE_UP = 1004;
73 /**
74 * Page down key
75 */
76 int PAGE_DOWN = 1005;
77 /**
78 * Home key
79 */
80 int HOME = 1006;
81 /**
82 * End key
83 */
84 int END = 1007;
85 /**
86 * Insert key
87 */
88 int INSERT = 1008;
89 /**
90 * Delete key
91 */
92 int DELETE = 1009;
93 /**
94 * Break key
95 */
96 int BREAK = 1009;
97 /**
98 * Function key 1
99 */
100 int F1 = 1101;
101 /**
102 * Function key 2
103 */
104 int F2 = 1102;
105 /**
106 * Function key 3
107 */
108 int F3 = 1103;
109 /**
110 * Function key 4
111 */
112 int F4 = 1104;
113 /**
114 * Function key 5
115 */
116 int F5 = 1105;
117 /**
118 * Function key 6
119 */
120 int F6 = 1106;
121 /**
122 * Function key 7
123 */
124 int F7 = 1107;
125 /**
126 * Function key 8
127 */
128 int F8 = 1108;
129 /**
130 * Function key 9
131 */
132 int F9 = 1109;
133 /**
134 * Function key 10
135 */
136 int F10 = 1110;
137 /**
138 * Function key 11
139 */
140 int F11 = 1111;
141 /**
142 * Function key 12
143 */
144 int F12 = 1112;
145
146 enum Attribute {
147 BLINK, UNDERLINE, STRIKE_THROUGH, BOLD, DIM, REVERSED;
148 }
149
150 enum Color {
151 NONE, BLACK, GREEN, YELLOW, MAGENTA, CYAN, BLUE, RED, WHITE;
152 }
153
154 /**
155 * Return the associated Input Stream that represents the keyboard. Note
156 * that this InputStream can return values > 256, these characters are
157 * defined in this interface as special keys. This Input Stream should not
158 * be closed by the client. If the client is done, it should unget the
159 * services.
160 *
161 * @return the current Input Stream.
162 */
163 InputStream getInputStream();
164
165 /**
166 * Return the Output Stream that is associated with the screen. Any writes
167 * Clear the complete screen and position the cursor at 0,0.
168 *
169 * @throws Exception
170 */
171 void clear() throws Exception;
172
173 /**
174 * Leave the cursor where it is but clear the remainder of the line.
175 */
176 void eraseEndOfLine();
177
178 /**
179 * Move the cursor up one line, this must not cause a scroll if the cursor
180 * moves off the screen.
181 *
182 * @throws Exception
183 */
184 void up() throws Exception;
185
186 /**
187 * Move the cursor down one line, this must not cause a scroll if the
188 * cursors moves off the screen.
189 *
190 * @throws Exception
191 */
192 void down() throws Exception;
193
194 /**
195 * Move the cursor backward. Must not wrap to previous line.
196 *
197 * @throws Exception
198 */
199 void backward() throws Exception;
200
201 /**
202 * Move the cursor forward. Must not wrap to next line if the cursor becomes
203 * higher than the width.
204 *
205 * @throws Exception
206 */
207 void forward() throws Exception;
208
209 /**
210 * Return the actual width of the screen. Some screens can change their size
211 * and this method must return the actual width.
212 *
213 * @return the width of the screen.
214 *
215 * @throws Exception
216 */
217 int width() throws Exception;
218
219 /**
220 * Return the actual height of the screen. Some screens can change their
221 * size and this method must return the actual height.
222 *
223 * @return the height of the screen.
224 *
225 * @throws Exception
226 */
227 int height() throws Exception;
228
229 /**
230 * Return the current cursor position.
231 *
232 * The position is returned as an array of 2 elements. The first element is
233 * the x position and the second elements is the y position. Both are zero
234 * based.
235 *
236 * @return the current position or null if not possible.
237 *
238 * @throws Exception
239 */
240 int[] getPosition() throws Exception;
241
242 /**
243 * Position the cursor on the screen. Positioning starts at 0,0 and the
244 * maxium value is given by {@link #width()}, {@link #height()}. The visible
245 * cursor is moved to this position and text insertion will continue from
246 * that position.
247 *
248 * @param x
249 * The x position, must be from 0-width
250 * @param y
251 * The y position, must be from 0-height
252 * @throws IllegalArgumenException
253 * when x or y is not in range
254 * @throws Exception
255 */
256 boolean position(int x, int y) throws Exception;
257
258 /**
259 * Set the attributes of the text to outputed. This method
260 * must reset all current attributes. That is, attributes
261 * are not inherited from the current position.
262 *
263 * @param foreground The foreground color
264 * @param background The background color (around the character)
265 * @param attr A number of attributes.
266 */
267 boolean attributes(Color foreground, Color background, Attribute... attr);
268}