Skip to content

Commit 7035d9b

Browse files
committed
Added a SuggestionHelper class to improved Time and general command args suggestions
1 parent 0d53bd6 commit 7035d9b

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package dev.lars.utilsmanager.utils;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collection;
6+
import java.util.List;
7+
8+
public class SuggestHelper {
9+
10+
public static Collection<String> filter(String input, String... values) {
11+
String lower = input.toLowerCase();
12+
return Arrays.stream(values)
13+
.filter(v -> v.toLowerCase().startsWith(lower))
14+
.toList();
15+
}
16+
17+
public static List<String> getTimeSuggestions(String input) {
18+
List<String> suggestions = new ArrayList<>();
19+
20+
if (input == null || input.isBlank()) {
21+
return List.of("30s", "5m", "15m", "1h", "12h", "1d", "7d");
22+
}
23+
24+
String trimmed = input.trim().toLowerCase();
25+
26+
if (trimmed.matches("\\d+")) {
27+
long number = Long.parseLong(trimmed);
28+
29+
suggestions.add(number + "s");
30+
suggestions.add(number + "m");
31+
suggestions.add(number + "h");
32+
suggestions.add(number + "d");
33+
34+
long nextNumber = number * 10;
35+
if (nextNumber <= 999999) {
36+
suggestions.add(nextNumber + "s");
37+
suggestions.add(nextNumber + "m");
38+
suggestions.add(nextNumber + "h");
39+
suggestions.add(nextNumber + "d");
40+
}
41+
42+
return suggestions;
43+
}
44+
45+
46+
if (trimmed.matches("\\d+[smhd]")) {
47+
suggestions.add(trimmed);
48+
return suggestions;
49+
}
50+
51+
String[] allSuggestions = {"30s", "5m", "15m", "1h", "12h", "1d", "7d"};
52+
for (String suggestion : allSuggestions) {
53+
if (suggestion.startsWith(trimmed)) {
54+
suggestions.add(suggestion);
55+
}
56+
}
57+
58+
return suggestions.isEmpty() ? List.of() : suggestions;
59+
}
60+
61+
public static List<String> getFilteredTimeSuggestions(String input) {
62+
List<String> allSuggestions = getTimeSuggestions(input);
63+
64+
if (input == null || input.isBlank()) {
65+
return allSuggestions;
66+
}
67+
68+
String lower = input.toLowerCase();
69+
return allSuggestions.stream()
70+
.filter(s -> s.startsWith(lower))
71+
.toList();
72+
}
73+
}

0 commit comments

Comments
 (0)