Skip to content

Commit

Permalink
quick
Browse files Browse the repository at this point in the history
  • Loading branch information
dedouwe26 committed Nov 5, 2024
1 parent b9747b5 commit ab19baa
Show file tree
Hide file tree
Showing 5 changed files with 365 additions and 334 deletions.
Original file line number Diff line number Diff line change
@@ -1,62 +1,79 @@
using System.Text;

namespace OxDED.Terminal.Window;
namespace OxDED.Terminal.Backend;

/// <summary>
/// Represents a terminal window that can be used.
/// Represents an interface of common methods of a terminal.
/// </summary>
public abstract class TerminalWindow : IDisposable {
public interface ITerminalBackend : IDisposable {
/// <summary>
/// Sets default values.
/// The data stream for reading from the terminal.
/// </summary>
protected TerminalWindow() {
IsDisposed = true;
}

/// <summary>
/// The title of the Terminal window.
/// </summary>
public abstract string Title { get; set;}
/// <summary>
/// The out (to terminal) stream.
/// </summary>
public abstract TextWriter Out {get;}
/// <summary>
/// The in (from terminal) stream.
/// </summary>
public abstract TextReader In {get;}
public TextReader StandardInput { get; }
/// <summary>
/// The error (to terminal) stream.
/// The data stream for writing to the terminal.
/// </summary>
public abstract TextWriter Error {get;}
public TextWriter StandardOutput { get; }
/// <summary>
/// Hides or shows terminal cursor.
/// The data stream for writing errors to the terminal.
/// </summary>
public abstract bool HideCursor {get; set;}
public TextWriter StandardError { get; }
/// <summary>
/// The width (in characters) of the terminal.
/// The encoding used for the <see cref="StandardInput"/> stream (default: UTF-8).
/// </summary>
public abstract int Width {get;}
public Encoding InputEncoding { get; set; }
/// <summary>
/// The height (in characters) of the terminal.
/// The encoding used for the <see cref="StandardOutput"/> stream (default: UTF-8).
/// </summary>
public abstract int Height {get;}
public Encoding OutputEncoding { get; set; }
/// <summary>
/// The encoding used for the in stream (default: UTF-8).
/// The encoding used for the <see cref="StandardError"/> stream (default: UTF-8).
/// </summary>
public abstract Encoding InEncoding {get; set;}
public Encoding ErrorEncoding { get; set; }
/// <summary>
/// The encoding used for the error and out streams (default: UTF-8).
/// The width and the height (in characters) of the terminal.
/// </summary>
public abstract Encoding OutEncoding {get; set;}
public (int Width, int Height) Size { get; set; }

}

/// <summary>
/// Represents an interface of common methods of a terminal.
/// </summary>
public abstract class TerminalBackend : ITerminalBackend {
/// <inheritdoc/>
public abstract TextReader StandardInput { get; }

/// <inheritdoc/>
public abstract TextWriter StandardOutput { get; }

/// <inheritdoc/>
public abstract TextWriter StandardError { get; }

/// <inheritdoc/>
public abstract Encoding InputEncoding { get; set; }

/// <inheritdoc/>
public abstract Encoding OutputEncoding { get; set; }

/// <inheritdoc/>
public abstract Encoding ErrorEncoding { get; set; }

/// <inheritdoc/>
public virtual (int Width, int Height) Size { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

/// <inheritdoc/>
public abstract void Dispose();


/// <summary>
/// Writes something (<see cref="object.ToString"/>) to the terminal, with a style.
/// </summary>
/// <typeparam name="T">The type of what to write (<see cref="object.ToString"/>).</typeparam>
/// <param name="text">The thing to write to the terminal.</param>
/// <param name="style">The text decoration to use.</param>
public virtual void Write<T>(T? text, Style? style = null) {
Out.Write((style ?? new Style()).ToANSI()+text?.ToString()+ANSI.Styles.ResetAll);
StandardOutput.Write((style ?? new Style()).ToANSI()+text?.ToString()+ANSI.Styles.ResetAll);
}
/// <summary>
/// Writes something (<see cref="object.ToString"/>) to the terminal, with a style.
Expand All @@ -65,7 +82,7 @@ public virtual void Write<T>(T? text, Style? style = null) {
/// <param name="text">The thing to write to the terminal.</param>
/// <param name="style">The text decoration to use.</param>
public virtual void WriteLine<T>(T? text, Style? style = null) {
Out.WriteLine((style ?? new Style()).ToANSI()+text?.ToString()+ANSI.Styles.ResetAll);
StandardOutput.WriteLine((style ?? new Style()).ToANSI()+text?.ToString()+ANSI.Styles.ResetAll);
}
/// <summary>
/// Writes something (<see cref="object.ToString"/>) to the error stream, with a style.
Expand All @@ -74,7 +91,7 @@ public virtual void WriteLine<T>(T? text, Style? style = null) {
/// <param name="text">The text to write to the error output stream.</param>
/// <param name="style">The style to use (default: with red foreground).</param>
public virtual void WriteErrorLine<T>(T? text, Style? style = null) {
Error.WriteLine((style ?? new Style {ForegroundColor = Colors.Red}).ToANSI()+text?.ToString()+ANSI.Styles.ResetAll);
StandardError.WriteLine((style ?? new Style {ForegroundColor = Colors.Red}).ToANSI()+text?.ToString()+ANSI.Styles.ResetAll);
}
/// <summary>
/// Writes something (<see cref="object.ToString"/>) to the error stream, with a style.
Expand All @@ -83,17 +100,19 @@ public virtual void WriteErrorLine<T>(T? text, Style? style = null) {
/// <param name="text">The text to write to the error output stream.</param>
/// <param name="style">The style to use (default: with red foreground).</param>
public virtual void WriteError<T>(T? text, Style? style = null) {
Error.Write((style ?? new Style {ForegroundColor = Colors.Red}).ToANSI()+text?.ToString()+ANSI.Styles.ResetAll);
StandardError.Write((style ?? new Style {ForegroundColor = Colors.Red}).ToANSI()+text?.ToString()+ANSI.Styles.ResetAll);
}
/// <summary>
/// Sets the cursor to that position.
/// </summary>
/// <param name="pos">The position.</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public virtual void Goto((int x, int y) pos) {
if (pos.x >= Width || pos.x < 0) { throw new ArgumentOutOfRangeException(nameof(pos), "pos x is higher than the width or is lower than 0."); }
if (pos.y >= Height || pos.y < 0) { throw new ArgumentOutOfRangeException(nameof(pos), "pos y is higher than the height or is lower than 0."); }
Out.Write(ANSI.MoveCursor(pos.x, pos.y));
try {
if (pos.x >= Size.Width || pos.x < 0) { throw new ArgumentOutOfRangeException(nameof(pos), "pos x is higher than the width or is lower than 0."); }
if (pos.y >= Size.Height || pos.y < 0) { throw new ArgumentOutOfRangeException(nameof(pos), "pos y is higher than the height or is lower than 0."); }
} catch (NotImplementedException) { }
StandardOutput.Write(ANSI.MoveCursor(pos.x, pos.y));
}
/// <summary>
/// Gets the cursor position.
Expand All @@ -103,7 +122,7 @@ public virtual void Goto((int x, int y) pos) {
/// <summary>
/// Sets the something (<see cref="object.ToString"/>) at a <paramref name="pos"/>, with a <paramref name="style"/>.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T">The type of what to write.</typeparam>
/// <param name="text">The thing to set at <paramref name="pos"/> to the terminal.</param>
/// <param name="pos">The position to set <paramref name="text"/> at.</param>
/// <param name="style">The text decoration to use.</param>
Expand All @@ -115,7 +134,7 @@ public virtual void Set<T>(T? text, (int x, int y) pos, Style? style = null) {
/// <summary>
/// Sets the something in the error stream (<see cref="object.ToString"/>) at a <paramref name="pos"/>, with a <paramref name="style"/>.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T">The type of what to write.</typeparam>
/// <param name="text">The thing to set at <paramref name="pos"/> to the terminal.</param>
/// <param name="pos">The position to set <paramref name="text"/> at.</param>
/// <param name="style">The text decoration to use.</param>
Expand All @@ -128,14 +147,14 @@ public virtual void SetError<T>(T? text, (int x, int y) pos, Style? style = null
/// </summary>
/// <returns>The character that has been read (-1 if everything has been read).</returns>
public virtual int Read() {
return In.Read();
return StandardInput.Read();
}
/// <summary>
/// Reads a line from the input stream.
/// </summary>
/// <returns>The line that has been read (null if everything has been read).</returns>
public virtual string? ReadLine() {
return In.ReadLine();
return StandardInput.ReadLine();
}
/// <summary>
/// Waits until a key is pressed.
Expand All @@ -162,31 +181,31 @@ protected void KeyPress(ConsoleKey key, char keyChar, bool alt, bool shift, bool
/// </summary>
public virtual void Clear() {
Goto((0,0));
Out.Write(ANSI.EraseScreenFromCursor);
StandardOutput.Write(ANSI.EraseScreenFromCursor);
}
/// <summary>
/// Clears screen from the position to end of the screen.
/// </summary>
/// <param name="pos">The start position.</param>
public virtual void ClearFrom((int x, int y) pos) {
Goto(pos);
Out.Write(ANSI.EraseLineFromCursor);
StandardOutput.Write(ANSI.EraseLineFromCursor);
}
/// <summary>
/// Clears (deletes) a line.
/// </summary>
/// <param name="line">The y-axis of the line.</param>
public virtual void ClearLine(int line) {
Goto((0, line));
Out.Write(ANSI.EraseLine);
StandardOutput.Write(ANSI.EraseLine);
}
/// <summary>
/// Clears the line from the position to the end of the line.
/// </summary>
/// <param name="pos">The start position.</param>
public virtual void ClearLineFrom((int x, int y) pos) {
Goto(pos);
Out.Write(ANSI.EraseLineFromCursor);
StandardOutput.Write(ANSI.EraseLineFromCursor);
}
/// <summary>
/// The thread that is running <see cref="ListenForKeysMethod"/>.
Expand All @@ -210,27 +229,9 @@ public virtual bool ListenForKeys {set {
} get {
return listenForKeys;
}}

/// <summary>
/// Method in new thread that should call <see cref="OnKeyPress"/> when a key is pressed.
/// </summary>
protected abstract void ListenForKeysMethod();

/// <summary>
/// If it already is disposed.
/// </summary>
public bool IsDisposed {get; protected set;}
/// <inheritdoc/>
public virtual void Dispose() {
if (IsDisposed) { return; }
IsDisposed = true;

GC.SuppressFinalize(this);
}

/// <summary>
/// Disposes the window.
/// </summary>
~TerminalWindow() {
Dispose();
}
}
Loading

0 comments on commit ab19baa

Please sign in to comment.