Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for non-process-based terminals #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Rahman Usta
:lang: en
:toc: left
:numbered:
:terminalfx-version: 1.0.9
:terminalfx-version: 1.1.0

== JavaFX Terminal Emulator

Expand All @@ -21,14 +21,15 @@ We use https://github.com/traff/pty4j[Pty4J] to get VT codes from running proces
* CygWin Support
* Configurable (Color, Size, Font)
* Multi Tab Support
* Support for non-process based terminals

== Usage

.Default Config
[source,java]
----
TerminalBuilder terminalBuilder = new TerminalBuilder();
TerminalTab terminal = terminalBuilder.newTerminal();
TerminalTab<ExternalProcessPty> terminal = terminalBuilder.newTerminal();

TabPane tabPane = new TabPane();
tabPane.getTabs().add(terminal);
Expand All @@ -45,14 +46,25 @@ darkConfig.setForegroundColor(Color.rgb(240, 240, 240));
darkConfig.setCursorColor(Color.rgb(255, 0, 0, 0.5));

TerminalBuilder terminalBuilder = new TerminalBuilder(darkConfig);
TerminalTab terminal = terminalBuilder.newTerminal();
TerminalTab<ExternalProcessPty> terminal = terminalBuilder.newTerminal();

TabPane tabPane = new TabPane();
tabPane.getTabs().add(terminal);
----

image::images\dark.png[]

.Using a non-Process Terminal
[source,java]
----
FileCapturePty filecap = new FileCapturePty(new File("log.in"), new File("README.adoc"), new File("LICENSE"));
GenericTerminal<FileCapturePty> view = new GenericTerminal<FileCapturePty>(darkConfig, filecap);
TerminalTab<FileCapturePty> terminal = new TerminalTab<>(view, new DefaultTabNameGenerator());

TabPane tabPane = new TabPane();
tabPane.getTabs().add(terminal);
----

== CygWin Support

[source,java]
Expand All @@ -61,7 +73,7 @@ TerminalConfig cygwinConfig = new TerminalConfig();
cygwinConfig.setWindowsTerminalStarter("C:\\cygwin64\\bin\\bash -i"); <1>

TerminalBuilder terminalBuilder = new TerminalBuilder(cygwinConfig);
TerminalTab terminal = terminalBuilder.newTerminal();
TerminalTab<ExternalProcessPty> terminal = terminalBuilder.newTerminal();

TabPane tabPane = new TabPane();
tabPane.getTabs().add(terminal);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.kodedu.terminalfx</groupId>
<artifactId>terminalfx</artifactId>
<version>1.0.9</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<name>TerminalFX</name>
Expand Down
108 changes: 108 additions & 0 deletions src/main/java/com/kodedu/terminalfx/GenericTerminal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.kodedu.terminalfx;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.concurrent.LinkedBlockingQueue;

import com.kodedu.terminalfx.annotation.WebkitCall;
import com.kodedu.terminalfx.config.TerminalConfig;
import com.kodedu.terminalfx.helper.ThreadHelper;
import com.kodedu.terminalfx.processes.Pty;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;

public class GenericTerminal<T extends Pty> extends TerminalView {

private T process;
private final ObjectProperty<Writer> outputWriterProperty;
private final LinkedBlockingQueue<String> commandQueue;

public GenericTerminal() {
this(null, null);
}

public GenericTerminal(TerminalConfig terminalConfig, T virtualProcess) {
setTerminalConfig(terminalConfig);
this.process = virtualProcess;
outputWriterProperty = new SimpleObjectProperty<>();
commandQueue = new LinkedBlockingQueue<>();
}

@WebkitCall
public void command(String command) {
try {
commandQueue.put(command);
} catch (final InterruptedException e) {
throw new RuntimeException(e);
}
ThreadHelper.start(() -> {
try {
final String commandToExecute = commandQueue.poll();
getOutputWriter().write(commandToExecute);
getOutputWriter().flush();
} catch (final IOException e) {
e.printStackTrace();
}
});
}

@Override
public void onTerminalReady() {
ThreadHelper.start(() -> {
try {
initializeProcess();
} catch (final Exception e) {
}
});
}

private void initializeProcess() throws Exception {
process.fork();

columnsProperty().addListener(evt -> updateWinSize());
rowsProperty().addListener(evt -> updateWinSize());
updateWinSize();
setInputReader(new BufferedReader(new InputStreamReader(process.getInputStream())));
setErrorReader(new BufferedReader(new InputStreamReader(process.getErrorStream())));
setOutputWriter(new BufferedWriter(new OutputStreamWriter(process.getOutputStream())));
focusCursor();

countDownLatch.countDown();

process.waitFor();
}

private void updateWinSize() {
try {
process.setWinSize(getColumns(), getRows());
} catch (Exception e) {
//
}
}

public ObjectProperty<Writer> outputWriterProperty() {
return outputWriterProperty;
}

public Writer getOutputWriter() {
return outputWriterProperty.get();
}

public void setOutputWriter(Writer writer) {
outputWriterProperty.set(writer);
}

public T getPty() {
return process;
}

public void destroy() {
getPty().close();
}

}
128 changes: 6 additions & 122 deletions src/main/java/com/kodedu/terminalfx/Terminal.java
Original file line number Diff line number Diff line change
@@ -1,143 +1,27 @@
package com.kodedu.terminalfx;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.LinkedBlockingQueue;

