Search in sources :

Example 6 with Process

use of org.apache.felix.service.command.Process in project felix by apache.

the class Builtin method setopt.

public void setopt(CommandSession session, String[] argv) {
    Process process = Process.Utils.current();
    Commands.setopt(Shell.getReader(session), process.out(), process.err(), argv);
}
Also used : Process(org.apache.felix.service.command.Process)

Example 7 with Process

use of org.apache.felix.service.command.Process in project felix by apache.

the class Builtin method widget.

public void widget(final CommandSession session, String[] argv) throws Exception {
    java.util.function.Function<String, Widget> creator = func -> () -> {
        try {
            session.execute(func);
        } catch (Exception e) {
            // TODO: log exception ?
            return false;
        }
        return true;
    };
    Process process = Process.Utils.current();
    Commands.widget(Shell.getReader(session), process.out(), process.err(), creator, argv);
}
Also used : Job(org.apache.felix.service.command.Job) Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Commands(org.jline.builtins.Commands) Shell.getCommands(org.apache.felix.gogo.jline.Shell.getCommands) CommandSession(org.apache.felix.service.command.CommandSession) ParsedLine(org.jline.reader.ParsedLine) Constructor(java.lang.reflect.Constructor) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Process(org.apache.felix.service.command.Process) Matcher(java.util.regex.Matcher) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) Method(java.lang.reflect.Method) Path(java.nio.file.Path) FilesCompleter(org.jline.builtins.Completers.FilesCompleter) LinkedHashSet(java.util.LinkedHashSet) Options(org.jline.builtins.Options) Terminal(org.jline.terminal.Terminal) PrintStream(java.io.PrintStream) DirectoriesCompleter(org.jline.builtins.Completers.DirectoriesCompleter) LineReader(org.jline.reader.LineReader) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) OpenOption(java.nio.file.OpenOption) StringWriter(java.io.StringWriter) StandardOpenOption(java.nio.file.StandardOpenOption) Candidate(org.jline.reader.Candidate) Set(java.util.Set) IOException(java.io.IOException) Field(java.lang.reflect.Field) InputStreamReader(java.io.InputStreamReader) StandardCharsets(java.nio.charset.StandardCharsets) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) Converter(org.apache.felix.service.command.Converter) TreeMap(java.util.TreeMap) Widget(org.jline.reader.Widget) Entry(java.util.Map.Entry) Function(org.apache.felix.service.command.Function) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) CommandSessionImpl(org.apache.felix.gogo.runtime.CommandSessionImpl) Widget(org.jline.reader.Widget) Process(org.apache.felix.service.command.Process) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 8 with Process

use of org.apache.felix.service.command.Process 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);
        }
    }
}
Also used : Options(org.jline.builtins.Options) ArrayList(java.util.ArrayList) Process(org.apache.felix.service.command.Process) Job(org.apache.felix.service.command.Job)

Example 9 with Process

use of org.apache.felix.service.command.Process in project felix by apache.

the class Posix method less.

