diff --git a/README.md b/README.md index f5509ee..317eeba 100644 --- a/README.md +++ b/README.md @@ -314,8 +314,9 @@ All commands support `--help`. Every tree takes the same flags; only the defaults differ. With `--json` (or `-o json`, `-o yaml`) a command prints one document on stdout and nothing else; when it fails, the error is on stderr -and stdout is empty. Three commands print text whatever the format, because their output is not a -document: `logs` (the container's own output), `env` (shell code) and `completion` (a script). Marks are plain ASCII without +and stdout is empty. Two commands print text whatever the format, because their output is not a +document: `logs` (the container's own output) and `completion` (a script). `env` prints shell +code in text mode and the variables as a map in JSON or YAML. Marks are plain ASCII without `--no-emoji` too whenever the output is not a terminal (a pipe, a file, CI logs) or `NO_COLOR` is set. `--json` wins when it is combined with `--output`. diff --git a/src/main/java/io/floci/cli/commands/config/ConfigProfileCommand.java b/src/main/java/io/floci/cli/commands/config/ConfigProfileCommand.java index 4a0ce9e..9664d9e 100644 --- a/src/main/java/io/floci/cli/commands/config/ConfigProfileCommand.java +++ b/src/main/java/io/floci/cli/commands/config/ConfigProfileCommand.java @@ -225,7 +225,7 @@ public class ConfigProfileCommand implements Callable { command.add(file.toString()); int exit; try { - exit = new ProcessBuilder(command).inheritIO().start().waitFor(); + exit = editorProcess(command, printer.format() != OutputFormat.text).start().waitFor(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); printer.error("Interrupted while " + editor.get(0) + " was open.\nRe-run '" + prefix + "edit " + name + "'."); @@ -263,6 +263,24 @@ public class ConfigProfileCommand implements Callable { return 0; } + /** + * How the editor is started. It shares this command's terminal, so an interactive editor + * works. With structured output its stdout goes to stderr instead: stdout carries the one + * result document, and anything an editor or its wrapper prints there would break it. + */ + public static ProcessBuilder editorProcess(List command, boolean structured) { + ProcessBuilder editor = new ProcessBuilder(command).inheritIO(); + if (structured) { + // /dev/stderr is this process's own stderr, still the terminal when only stdout is + // captured. Where there is no such file (Windows), the editor's output is dropped. + Path stderr = Path.of("/dev/stderr"); + editor.redirectOutput(Files.isWritable(stderr) + ? ProcessBuilder.Redirect.appendTo(stderr.toFile()) + : ProcessBuilder.Redirect.DISCARD); + } + return editor; + } + /** * The editor to run, as a command and its arguments: {@code VISUAL} first, then * {@code EDITOR}, as other tools do. Empty when neither is set. diff --git a/src/test/java/io/floci/cli/unit/ErgonomicsTest.java b/src/test/java/io/floci/cli/unit/ErgonomicsTest.java index d7bffd3..db3f789 100644 --- a/src/test/java/io/floci/cli/unit/ErgonomicsTest.java +++ b/src/test/java/io/floci/cli/unit/ErgonomicsTest.java @@ -176,6 +176,42 @@ class ErgonomicsTest { assertFalse(r.out().contains("saved ("), r.out()); } + /** + * With structured output the editor's stdout must not be this command's stdout, or whatever + * the editor prints lands in front of the result. A real child process shows it: its stdout is + * captured in a file here, as a shell redirect would. + */ + @Test + @DisabledOnOs(OS.WINDOWS) + void anEditorsOutputStaysOffStdoutInStructuredMode() throws Exception { + Path noisy = editorThat("echo 'Waiting for your editor to close the file...'"); + Path captured = tempDir.resolve("stdout.txt"); + Path errors = tempDir.resolve("stderr.txt"); + + ProcessBuilder structured = ConfigProfileCommand.editorProcess(List.of(noisy.toString(), "file"), true); + assertNotEquals(ProcessBuilder.Redirect.INHERIT, structured.redirectOutput()); + assertEquals(ProcessBuilder.Redirect.INHERIT, structured.redirectInput(), "the editor still reads the terminal"); + + // The same decision, observed from outside: a shell runs the editor the way the command + // would, with fd 1 and fd 2 captured separately. + String redirect = structured.redirectOutput().file() == null ? "/dev/null" : structured.redirectOutput().file().toString(); + Process shell = new ProcessBuilder("sh", "-c", "'" + noisy + "' file >> " + redirect) + .redirectOutput(captured.toFile()).redirectError(errors.toFile()).start(); + assertEquals(0, shell.waitFor()); + + assertEquals("", Files.readString(captured), "nothing from the editor on stdout"); + assertTrue(Files.readString(errors).contains("Waiting for your editor"), "it went to stderr instead"); + } + + @Test + void inTextModeTheEditorSharesTheTerminal() { + ProcessBuilder text = ConfigProfileCommand.editorProcess(List.of("vi", "file"), false); + + assertEquals(ProcessBuilder.Redirect.INHERIT, text.redirectOutput()); + assertEquals(ProcessBuilder.Redirect.INHERIT, text.redirectInput()); + assertEquals(ProcessBuilder.Redirect.INHERIT, text.redirectError()); + } + /** VISUAL wins over EDITOR, and a value with arguments is a command line. */ @Test @DisabledOnOs(OS.WINDOWS)