Example #1
0
 private String statusLine(Job job, ExecStatus status) {
   StringBuilder sb = new StringBuilder("[").append(job.id()).append("]");
   if (findJob() == job) {
     sb.append("+");
   }
   sb.append(" ")
       .append(Character.toUpperCase(status.name().charAt(0)))
       .append(job.status().name().substring(1).toLowerCase());
   sb.append(" ").append(job.line());
   return sb.toString();
 }
Example #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);
        });
  }