blob: a74ff8ec0b1ed01e92f585647917e335c6cff9c5 [file] [log] [blame]
Richard S. Hallfbd735b2009-06-11 16:07:20 +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 */
19
20package aQute.shell.console;
21
22import java.io.*;
23import java.lang.reflect.*;
24import java.util.*;
25
26import org.osgi.service.command.*;
27
28public class Console implements Runnable {
29 StringBuilder sb;
30 CommandSession session;
31 List<CharSequence> history = new ArrayList<CharSequence>();
32 int current = 0;
33 boolean quit;
34
35 public void setSession(CommandSession session) {
36 this.session= session;
37 }
38
39 public void run() {
40 try {
41 while (!quit) {
42 try {
43 CharSequence line = getLine(session.getKeybord());
44 if (line != null) {
45 history.add(line);
46 if (history.size() > 40)
47 history.remove(0);
48 Object result = session.execute(line);
49 if (result != null)
50 session.getConsole().println(
51 session.format(result, Converter.INSPECT));
52 } else
53 quit = true;
54
55 } catch (InvocationTargetException ite) {
56 session.getConsole().println(
57 "E: " + ite.getTargetException());
58 session.put("exception", ite.getTargetException());
59 } catch (Throwable e) {
60 if (!quit) {
61 session.getConsole().println("E: " + e.getMessage());
62 session.put("exception", e);
63 }
64 }
65 }
66 } catch (Exception e) {
67 if (!quit)
68 e.printStackTrace();
69 }
70 }
71
72 CharSequence getLine(InputStream in) throws IOException {
73 sb = new StringBuilder();
74 session.getConsole().print("$ ");
75 int outer = 0;
76 while (!quit) {
77 session.getConsole().flush();
78 int c = in.read();
79 if (c < 0)
80 quit = true;
81 else {
82 switch (c) {
83 case '\r':
84 break;
85 case '\n':
86 if (outer == 0 && sb.length() > 0) {
87 return sb;
88 } else {
89 session.getConsole().print("$ ");
90 }
91 break;
92
93 case '\u001b':
94 c = in.read();
95 if (c == '[') {
96 c = in.read();
97 session.getConsole().print("\b\b\b");
98 switch (c) {
99 case 'A':
100 history(current - 1);
101 break;
102 case 'B':
103 history(current + 1);
104 break;
105 case 'C': // right(); break;
106 case 'D': // left(); break;
107 }
108 }
109 break;
110
111 case '\b':
112 if (sb.length() > 0) {
113 session.getConsole().print("\b \b");
114 sb.deleteCharAt(sb.length() - 1);
115 }
116 break;
117
118 default:
119 sb.append((char) c);
120 break;
121 }
122 }
123 }
124 return null;
125 }
126
127 void history(int n) {
128 if (n < 0 || n > history.size())
129 return;
130 current = n;
131 for (int i = 0; i < sb.length(); i++)
132 session.getConsole().print("\b \b");
133
134 sb = new StringBuilder(history.get(current));
135 session.getConsole().print(sb);
136 }
137
138 public void close() {
139 quit = true;
140 }
141
142 public void open() {}
143}