Skip to content

Commit

Permalink
Added support for web console in local repl (fixes #110)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertlatacz committed Dec 5, 2016
1 parent 55abe44 commit 57a05f5
Show file tree
Hide file tree
Showing 167 changed files with 144 additions and 29 deletions.
12 changes: 6 additions & 6 deletions src/javarepl/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,17 @@ private static boolean handleTerminalCommand(EvaluationLog log) {
return false;
}



private static String welcomeMessage() {
return format("Welcome to JavaREPL version %s (%s, Java %s)",
applicationVersion(),
getProperty("java.vm.name"),
getProperty("java.version"));
}

private static String welcomeInstructions() {
return "Type expression to evaluate, \u001B[32m:help\u001B[0m for more options or press \u001B[32mtab\u001B[0m to auto-complete.";
}

private static JavaREPLClient clientFor(Option<String> hostname, Option<Integer> port) throws Exception {
console.printInfo(welcomeMessage());

Expand Down Expand Up @@ -146,6 +148,8 @@ private static JavaREPLClient startNewLocalInstance(String hostname, Integer por
ProcessBuilder builder = new ProcessBuilder("java", "-cp", System.getProperty("java.class.path"), Repl.class.getCanonicalName(), "--port=" + port);
builder.redirectErrorStream(true);

console.printInfo("Connected to local instance at http://" + hostname + ":" + port);

process = some(builder.start());
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
Expand All @@ -163,10 +167,6 @@ public void run() {
return replClient;
}

private static String welcomeInstructions() {
return "Type expression to evaluate, \u001B[32m:help\u001B[0m for more options or press \u001B[32mtab\u001B[0m to auto-complete.";
}

private static boolean waitUntilInstanceStarted(JavaREPLClient client) throws Exception {
for (int i = 0; i < 500; i++) {
Thread.sleep(10);
Expand Down
3 changes: 3 additions & 0 deletions src/javarepl/console/rest/RestConsoleApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.googlecode.utterlyidle.BasePath;
import com.googlecode.utterlyidle.RestApplication;
import javarepl.console.ui.ConsoleUiModule;

import static com.googlecode.utterlyidle.annotations.AnnotatedBindings.annotatedClass;
import static com.googlecode.utterlyidle.modules.Modules.applicationInstance;
Expand All @@ -14,6 +15,8 @@ public RestConsoleApplication(BasePath basePath, RestConsole console) {
applicationInstance(console),
applicationInstance(new RestConsoleExpressionReader()));

add(new ConsoleUiModule());
add(new RestConsoleModule());

}
}
30 changes: 30 additions & 0 deletions src/javarepl/console/rest/RestConsoleResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.googlecode.totallylazy.Option;
import com.googlecode.totallylazy.functions.Function1;
import com.googlecode.utterlyidle.MediaType;
import com.googlecode.utterlyidle.Response;
import com.googlecode.utterlyidle.annotations.*;
import javarepl.console.ConsoleLog;
import javarepl.console.ConsoleResult;
Expand All @@ -11,6 +12,9 @@
import java.util.Map;

import static com.googlecode.totallylazy.collections.PersistentMap.constructors.emptyMap;
import static com.googlecode.utterlyidle.Response.seeOther;
import static java.lang.String.format;
import static java.lang.System.getProperty;
import static javarepl.Utils.applicationVersion;
import static javarepl.completion.CompletionResult.methods.toJson;

Expand All @@ -23,6 +27,21 @@ public RestConsoleResource(RestConsole console, RestConsoleExpressionReader expr
this.expressionReader = expressionReader;
}

@GET
@Path("")
public Response main() {
return seeOther("/ui/console.html");
}

@POST
@Path("create")
@Produces(MediaType.APPLICATION_JSON)
public Map<String, Object> create() throws Exception {
return emptyMap(String.class, Object.class)
.insert("id", this.hashCode()+"")
.insert("welcomeMessage", welcomeMessage()+"\n"+welcomeInstructions());
}

@GET
@Path("version")
@Produces(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -91,4 +110,15 @@ private static Function1<ConsoleLog, Map<String, Object>> toCommandResultMap() {
.insert("type", consoleLog.type())
.insert("message", consoleLog.message());
}

private String welcomeMessage() {
return format("Welcome to JavaREPL version %s (%s, Java %s)",
applicationVersion(),
getProperty("java.vm.name"),
getProperty("java.version"));
}

private String welcomeInstructions() {
return "Type expression to evaluate, :help for more options or press tab to auto-complete.";
}
}
Binary file added src/javarepl/console/rest/favicon.ico
Binary file not shown.
15 changes: 15 additions & 0 deletions src/javarepl/console/ui/ConsoleUiModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package javarepl.console.ui;

import com.googlecode.utterlyidle.Resources;
import com.googlecode.utterlyidle.modules.ResourcesModule;

import static com.googlecode.totallylazy.io.URLs.packageUrl;
import static com.googlecode.utterlyidle.dsl.DslBindings.bindings;
import static com.googlecode.utterlyidle.dsl.StaticBindingBuilder.in;

public class ConsoleUiModule implements ResourcesModule {
@Override
public Resources addResources(Resources resources) throws Exception {
return resources.add(bindings(in(packageUrl(ConsoleUiModule.class)).path("/ui")));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
define( [
"../core",
"../var/document",
"../core/init",
"./init",
"../deferred"
], function( jQuery, document ) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define( [
"../var/pnum"
"./pnum"
], function( pnum ) {

return new RegExp( "^(?:([+-])=|)(" + pnum + ")([a-z%]*)$", "i" );
Expand Down
65 changes: 65 additions & 0 deletions src/javarepl/console/ui/console.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Java REPL</title>
<meta name="Content-Type" content="text/html; charset=UTF-8">

<script src="/ui/bower_components/underscore/underscore.js" type="text/javascript"></script>
<script src="/ui/bower_components/jquery/dist/jquery.js" type="text/javascript"></script>
<script src="/ui/bower_components/jquery.terminal/js/jquery.terminal.min.js" type="text/javascript"></script>
<script src="/ui/bower_components/jquery.terminal/js/jquery.mousewheel-min.js" type="text/javascript"></script>

<link href="/ui/bower_components/jquery.terminal/css/jquery.terminal.min.css" rel="stylesheet" type="text/css">
<link href="/ui/bower_components/github-fork-ribbon-css/gh-fork-ribbon.css" rel="stylesheet" type="text/css">

<!--[if lt IE 9]>
<link href="/ui/bower_components/github-fork-ribbon-css/gh-fork-ribbon.ie.css" rel="stylesheet" type="text/css">
<![endif]-->


<link href="term.css" rel="stylesheet" type="text/css">

<script type="text/javascript">
(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');

ga('create', 'UA-40421018-1', 'javarepl.com');
ga('send', 'pageview');
</script>


<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-40421018-1']);
_gaq.push(['_trackPageview']);

(function () {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();

</script>
</head>


<body>


<script type="text/javascript" src="/ui/term.js"></script>

</body>
</html>
Binary file added src/javarepl/console/ui/favicon.ico
Binary file not shown.
File renamed without changes
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ body {
height: 72px;
margin: 20px 10px 20px;
line-height: 72px;
background: url(javarepl-icon.png) no-repeat;
background: url(/ui/javarepl-icon.png) no-repeat;
padding: 0;
padding-left: 75px;

Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions src/javarepl/web/WebConsoleApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.googlecode.utterlyidle.BasePath;
import com.googlecode.utterlyidle.RestApplication;
import javarepl.console.ui.ConsoleUiModule;

import static com.googlecode.utterlyidle.annotations.AnnotatedBindings.annotatedClass;
import static com.googlecode.utterlyidle.modules.Modules.applicationInstance;
Expand All @@ -14,6 +15,7 @@ public WebConsoleApplication(BasePath basePath) {
applicationInstance(new WebConsole())
);

add(new ConsoleUiModule());
add(new WebConsoleModule());
}
}
22 changes: 11 additions & 11 deletions src/javarepl/web/embed.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
<title>Java REPL</title>
<meta name="Content-Type" content="text/html; charset=UTF-8">

<script src="/bower_components/underscore/underscore.js" type="text/javascript"></script>
<script src="/bower_components/jquery/dist/jquery.js" type="text/javascript"></script>
<script src="/bower_components/jquery.terminal/js/jquery.terminal.min.js" type="text/javascript"></script>
<script src="/bower_components/jquery.terminal/js/jquery.mousewheel-min.js" type="text/javascript"></script>
<script src="/ui/bower_components/underscore/underscore.js" type="text/javascript"></script>
<script src="/ui/bower_components/jquery/dist/jquery.js" type="text/javascript"></script>
<script src="/ui/bower_components/jquery.terminal/js/jquery.terminal.min.js" type="text/javascript"></script>
<script src="/ui/bower_components/jquery.terminal/js/jquery.mousewheel-min.js" type="text/javascript"></script>

<link href="/bower_components/jquery.terminal/css/jquery.terminal.min.css" rel="stylesheet" type="text/css">
<link href="/bower_components/github-fork-ribbon-css/gh-fork-ribbon.css" rel="stylesheet" type="text/css">
<link href="/ui/bower_components/jquery.terminal/css/jquery.terminal.min.css" rel="stylesheet" type="text/css">
<link href="/ui/bower_components/github-fork-ribbon-css/gh-fork-ribbon.css" rel="stylesheet" type="text/css">

<!--[if lt IE 9]>
<link href="/bower_components/github-fork-ribbon-css/gh-fork-ribbon.ie.css" rel="stylesheet" type="text/css">
<link href="/ui/bower_components/github-fork-ribbon-css/gh-fork-ribbon.ie.css" rel="stylesheet" type="text/css">
<![endif]-->


Expand All @@ -24,10 +24,10 @@
(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)
Expand Down Expand Up @@ -59,7 +59,7 @@
<body>


<script type="text/javascript" src="term.js"></script>
<script type="text/javascript" src="/ui/term.js"></script>

</body>
</html>
18 changes: 9 additions & 9 deletions src/javarepl/web/term.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
<title>Java REPL</title>
<meta name="Content-Type" content="text/html; charset=UTF-8">

<script src="/bower_components/underscore/underscore.js" type="text/javascript"></script>
<script src="/bower_components/jquery/dist/jquery.js" type="text/javascript"></script>
<script src="/bower_components/jquery.terminal/js/jquery.terminal.min.js" type="text/javascript"></script>
<script src="/bower_components/jquery.terminal/js/jquery.mousewheel-min.js" type="text/javascript"></script>
<script src="/ui/bower_components/underscore/underscore.js" type="text/javascript"></script>
<script src="/ui/bower_components/jquery/dist/jquery.js" type="text/javascript"></script>
<script src="/ui/bower_components/jquery.terminal/js/jquery.terminal.min.js" type="text/javascript"></script>
<script src="/ui/bower_components/jquery.terminal/js/jquery.mousewheel-min.js" type="text/javascript"></script>

<link href="/bower_components/jquery.terminal/css/jquery.terminal.min.css" rel="stylesheet" type="text/css">
<link href="/bower_components/github-fork-ribbon-css/gh-fork-ribbon.css" rel="stylesheet" type="text/css">
<link href="/ui/bower_components/jquery.terminal/css/jquery.terminal.min.css" rel="stylesheet" type="text/css">
<link href="/ui/bower_components/github-fork-ribbon-css/gh-fork-ribbon.css" rel="stylesheet" type="text/css">

<!--[if lt IE 9]>
<link href="/bower_components/github-fork-ribbon-css/gh-fork-ribbon.ie.css" rel="stylesheet" type="text/css">
<link href="/ui/bower_components/github-fork-ribbon-css/gh-fork-ribbon.ie.css" rel="stylesheet" type="text/css">
<![endif]-->


<link href="term.css" rel="stylesheet" type="text/css">
<link href="/ui/term.css" rel="stylesheet" type="text/css">

<script type="text/javascript">
(function (i, s, o, g, r, a, m) {
Expand Down Expand Up @@ -77,7 +77,7 @@ <h1><span class="logo-javarepl">Java REPL</span></h1>
</div>
</header>

<script type="text/javascript" src="term.js"></script>
<script type="text/javascript" src="/ui/term.js"></script>

<footer class="terminal-footer">
<p class="bottom">
Expand Down

0 comments on commit 57a05f5

Please sign in to comment.