1
1
package javarepl ;
2
2
3
- import com .googlecode .totallylazy .Exceptions ;
3
+ import com .googlecode .totallylazy .Lists ;
4
4
import com .googlecode .totallylazy .Option ;
5
5
import com .googlecode .totallylazy .Sequence ;
6
6
import com .googlecode .totallylazy .Strings ;
7
7
import com .googlecode .totallylazy .functions .Function1 ;
8
+ import javarepl .client .EvaluationLog ;
8
9
import javarepl .client .EvaluationResult ;
9
10
import javarepl .client .JavaREPLClient ;
10
11
import javarepl .completion .CompletionCandidate ;
11
12
import javarepl .completion .CompletionResult ;
12
- import jline .Terminal ;
13
- import jline .TerminalFactory ;
14
13
import jline .console .ConsoleReader ;
15
14
import jline .console .CursorBuffer ;
16
15
import jline .console .completer .CandidateListCompletionHandler ;
24
23
import java .nio .file .Files ;
25
24
import java .util .*;
26
25
27
- import static com .googlecode .totallylazy .Exceptions .captureException ;
28
26
import static com .googlecode .totallylazy .Files .fileOption ;
27
+ import static com .googlecode .totallylazy .Lists .list ;
29
28
import static com .googlecode .totallylazy .Option .none ;
30
29
import static com .googlecode .totallylazy .Option .some ;
31
30
import static com .googlecode .totallylazy .Sequences .empty ;
38
37
import static com .googlecode .totallylazy .predicates .Not .not ;
39
38
import static java .lang .String .format ;
40
39
import static java .lang .System .getProperty ;
41
- import static java .util .Arrays . asList ;
40
+ import static java .util .Collections . singletonList ;
42
41
import static javarepl .Utils .applicationVersion ;
43
42
import static javarepl .Utils .randomServerPort ;
43
+ import static javarepl .client .EvaluationLog .Type .CONTROL ;
44
44
import static javarepl .completion .CompletionResult .methods .fromJson ;
45
45
import static javarepl .completion .CompletionResult .methods .toJson ;
46
+ import static javarepl .console .commands .ClearScreen .CLEAR_SCREEN_CMD ;
46
47
import static javax .tools .ToolProvider .getSystemJavaCompiler ;
47
48
48
49
public class Main {
@@ -53,9 +54,10 @@ public class Main {
53
54
public static void main (String ... args ) throws Exception {
54
55
console = new ResultPrinter (printColors (args ));
55
56
56
- Sequence < String > initialExpressions = initialExpressionsFromFile (). join ( initialExpressionsFromArgs ( args ) );
57
+ ConsoleReader consoleReader = new ConsoleReader ( System . in , AnsiConsole . out );
57
58
JavaREPLClient client = clientFor (hostname (args ), port (args ));
58
- ExpressionReader expressionReader = expressionReaderFor (client , initialExpressions );
59
+ Sequence <String > initialExpressions = initialExpressionsFromFile ().join (initialExpressionsFromArgs (args ));
60
+ ExpressionReader expressionReader = expressionReaderFor (consoleReader , client , initialExpressions );
59
61
60
62
Option <String > expression = none ();
61
63
Option <EvaluationResult > result = none ();
@@ -64,17 +66,48 @@ public static void main(String... args) throws Exception {
64
66
65
67
if (!expression .isEmpty ()) {
66
68
result = client .execute (expression .get ());
67
- if (!result .isEmpty ())
68
- console .printEvaluationResult (result .get ());
69
+ if (!result .isEmpty ()) {
70
+ for (EvaluationLog log : result .get ().logs ()) {
71
+ if (!handleTerminalCommand (log )) {
72
+ handleTerminalMessage (log );
73
+ }
74
+ }
75
+ }
69
76
}
70
77
}
71
78
}
72
79
73
- private static JavaREPLClient clientFor (Option <String > hostname , Option <Integer > port ) throws Exception {
74
- console .printInfo (format ("Welcome to JavaREPL version %s (%s, Java %s)" ,
80
+ private static void handleTerminalMessage (EvaluationLog log ) {
81
+ console .printEvaluationLog (log );
82
+ }
83
+
84
+ private static boolean handleTerminalCommand (EvaluationLog log ) {
85
+ if (log .type ().equals (CONTROL )){
86
+ switch (log .message ()) {
87
+ case CLEAR_SCREEN_CMD : {
88
+ console .printInfo ("\033 c" );
89
+ console .printInfo (welcomeMessage ());
90
+ console .printInfo (welcomeInstructions ());
91
+ } break ;
92
+
93
+ default : return false ;
94
+ }
95
+ return true ;
96
+ }
97
+ return false ;
98
+ }
99
+
100
+
101
+
102
+ private static String welcomeMessage () {
103
+ return format ("Welcome to JavaREPL version %s (%s, Java %s)" ,
75
104
applicationVersion (),
76
105
getProperty ("java.vm.name" ),
77
- getProperty ("java.version" )));
106
+ getProperty ("java.version" ));
107
+ }
108
+
109
+ private static JavaREPLClient clientFor (Option <String > hostname , Option <Integer > port ) throws Exception {
110
+ console .printInfo (welcomeMessage ());
78
111
79
112
if (hostname .isEmpty () && port .isEmpty ()) {
80
113
return startNewLocalInstance ("localhost" , randomServerPort ());
@@ -108,7 +141,7 @@ private static JavaREPLClient startNewLocalInstance(String hostname, Integer por
108
141
System .exit (0 );
109
142
}
110
143
111
- console .printInfo ("Type expression to evaluate, \u001B [32m:help \u001B [0m for more options or press \u001B [32mtab \u001B [0m to auto-complete." );
144
+ console .printInfo (welcomeInstructions () );
112
145
113
146
ProcessBuilder builder = new ProcessBuilder ("java" , "-cp" , System .getProperty ("java.class.path" ), Repl .class .getCanonicalName (), "--port=" + port );
114
147
builder .redirectErrorStream (true );
@@ -130,6 +163,10 @@ public void run() {
130
163
return replClient ;
131
164
}
132
165
166
+ private static String welcomeInstructions () {
167
+ return "Type expression to evaluate, \u001B [32m:help\u001B [0m for more options or press \u001B [32mtab\u001B [0m to auto-complete." ;
168
+ }
169
+
133
170
private static boolean waitUntilInstanceStarted (JavaREPLClient client ) throws Exception {
134
171
for (int i = 0 ; i < 500 ; i ++) {
135
172
Thread .sleep (10 );
@@ -169,15 +206,13 @@ private static Boolean printColors(String[] args) {
169
206
return !sequence (args ).contains ("--noColors" );
170
207
}
171
208
172
- private static ExpressionReader expressionReaderFor (final JavaREPLClient client , Sequence <String > initialExpressions ) throws IOException {
209
+ private static ExpressionReader expressionReaderFor (ConsoleReader consoleReader , final JavaREPLClient client , Sequence <String > initialExpressions ) throws IOException {
173
210
return new ExpressionReader (new Function1 <Sequence <String >, String >() {
174
211
private static final char CTRL_C = (char ) 3 ;
175
212
private static final char CTRL_D = (char ) 4 ;
176
- private final ConsoleReader consoleReader ;
177
213
private Sequence <String > expressions = initialExpressions ;
178
214
179
215
{
180
- consoleReader = new ConsoleReader (System .in , AnsiConsole .out );
181
216
consoleReader .setCompletionHandler (new JlineCompletionHandler ());
182
217
consoleReader .setHistoryEnabled (true );
183
218
consoleReader .setExpandEvents (false );
@@ -213,7 +248,7 @@ private jline.console.completer.Completer clientCompleter() {
213
248
return (expression , cursor , candidates ) -> {
214
249
try {
215
250
CompletionResult result = client .completions (expression );
216
- candidates .addAll (asList (toJson (result )));
251
+ candidates .addAll (singletonList (toJson (result )));
217
252
return result .candidates ().isEmpty () ? -1 : result .position ();
218
253
} catch (Exception e ) {
219
254
return -1 ;
0 commit comments