The CommandCall class is used to parse a command from a text line to give easy access to its parameters.
Esempio n. 1
0
        public override string[] AutoComplete(CommandCall call, int paramIndex)
        {
            List<string> result = new List<string>();
            switch (paramIndex)
            {
                case 0:
                    if (string.IsNullOrEmpty(call.Parameters[0]) || call.Parameters[0].Length <= 1)
                    {
                        return new string[] { "-r", "-a", "-l", "-u" };
                    }

                    break;
                case 1:
                    switch (call.Parameters[0])
                    {
                        case "-l":
                            foreach (ExtensionAttribute info in extManager.AvailableExtensions)
                            {
                                if (string.IsNullOrEmpty(call.Parameters[1]) || info.Id.StartsWith(call.Parameters[1]))
                                {
                                    result.Add(info.Id + "\n");
                                }
                            }

                            if (result.Count > 0)
                            {
                                string lastComplete = result[result.Count-1];
                                lastComplete = lastComplete.Remove(lastComplete.Length-1);
                                result[result.Count-1] = lastComplete;
                                return result.ToArray();
                            }

                            break;
                        case "-u":
                            foreach (string id in extManager.Keys)
                            {
                                if (string.IsNullOrEmpty(call.Parameters[1]) || id.StartsWith(call.Parameters[1]))
                                {
                                    result.Add(id + "\n");
                                }
                            }

                            if (result.Count > 0)
                            {
                                return result.ToArray();
                            }

                            break;
                    }

                    break;
            }

            return base.AutoComplete(call, paramIndex);
        }
Esempio n. 2
0
        public void Constructor()
        {
            CommandCall call;
            try
            {
                call = new CommandCall(" ");
                Assert.Fail("We shouldn't be able to pass an empty string.");
            }
            catch (Exception)
            {
                // TODO: Should add some action here.
            }

            call = new CommandCall("testline");
            Assert.NotNull(call);
            Assert.AreEqual("testline", call.CommandName);
            Assert.AreEqual(0, call.Parameters.Length);

            call = new CommandCall("testline with parameters");
            Assert.AreEqual("testline", call.CommandName);
            Assert.AreEqual(2, call.Parameters.Length);
            Assert.AreEqual("with", call.Parameters[0]);
            Assert.AreEqual("parameters", call.Parameters[1]);

            call = new CommandCall("another testline with parameters and \"multi word\" parameters");
            Assert.AreEqual("another", call.CommandName);
            Assert.AreEqual(6, call.Parameters.Length);
            Assert.AreEqual("testline", call.Parameters[0]);
            Assert.AreEqual("with", call.Parameters[1]);
            Assert.AreEqual("parameters", call.Parameters[2]);
            Assert.AreEqual("and", call.Parameters[3]);
            Assert.AreEqual("multi word", call.Parameters[4]);
            Assert.AreEqual("parameters", call.Parameters[5]);

            call = new CommandCall("testline with \\\" and \"multi word \\\"\"");
            Assert.AreEqual("testline", call.CommandName);
            Assert.AreEqual(4, call.Parameters.Length);
            Assert.AreEqual("with", call.Parameters[0]);
            Assert.AreEqual("\"", call.Parameters[1]);
            Assert.AreEqual("and", call.Parameters[2]);
            Assert.AreEqual("multi word \"", call.Parameters[3]);
        }
Esempio n. 3
0
        /// <summary>
        /// Autocompletes networknames on deletion of networks.
        /// </summary>
        /// <param name="call">The current line.</param>
        /// <param name="paramIndex">The parameter where the cursor stand on.</param>
        /// <returns></returns>
        public override string[] AutoComplete(CommandCall call, int paramIndex)
        {
            switch (paramIndex)
            {
                case 0:
                    if (string.IsNullOrEmpty(call.Parameters[0]) || call.Parameters[0] == "-")
                    {
                        return new string[] { "-d", "-a" };
                    }

                    break;
                case 1:
                    switch (call.Parameters[0])
                    {
                        case "-d":
                        case "--delete":
                            List<string> completitions = new List<string>();
                            foreach (INetwork n in chatting.Networks)
                            {
                                if (!string.IsNullOrEmpty(call.Parameters[1]) && n.Name.StartsWith(call.Parameters[1]))
                                {
                                    continue;
                                }

                                completitions.Add(n.Name);
                            }

                            if (completitions.Count > 0)
                            {
                                return completitions.ToArray();
                            }

                            break;
                    }

                    break;
            }

            return base.AutoComplete(call, paramIndex);
        }
Esempio n. 4
0
        /// <summary>
        /// Autocompletes the word, the cursor is currently on.
        /// </summary>
        private Completion AutoComplete(string text, int cursor)
        {
            if (text.Length == 0)
            {
                return new Completion(text, null);
            }

            CommandCall call = new CommandCall(text.Substring(0, cursor));
            string[] result = null;
            string prefix = "";
            if (call.Parameters.Length == 0)
            {
                List<string> list = new List<string>();
                prefix = call.CommandName;
                foreach (Mono.Addins.TypeExtensionNode<TerminalCommandAttribute> cmd in commands)
                {
                    if (cmd.Data.Name.StartsWith(prefix))
                    {
                        list.Add(cmd.Data.Name);
                    }
                }

                if (list.Count > 0)
                {
                    result = list.ToArray();
                }
            }
            else
            {
                foreach (Mono.Addins.TypeExtensionNode<TerminalCommandAttribute> cmdNode in commands)
                {
                    if (cmdNode.Data.Name.Equals(call.CommandName))
                    {
                        ITerminalCommand cmd = cmdNode.CreateInstance() as ITerminalCommand;
                        cmd.Init(this);
                        result = cmd.AutoComplete(call, call.Parameters.Length - 1);
                        prefix = call.Parameters[call.Parameters.Length - 1];
                        break;
                    }
                }
            }

            if (result != null)
            {
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = result[i].Substring(prefix.Length);
                }
            }

            return new Completion(prefix, result);
        }
Esempio n. 5
0
 /// <summary>
 /// Executes the command with the given name.
 /// </summary>
 /// <param name="call">The CommandCall to execute.</param>
 public void ExecuteCommand(CommandCall call)
 {
     foreach (Mono.Addins.TypeExtensionNode<TerminalCommandAttribute> cmdNode in Commands)
     {
         if (cmdNode.Data.Name == call.CommandName)
         {
             ITerminalCommand cmd = cmdNode.CreateInstance() as ITerminalCommand;
             cmd.Init(this);
             cmd.Execute(call.Parameters);
             break;
         }
     }
 }
Esempio n. 6
0
 /// <summary>
 /// Get autocomplete information for the given CommandCall.
 /// </summary>
 /// <param name="call">The commandline to create auto complete info for.</param>
 /// <param name="paramIndex">The parameter to complete.</param>
 /// <returns></returns>
 public virtual string[] AutoComplete(CommandCall call, int paramIndex)
 {
     return null;
 }
Esempio n. 7
0
 /// <summary>
 /// Reads a command from the terminal.
 /// </summary>
 /// <returns>
 /// The CommandCall instance for the command or null, if the user din't type a command.
 /// </returns>
 public CommandCall ReadCommand()
 {
     string command = Edit("shell>", "");
     if (!string.IsNullOrEmpty(command))
     {
         CommandCall call = new CommandCall(command);
         return call;
     }
     return null;
 }