-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTestTerminal.java
272 lines (222 loc) · 7.92 KB
/
TestTerminal.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import java.io.*;
import java.io.PrintStream;
import java.util.*;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;
import javax.swing.*;
import java.awt.*;
import javax.swing.text.BadLocationException;
public class TestTerminal {
public static void main(String[] args) {
Terminal term = Terminal.getInstance();
term.open(0, 410, 1355, 300);
}
}
class CustomOutputStream extends OutputStream {
private JTextArea textArea;
public CustomOutputStream(JTextArea textArea) {
this.textArea = textArea;
}
@Override
public void write(int b) throws IOException {
textArea.append(String.valueOf((char)b));
textArea.setCaretPosition(textArea.getDocument().getLength());
}
}
class Terminal{
private JFrame frm = new JFrame("Terminal");
public static JTextArea txtArea = new JTextArea();
private JScrollPane scrollPane = new JScrollPane();
private CommandProcessor processor = CommandProcessor.getInstance();
private final String LINE_SEPARATOR = System.lineSeparator();
private Font font = new Font("Consolas", Font.BOLD, 12);
private PrintStream standardOut;
private Terminal() {
PrintStream printStream = new PrintStream(new CustomOutputStream(txtArea));
standardOut = System.out;
System.setOut(printStream);
System.setErr(printStream);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.getContentPane().add(scrollPane);
scrollPane.setViewportView(txtArea);
txtArea.addKeyListener(new KeyListener());
txtArea.setFont(font);
txtArea.setForeground(Color.WHITE);
txtArea.setBackground(Color.BLACK);
disableArrowKeys(txtArea.getInputMap());
}
private void disableArrowKeys(InputMap inputMap) {
String[] keystrokeNames = { "UP", "DOWN", "LEFT", "RIGHT", "HOME" };
for (int i = 0; i < keystrokeNames.length; ++i)
inputMap.put(KeyStroke.getKeyStroke(keystrokeNames[i]), "none");
}
public void open(final int xLocation, final int yLocation, final int width,
final int height) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frm.setBounds(xLocation, yLocation, width, height);
frm.setVisible(true);
showPrompt();
}
});
}
public void close() {
frm.dispose();
}
public void clear() {
txtArea.setText("");
showPrompt();
}
private void showPrompt() {
txtArea.setText(txtArea.getText() + "> ");
}
private void showNewLine() {
txtArea.setText(txtArea.getText() + LINE_SEPARATOR);
}
private class KeyListener extends KeyAdapter {
private final int ENTER_KEY = KeyEvent.VK_ENTER;
private final int BACK_SPACE_KEY = KeyEvent.VK_BACK_SPACE;
private final String BACK_SPACE_KEY_BINDING = getKeyBinding(
txtArea.getInputMap(), "BACK_SPACE");
private final int INITIAL_CURSOR_POSITION = 2;
private boolean isKeysDisabled;
private int minCursorPosition = INITIAL_CURSOR_POSITION;
private String getKeyBinding(InputMap inputMap, String name) {
return (String) inputMap.get(KeyStroke.getKeyStroke(name));
}
public void keyPressed(KeyEvent evt) {
int keyCode = evt.getKeyCode();
if (keyCode == BACK_SPACE_KEY) {
int cursorPosition = txtArea.getCaretPosition();
if (cursorPosition == minCursorPosition && !isKeysDisabled) {
disableBackspaceKey();
} else if (cursorPosition > minCursorPosition && isKeysDisabled) {
enableBackspaceKey();
}
} else if (keyCode == ENTER_KEY) {
disableTerminal();
String command = extractCommand();
executeCommand(command);
showNewLine();
showPrompt();
enableTerminal();
}
}
public void keyReleased(KeyEvent evt) {
int keyCode = evt.getKeyCode();
if (keyCode == ENTER_KEY) {
txtArea.setCaretPosition(txtArea.getCaretPosition() - 1);
setMinCursorPosition();
}
}
private void disableBackspaceKey() {
isKeysDisabled = true;
txtArea.getInputMap().put(KeyStroke.getKeyStroke("BACK_SPACE"),
"none");
}
private void enableBackspaceKey() {
isKeysDisabled = false;
txtArea.getInputMap().put(KeyStroke.getKeyStroke("BACK_SPACE"),
BACK_SPACE_KEY_BINDING);
}
private void setMinCursorPosition() {
minCursorPosition = txtArea.getCaretPosition();
}
}
public void enableTerminal() {
txtArea.setEnabled(true);
}
public void disableTerminal() {
txtArea.setEnabled(false);
}
private void executeCommand(String command) {
processor.processCmd(command);
}
private String extractCommand() {
removeLastLineSeparator();
String newCommand = stripPreviousCommands();
return newCommand;
}
private void removeLastLineSeparator() {
String terminalText = txtArea.getText();
terminalText = terminalText.substring(0, terminalText.length()-1);
txtArea.setText(terminalText);
}
private String stripPreviousCommands() {
String terminalText = txtArea.getText();
int lastPromptIndex = terminalText.lastIndexOf('>') + 2;
if (lastPromptIndex < 0 || lastPromptIndex >= terminalText.length())
return "";
else
return terminalText.substring(lastPromptIndex);
}
public static Terminal getInstance() {
return TerminalHolder.INSTANCE;
}
private static final class TerminalHolder {
static final Terminal INSTANCE = new Terminal();
}
}
class CommandProcessor {
private CommandProcessor() {
}
public void processCmd(String command) {
// System.out.println("User command: " + command);
ProcessBuilder processBuilder = new ProcessBuilder();
// -- Linux --
if(command.equals("clear")) Terminal.txtArea.setText("");
if(System.getProperty("os.name").equals("Linux"))
processBuilder.command("bash", "-c", command);
else
processBuilder.command("cmd.exe", "/c", command);
// Run a shell script
//processBuilder.command("path/to/hello.sh");
// -- Windows --
//Run a command
// Run a bat file
//processBuilder.command("C:\\Users\\mkyong\\hello.bat");
try {
Process process = processBuilder.start();
StringBuilder output = new StringBuilder();
int exitVal = process.waitFor();
if (exitVal == 0) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
System.out.print("\n"+output);
} else {
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
System.out.print("\n"+output);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static CommandProcessor getInstance() {
return CommandProcessorHolder.INSTANCE;
}
private static final class CommandProcessorHolder {
static final CommandProcessor INSTANCE = new CommandProcessor();
}
}