package org.strykeforce.thirdcoast.telemetry.tct.talon;

import com.ctre.CANTalon;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.inject.Inject;
import org.jline.reader.EndOfFileException;
import org.jline.reader.LineReader;
import org.jline.reader.UserInterruptException;
import org.strykeforce.thirdcoast.telemetry.tct.AbstractCommand;
import org.strykeforce.thirdcoast.telemetry.tct.Messages;

@ParametersAreNonnullByDefault
public class RunCommand extends AbstractCommand {

  public final static String NAME = "Run Selected Talons";
  private final TalonSet talonSet;

  @Inject
  RunCommand(TalonSet talonSet, LineReader reader) {
    super(NAME, reader);
    this.talonSet = talonSet;
  }

  @Override
  public void perform() {
    if (talonSet.selected().isEmpty()) {
      terminal.writer().println(Messages.NO_TALONS);
      return;
    }
    terminal.writer().println(Messages.bold("Enter motor setpoint, press <enter> to go back"));
    while (true) {
      String line;
      try {
        line = reader.readLine(Messages.prompt("setpoint or <return> to exit> ")).trim();
      } catch (EndOfFileException | UserInterruptException e) {
        continue;
      }

      if (line.isEmpty()) {
        return;
      }
      double setpoint;
      try {
        setpoint = Double.valueOf(line);
      } catch (NumberFormatException nfe) {
        terminal.writer().println(Messages.boldRed("please enter a number"));
        continue;
      }
      terminal.writer().print(Messages.bold(String.format("setting talons to %.2f%n", setpoint)));
      for (CANTalon talon : talonSet.selected()) {
        talon.set(setpoint);
      }
    }
  }
}

@@ -72,24 +72,108 @@
 e.CANTalon;%0A
+import edu.wpi.first.wpilibj.Timer;%0Aimport java.util.Arrays;%0Aimport java.util.List;%0A
 import javax
@@ -992,17 +992,17 @@
 s.bold(%22
-E
+e
 nter mot
@@ -1039,24 +1039,119 @@
 go back%22));%0A
+    terminal.writer().println(Messages.bold(%22optionally, enter setpoint, duration to pulse%22));%0A
     while (t
@@ -1454,95 +1454,436 @@
     
-try %7B%0A        setpoint = Double.valueOf(line);%0A      %7D catch (NumberFormatException nfe
+double duration;%0A      List%3CString%3E entries = Arrays.asList(line.split(%22,%22));%0A      try %7B%0A        if (entries.size() %3E 0) %7B%0A          setpoint = Double.valueOf(entries.get(0));%0A        %7D else %7B%0A          help();%0A          continue;%0A        %7D%0A        duration = entries.size() %3E 1 ? Double.valueOf(entries.get(1)) : -1;%0A      %7D catch (NumberFormatException nfe) %7B%0A        help();%0A        continue;%0A      %7D%0A%0A      if (duration %3E 0
 ) %7B%0A
@@ -1905,34 +1905,32 @@
 l.writer().print
-ln
 (Messages.boldRe
@@ -1926,171 +1926,545 @@
 ages
-.boldRed(%22please enter a number%22));%0A        continue;%0A      %7D%0A      terminal.writer().print(Messages.bold(String.format(%22setting talons to %25.2f%25n%22, setpoint)));%0A  
+%0A            .bold(String.format(%22setting talons to %25.2f for %25.2f sec%25n%22, setpoint, duration)));%0A        terminal.flush();%0A        double old = talonSet.selected().stream().findFirst().map(CANTalon::get).orElse(0.0);%0A        setTalons(setpoint);%0A        Timer.delay(duration);%0A        setTalons(old);%0A      %7D else %7B%0A        terminal.writer().print(Messages.bold(String.format(%22setting talons to %25.2f%25n%22, setpoint)));%0A        terminal.flush();%0A        setTalons(setpoint);%0A      %7D%0A    %7D%0A  %7D%0A%0A  protected void setTalons(double setpoint) %7B%0A
     
@@ -2514,18 +2514,16 @@
 %7B%0A      
-  
 talon.se
@@ -2539,24 +2539,168 @@
 t);%0A    
+%7D%0A
   %7D%0A
+%0A
   
-  %7D
+protected void help() %7B%0A    terminal.writer()%0A        .println(Messages.boldRed(%22please enter a number or two numbers separated by a commma%22));
 %0A  %7D%0A
+%0A
 %7D%0A

Add pulse run feature