<commit_before>package com.spaceapplications.yamcs.scpi.commander;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class DeviceConnect extends Command {
  private List<Device> devices = new ArrayList<>();

  public interface Device {
    public String id();

    public void open();

    public void close();

    public String exec(String cmd);
  }

  public DeviceConnect(String cmd, String description, HasContext context, List<Device> devices) {
    super(cmd, description, context);
    this.devices = devices;
  }

  @Override
  String handleExecute(String deviceId) {
    Optional<Device> device = findDevice(deviceId);
    if (!device.isPresent())
      return deviceId + ": device not found";
    device.get().open();
    String prompt = "device:" + deviceId + Command.DEFAULT_PROMPT;
    setPrompt(prompt);
    Command parent = this;
    Command contextCmd = new Command("", "", context) {
      @Override
      String handleExecute(String cmd) {
        if (Commander.isCtrlD(cmd))
          return disconnect(deviceId, parent, device.get());
        return device.get().exec(cmd);
      }
    };
    contextCmd.setPrompt(prompt);
    context.setContextCmd(contextCmd);
    return "connect to: " + deviceId;
  }

  private Optional<Device> findDevice(String deviceId) {
    return devices.stream().filter(d -> deviceId.equals(d.id())).findFirst();
  }

  private String disconnect(String deviceId, Command parent, Device device) {
    setPrompt(Command.DEFAULT_PROMPT);
    parent.setPrompt(Command.DEFAULT_PROMPT);
    context.clearContextCmd();
    device.close();
    return "\ndisconnect from " + deviceId;
  }
}<commit_msg>Exiting context correctly clears prompt<commit_after>package com.spaceapplications.yamcs.scpi.commander;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class DeviceConnect extends Command {
  private List<Device> devices = new ArrayList<>();

  public interface Device {
    public String id();

    public void open();

    public void close();

    public String exec(String cmd);
  }

  public DeviceConnect(String cmd, String description, HasContext context, List<Device> devices) {
    super(cmd, description, context);
    this.devices = devices;
  }

  @Override
  String handleExecute(String deviceId) {
    Optional<Device> device = findDevice(deviceId);
    if (!device.isPresent())
      return deviceId + ": device not found";
    device.get().open();
    String prompt = "device:" + deviceId + Command.DEFAULT_PROMPT;
    setPrompt(prompt);
    Command parent = this;

    Command contextCmd = new Command("", "", context) {
      @Override
      String handleExecute(String cmd) {
        if (Commander.isCtrlD(cmd)) {
          setPrompt(Command.DEFAULT_PROMPT);
          parent.setPrompt(Command.DEFAULT_PROMPT);
          context.clearContextCmd();
          device.get().close();
          return "\ndisconnect from " + deviceId;
        }
        return device.get().exec(cmd);
      }
    };
    
    contextCmd.setPrompt(prompt);
    context.setContextCmd(contextCmd);
    return "connect to: " + deviceId;
  }

  private Optional<Device> findDevice(String deviceId) {
    return devices.stream().filter(d -> deviceId.equals(d.id())).findFirst();
  }
}
{'fraction_non_alphanumeric': 0.11998784933171325, 'fraction_numerical': 0.0, 'mean_word_length': 3.462059620596206, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2895252', 'n_tokens_mistral': 949, 'n_tokens_neox': 919, 'n_words': 228}