import com.kodedu.terminalfx.annotation.WebkitCall;
import com.kodedu.terminalfx.config.TerminalConfig;
import com.kodedu.terminalfx.helper.IOHelper;
import com.kodedu.terminalfx.helper.ThreadHelper;
import com.kodedu.terminalfx.processes.ExternalProcessPty;
import com.pty4j.PtyProcess;
import com.pty4j.WinSize;
import com.sun.jna.Platform;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;

public class Terminal extends TerminalView {

private PtyProcess process;
private final ObjectProperty<Writer> outputWriterProperty;
private final Path terminalPath;
private String[] termCommand;
private final LinkedBlockingQueue<String> commandQueue;
public class Terminal extends GenericTerminal<ExternalProcessPty> {

public Terminal() {
this(null, null);
}

public Terminal(TerminalConfig terminalConfig, Path terminalPath) {
setTerminalConfig(terminalConfig);
this.terminalPath = terminalPath;
outputWriterProperty = new SimpleObjectProperty<>();
commandQueue = new LinkedBlockingQueue<>();
}

@WebkitCall
public void command(String command) {
try {
commandQueue.put(command);
} catch (final InterruptedException e) {
throw new RuntimeException(e);
}
ThreadHelper.start(() -> {
try {
final String commandToExecute = commandQueue.poll();
getOutputWriter().write(commandToExecute);
getOutputWriter().flush();
} catch (final IOException e) {
e.printStackTrace();
}
});
}

@Override
public void onTerminalReady() {
ThreadHelper.start(() -> {
try {
initializeProcess();
} catch (final Exception e) {
}
});
}

private void initializeProcess() throws Exception {
final Path dataDir = getDataDir();
IOHelper.copyLibPty(dataDir);

if (Platform.isWindows()) {
this.termCommand = getTerminalConfig().getWindowsTerminalStarter().split("\\s+");
} else {
this.termCommand = getTerminalConfig().getUnixTerminalStarter().split("\\s+");
}

final Map<String, String> envs = new HashMap<>(System.getenv());
envs.put("TERM", "xterm");

System.setProperty("PTY_LIB_FOLDER", dataDir.resolve("libpty").toString());

if (Objects.nonNull(terminalPath) && Files.exists(terminalPath)) {
this.process = PtyProcess.exec(termCommand, envs, terminalPath.toString());
} else {
this.process = PtyProcess.exec(termCommand, envs);
}

columnsProperty().addListener(evt -> updateWinSize());
rowsProperty().addListener(evt -> updateWinSize());
updateWinSize();
setInputReader(new BufferedReader(new InputStreamReader(process.getInputStream())));
setErrorReader(new BufferedReader(new InputStreamReader(process.getErrorStream())));
setOutputWriter(new BufferedWriter(new OutputStreamWriter(process.getOutputStream())));
focusCursor();

countDownLatch.countDown();

process.waitFor();
}

private Path getDataDir() {
final String userHome = System.getProperty("user.home");
final Path dataDir = Paths.get(userHome).resolve(".terminalfx");
return dataDir;
super(terminalConfig, new ExternalProcessPty(terminalPath));
getPty().setTerminalConfig(getTerminalConfig());
}

public Path getTerminalPath() {
return terminalPath;
}

private void updateWinSize() {
try {
process.setWinSize(new WinSize(getColumns(), getRows()));
} catch (Exception e) {
//
}
}

public ObjectProperty<Writer> outputWriterProperty() {
return outputWriterProperty;
}

public Writer getOutputWriter() {
return outputWriterProperty.get();
}

public void setOutputWriter(Writer writer) {
outputWriterProperty.set(writer);
return getPty().getTerminalPath();
}

public PtyProcess getProcess() {
return process;
return getPty().getProcess();
}

}
3 changes: 2 additions & 1 deletion src/main/java/com/kodedu/terminalfx/TerminalAppStarter.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.kodedu.terminalfx;

import java.io.InputStream;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.InputStream;


public class TerminalAppStarter extends Application {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/kodedu/terminalfx/TerminalBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.kodedu.terminalfx.config.DefaultTabNameGenerator;
import com.kodedu.terminalfx.config.TerminalConfig;
import com.kodedu.terminalfx.processes.ExternalProcessPty;
import com.kodedu.terminalfx.config.TabNameGenerator;

import java.nio.file.Path;
Expand Down Expand Up @@ -53,8 +54,8 @@ public void setTerminalPath(Path terminalPath) {
this.terminalPath = terminalPath;
}

public TerminalTab newTerminal() {
TerminalTab terminalTab = new TerminalTab(getTerminalConfig(), getNameGenerator(), getTerminalPath());
public TerminalTab<ExternalProcessPty> newTerminal() {
TerminalTab<ExternalProcessPty> terminalTab = new TerminalTab<>(() -> new Terminal(getTerminalConfig(), getTerminalPath()), getNameGenerator());
return terminalTab;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/kodedu/terminalfx/TerminalGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.kodedu.terminalfx;

import com.kodedu.terminalfx.processes.Pty;

@FunctionalInterface
public interface TerminalGenerator<T extends Pty> {

GenericTerminal<T> generate();
}
Loading