BadScript 2
Loading...
Searching...
No Matches
BadConsoleApi.cs
Go to the documentation of this file.
5
10
14[BadInteropApi("Console", true)]
15internal partial class BadConsoleApi
16{
20 private readonly IBadConsole? m_Console;
21
22
27 public BadConsoleApi(IBadConsole console) : this()
28 {
29 m_Console = console;
30 OnWrite = Write;
32 OnClear = Clear;
35 }
36
41
45 public Action<BadObject> OnWrite { get; set; } = delegate { };
46
50 public Action<BadObject> OnWriteLine { get; set; } = delegate { };
51
55 public Action OnClear { get; set; } = delegate { };
56
60 public Func<string> OnReadLine { get; set; } = () => string.Empty;
61
65 public Func<Task<string>> OnReadLineAsync { get; set; } =
66 () => System.Threading.Tasks.Task.FromResult(string.Empty);
67
71 public bool AllowInput { get; set; } = true;
72
77 private Task<string> ReadLineAsync()
78 {
79 return System.Threading.Tasks.Task.Run(Console.ReadLine);
80 }
81
82
83 [BadMethod("WriteLine", "Writes a line to the Console")]
84 private void InvokeWriteLine([BadParameter(description: "The Object to Write")] BadObject obj)
85 {
86 OnWriteLine(obj);
87 }
88
89 [BadMethod("Write", "Writes to the Console")]
90 private void InvokeWrite([BadParameter(description: "The Object to Write")] BadObject obj)
91 {
92 OnWrite(obj);
93 }
94
95 [BadMethod("Clear", "Clears the Console")]
96 private void InvokeClear()
97 {
98 OnClear();
99 }
100
101 [BadMethod("ReadLine", "Reads a line from the Console")]
102 [return: BadReturn("The Console Input")]
103 private string InvokeReadLine()
104 {
105 return OnReadLine();
106 }
107
108 [BadMethod("ReadLineAsync", "Reads a line from the Console asynchronously")]
109 [return: BadReturn("The Console Input")]
111 {
113 .GetEnumerator()
114 ),
115 "Console.ReadLineAsync"
116 );
117 }
118
123 private IEnumerable<BadObject> ReadLineAsyncBlocking()
124 {
125 Task<string>? t = OnReadLineAsync();
126
127 while (!t.IsCompleted)
128 {
129 yield return BadObject.Null;
130 }
131
132 yield return t.Result;
133 }
134
139 private void Write(BadObject obj)
140 {
141 if (obj is IBadString str)
142 {
143 Console.Write(str.Value);
144 }
145 else
146 {
147 Console.Write(obj.ToString());
148 }
149 }
150
155 private void WriteLine(BadObject obj)
156 {
157 if (obj is IBadString str)
158 {
159 Console.WriteLine(str.Value);
160 }
161 else
162 {
164 }
165 }
166
170 public void Clear()
171 {
172 Console.Clear();
173 }
174
180 public string ReadLine()
181 {
182 if (!AllowInput)
183 {
184 throw new NotSupportedException("Input is not allowed");
185 }
186
187 return Console.ReadLine();
188 }
189
190#region Nested type: BadReadLineAsyncRunnable
191
196 {
200 private readonly IEnumerator<BadObject> m_Task;
201
206
211 public BadReadLineAsyncRunnable(IEnumerator<BadObject> task)
212 {
213 m_Task = task;
214 }
215
219 public override IEnumerator<BadObject> Enumerator
220 {
221 get
222 {
223 while (m_Task.MoveNext())
224 {
225 m_Return = m_Task.Current!;
226
227 yield return m_Return;
228 }
229 }
230 }
231
236 public override BadObject GetReturn()
237 {
238 return m_Return;
239 }
240 }
241
242#endregion
243}
Wrapper class for the console abstraction.
Definition BadConsole.cs:12
static IBadConsole GetConsole()
Returns the Current Console Implementation.
Definition BadConsole.cs:51
Awaitable Enumeration that wraps the ReadLineAsync Task.
override IEnumerator< BadObject > Enumerator
The Enumerator that wraps the Task.
readonly IEnumerator< BadObject > m_Task
The Wrapped Task.
BadReadLineAsyncRunnable(IEnumerator< BadObject > task)
Constructs a new Instance.
Func< string > OnReadLine
Event Handler for the "ReadLine" Function.
BadConsoleApi(IBadConsole console)
Constructs a new Console API Instance.
void WriteLine(BadObject obj)
Writes an Object to the Console. Appends a newline.
IEnumerable< BadObject > ReadLineAsyncBlocking()
Wrapper that will block until the Task is completed.
IBadConsole Console
The Console Implementation that is used.
Func< Task< string > > OnReadLineAsync
Event Handler for the "ReadLineAsync" Function.
void InvokeWrite([BadParameter(description:"The Object to Write")] BadObject obj)
bool AllowInput
If Set to false, the Console will throw an error if console input is requested.
void InvokeWriteLine([BadParameter(description:"The Object to Write")] BadObject obj)
Action OnClear
Event Handler for the "Clear" Function.
Action< BadObject > OnWrite
Event Handler for the "Write" Function.
string ReadLine()
Reads a line from the Console.
Task< string > ReadLineAsync()
Wrapper that calls the "Write" Function in a new Task.
void Write(BadObject obj)
Writes an Object to the Console.
readonly? IBadConsole m_Console
The Console Implementation that is used.
Action< BadObject > OnWriteLine
Event Handler for the "WriteLine" Function.
Implements a Runnable Object.
Implements a Task Object.
Definition BadTask.cs:17
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
override string ToString()
Returns a String Representation of this Object.
Definition BadObject.cs:224
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
Interface that abstracts the console.
void WriteLine(string str)
Writes a string to the console and appends a newline.
string ReadLine()
Reads a line from the console.
void Write(string str)
Writes a string to the console.
Implements the Interface for Native Strings.
Definition IBadString.cs:7
Contains a Console Abstraction Layer to be able to Simulate Console Input/Output over the Network.
Definition BadConsole.cs:6
Contains Common Interop APIs for the BadScript2 Runtime.
Contains task/async Extensions and Integrations for the BadScript2 Runtime.
Contains the Native Runtime Objects.
Definition BadBoolean.cs:6
Contains the Runtime Objects.
Definition BadArray.cs:10