Skip to content

Commit

Permalink
refactoring REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
juerg committed Jan 15, 2024
1 parent 38921ae commit 0d28711
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 34 deletions.
39 changes: 39 additions & 0 deletions src/main/java/com/github/jlangch/venice/IRepl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* __ __ _
* \ \ / /__ _ __ (_) ___ ___
* \ \/ / _ \ '_ \| |/ __/ _ \
* \ / __/ | | | | (_| __/
* \/ \___|_| |_|_|\___\___|
*
*
* Copyright 2017-2024 Venice
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.jlangch.venice;

import java.util.function.Consumer;


public interface IRepl {

void setHandler(final Consumer<String> handler);

void setPrompt(final String prompt);

void setPrompt(final String prompt, final String secondaryPrompt);

int getTerminalWidth();

int getTerminalHeight();

}
48 changes: 38 additions & 10 deletions src/main/java/com/github/jlangch/venice/impl/repl/CustomREPL.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
import org.jline.utils.OSUtils;

import com.github.jlangch.venice.EofException;
import com.github.jlangch.venice.IRepl;
import com.github.jlangch.venice.Venice;
import com.github.jlangch.venice.VncException;
import com.github.jlangch.venice.impl.IVeniceInterpreter;
import com.github.jlangch.venice.impl.RunMode;
import com.github.jlangch.venice.impl.VeniceInterpreter;
Expand All @@ -60,7 +62,8 @@
import com.github.jlangch.venice.javainterop.IInterceptor;
import com.github.jlangch.venice.javainterop.ILoadPaths;

