Skip to content

Commit

Permalink
add initialColumns and initialRow to PtyProcessBuilder
Browse files Browse the repository at this point in the history
Wrap common process parameters into PtyProcessOptions for easier modifying process parameters in future.
  • Loading branch information
segrey committed Oct 23, 2018
1 parent 9d51401 commit d09ec5a
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 18 deletions.
43 changes: 30 additions & 13 deletions src/com/pty4j/PtyProcessBuilder.java
@@ -1,19 +1,16 @@
package com.pty4j;

import com.pty4j.unix.Pty;
import com.pty4j.unix.UnixPtyProcess;
import com.pty4j.util.PtyUtil;
import com.pty4j.windows.CygwinPtyProcess;
import com.pty4j.windows.WinPtyProcess;
import com.sun.jna.Platform;
import com.sun.jna.platform.win32.Advapi32Util;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;

public class PtyProcessBuilder {
private String[] myCommand;
Expand All @@ -23,13 +20,20 @@ public class PtyProcessBuilder {
private boolean myCygwin;
private File myLogFile;
private boolean myRedirectErrorStream = false;
private Integer myInitialColumns;
private Integer myInitialRows;

public PtyProcessBuilder() {
}

public PtyProcessBuilder(@NotNull String[] command) {
myCommand = command;
}

public void setCommand(@NotNull String[] command) {
@NotNull
public PtyProcessBuilder setCommand(@NotNull String[] command) {
myCommand = command;
return this;
}

@NotNull
Expand Down Expand Up @@ -68,20 +72,33 @@ public PtyProcessBuilder setRedirectErrorStream(boolean redirectErrorStream) {
return this;
}

@NotNull
public PtyProcessBuilder setInitialColumns(@Nullable Integer initialColumns) {
myInitialColumns = initialColumns;
return this;
}

@NotNull
public PtyProcessBuilder setInitialRows(@Nullable Integer initialRows) {
myInitialRows = initialRows;
return this;
}

@NotNull
public PtyProcess start() throws IOException {
PtyProcessOptions options = new PtyProcessOptions(myCommand,
myEnvironment,
myDirectory,
myRedirectErrorStream,
myInitialColumns,
myInitialRows);
if (Platform.isWindows()) {
Map<String, String> environment = myEnvironment;
if (environment == null) {
environment = new TreeMap<String, String>();
}
if (myCygwin) {
Map<String, String> environment = myEnvironment != null ? myEnvironment : Collections.emptyMap();
return new CygwinPtyProcess(myCommand, environment, myDirectory, myLogFile, myConsole);
}
return new WinPtyProcess(myCommand, Advapi32Util.getEnvironmentBlock(environment), myDirectory, myConsole);
return new WinPtyProcess(options, myConsole);
}
Pty pty = new Pty(myConsole);
Pty errPty = myRedirectErrorStream ? pty : (myConsole ? new Pty() : null);
return new UnixPtyProcess(myCommand, PtyUtil.toStringArray(myEnvironment), myDirectory, pty, errPty);
return new UnixPtyProcess(options, myConsole);
}
}
58 changes: 58 additions & 0 deletions src/com/pty4j/PtyProcessOptions.java
@@ -0,0 +1,58 @@
package com.pty4j;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;

public class PtyProcessOptions {
private final String[] myCommand;
private final Map<String, String> myEnvironment;
private final String myDirectory;
private final boolean myRedirectErrorStream;
private final Integer myInitialColumns;
private final Integer myInitialRows;

PtyProcessOptions(@NotNull String[] command,
@Nullable Map<String, String> environment,
@Nullable String directory,
boolean redirectErrorStream,
@Nullable Integer initialColumns,
@Nullable Integer initialRows) {
myCommand = command;
myEnvironment = environment;
myDirectory = directory;
myRedirectErrorStream = redirectErrorStream;
myInitialColumns = initialColumns;
myInitialRows = initialRows;
}

@NotNull
public String[] getCommand() {
return myCommand;
}

@Nullable
public Map<String, String> getEnvironment() {
return myEnvironment;
}

@Nullable
public String getDirectory() {
return myDirectory;
}

public boolean isRedirectErrorStream() {
return myRedirectErrorStream;
}

@Nullable
public Integer getInitialColumns() {
return myInitialColumns;
}

@Nullable
public Integer getInitialRows() {
return myInitialRows;
}
}
16 changes: 14 additions & 2 deletions src/com/pty4j/unix/UnixPtyProcess.java
Expand Up @@ -7,9 +7,13 @@
*******************************************************************************/
package com.pty4j.unix;

import com.google.common.base.MoreObjects;
import com.pty4j.PtyProcess;
import com.pty4j.PtyProcessOptions;
import com.pty4j.WinSize;
import com.pty4j.util.PtyUtil;
import jtermios.JTermios;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
Expand Down Expand Up @@ -59,9 +63,10 @@ public class UnixPtyProcess extends PtyProcess {
private OutputStream out;
private InputStream in;
private InputStream err;
private Pty myPty;
private Pty myErrPty;
private final Pty myPty;
private final Pty myErrPty;

@Deprecated
public UnixPtyProcess(String[] cmdarray, String[] envp, String dir, Pty pty, Pty errPty) throws IOException {
if (dir == null) {
dir = ".";
Expand All @@ -74,6 +79,13 @@ public UnixPtyProcess(String[] cmdarray, String[] envp, String dir, Pty pty, Pty
execInPty(cmdarray, envp, dir, pty, errPty);
}

public UnixPtyProcess(@NotNull PtyProcessOptions options, boolean consoleMode) throws IOException {
myPty = new Pty(consoleMode);
myErrPty = options.isRedirectErrorStream() ? myPty : (consoleMode ? new Pty() : null);
String dir = MoreObjects.firstNonNull(options.getDirectory(), ".");
execInPty(options.getCommand(), PtyUtil.toStringArray(options.getEnvironment()), dir, myPty, myErrPty);
}

public Pty getPty() {
return myPty;
}
Expand Down
13 changes: 11 additions & 2 deletions src/com/pty4j/windows/WinPty.java
Expand Up @@ -9,6 +9,7 @@
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
Expand Down Expand Up @@ -36,9 +37,17 @@ public class WinPty {
private int openInputStreamCount = 0;

public WinPty(String cmdline, String cwd, String env, boolean consoleMode) throws PtyException, IOException {
int cols = Integer.getInteger("win.pty.cols", 80);
int rows = Integer.getInteger("win.pty.rows", 25);
this(cmdline, cwd, env, consoleMode, null, null);
}

WinPty(@NotNull String cmdline,
@Nullable String cwd,
@NotNull String env,
boolean consoleMode,
@Nullable Integer initialColumns,
@Nullable Integer initialRows) throws PtyException, IOException {
int cols = initialColumns != null ? initialColumns : Integer.getInteger("win.pty.cols", 80);
int rows = initialRows != null ? initialRows : Integer.getInteger("win.pty.rows", 25);
IntByReference errCode = new IntByReference();
PointerByReference errPtr = new PointerByReference(null);
Pointer agentCfg = null;
Expand Down
31 changes: 30 additions & 1 deletion src/com/pty4j/windows/WinPtyProcess.java
Expand Up @@ -2,12 +2,17 @@

import com.pty4j.PtyException;
import com.pty4j.PtyProcess;
import com.pty4j.PtyProcessOptions;
import com.pty4j.WinSize;
import com.sun.jna.platform.win32.Advapi32Util;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.Map;

/**
* @author traff
Expand Down Expand Up @@ -36,9 +41,33 @@ private static String convertEnvironment(String[] environment) {
return envString.toString();
}

@Deprecated
public WinPtyProcess(String[] command, String environment, String workingDirectory, boolean consoleMode) throws IOException {
this(command, environment, workingDirectory, null, null, consoleMode);
}

public WinPtyProcess(@NotNull PtyProcessOptions options, boolean consoleMode) throws IOException {
this(options.getCommand(),
convertEnvironment(options.getEnvironment()),
options.getDirectory(),
options.getInitialColumns(),
options.getInitialRows(),
consoleMode);
}

@NotNull
private static String convertEnvironment(@Nullable Map<String, String> environment) {
return Advapi32Util.getEnvironmentBlock(environment != null ? environment : Collections.emptyMap());
}

private WinPtyProcess(@NotNull String[] command,
@NotNull String environment,
@Nullable String workingDirectory,
@Nullable Integer initialColumns,
@Nullable Integer initialRows,
boolean consoleMode) throws IOException {
try {
myWinPty = new WinPty(joinCmdArgs(command), workingDirectory, environment, consoleMode);
myWinPty = new WinPty(joinCmdArgs(command), workingDirectory, environment, consoleMode, initialColumns, initialRows);
} catch (PtyException e) {
throw new IOException("Couldn't create PTY", e);
}
Expand Down

0 comments on commit d09ec5a

Please sign in to comment.