All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.jy2.commandline.picocli.tf.completion.TfChildPositionalCompletionCandidates Maven / Gradle / Ivy

There is a newer version: 0.0.39
Show newest version
package com.github.jy2.commandline.picocli.tf.completion;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.function.Consumer;

import com.github.jy2.Subscriber;
import com.github.jy2.commandline.picocli.Main;
import com.github.jy2.tf.mat.TfManager;

import go.jyroscope.ros.geometry_msgs.TransformStamped;
import go.jyroscope.ros.tf2_msgs.TFMessage;
import picocli.AutoComplete;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Model.OptionSpec;
import picocli.CommandLine.Model.PositionalParamSpec;

public class TfChildPositionalCompletionCandidates implements Iterable {

	@Override
	public Iterator iterator() {
		ArrayList list = new ArrayList<>();
		if (AutoComplete.argIndex != getTfChildIndex()) {
			return list.iterator();
		}
		Subscriber subscriber = null;
		try {
			subscriber = Main.di.createSubscriber("/tf", TFMessage.class);
			TfManager tfManager = new TfManager();
			subscriber.addMessageListener(new Consumer() {
				@Override
				public void accept(TFMessage t) {
					try {
						tfManager.add(t);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
			try {
				// static transforms usually published every 1000ms, so wait 500ms more
				Thread.sleep(1500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			//subscriber.removeAllMessageListeners();
			subscriber.shutdown();
			ArrayList tfList = tfManager.getTransformList();
			for (TransformStamped t : tfList) {
				list.add(t.childFrameId);
			}
		} finally {
			if (subscriber != null) {
				//subscriber.removeAllMessageListeners();
				subscriber.shutdown();
			}
		}
		list.sort(String::compareToIgnoreCase);
		return list.iterator();
	}

	private int getTfChildIndex() {
		if (AutoComplete.tentativeMatch == null) {
			return -1;
		}
		int i = 0;
		for (Object obj : AutoComplete.tentativeMatch) {
			if (obj instanceof CommandSpec) { // subcommand
			} else if (obj instanceof OptionSpec) { // option
			} else if (obj instanceof PositionalParamSpec) { // positional
				PositionalParamSpec pos = (PositionalParamSpec) obj;
				if (pos.index().min == 1) {
					return i;
				}
			}
			i++;
		}
		return -1;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy