|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Christoph Läubrich and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Christoph Läubrich - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.debug.terminal; |
| 15 | + |
| 16 | +import java.io.File; |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.Optional; |
| 20 | + |
| 21 | +import org.eclipse.cdt.utils.pty.PTY; |
| 22 | +import org.eclipse.cdt.utils.pty.PTY.Mode; |
| 23 | +import org.eclipse.cdt.utils.spawner.ProcessFactory; |
| 24 | +import org.eclipse.core.runtime.CoreException; |
| 25 | +import org.eclipse.core.runtime.Status; |
| 26 | +import org.eclipse.debug.core.ExecFactory; |
| 27 | + |
| 28 | +public class PtyExecFactory implements ExecFactory { |
| 29 | + |
| 30 | + @Override |
| 31 | + public Optional<Process> exec(String[] cmdLine, Optional<File> workingDirectory, |
| 32 | + Optional<Map<String, String>> environment, boolean mergeOutput) throws CoreException { |
| 33 | + if (mergeOutput || !PTY.isSupported(Mode.TERMINAL)) { |
| 34 | + return Optional.empty(); |
| 35 | + } |
| 36 | + try { |
| 37 | + PTY pty = new PTY(Mode.TERMINAL); |
| 38 | + pty.setTerminalSize(80, 24); |
| 39 | + String[] env; |
| 40 | + if (environment.isEmpty()) { |
| 41 | + env = null; |
| 42 | + } else { |
| 43 | + env = environment.stream().flatMap(m -> m.entrySet().stream()).map(e -> e.getKey() + "=" + e.getValue()) |
| 44 | + .toArray(String[]::new); |
| 45 | + } |
| 46 | + File wd = workingDirectory.orElse(null); |
| 47 | + return Optional.of(ProcessFactory.getFactory().exec(cmdLine, env, wd, pty)); |
| 48 | + } catch (IOException e) { |
| 49 | + throw new CoreException(Status.error("Execution failed", e)); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | +} |
0 commit comments