Terminal.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. namespace Terminal.Gui.TextEffects;
  2. public class TerminalConfig
  3. {
  4. public int TabWidth { get; set; } = 4;
  5. public bool XtermColors { get; set; } = false;
  6. public bool NoColor { get; set; } = false;
  7. public bool WrapText { get; set; } = false;
  8. public float FrameRate { get; set; } = 100.0f;
  9. public int CanvasWidth { get; set; } = -1;
  10. public int CanvasHeight { get; set; } = -1;
  11. public string AnchorCanvas { get; set; } = "sw";
  12. public string AnchorText { get; set; } = "sw";
  13. public bool IgnoreTerminalDimensions { get; set; } = false;
  14. }
  15. public class Canvas
  16. {
  17. public int Top { get; private set; }
  18. public int Right { get; private set; }
  19. public int Bottom { get; private set; } = 1;
  20. public int Left { get; private set; } = 1;
  21. public int CenterRow => (Top + Bottom) / 2;
  22. public int CenterColumn => (Right + Left) / 2;
  23. public Coord Center => new Coord (CenterColumn, CenterRow);
  24. public int Width => Right - Left + 1;
  25. public int Height => Top - Bottom + 1;
  26. public Canvas (int top, int right)
  27. {
  28. Top = top;
  29. Right = right;
  30. }
  31. public bool IsCoordInCanvas (Coord coord)
  32. {
  33. return coord.Column >= Left && coord.Column <= Right &&
  34. coord.Row >= Bottom && coord.Row <= Top;
  35. }
  36. public Coord GetRandomCoord (bool outsideScope = false)
  37. {
  38. var random = new Random ();
  39. if (outsideScope)
  40. {
  41. switch (random.Next (4))
  42. {
  43. case 0: return new Coord (random.Next (Left, Right + 1), Top + 1);
  44. case 1: return new Coord (random.Next (Left, Right + 1), Bottom - 1);
  45. case 2: return new Coord (Left - 1, random.Next (Bottom, Top + 1));
  46. case 3: return new Coord (Right + 1, random.Next (Bottom, Top + 1));
  47. }
  48. }
  49. return new Coord (
  50. random.Next (Left, Right + 1),
  51. random.Next (Bottom, Top + 1));
  52. }
  53. }
  54. public class TerminalA
  55. {
  56. public TerminalConfig Config { get; }
  57. public Canvas Canvas { get; private set; }
  58. private Dictionary<Coord, EffectCharacter> CharacterByInputCoord = new Dictionary<Coord, EffectCharacter> ();
  59. public TerminalA (string input, TerminalConfig config = null)
  60. {
  61. Config = config ?? new TerminalConfig ();
  62. var dimensions = GetTerminalDimensions ();
  63. Canvas = new Canvas (dimensions.height, dimensions.width);
  64. ProcessInput (input);
  65. }
  66. private void ProcessInput (string input)
  67. {
  68. // Handling input processing logic similar to Python's version
  69. }
  70. public string GetPipedInput ()
  71. {
  72. // C# way to get piped input or indicate there's none
  73. return Console.IsInputRedirected ? Console.In.ReadToEnd () : string.Empty;
  74. }
  75. public void Print (string output, bool enforceFrameRate = true)
  76. {
  77. if (enforceFrameRate)
  78. EnforceFrameRate ();
  79. // Move cursor to top and clear the current console line
  80. Console.SetCursorPosition (0, 0);
  81. Console.Write (output);
  82. Console.ResetColor ();
  83. }
  84. private void EnforceFrameRate ()
  85. {
  86. // Limit the printing speed based on the Config.FrameRate
  87. }
  88. private (int width, int height) GetTerminalDimensions ()
  89. {
  90. // Return terminal dimensions or defaults if not determinable
  91. return (Console.WindowWidth, Console.WindowHeight);
  92. }
  93. }