use of org.apache.felix.service.command.Job in project felix by apache.
the class Builtin method bg.
public void bg(CommandSession session, String[] argv) {
final String[] usage = { "bg - put job in background", "Usage: bg [OPTIONS] [jobid]", " -? --help show help" };
Process process = Process.Utils.current();
Options opt = Options.compile(usage).parse(argv);
if (opt.isSet("help")) {
opt.usage(process.err());
return;
}
if (opt.args().size() > 1) {
process.err().println("usage: bg [jobid]");
process.error(2);
return;
}
List<Job> jobs = new ArrayList<>(session.jobs());
Collections.reverse(jobs);
Job current = Job.Utils.current();
if (argv.length == 0) {
Job job = jobs.stream().filter(j -> j != current).findFirst().orElse(null);
if (job != null) {
job.background();
} else {
process.err().println("bg: no current job");
process.error(1);
}
} else {
Job job = jobs.stream().filter(j -> j != current && argv[0].equals(Integer.toString(j.id()))).findFirst().orElse(null);
if (job != null) {
job.background();
} else {
process.err().println("bg: job not found: " + argv[0]);
process.error(1);
}
}
}
use of org.apache.felix.service.command.Job in project felix by apache.
the class Builtin method jobs.
public void jobs(CommandSession session, String[] argv) {
final String[] usage = { "jobs - list jobs", "Usage: jobs [OPTIONS]", " -? --help show help" };
Process process = Process.Utils.current();
Options opt = Options.compile(usage).parse(argv);
if (opt.isSet("help")) {
opt.usage(process.err());
return;
}
if (!opt.args().isEmpty()) {
process.err().println("usage: jobs");
process.error(2);
return;
}
List<Job> jobs = session.jobs();
Job current = Job.Utils.current();
for (Job job : jobs) {
if (job != current) {
process.out().println("[" + job.id() + "] " + job.status().toString().toLowerCase() + " " + job.command());
}
}
}
use of org.apache.felix.service.command.Job in project felix by apache.
the class Builtin method fg.
public void fg(CommandSession session, String[] argv) {
final String[] usage = { "fg - put job in foreground", "Usage: fg [OPTIONS] [jobid]", " -? --help show help" };
Process process = Process.Utils.current();
Options opt = Options.compile(usage).parse(argv);
if (opt.isSet("help")) {
opt.usage(process.err());
return;
}
if (opt.args().size() > 1) {
process.err().println("usage: fg [jobid]");
process.error(2);
return;
}
List<Job> jobs = new ArrayList<>(session.jobs());
Collections.reverse(jobs);
Job current = Job.Utils.current();
if (argv.length == 0) {
Job job = jobs.stream().filter(j -> j != current).findFirst().orElse(null);
if (job != null) {
job.foreground();
} else {
process.err().println("fg: no current job");
process.error(1);
}
} else {
Job job = jobs.stream().filter(j -> j != current && argv[0].equals(Integer.toString(j.id()))).findFirst().orElse(null);
if (job != null) {
job.foreground();
} else {
process.err().println("fg: job not found: " + argv[0]);
process.error(1);
}
}
}
use of org.apache.felix.service.command.Job in project karaf by apache.
the class ConsoleSessionImpl method run.
public void run() {
try {
threadIO.setStreams(session.getKeyboard(), out, err);
thread = Thread.currentThread();
running = true;
welcomeBanner();
AtomicBoolean reading = new AtomicBoolean();
session.setJobListener((job, previous, current) -> {
if (previous == Status.Background || current == Status.Background || previous == Status.Suspended || current == Status.Suspended) {
int width = terminal.getWidth();
String status = current.name().toLowerCase();
jlineTerminal.writer().write(getStatusLine(job, width, status));
jlineTerminal.flush();
if (reading.get()) {
reader.callWidget(LineReader.REDRAW_LINE);
reader.callWidget(LineReader.REDISPLAY);
}
}
});
jlineTerminal.handle(Signal.TSTP, s -> {
Job current = session.foregroundJob();
if (current != null) {
current.suspend();
}
});
jlineTerminal.handle(Signal.INT, s -> {
Job current = session.foregroundJob();
if (current != null) {
current.interrupt();
}
});
String scriptFileName = System.getProperty(SHELL_INIT_SCRIPT);
executeScript(scriptFileName);
while (running) {
CharSequence command = readCommand(reading);
if (command == null) {
break;
}
if (command.length() > 0) {
doExecute(command);
}
}
close();
} finally {
try {
threadIO.close();
} catch (Throwable t) {
// Ignore
}
}
}
use of org.apache.felix.service.command.Job in project felix by apache.
the class Shell method runShell.
private Object runShell(final CommandSession session, Terminal terminal, LineReader reader) throws InterruptedException {
AtomicBoolean reading = new AtomicBoolean();
session.setJobListener((job, previous, current) -> {
if (previous == Status.Background || current == Status.Background || previous == Status.Suspended || current == Status.Suspended) {
int width = terminal.getWidth();
String status = current.name().toLowerCase();
terminal.writer().write(getStatusLine(job, width, status));
terminal.flush();
if (reading.get() && !stopping.get()) {
reader.callWidget(LineReader.REDRAW_LINE);
reader.callWidget(LineReader.REDISPLAY);
}
}
});
SignalHandler intHandler = terminal.handle(Signal.INT, s -> {
Job current = session.foregroundJob();
if (current != null) {
current.interrupt();
}
});
SignalHandler suspHandler = terminal.handle(Signal.TSTP, s -> {
Job current = session.foregroundJob();
if (current != null) {
current.suspend();
}
});
Object result = null;
try {
while (!stopping.get()) {
try {
reading.set(true);
try {
String prompt = Shell.getPrompt(session);
String rprompt = Shell.getRPrompt(session);
if (stopping.get()) {
break;
}
reader.readLine(prompt, rprompt, null, null);
} finally {
reading.set(false);
}
ParsedLine parsedLine = reader.getParsedLine();
if (parsedLine == null) {
throw new EndOfFileException();
}
try {
result = session.execute(((ParsedLineImpl) parsedLine).program());
// set $_ to last result
session.put(Shell.VAR_RESULT, result);
if (result != null && !Boolean.FALSE.equals(session.get(".Gogo.format"))) {
System.out.println(session.format(result, Converter.INSPECT));
}
} catch (Exception e) {
AttributedStringBuilder sb = new AttributedStringBuilder();
sb.style(sb.style().foreground(AttributedStyle.RED));
sb.append(e.toString());
sb.style(sb.style().foregroundDefault());
terminal.writer().println(sb.toAnsi(terminal));
terminal.flush();
session.put(Shell.VAR_EXCEPTION, e);
}
waitJobCompletion(session);
} catch (UserInterruptException e) {
// continue;
} catch (EndOfFileException e) {
reader.getHistory().save();
break;
}
}
} finally {
terminal.handle(Signal.INT, intHandler);
terminal.handle(Signal.TSTP, suspHandler);
}
return result;
}
Aggregations