001 /*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019 package org.crsh.shell.impl.command.system;
020
021 import org.crsh.cli.Argument;
022 import org.crsh.cli.Command;
023 import org.crsh.cli.Usage;
024 import org.crsh.cli.descriptor.ParameterDescriptor;
025 import org.crsh.cli.spi.Completion;
026 import org.crsh.command.BaseCommand;
027 import org.crsh.command.InvocationContext;
028 import org.crsh.command.ScriptException;
029 import org.crsh.lang.script.ScriptRepl;
030 import org.crsh.repl.Repl;
031 import org.crsh.shell.impl.command.CRaSHSession;
032 import org.crsh.text.Color;
033 import org.crsh.text.Decoration;
034 import org.crsh.text.Style;
035 import org.crsh.text.ui.LabelElement;
036 import org.crsh.text.ui.RowElement;
037 import org.crsh.text.ui.TableElement;
038
039 import java.io.IOException;
040 import java.util.ArrayList;
041
042 /** @author Julien Viet */
043 public class repl extends BaseCommand implements ReplCompleter {
044
045 @Usage("list the repl or change the current repl")
046 @Command
047 public void main(
048 InvocationContext<Object> context,
049 @Argument(completer = ReplCompleter.class)
050 @Usage("the optional repl name")
051 String name) throws IOException {
052 CRaSHSession session = (CRaSHSession)context.getSession();
053 Repl current = session.getRepl();
054 if (name != null) {
055 if (name.equals(current.getName())) {
056 context.provide("Using repl " + name);
057 } else {
058 Repl found = null;
059 if ("script".equals(name)) {
060 found = ScriptRepl.getInstance();
061 } else {
062 for (Repl repl : session.crash.getContext().getPlugins(Repl.class)) {
063 if (repl.getName().equals(name)) {
064 if (repl.isActive()) {
065 found = repl;
066 break;
067 } else {
068 throw new ScriptException("Repl " + name + " is not active");
069 }
070 }
071 }
072 }
073 if (found != null) {
074 session.setRepl(found);
075 context.provide("Using repl " + name);
076 } else {
077 throw new ScriptException("Repl " + name + " not found");
078 }
079 }
080 } else {
081
082 //
083 ArrayList<Repl> repls = new ArrayList<Repl>();
084 repls.add(ScriptRepl.getInstance());
085 for (Repl repl : session.crash.getContext().getPlugins(Repl.class)) {
086 repls.add(repl);
087 }
088
089 //
090 TableElement table = new TableElement().rightCellPadding(1);
091 table.add(
092 new RowElement().
093 add(new LabelElement("NAME").style(Style.style(Decoration.bold))).
094 add(new LabelElement("DESCRIPTION")).
095 add(new LabelElement("ACTIVE")));
096 for (Repl repl : repls) {
097 table.add(
098 new RowElement().
099 add(new LabelElement(repl.getName()).style(Style.style(Color.red))).
100 add(new LabelElement(repl.getDescription())).
101 add(new LabelElement(repl.isActive())));
102 }
103
104 //
105 context.provide(new LabelElement("Current repl is \" + current.getName() + \"available repl are:\n"));
106 context.provide(table);
107 }
108 }
109
110 @Override
111 public Completion complete(ParameterDescriptor parameter, String prefix) throws Exception {
112 CRaSHSession session = (CRaSHSession)context.getSession();
113 Completion.Builder builder = Completion.builder(prefix);
114 if ("script".startsWith(prefix)) {
115 builder.add("script".substring(prefix.length()), true);
116 }
117 for (Repl repl : session.crash.getContext().getPlugins(Repl.class)) {
118 String name = repl.getName();
119 if (name.startsWith(prefix)) {
120 builder.add(name.substring(prefix.length()), true);
121 }
122 }
123 return builder.build();
124 }
125 }