From 344881468614b4b23aaf8646bdd46ae44a7d62fe Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Sun, 25 Jan 2026 14:10:24 +0200 Subject: [PATCH] Refactor `Menu` class: Simplify `updateDisplay` method, remove redundant logic, and adjust filtered options handling for readability. --- .../eu/svjatoslav/commons/cli_helper/Menu.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/Menu.java b/src/main/java/eu/svjatoslav/commons/cli_helper/Menu.java index 8aedcad..73356fb 100644 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/Menu.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/Menu.java @@ -36,7 +36,13 @@ public class Menu { int lastTotalLines = 0; while (true) { - selectedIndex = updateDisplay(prompt, options, selectedIndex, filterString, lastTotalLines); + + List filtered = filterOptions(options, filterString); + + // Clamp selected to valid range + selectedIndex = Math.max(0, Math.min(selectedIndex, filtered.size() - 1)); + + updateDisplay(prompt, filtered, selectedIndex, filterString, lastTotalLines); lastTotalLines = getCurrentTotalLines(options, filterString); int c = System.in.read(); @@ -61,7 +67,6 @@ public class Menu { // Handle Enter key else if (c == 13 || c == 10) { - List filtered = filterOptions(options, filterString.toString()); if (filtered.isEmpty()) { return null; } @@ -172,16 +177,12 @@ public class Menu { /** * Updates the display based on the current state and returns the adjusted selected index. */ - private static int updateDisplay(String prompt, List options, int selected, String filter, int lastTotalLines) { - List filtered = filterOptions(options, filter); + private static void updateDisplay(String prompt, List filtered, int selected, String filter, int lastTotalLines) { if (filtered.isEmpty()) { filtered = Collections.singletonList("No matches found"); } - // Clamp selected to valid range - selected = Math.max(0, Math.min(selected, filtered.size() - 1)); - // ──────────────────────────────────────────────── // 1. Position cursor at the START of our display area // ──────────────────────────────────────────────── @@ -210,8 +211,6 @@ public class Menu { System.out.print("\r"); System.out.flush(); // ensure immediate display - - return selected; } private static int getCurrentTotalLines(List options, String filter) { -- 2.20.1