예제 #1
0
 @Override
 public synchronized Job createJob(List<CliToken> args) {
   StringBuilder line = new StringBuilder();
   args.stream().map(CliToken::raw).forEach(line::append);
   Process process = commandManager.createProcess(args);
   return jobController.createJob(process, line.toString());
 }
예제 #2
0
 public void close() {
   if (term != null) {
     term.close();
   } else {
     jobController.close(ar -> closedFuture.complete());
   }
 }
예제 #3
0
 private Job findJob() {
   Job foregroundJob = jobController.foregroundJob();
   return jobController()
       .jobs()
       .stream()
       .filter(job -> job != foregroundJob)
       .sorted((j1, j2) -> ((Long) j1.lastStopped()).compareTo(j2.lastStopped()))
       .findFirst()
       .orElse(null);
 }
예제 #4
0
  public ShellImpl init() {

    term.interruptHandler(key -> jobController().foregroundJob().interrupt());

    term.suspendHandler(
        key -> {
          term.echo(Helper.fromCodePoints(new int[] {key, '\n'}));
          Job job = jobController.foregroundJob();
          term.echo(statusLine(job, ExecStatus.STOPPED) + "\n");
          job.suspend();
          return true;
        });

    term.closeHandler(v -> jobController.close(ar -> closedFuture.complete()));
    if (welcome != null && welcome.length() > 0) {
      term.write(welcome);
    }
    return this;
  }
예제 #5
0
  public ShellImpl(Term term, InternalCommandManager commandManager) {

    session.put("vert.x-command-manager", commandManager);

    this.id = UUID.randomUUID().toString();
    this.jobController = new JobControllerImpl();
    this.commandManager = commandManager;
    this.closedFuture = Future.future();
    this.term = term;

    if (term != null) {
      term.setSession(session);
      jobController.foregroundUpdatedHandler(
          job -> {
            if (job == null) {
              readline();
            }
          });
    }
  }
예제 #6
0
  public void readline() {
    term.readline(
        "% ",
        line -> {
          if (line == null) {
            // EOF
            term.close();
            return;
          }

          List<CliToken> tokens = CliToken.tokenize(line);

          if (tokens.stream().filter(CliToken::isText).count() == 0) {
            // For now do like this
            ShellImpl.this.readline();
            return;
          }

          Optional<CliToken> first = tokens.stream().filter(CliToken::isText).findFirst();
          if (first.isPresent()) {
            String name = first.get().value();
            switch (name) {
              case "exit":
              case "logout":
                term.close();
                return;
              case "jobs":
                jobController
                    .jobs()
                    .forEach(
                        job -> {
                          String statusLine = statusLine(job, job.status()) + "\n";
                          term.write(statusLine);
                        });
                readline();
                return;
              case "fg":
                {
                  Job job = findJob();
                  if (job == null) {
                    term.write("no such job\n");
                    readline();
                  } else {
                    if (job.status() == ExecStatus.STOPPED) {
                      job.resume(true);
                    } else {
                      job.toForeground();
                    }
                  }
                  return;
                }
              case "bg":
                {
                  Job job = findJob();
                  if (job == null) {
                    term.write("no such job\n");
                    readline();
                  } else {
                    if (job.status() == ExecStatus.STOPPED) {
                      job.resume(false);
                      term.echo(statusLine(job, ExecStatus.RUNNING) + "\n");
                      readline();
                    } else {
                      term.write("job " + job.id() + " already in background\n");
                      readline();
                    }
                  }
                  return;
                }
            }
          }

          Job job;
          try {
            job = createJob(tokens);
          } catch (Exception e) {
            term.echo(e.getMessage() + "\n");
            readline();
            return;
          }
          job.setTty(term);
          job.setSession(session);
          job.statusUpdateHandler(
              status -> {
                if (status == ExecStatus.RUNNING && job == jobController.foregroundJob()) {
                  term.echo(job.line() + "\n");
                }
              });
          job.run();
        },
        completion -> {
          commandManager.complete(completion);
        });
  }