79 lines
2.0 KiB
Java
79 lines
2.0 KiB
Java
package com.example.plugin;
|
|
|
|
import com.jcraft.jsch.ChannelShell;
|
|
import com.jediterm.terminal.TtyConnector;
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import java.awt.*;
|
|
import java.io.*;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
public class SshTtyConnector implements TtyConnector {
|
|
private final ChannelShell channel;
|
|
private final InputStream inputStream;
|
|
private final OutputStream outputStream;
|
|
private final Reader reader;
|
|
|
|
public SshTtyConnector(ChannelShell channel) {
|
|
this.channel = channel;
|
|
try {
|
|
this.inputStream = channel.getInputStream();
|
|
this.outputStream = channel.getOutputStream();
|
|
this.reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
|
|
} catch (Exception e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean isConnected() {
|
|
return channel.isConnected() && !channel.isClosed();
|
|
}
|
|
|
|
@Override
|
|
public void close() {
|
|
if (channel.isConnected()) {
|
|
channel.disconnect();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public @Nullable String getName() {
|
|
return "SSH Remote";
|
|
}
|
|
|
|
@Override
|
|
public int read(char[] buf, int offset, int length) throws IOException {
|
|
return reader.read(buf, offset, length);
|
|
}
|
|
|
|
@Override
|
|
public void write(String string) throws IOException {
|
|
write(string.getBytes(StandardCharsets.UTF_8));
|
|
}
|
|
|
|
@Override
|
|
public void write(byte[] bytes) throws IOException {
|
|
outputStream.write(bytes);
|
|
outputStream.flush();
|
|
}
|
|
|
|
@Override
|
|
public void resize(@NotNull Dimension size) {
|
|
channel.setPtySize(size.width, size.height, 0, 0);
|
|
}
|
|
|
|
@Override
|
|
public int waitFor() throws InterruptedException {
|
|
while (isConnected()) {
|
|
Thread.sleep(100);
|
|
}
|
|
return channel.getExitStatus();
|
|
}
|
|
|
|
@Override
|
|
public boolean ready() throws IOException {
|
|
return reader.ready();
|
|
}
|
|
} |