1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.scala_tools.maven;
17
18 import java.io.File;
19 import java.io.InputStream;
20 import java.io.PrintWriter;
21
22 import jline.ClassNameCompletor;
23 import jline.ConsoleReader;
24 import jline.FileNameCompletor;
25 import jline.SimpleCompletor;
26
27 import org.codehaus.plexus.util.IOUtil;
28
29
30
31
32
33
34
35 public class ConsolePiper extends Thread {
36 private ConsoleReader console_;
37 private PrintWriter processInput_;
38 private InputStream processOutput_;
39
40 public ConsolePiper(Process p) throws Exception {
41 console_ = new ConsoleReader();
42 File histoFile = new File(System.getProperty("user.home"), ".m2/scala-console.histo");
43 histoFile.getParentFile().mkdirs();
44 console_.getHistory().setHistoryFile(histoFile);
45 console_.addCompletor(new FileNameCompletor());
46 console_.addCompletor(new ClassNameCompletor());
47 console_.addCompletor(new SimpleCompletor(new String [] {
48 "abstract", "case", "catch", "class", "def",
49 "do", "else", "extends", "false", "final",
50 "finally", "for", "if", "implicit", "import", "lazy",
51 "match", "new", "null", "object", "override",
52 "package", "private", "protected", "requires", "return",
53 "sealed", "super", "this", "throw", "trait",
54 "try", "true", "type", "val", "var",
55 "while", "with", "yield"
56 }
57 )
58 );
59 processInput_ = new PrintWriter(p.getOutputStream());
60
61
62
63
64 }
65
66 @Override
67 public void run() {
68 try {
69 while (true) {
70
71
72
73
74
75
76
77
78
79
80
81
82
83 processInput_.println(console_.readLine());
84 processInput_.flush();
85 yield();
86 sleep(500l);
87 }
88 } catch (InterruptedException exc) {
89 System.err.print("stop by interrupt");
90 return;
91 } catch (Exception exc) {
92 System.err.print("!!!! exc !!!");
93 exc.printStackTrace();
94 throw new RuntimeException("wrap: " + exc.getMessage(), exc);
95 } finally {
96 IOUtil.close(processInput_);
97 IOUtil.close(processOutput_);
98 }
99 }
100
101 }