Skip to content

Commit f0025c4

Browse files
Update InteractiveSearchCommand.java
1 parent ffc8662 commit f0025c4

1 file changed

Lines changed: 30 additions & 28 deletions

File tree

src/main/java/com/mycmd/commands/InteractiveSearchCommand.java

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,60 +20,62 @@ public class InteractiveSearchCommand implements Command {
2020
@Override
2121
public void execute(String[] args, ShellContext context) {
2222
List<String> history = context.getHistory();
23-
23+
2424
if (history.isEmpty()) {
2525
System.out.println("No command history available.");
2626
return;
2727
}
28-
29-
Scanner scanner = new Scanner(System.in);
28+
29+
// ✅ Use shared scanner from ShellContext instead of creating a new one
30+
Scanner scanner = context.getScanner();
31+
3032
System.out.println("=== Interactive History Search ===");
3133
System.out.println("Type to search, 'q' to quit");
3234
System.out.println();
33-
35+
3436
while (true) {
3537
System.out.print("search> ");
3638
String searchTerm = scanner.nextLine().trim();
37-
39+
3840
if (searchTerm.equalsIgnoreCase("q") || searchTerm.equalsIgnoreCase("quit")) {
3941
System.out.println("Search cancelled.");
4042
break;
4143
}
42-
44+
4345
if (searchTerm.isEmpty()) {
4446
System.out.println("Enter a search term or 'q' to quit.");
4547
continue;
4648
}
47-
49+
4850
// Search and display results
4951
List<String> matches = searchHistory(history, searchTerm);
50-
52+
5153
if (matches.isEmpty()) {
5254
System.out.println("No matches found for: '" + searchTerm + "'");
5355
System.out.println();
5456
continue;
5557
}
56-
58+
5759
// Display matches with numbers
5860
System.out.println("\nMatches for '" + searchTerm + "':");
5961
for (int i = 0; i < Math.min(matches.size(), 10); i++) {
6062
System.out.println(" " + (i + 1) + ". " + matches.get(i));
6163
}
62-
64+
6365
if (matches.size() > 10) {
6466
System.out.println(" ... and " + (matches.size() - 10) + " more");
6567
}
66-
68+
6769
// Ask user to select or refine
6870
System.out.println();
69-
System.out.print("Select number to copy (1-" + Math.min(matches.size(), 10) +
70-
"), or press Enter to search again: ");
71+
System.out.print("Select number to copy (1-" + Math.min(matches.size(), 10) +
72+
"), or press Enter to search again: ");
7173
String selection = scanner.nextLine().trim();
72-
74+
7375
if (selection.isEmpty()) {
7476
continue;
7577
}
76-
78+
7779
try {
7880
int index = Integer.parseInt(selection) - 1;
7981
if (index >= 0 && index < Math.min(matches.size(), 10)) {
@@ -87,35 +89,35 @@ public void execute(String[] args, ShellContext context) {
8789
} catch (NumberFormatException e) {
8890
System.out.println("Invalid input. Enter a number or press Enter.");
8991
}
90-
92+
9193
System.out.println();
9294
}
9395
}
94-
96+
9597
/**
9698
* Search history for commands containing the search term.
9799
* Returns matches in reverse order (most recent first).
98100
*/
99101
private List<String> searchHistory(List<String> history, String searchTerm) {
100102
String lowerSearch = searchTerm.toLowerCase();
101-
103+
102104
// Search in reverse order (most recent first)
103105
return history.stream()
104-
.filter(cmd -> cmd.toLowerCase().contains(lowerSearch))
105-
.collect(Collectors.collectingAndThen(
106-
Collectors.toList(),
107-
list -> {
108-
java.util.Collections.reverse(list);
109-
return list;
110-
}
111-
));
106+
.filter(cmd -> cmd.toLowerCase().contains(lowerSearch))
107+
.collect(Collectors.collectingAndThen(
108+
Collectors.toList(),
109+
list -> {
110+
java.util.Collections.reverse(list);
111+
return list;
112+
}
113+
));
112114
}
113-
115+
114116
@Override
115117
public String description() {
116118
return "Interactive history search (like Ctrl+R)";
117119
}
118-
120+
119121
@Override
120122
public String usage() {
121123
return "isearch\n" +

0 commit comments

Comments
 (0)