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; } = () => System.Threading.Tasks.Task.FromResult(string.Empty);
66
70 public bool AllowInput { get; set; } = true;
71
76 private Task<string> ReadLineAsync()
77 {
78 return System.Threading.Tasks.Task.Run(Console.ReadLine);
79 }
80
81
82 [BadMethod("WriteLine", "Writes a line to the Console")]
83 private void InvokeWriteLine([BadParameter(description: "The Object to Write")] BadObject obj)
84 {
85 OnWriteLine(obj);
86 }
87
88 [BadMethod("Write", "Writes to the Console")]
89 private void InvokeWrite([BadParameter(description: "The Object to Write")] BadObject obj)
90 {
91 OnWrite(obj);
92 }
93
94 [BadMethod("Clear", "Clears the Console")]
95 private void InvokeClear()
96 {
97 OnClear();
98 }
99
100 [BadMethod("ReadLine", "Reads a line from the Console")]
101 [return: BadReturn("The Console Input")]
102 private string InvokeReadLine()
103 {
104 return OnReadLine();
105 }
106
107 [BadMethod("ReadLineAsync", "Reads a line from the Console asynchronously")]
108 [return: BadReturn("The Console Input")]
110 {
111 return new BadTask(
112 new BadReadLineAsyncRunnable(ReadLineAsyncBlocking().GetEnumerator()),
113 "Console.ReadLineAsync"
114 );
115 }
116
121 private IEnumerable<BadObject> ReadLineAsyncBlocking()
122 {
123 Task<string>? t = OnReadLineAsync();
124
125 while (!t.IsCompleted)
126 {
127 yield return BadObject.Null;
128 }
129
130 yield return t.Result;
131 }
132
137 private void Write(BadObject obj)
138 {
139 if (obj is IBadString str)
140 {
141 Console.Write(str.Value);
142 }
143 else
144 {
145 Console.Write(obj.ToString());
146 }
147 }
148
153 private void WriteLine(BadObject obj)
154 {
155 if (obj is IBadString str)
156 {
157 Console.WriteLine(str.Value);
158 }
159 else
160 {
162 }
163 }
164
168 public void Clear()
169 {
170 Console.Clear();
171 }
172
178 public string ReadLine()
179 {
180 if (!AllowInput)
181 {
182 throw new NotSupportedException("Input is not allowed");
183 }
184
185 return Console.ReadLine();
186 }
187
192 {
196 private readonly IEnumerator<BadObject> m_Task;
197
202
207 public BadReadLineAsyncRunnable(IEnumerator<BadObject> task)
208 {
209 m_Task = task;
210 }
211
215 public override IEnumerator<BadObject> Enumerator
216 {
217 get
218 {
219 while (m_Task.MoveNext())
220 {
221 m_Return = m_Task.Current!;
222
223 yield return m_Return;
224 }
225 }
226 }
227
232 public override BadObject GetReturn()
233 {
234 return m_Return;
235 }
236 }
237}
Wrapper class for the console abstraction.
Definition BadConsole.cs:12
static IBadConsole GetConsole()
Returns the Current Console Implementation.
Definition BadConsole.cs:49
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:210
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