From 0c67927debf8073aa06e55e54685910039b1bbcf Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Sun, 23 Oct 2022 18:35:25 +0200 Subject: [PATCH] [lgwebos] Console command completion Signed-off-by: Laurent Garnier --- .../console/LGWebOSCommandExtension.java | 49 ++++++++++++++++--- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/console/LGWebOSCommandExtension.java b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/console/LGWebOSCommandExtension.java index 7ca1efbb04b87..11c54e0ccada7 100644 --- a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/console/LGWebOSCommandExtension.java +++ b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/console/LGWebOSCommandExtension.java @@ -14,10 +14,15 @@ import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.binding.lgwebos.internal.LGWebOSBindingConstants; import org.openhab.binding.lgwebos.internal.handler.LGWebOSHandler; 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.Thing; @@ -36,11 +41,13 @@ @NonNullByDefault @Component(service = ConsoleCommandExtension.class) -public class LGWebOSCommandExtension extends AbstractConsoleCommandExtension { +public class LGWebOSCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter { private static final String APPLICATIONS = "applications"; private static final String CHANNELS = "channels"; private static final String ACCESS_KEY = "accesskey"; + private static final StringsCompleter SUBCMD_COMPLETER = new StringsCompleter( + List.of(APPLICATIONS, CHANNELS, ACCESS_KEY), false); private final ThingRegistry thingRegistry; @@ -53,13 +60,7 @@ public LGWebOSCommandExtension(final @Reference ThingRegistry thingRegistry) { @Override public void execute(String[] args, Console console) { if (args.length == 2) { - Thing thing = null; - try { - ThingUID thingUID = new ThingUID(args[0]); - thing = thingRegistry.get(thingUID); - } catch (IllegalArgumentException e) { - thing = null; - } + Thing thing = getThing(args[0]); ThingHandler thingHandler = null; LGWebOSHandler handler = null; if (thing != null) { @@ -104,4 +105,36 @@ public List getUsages() { buildCommandUsage(" " + CHANNELS, "list channels"), buildCommandUsage(" " + ACCESS_KEY, "show the access key") }); } + + @Override + public @Nullable ConsoleCommandCompleter getCompleter() { + return this; + } + + @Override + public boolean complete(String[] args, int cursorArgumentIndex, int cursorPosition, List candidates) { + if (cursorArgumentIndex <= 0) { + return new StringsCompleter(thingRegistry.getAll().stream() + .filter(t -> LGWebOSBindingConstants.THING_TYPE_WEBOSTV.equals(t.getThingTypeUID())) + .map(t -> t.getUID().getAsString()).collect(Collectors.toList()), true).complete(args, + cursorArgumentIndex, cursorPosition, candidates); + } else if (cursorArgumentIndex == 1) { + Thing thing = getThing(args[0]); + if (thing != null && LGWebOSBindingConstants.THING_TYPE_WEBOSTV.equals(thing.getThingTypeUID())) { + return SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates); + } + } + return false; + } + + private @Nullable Thing getThing(String uid) { + Thing thing = null; + try { + ThingUID thingUID = new ThingUID(uid); + thing = thingRegistry.get(thingUID); + } catch (IllegalArgumentException e) { + thing = null; + } + return thing; + } }