public class CustomREPL {

public class CustomREPL implements IRepl {

public CustomREPL(
final IInterceptor interceptor,
Expand All @@ -73,6 +76,11 @@ public CustomREPL(
public void run(final String[] args) {
ThreadContext.setInterceptor(interceptor);

if (terminal != null) {
throw new VncException("The REPL is already running!");
}


final CommandLineArgs cli = new CommandLineArgs(args);
final ILoadPaths loadpaths = interceptor.getLoadPaths();

Expand Down Expand Up @@ -125,20 +133,33 @@ else if (!setupMode) {
}
}

@Override
public void setHandler(final Consumer<String> handler) {
this.cmdHandler = handler;
}

@Override
public void setPrompt(final String prompt) {
this.prompt = prompt;
this.secondaryPrompt = "";
}

@Override
public void setPrompt(final String prompt, final String secondaryPrompt) {
this.prompt = prompt;
this.secondaryPrompt = secondaryPrompt;
}

@Override
public int getTerminalWidth() {
return terminal.getWidth();
}

@Override
public int getTerminalHeight() {
return terminal.getHeight();
}

private void repl(final CommandLineArgs cli) throws Exception {
setPrompt(config.getPrompt(), ansiTerminal ? config.getSecondaryPrompt() : "");

Expand All @@ -151,15 +172,17 @@ private void repl(final CommandLineArgs cli) throws Exception {
.dumb(!ansiTerminal)
.jna(false);

final Terminal terminal = OSUtils.IS_WINDOWS
? builder
.jansi(ansiTerminal)
.build()
: builder
.encoding("UTF-8")
.build();
terminal = OSUtils.IS_WINDOWS
? builder
.jansi(ansiTerminal)
.build()
: builder
.encoding("UTF-8")
.build();

terminal.handle(Signal.INT, signal -> mainThread.interrupt());


final TerminalPrinter printer = new TerminalPrinter(
config,
terminal,
Expand Down Expand Up @@ -232,7 +255,8 @@ private Env loadEnv(
final PrintStream err,
final BufferedReader in
) {
return venice.createEnv(macroexpand, ansiTerminal, RunMode.REPL)
final Env env =
venice.createEnv(macroexpand, ansiTerminal, RunMode.REPL)
.setGlobal(new Var(
new VncSymbol("*ARGV*"),
cli.argsAsList(),
Expand All @@ -251,6 +275,9 @@ private Env loadEnv(
.setStdoutPrintStream(out)
.setStderrPrintStream(err)
.setStdinReader(in);

return ReplFunctions.register(
env, terminal, config, macroexpand, ReplDirs.create());
}

private PrintStream createPrintStream(final String context, final Terminal terminal) {
Expand Down Expand Up @@ -377,12 +404,13 @@ else if (cli.switchPresent("-setup")) {
}



private static final String DEFAULT_PROMPT_PRIMARY = "venice> ";
private static final String DEFAULT_PROMPT_SECONDARY = " | ";

private final File app;

private Terminal terminal;

private String prompt = DEFAULT_PROMPT_PRIMARY;
private String secondaryPrompt = DEFAULT_PROMPT_SECONDARY;
private Consumer<String> cmdHandler;
Expand Down
42 changes: 40 additions & 2 deletions src/main/java/com/github/jlangch/venice/impl/repl/REPL.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -55,6 +56,7 @@
import org.jline.utils.OSUtils;

import com.github.jlangch.venice.ContinueException;
import com.github.jlangch.venice.IRepl;
import com.github.jlangch.venice.ParseError;
import com.github.jlangch.venice.SourceCodeRenderer;
import com.github.jlangch.venice.SymbolNotFoundException;
Expand Down Expand Up @@ -96,7 +98,7 @@
import com.github.jlangch.venice.javainterop.SandboxRules;


public class REPL {
public class REPL implements IRepl {

public REPL(final IInterceptor interceptor) {
this.interceptor = interceptor;
Expand All @@ -105,6 +107,10 @@ public REPL(final IInterceptor interceptor) {
public void run(final String[] args) {
ThreadContext.setInterceptor(interceptor);

if (terminal != null) {
throw new VncException("The REPL is already running!");
}

final CommandLineArgs cli = new CommandLineArgs(args);
final ILoadPaths loadpaths = interceptor.getLoadPaths();

Expand Down Expand Up @@ -179,6 +185,32 @@ else if (!setupMode) {
}
}

@Override
public void setHandler(final Consumer<String> handler) {
// not supported
}

@Override
public void setPrompt(final String prompt) {
// not supported
}

@Override
public void setPrompt(final String prompt, final String secondaryPrompt) {
// not supported
}

@Override
public int getTerminalWidth() {
return terminal.getWidth();
}

@Override
public int getTerminalHeight() {
return terminal.getHeight();
}


private void repl(
final CommandLineArgs cli,
final boolean macroexpand
Expand Down Expand Up @@ -211,7 +243,7 @@ else if (isRunningOnLinuxGitPod()) {
builder.encoding("UTF-8");
}

final Terminal terminal = builder.build();
terminal = builder.build();

terminal.handle(Signal.INT, signal -> mainThread.interrupt());

Expand Down Expand Up @@ -1184,6 +1216,11 @@ private Env loadEnv(
cli.argsAsList(),
false,
Var.Scope.Global))
.setGlobal(new Var(
new VncSymbol("*REPL*"),
new VncJavaObject(this),
false,
Var.Scope.Global))
.setGlobal(new Var(
new VncSymbol("*repl-color-theme*"),
new VncKeyword(config.getColorMode().name().toLowerCase()),
Expand Down Expand Up @@ -1456,6 +1493,7 @@ public static enum SetupMode { Minimal, Extended };

private final static String HISTORY_FILE = ".repl.history";

private Terminal terminal;

private ReplConfig config;
private IInterceptor interceptor;
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/com/github/jlangch/venice/gradle.venice
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@

(defn gradle-script-path [gradle-home]
(case (os-type)
:mac-osx (str gradle- "/bin/gradle")
:linux (str gradle- "/bin/gradle")
:windows (str gradle- "/bin/gradle.bat")))
:mac-osx (str gradle-home "/bin/gradle")
:linux (str gradle-home "/bin/gradle")
:windows (str gradle-home "/bin/gradle.bat")))


(defn exec []
Expand Down
23 changes: 4 additions & 19 deletions src/main/resources/com/github/jlangch/venice/tput.venice
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@

cols []

(if (or (os-type? :mac-osx) (os-type? :linux))
(long (str/trim (:out (core/sh "tput" "cols" :throw-ex true))))
(long (windows-console-mode-attribute "Columns"))))
(shell/require-mac-or-linux)
(long (str/trim (:out (core/sh "tput" "cols" :throw-ex true)))))


(defn
Expand All @@ -81,9 +80,8 @@

lines []

(if (or (os-type? :mac-osx) (os-type? :linux))
(long (str/trim (:out (core/sh "tput" "lines" :throw-ex true)))))
(long (windows-console-mode-attribute "Lines")))
(shell/require-mac-or-linux)
(long (str/trim (:out (core/sh "tput" "lines" :throw-ex true)))))


(defn
Expand All @@ -97,19 +95,6 @@
(long (str/trim (:out (core/sh "tput" "colors" :throw-ex true)))))


(defn- windows-console-mode-attribute [attr-name]
(shell/require-windows)
(->> (core/sh "cmd" "/C" "mode con" :throw-ex true)
(:out)
(str/split-lines)
(filter #(match? % " +~{attr-name}: +[0-9]+"))
(map str/trim)
(first)
(str/split " +")
(second)))



;; -----------------------------------------------------------------------------
;; Controlling The Cursor
;; -----------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/com/github/jlangch/venice/venice.bat
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ cd %VENICE_CONSOLE_HOME%
-XX:-OmitStackTraceInFastThrow ^
-cp "libs;libs/*" ^
com.github.jlangch.venice.Launcher ^
-Dvenice.repl.home=${VENICE_CONSOLE_HOME} ^
-colors-darkmode ^
-macroexpand ^
-app-repl venice.venice
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/com/github/jlangch/venice/venice.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# scripts #
# +--libs #
# | +-- venice-x.y.z.jar #
# | +-- jansi-2.4.1.jar #
# +--venice.sh #
# +--venice.venice #
###############################################################################
Expand Down Expand Up @@ -39,6 +40,7 @@ ${JAVA_11_HOME}/bin/java \
-cp "libs/*" com.github.jlangch.venice.Launcher \
-Xmx2G \
-XX:-OmitStackTraceInFastThrow \
-Dvenice.repl.home=${VENICE_REPL_HOME} \
-colors \
-macroexpand \
-app-repl venice.venice

0 comments on commit 0d28711

Please sign in to comment.