diff --git a/cli/src/main/java/io/github/dfa1/vortex/cli/CliHandles.java b/cli/src/main/java/io/github/dfa1/vortex/cli/CliHandles.java index a5df00a2..4e800d21 100644 --- a/cli/src/main/java/io/github/dfa1/vortex/cli/CliHandles.java +++ b/cli/src/main/java/io/github/dfa1/vortex/cli/CliHandles.java @@ -10,6 +10,7 @@ import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; /// Shared handle plumbing for the interactive subcommands (`view`, `tui`). @@ -27,10 +28,10 @@ private CliHandles() { /// /// @param worker the I/O worker that owns the handle's arena /// @param target a local path or an `http(s)://` URL - /// @return the opened handle, or `null` if the target is missing or malformed + /// @return the opened handle, or empty if the target is missing or malformed /// @throws InterruptedException if interrupted while waiting on the worker /// @throws IOException if opening the file or URL fails - static VortexHandle openOnWorker(IoWorker worker, String target) + static Optional openOnWorker(IoWorker worker, String target) throws InterruptedException, IOException { AtomicReference handle = new AtomicReference<>(); AtomicReference failure = new AtomicReference<>(); @@ -44,7 +45,7 @@ static VortexHandle openOnWorker(IoWorker worker, String target) if (failure.get() != null) { throw failure.get(); } - return handle.get(); + return Optional.ofNullable(handle.get()); } /// Closes `handle` on the worker thread that opened it. diff --git a/cli/src/main/java/io/github/dfa1/vortex/cli/TuiCommand.java b/cli/src/main/java/io/github/dfa1/vortex/cli/TuiCommand.java index 2412d256..581ccd25 100644 --- a/cli/src/main/java/io/github/dfa1/vortex/cli/TuiCommand.java +++ b/cli/src/main/java/io/github/dfa1/vortex/cli/TuiCommand.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.io.PrintStream; +import java.util.Optional; final class TuiCommand { @@ -19,10 +20,11 @@ static int run(String[] args) { return ExitStatus.USAGE_ERROR; } try (IoWorker worker = new IoWorker("vortex-tui-io")) { - VortexHandle handle = CliHandles.openOnWorker(worker, args[1]); - if (handle == null) { + Optional opened = CliHandles.openOnWorker(worker, args[1]); + if (opened.isEmpty()) { return ExitStatus.FILE_NOT_FOUND; } + VortexHandle handle = opened.get(); try { VortexInspectorTui.show(handle, worker, progressBar(System.err)); } finally { diff --git a/cli/src/main/java/io/github/dfa1/vortex/cli/ViewCommand.java b/cli/src/main/java/io/github/dfa1/vortex/cli/ViewCommand.java index d4421f46..62d59574 100644 --- a/cli/src/main/java/io/github/dfa1/vortex/cli/ViewCommand.java +++ b/cli/src/main/java/io/github/dfa1/vortex/cli/ViewCommand.java @@ -6,6 +6,7 @@ import io.github.dfa1.vortex.reader.VortexHandle; import java.io.IOException; +import java.util.Optional; @SuppressWarnings("java:S106") final class ViewCommand { @@ -22,24 +23,27 @@ static int run(String[] args) { System.err.print("Opening file... "); System.err.flush(); long tOpen = System.nanoTime(); - VortexHandle handle = CliHandles.openOnWorker(worker, args[1]); - if (handle == null) { + Optional opened = CliHandles.openOnWorker(worker, args[1]); + if (opened.isEmpty()) { return ExitStatus.FILE_NOT_FOUND; } - System.err.println("done (" + (System.nanoTime() - tOpen) / 1_000_000L + " ms)"); + VortexHandle handle = opened.get(); + try { + System.err.println("done (" + (System.nanoTime() - tOpen) / 1_000_000L + " ms)"); - System.err.print("Indexing chunks... "); - System.err.flush(); - long tIdx = System.nanoTime(); - try (LazyGridSource source = LazyGridSource.open(handle, worker)) { - long ms = (System.nanoTime() - tIdx) / 1_000_000L; - System.err.println("done — " + source.totalRows() + " rows × " - + source.columns().size() + " cols (" + ms + " ms)"); - VortexGridTui.show(args[1], source); + System.err.print("Indexing chunks... "); + System.err.flush(); + long tIdx = System.nanoTime(); + try (LazyGridSource source = LazyGridSource.open(handle, worker)) { + long ms = (System.nanoTime() - tIdx) / 1_000_000L; + System.err.println("done — " + source.totalRows() + " rows × " + + source.columns().size() + " cols (" + ms + " ms)"); + VortexGridTui.show(args[1], source); + } + return ExitStatus.OK; } finally { CliHandles.closeOnWorker(worker, handle); } - return ExitStatus.OK; } catch (IOException | RuntimeException | InterruptedException e) { if (e instanceof InterruptedException) { Thread.currentThread().interrupt();