protected void less(CommandSession session, Process process, String[] argv) throws Exception {
    String[] usage = { "less -  file pager", "Usage: less [OPTIONS] [FILES]", "  -? --help                    Show help", "  -e --quit-at-eof             Exit on second EOF", "  -E --QUIT-AT-EOF             Exit on EOF", "  -F --quit-if-one-screen      Exit if entire file fits on first screen", "  -q --quiet --silent          Silent mode", "  -Q --QUIET --SILENT          Completely  silent", "  -S --chop-long-lines         Do not fold long lines", "  -i --ignore-case             Search ignores lowercase case", "  -I --IGNORE-CASE             Search ignores all case", "  -x --tabs                    Set tab stops", "  -N --LINE-NUMBERS            Display line number for each line", "     --no-init                 Disable terminal initialization", "     --no-keypad               Disable keypad handling" };
    boolean hasExtendedOptions = false;
    try {
        Less.class.getField("quitIfOneScreen");
        hasExtendedOptions = true;
    } catch (NoSuchFieldException e) {
        List<String> ustrs = new ArrayList<>(Arrays.asList(usage));
        ustrs.removeIf(s -> s.contains("--quit-if-one-screen") || s.contains("--no-init") || s.contains("--no-keypad"));
        usage = ustrs.toArray(new String[ustrs.size()]);
    }
    Options opt = parseOptions(session, usage, argv);
    List<Source> sources = new ArrayList<>();
    if (opt.args().isEmpty()) {
        opt.args().add("-");
    }
    for (String arg : opt.args()) {
        if ("-".equals(arg)) {
            sources.add(new StdInSource(process));
        } else {
            sources.add(new PathSource(session.currentDir().resolve(arg), arg));
        }
    }
    if (!process.isTty(1)) {
        for (Source source : sources) {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(source.read()))) {
                cat(process, reader, opt.isSet("LINE-NUMBERS"));
            }
        }
        return;
    }
    Less less = new Less(Shell.getTerminal(session));
    less.quitAtFirstEof = opt.isSet("QUIT-AT-EOF");
    less.quitAtSecondEof = opt.isSet("quit-at-eof");
    less.quiet = opt.isSet("quiet");
    less.veryQuiet = opt.isSet("QUIET");
    less.chopLongLines = opt.isSet("chop-long-lines");
    less.ignoreCaseAlways = opt.isSet("IGNORE-CASE");
    less.ignoreCaseCond = opt.isSet("ignore-case");
    if (opt.isSet("tabs")) {
        less.tabs = opt.getNumber("tabs");
    }
    less.printLineNumbers = opt.isSet("LINE-NUMBERS");
    if (hasExtendedOptions) {
        Less.class.getField("quitIfOneScreen").set(less, opt.isSet("quit-if-one-screen"));
        Less.class.getField("noInit").set(less, opt.isSet("no-init"));
        Less.class.getField("noKeypad").set(less, opt.isSet("no-keypad"));
    }
    less.run(sources);
}
Also used : Arrays(java.util.Arrays) PathSource(org.jline.builtins.Source.PathSource) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) IntConsumer(java.util.function.IntConsumer) FileTime(java.nio.file.attribute.FileTime) IntBinaryOperator(java.util.function.IntBinaryOperator) CommandSession(org.apache.felix.service.command.CommandSession) AttributedString(org.jline.utils.AttributedString) WatchKey(java.nio.file.WatchKey) Matcher(java.util.regex.Matcher) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Context(org.apache.felix.gogo.jline.Shell.Context) Path(java.nio.file.Path) EnumSet(java.util.EnumSet) Options(org.jline.builtins.Options) TTop(org.jline.builtins.TTop) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) Predicate(java.util.function.Predicate) Set(java.util.Set) Source(org.jline.builtins.Source) Reader(java.io.Reader) Instant(java.time.Instant) OSUtils(org.jline.utils.OSUtils) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) Executors(java.util.concurrent.Executors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Pattern(java.util.regex.Pattern) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Capability(org.jline.utils.InfoCmp.Capability) Commands(org.jline.builtins.Commands) SimpleDateFormat(java.text.SimpleDateFormat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Attributes(org.jline.terminal.Attributes) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Process(org.apache.felix.service.command.Process) LinkOption(java.nio.file.LinkOption) FilterInputStream(java.io.FilterInputStream) StandardWatchEventKinds(java.nio.file.StandardWatchEventKinds) PosixFilePermissions(java.nio.file.attribute.PosixFilePermissions) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Nano(org.jline.builtins.Nano) Terminal(org.jline.terminal.Terminal) OutputStream(java.io.OutputStream) PrintStream(java.io.PrintStream) CommandProcessor(org.apache.felix.service.command.CommandProcessor) AttributedStyle(org.jline.utils.AttributedStyle) Files(java.nio.file.Files) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) WatchService(java.nio.file.WatchService) TreeMap(java.util.TreeMap) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder) Closeable(java.io.Closeable) DateTimeFormatter(java.time.format.DateTimeFormatter) BufferedReader(java.io.BufferedReader) Comparator(java.util.Comparator) Less(org.jline.builtins.Less) Collections(java.util.Collections) InputStream(java.io.InputStream) Options(org.jline.builtins.Options) InputStreamReader(java.io.InputStreamReader) PathSource(org.jline.builtins.Source.PathSource) ArrayList(java.util.ArrayList) AttributedString(org.jline.utils.AttributedString) PathSource(org.jline.builtins.Source.PathSource) Source(org.jline.builtins.Source) BufferedReader(java.io.BufferedReader) List(java.util.List) ArrayList(java.util.ArrayList) Less(org.jline.builtins.Less)

Example 10 with Process

use of org.apache.felix.service.command.Process in project felix by apache.

the class Builtin method complete.

public void complete(CommandSession session, String[] argv) {
    Process process = Process.Utils.current();
    Commands.complete(Shell.getReader(session), process.out(), process.err(), Shell.getCompletions(session), argv);
}
Also used : Process(org.apache.felix.service.command.Process)

Aggregations

Process (org.apache.felix.service.command.Process)16 Options (org.jline.builtins.Options)10 ArrayList (java.util.ArrayList)6 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 Path (java.nio.file.Path)5 HashSet (java.util.HashSet)5 TreeMap (java.util.TreeMap)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 IOException (java.io.IOException)4 PrintStream (java.io.PrintStream)4 Files (java.nio.file.Files)4 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 List (java.util.List)4 Map (java.util.Map)4 Set (java.util.Set)4 Job (org.apache.felix.service.command.Job)4 Closeable (java.io.Closeable)3