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 AntShell.Commands;
10 using System.Text;
11 using System.IO;
12 
13 namespace Antmicro.Renode.UserInterface
14 {
15     public class CommandInteractionWrapper : ICommandInteraction
16     {
CommandInteractionWrapper(ICommandInteraction commandInteraction)17         public CommandInteractionWrapper(ICommandInteraction commandInteraction)
18         {
19             underlyingCommandInteraction = commandInteraction;
20             data = new StringBuilder();
21             error = new StringBuilder();
22         }
23 
Clear()24         public void Clear()
25         {
26             data.Clear();
27             error.Clear();
28         }
29 
GetContents()30         public string GetContents()
31         {
32             return data.ToString();
33         }
34 
GetError()35         public string GetError()
36         {
37             return error.ToString();
38         }
39 
GetRawInputStream()40         public Stream GetRawInputStream()
41         {
42             return underlyingCommandInteraction.GetRawInputStream();
43         }
44 
ReadLine()45         public string ReadLine()
46         {
47             return underlyingCommandInteraction.ReadLine();
48         }
49 
Write(char c, ConsoleColor? color)50         public void Write(char c, ConsoleColor? color)
51         {
52             data.Append(c);
53             underlyingCommandInteraction.Write(c, color);
54         }
55 
WriteError(string msg)56         public void WriteError(string msg)
57         {
58             error.Append(msg);
59             underlyingCommandInteraction.WriteError(msg);
60         }
61 
62         public string CommandToExecute { get { return underlyingCommandInteraction.CommandToExecute; } set { underlyingCommandInteraction.CommandToExecute = value; } }
63         public bool QuitEnvironment { get { return underlyingCommandInteraction.QuitEnvironment; } set { underlyingCommandInteraction.QuitEnvironment = value; } }
64         public ICommandInteraction UnderlyingCommandInteraction { get { return underlyingCommandInteraction; } }
65 
66         private readonly ICommandInteraction underlyingCommandInteraction;
67         private readonly StringBuilder data;
68         private readonly StringBuilder error;
69     }
70 
71     public class CommandInteractionEater : ICommandInteraction
72     {
Clear()73         public void Clear()
74         {
75             data.Clear();
76             error.Clear();
77         }
78 
Write(char c, ConsoleColor? color = null)79         public void Write(char c, ConsoleColor? color = null)
80         {
81             data.Append(c);
82         }
83 
WriteError(string error)84         public void WriteError(string error)
85         {
86             this.error.AppendLine(error);
87         }
88 
ReadLine()89         public string ReadLine()
90         {
91             return String.Empty;
92         }
93 
94         public string CommandToExecute { get; set; }
95 
96         public bool QuitEnvironment { get; set; }
97 
98         public bool HasError => error.Length > 0;
99 
GetContents()100         public string GetContents()
101         {
102             return data.ToString();
103         }
104 
GetError()105         public string GetError()
106         {
107             return error.ToString();
108         }
109 
GetRawInputStream()110         public Stream GetRawInputStream()
111         {
112             return null;
113         }
114 
115         private readonly StringBuilder data = new StringBuilder();
116         private readonly StringBuilder error = new StringBuilder();
117     }
118 
119     public class DummyCommandInteraction : ICommandInteraction
120     {
DummyCommandInteraction(bool verbose = false)121         public DummyCommandInteraction(bool verbose = false)
122         {
123             this.verbose = verbose;
124         }
125 
ReadLine()126         public string ReadLine()
127         {
128             return String.Empty;
129         }
130 
Write(char c, ConsoleColor? color = default(ConsoleColor?))131         public void Write(char c, ConsoleColor? color = default(ConsoleColor?))
132         {
133             if(verbose)
134             {
135                 Console.Write(c);
136             }
137         }
138 
WriteError(string error)139         public void WriteError(string error)
140         {
141             ErrorDetected = true;
142             if(verbose)
143             {
144                 Console.WriteLine("ERROR: " + error);
145             }
146         }
147 
GetRawInputStream()148         public Stream GetRawInputStream()
149         {
150             return null;
151         }
152 
153         public bool ErrorDetected
154         {
155             get
156             {
157                 var result = errorDetected;
158                 errorDetected = false;
159                 return result;
160             }
161 
162             private set
163             {
164                 errorDetected = value;
165             }
166         }
167 
168         public string CommandToExecute { get; set; }
169 
170         public bool QuitEnvironment { get; set; }
171 
172         private bool verbose;
173         private bool errorDetected;
174     }
175 }
176 
177