mirror of
https://github.com/danieldemus/openhab-core.git
synced 2026-07-29 12:34:22 +02:00
Add command completion (#4063)
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
+41
@@ -15,16 +15,21 @@ package org.openhab.core.config.discovery.internal.console;
|
||||
import static org.openhab.core.config.discovery.inbox.InboxPredicates.*;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.core.config.discovery.DiscoveryResult;
|
||||
import org.openhab.core.config.discovery.DiscoveryResultFlag;
|
||||
import org.openhab.core.config.discovery.inbox.Inbox;
|
||||
import org.openhab.core.config.discovery.internal.PersistentInbox;
|
||||
import org.openhab.core.io.console.Console;
|
||||
import org.openhab.core.io.console.ConsoleCommandCompleter;
|
||||
import org.openhab.core.io.console.StringsCompleter;
|
||||
import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
|
||||
import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
|
||||
import org.openhab.core.thing.ThingTypeUID;
|
||||
@@ -50,8 +55,39 @@ public class InboxConsoleCommandExtension extends AbstractConsoleCommandExtensio
|
||||
private static final String SUBCMD_CLEAR = "clear";
|
||||
private static final String SUBCMD_REMOVE = "remove";
|
||||
|
||||
private static final StringsCompleter SUBCMD_COMPLETER = new StringsCompleter(
|
||||
List.of(SUBCMD_APPROVE, SUBCMD_IGNORE, SUBCMD_LIST, SUBCMD_LIST_IGNORED, SUBCMD_CLEAR, SUBCMD_REMOVE),
|
||||
false);
|
||||
|
||||
private final Inbox inbox;
|
||||
|
||||
private class InboxConsoleCommandCompleter implements ConsoleCommandCompleter {
|
||||
@Override
|
||||
public boolean complete(String[] args, int cursorArgumentIndex, int cursorPosition, List<String> candidates) {
|
||||
if (cursorArgumentIndex <= 0) {
|
||||
return SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
|
||||
} else if (cursorArgumentIndex == 1) {
|
||||
if (SUBCMD_IGNORE.equalsIgnoreCase(args[0]) || SUBCMD_APPROVE.equalsIgnoreCase(args[0])) {
|
||||
return new StringsCompleter(getThingUIDs(), true).complete(args, cursorArgumentIndex,
|
||||
cursorPosition, candidates);
|
||||
} else if (SUBCMD_REMOVE.equalsIgnoreCase(args[0])) {
|
||||
return new StringsCompleter(getThingAndThingTypeUIDs(), true).complete(args, cursorArgumentIndex,
|
||||
cursorPosition, candidates);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private Collection<String> getThingUIDs() {
|
||||
return inbox.stream().map(r -> r.getThingUID().getAsString()).toList();
|
||||
}
|
||||
|
||||
private Collection<String> getThingAndThingTypeUIDs() {
|
||||
return inbox.stream()
|
||||
.flatMap(r -> Stream.of(r.getThingUID().getAsString(), r.getThingTypeUID().getAsString())).toList();
|
||||
}
|
||||
}
|
||||
|
||||
@Activate
|
||||
public InboxConsoleCommandExtension(final @Reference Inbox inbox) {
|
||||
super("inbox", "Manage your inbox.");
|
||||
@@ -203,4 +239,9 @@ public class InboxConsoleCommandExtension extends AbstractConsoleCommandExtensio
|
||||
"remove the inbox entries of a given thing id or thing type"),
|
||||
buildCommandUsage(SUBCMD_IGNORE + " <thingUID>", "ignores an inbox entry permanently"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable ConsoleCommandCompleter getCompleter() {
|
||||
return new InboxConsoleCommandCompleter();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user