package com.ankurm.interop; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.nio.file.Files; import java.nio.file.Path; /** Writes a numbered transcript under {@code output/} and echoes it. Every console block in the article is one of these files. */ final class Transcript implements AutoCloseable { private final Path path; private final StringWriter buffer = new StringWriter(); private final PrintWriter out = new PrintWriter(buffer); Transcript(String fileName, String title) { this.path = Path.of("output", fileName); out.println("# " + title); out.println(); } Transcript line(String format, Object... args) { out.println(args.length == 0 ? format : String.format(format, args)); return this; } Transcript blank() { out.println(); return this; } @Override public void close() { out.flush(); try { Files.createDirectories(path.getParent()); Files.writeString(path, buffer.toString()); } catch (IOException e) { throw new IllegalStateException("could not write " + path, e); } System.out.print(buffer); } }