]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.platform.ui/src/org/simantics/platform/ui/SimanticsConsole.java
c7aa2e1856e4af45785e1250abe99404eb318b26
[simantics/platform.git] / bundles / org.simantics.platform.ui / src / org / simantics / platform / ui / SimanticsConsole.java
1 /*******************************************************************************
2  * Copyright (c) 2019 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.platform.ui;
13
14 import java.io.IOException;
15 import java.util.concurrent.ExecutorService;
16 import java.util.concurrent.Executors;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.graphics.Color;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.ui.console.ConsolePlugin;
24 import org.eclipse.ui.console.IConsole;
25 import org.eclipse.ui.console.MessageConsole;
26 import org.eclipse.ui.console.MessageConsoleStream;
27
28 public class SimanticsConsole extends MessageConsole {
29
30     // We don't want to constantly lose data, but this is still only half MB
31     private static int HIGH_WATERMARK = 2<<18;
32     // 75%
33     private static int LOW_WATERMARK = 3 * (HIGH_WATERMARK>>2);
34
35     private ExecutorService executor = Executors.newSingleThreadExecutor();
36
37     private Color black;
38     private Color red;
39     private Color green;
40     private Color yellow;
41     private Color blue;
42     private Color magenta;
43     private Color cyan;
44     private Color white;
45
46     private MessageConsoleStream stream;
47
48     public SimanticsConsole() {
49         super("Simantics Console", "Simantics Console", null, true);
50     }
51
52     @Override
53     protected void init() {
54         Display d = Display.getDefault();
55
56         black = tuneColor(d, SWT.COLOR_BLACK);
57         red = tuneColor(d, SWT.COLOR_RED);
58         green = tuneColor(d, SWT.COLOR_GREEN);
59         yellow = tuneColor(d, SWT.COLOR_YELLOW);
60         blue = tuneColor(d, SWT.COLOR_BLUE);
61         magenta = tuneColor(d, SWT.COLOR_MAGENTA);
62         cyan = tuneColor(d, SWT.COLOR_CYAN);
63         white = tuneColor(d, SWT.COLOR_WHITE);
64
65         setBackground(black);
66         stream = newMessageStream();
67         stream.setFontStyle(SWT.NORMAL);
68         stream.setColor(white);
69
70         setWaterMarks(LOW_WATERMARK, HIGH_WATERMARK);
71
72     }
73
74     @Override
75     protected void dispose() {
76         super.dispose();
77         executor.shutdown();
78         try {
79             stream.close();
80         } catch (IOException e) {
81             // nothing to do
82         }
83
84         black.dispose();
85         red.dispose();
86         green.dispose();
87         green.dispose();
88         yellow.dispose();
89         blue.dispose();
90         magenta.dispose();
91         cyan.dispose();
92         white.dispose();
93     }
94
95     private Color tuneColor(Display d, int color) {
96         Color c = d.getSystemColor(color);
97         return new Color(d, (int)(c.getRed() * 0.75), (int)(c.getGreen() * 0.75), (int)(c.getBlue() * 0.75));
98     }
99
100     public static SimanticsConsole show() {
101
102         if(!Thread.currentThread().equals(Display.getDefault().getThread())) {
103             Display.getDefault().asyncExec(() -> show());
104             return null;
105         }
106
107         for (IConsole c : ConsolePlugin.getDefault().getConsoleManager().getConsoles()) {
108             if (c instanceof SimanticsConsole) {
109                 SimanticsConsole sc = (SimanticsConsole) c;
110                 sc.activate();
111                 return sc;
112             }
113         }
114
115         SimanticsConsole sc = new SimanticsConsole();
116         ConsolePlugin.getDefault().getConsoleManager().addConsoles(new SimanticsConsole[] {sc});
117         return sc;
118
119     }
120
121     public static SimanticsConsole findConsole() {
122
123         for (IConsole c : ConsolePlugin.getDefault().getConsoleManager().getConsoles()) {
124             if (c instanceof SimanticsConsole) {
125                 return (SimanticsConsole) c;
126             }
127         }
128
129         SimanticsConsole sc = new SimanticsConsole();
130         ConsolePlugin.getDefault().getConsoleManager().addConsoles(new SimanticsConsole[] {sc});
131         return sc;
132
133     }
134
135     public void write(String data) {
136         executor.submit(() -> {
137             Pattern p = Pattern.compile("(\\x1b\\[(?<cmd>.*?(m)))");
138             Matcher m = p.matcher(data);
139             int pos = 0;
140             while (m.find()) {
141                 if (m.start() > pos) {
142                     stream.print(data.substring(pos, m.start()));
143                 }
144                 pos = m.end();
145
146                 String cmd = m.group("cmd");
147                 if (cmd.endsWith("m")) {
148                     // Stream style can be set only in UI thread
149                     Display.getDefault().syncExec(() -> {
150                         int fontStyle = stream.getFontStyle();
151                         Color color = stream.getColor();
152                         try {
153                             stream.close();
154                         } catch (IOException e) {
155                             // nothing to do
156                         }
157                         stream = newMessageStream();
158
159                         String attributes[] = cmd.substring(0, cmd.length() - 1).split(";");
160                         for (String attribute : attributes) {
161                             switch (Integer.parseInt(attribute)) {
162                                 case 0: fontStyle = SWT.NORMAL; break;
163                                 case 1: fontStyle = SWT.BOLD; break;
164                                 case 4: break; // underline
165                                 case 5: break; // blink
166                                 case 7: break; // reverse video
167                                 case 8: break; // nondisplayed
168                                 case 30: color = black; break;
169                                 case 31: color = red; break;
170                                 case 32: color = green; break;
171                                 case 33: color = yellow; break;
172                                 case 34: color = blue; break;
173                                 case 35: color = magenta; break;
174                                 case 36: color = cyan; break;
175                                 case 37: color = white; break;
176                                 // background colors not supported
177                                 default:break;
178                             }
179                         }
180                         stream.setColor(color);
181                         stream.setFontStyle(fontStyle);
182                     });
183                 }
184             }
185             if (pos < data.length()) {
186                 stream.print(data.substring(pos));
187             }
188             stream.println();
189         });
190     }
191 }