1 //
2 // Copyright (c) 2010-2018 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 using System;
9 using System.Collections.Generic;
10 using Antmicro.Renode.UserInterface.Tokenizer;
11 using AntShell.Commands;
12 using System.Linq;
13 
14 namespace Antmicro.Renode.UserInterface.Commands
15 {
16     public class UsingCommand : Command
17     {
PrintHelp(ICommandInteraction writer)18         public override void PrintHelp(ICommandInteraction writer)
19         {
20             base.PrintHelp(writer);
21             writer.WriteLine();
22             writer.WriteLine("Current 'using':");
23             if(!GetUsings().Any())
24             {
25                 writer.WriteLine("\t[none]");
26             }
27             foreach(var use in GetUsings())
28             {
29                 writer.WriteLine ("\t"+ use.Substring(0, use.Length-1)); //to remove the trailing dot
30             }
31             writer.WriteLine();
32             writer.WriteLine(String.Format("To clear all current usings execute \"{0} -\".", Name));
33         }
34 
35         [Runnable]
Run(ICommandInteraction writer, LiteralToken use)36         public void Run(ICommandInteraction writer, LiteralToken use)
37         {
38             if(use.Value == "-")
39             {
40                 GetUsings().Clear();
41             }
42             else
43             {
44                 var dotted = use.Value + (use.Value.Last() == '.' ? "" : "."); //dot suffix
45                 if(!GetUsings().Contains(dotted))
46                 {
47                     GetUsings().Add(dotted);
48                 }
49             }
50         }
51 
52         private Func<List<string>> GetUsings;
53 
UsingCommand(Monitor monitor, Func<List<string>> getUsings)54         public UsingCommand(Monitor monitor, Func<List<string>> getUsings) : base(monitor, "using", "expose a prefix to avoid typing full object names.")
55         {
56             GetUsings = getUsings;
57         }
58     }
59 }
60 
61