You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.4 KiB
61 lines
1.4 KiB
1 year ago
|
using System.Diagnostics.CodeAnalysis;
|
||
2 years ago
|
using System.Text;
|
||
2 years ago
|
|
||
1 year ago
|
namespace StabilityMatrix.Core.Python.Interop;
|
||
2 years ago
|
|
||
|
/// <summary>
|
||
|
/// Implement the interface of the sys.stdout redirection
|
||
|
/// </summary>
|
||
|
[SuppressMessage("ReSharper", "InconsistentNaming")]
|
||
2 years ago
|
public class PyIOStream
|
||
2 years ago
|
{
|
||
2 years ago
|
private readonly StringBuilder TextBuilder;
|
||
|
private readonly StringWriter TextWriter;
|
||
2 years ago
|
|
||
2 years ago
|
public PyIOStream(StringBuilder? builder = null)
|
||
2 years ago
|
{
|
||
2 years ago
|
TextBuilder = builder ?? new StringBuilder();
|
||
|
TextWriter = new StringWriter(TextBuilder);
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
|
public event EventHandler<string>? OnWriteUpdate;
|
||
2 years ago
|
|
||
2 years ago
|
public void ClearBuffer()
|
||
|
{
|
||
|
TextBuilder.Clear();
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
public string GetBuffer()
|
||
|
{
|
||
|
return TextBuilder.ToString();
|
||
|
}
|
||
|
|
||
|
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
|
||
2 years ago
|
public void write(string str)
|
||
|
{
|
||
|
TextWriter.Write(str);
|
||
|
OnWriteUpdate?.Invoke(this, str);
|
||
|
}
|
||
|
|
||
2 years ago
|
[SuppressMessage("ReSharper", "UnusedMember.Global")]
|
||
|
public void writelines(IEnumerable<string> str)
|
||
2 years ago
|
{
|
||
|
foreach (var line in str)
|
||
|
{
|
||
|
write(line);
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
[SuppressMessage("ReSharper", "UnusedMember.Global")]
|
||
2 years ago
|
public void flush()
|
||
|
{
|
||
|
TextWriter.Flush();
|
||
|
}
|
||
|
|
||
2 years ago
|
[SuppressMessage("ReSharper", "UnusedMember.Global")]
|
||
2 years ago
|
public void close()
|
||
|
{
|
||
|
TextWriter?.Close();
|
||
|
}
|
||
2 years ago
|
}
|