Exemple #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());
 }
Exemple #2
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);
        });
  }