Advertisement
Merx3

Test Console class

Jul 15th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5.  
  6. /*
  7.  *
  8.  *  <copyright> Merx3- Telerik </copyright>
  9.  *
  10.  */
  11.  
  12. public class TestConsole
  13. {
  14.     struct BufferIndex
  15.     {
  16.         public int Row;
  17.         public int Column;
  18.  
  19.         public BufferIndex(int r, int c)
  20.         {
  21.             this.Row = r;
  22.             this.Column = c;
  23.         }
  24.     }
  25.  
  26.     private int windowsWidth;
  27.     private int windowsHeight;
  28.     private int bufferWidth;
  29.     private int bufferHeight;
  30.     private BufferIndex bufferIndex;
  31.     private string input;
  32.     private char[,] buffer;
  33.     private LinkedList<ConsoleKeyInfo> keysAvailable;
  34.     private LinkedList<long> keysTimer;
  35.  
  36.  
  37.     public bool KeyAvailable
  38.     {
  39.         get
  40.         {
  41.             if (keysAvailable.Count > 0)
  42.             {
  43.                 if (keysTimer == null)
  44.                 {
  45.                     return true;
  46.                 }
  47.                 else if (keysTimer.First.Value == 0)
  48.                 {
  49.                     return true;
  50.                 }
  51.                 else
  52.                 {
  53.                     return false;
  54.                 }
  55.             }
  56.             else
  57.             {
  58.                 return false;
  59.             }
  60.         }
  61.     }
  62.  
  63.     public int WindowWidth
  64.     {
  65.         get
  66.         {
  67.             return this.windowsWidth;
  68.         }
  69.         set
  70.         {
  71.             if (value <= 0)
  72.             {
  73.                 throw new ArgumentOutOfRangeException("Console buffer size must be greater" +
  74.                     " than 0(zero) and less than the current window's width.");
  75.             }
  76.             if (value > bufferWidth)
  77.             {
  78.                 BufferWidth = value;
  79.             }
  80.             this.windowsWidth = value;
  81.         }
  82.     }
  83.  
  84.     public int WindowHeight
  85.     {
  86.         get
  87.         {
  88.             return this.windowsHeight;
  89.         }
  90.         private set
  91.         {
  92.             if (value <= 0)
  93.             {
  94.                 throw new ArgumentOutOfRangeException("Console buffer size must be greater" +
  95.                     " than 0(zero) and less than the current window's width.");
  96.             }
  97.             if (value > bufferHeight)
  98.             {
  99.                 BufferHeight = value;
  100.             }
  101.             this.windowsHeight = value;
  102.         }
  103.     }
  104.  
  105.     public int BufferWidth
  106.     {
  107.         get
  108.         {
  109.             return this.bufferWidth;
  110.         }
  111.         set
  112.         {
  113.             if (value <= 0)
  114.             {
  115.                 throw new ArgumentOutOfRangeException("Console buffer size must be greater" +
  116.                     " than 0(zero) and less than the current window's width.");
  117.             }
  118.             if (value < windowsWidth)
  119.             {
  120.                 throw new ArgumentOutOfRangeException("Console buffer size must be greater" +
  121.                     " than 0(zero) and less than the current window's width.");
  122.             }
  123.             this.bufferWidth = value;
  124.             RecreateBuffer();
  125.         }
  126.     }
  127.  
  128.     public int BufferHeight
  129.     {
  130.         get
  131.         {
  132.             return this.bufferHeight;
  133.         }
  134.         private set
  135.         {
  136.             if (value <= 0)
  137.             {
  138.                 throw new ArgumentOutOfRangeException("Console buffer size must be greater" +
  139.                     " than 0(zero) and less than the current window's width.");
  140.             }
  141.             if (value < windowsHeight)
  142.             {
  143.                 throw new ArgumentOutOfRangeException("Console buffer size must be greater" +
  144.                     " than 0(zero) and less than the current window's width.");
  145.             }
  146.             this.bufferWidth = value;
  147.             RecreateBuffer();
  148.         }
  149.     }
  150.  
  151.  
  152.     public TestConsole()
  153.     {        
  154.         windowsWidth = 80;
  155.         windowsHeight = 25;
  156.         bufferWidth = 80;
  157.         bufferHeight = 300;
  158.         bufferIndex = new BufferIndex(0, 0);
  159.         buffer = new char[bufferHeight, bufferWidth];
  160.         keysAvailable = new LinkedList<ConsoleKeyInfo>();
  161.     }
  162.    
  163.     private void RecreateBuffer()
  164.     {      
  165.         char[,] newBuffer = new char[bufferHeight, bufferWidth];
  166.         for (int row = 0; row < newBuffer.GetLength(0) && row < buffer.GetLength(0); row++)
  167.         {
  168.             for (int column = 0; column < newBuffer.GetLength(1) && column < buffer.GetLength(1); column++)
  169.             {
  170.                 newBuffer[row, column] = buffer[row, column];
  171.             }
  172.         }
  173.  
  174.         if (bufferIndex.Column >= bufferWidth)
  175.         {
  176.             bufferIndex.Row++;
  177.             bufferIndex.Column = 0;
  178.         }
  179.         if (bufferIndex.Row >= bufferHeight)
  180.         {
  181.             PushBufferUp(1);
  182.         }
  183.     }
  184.  
  185.     /// <summary>
  186.     /// Sets the keys, which will be hit.
  187.     /// </summary>
  188.     /// <param name="keys"></param>
  189.     public void SetKeys(IEnumerable<ConsoleKeyInfo> keys)
  190.     {
  191.         foreach (ConsoleKeyInfo key in keys)
  192.         {
  193.             keysAvailable.AddLast(key);
  194.         }
  195.     }
  196.  
  197.     /// <summary>
  198.     /// Sets the keys, which will be hit after timer gets 0.
  199.     /// Note: the timers are manual!
  200.     /// </summary>
  201.     /// <param name="keys"></param>
  202.     /// <param name="timers"></param>
  203.     public void SetKeys(IEnumerable<ConsoleKeyInfo> keys, IEnumerable<long> timers)
  204.     {
  205.         keysTimer = new LinkedList<long>();
  206.         foreach (ConsoleKeyInfo key in keys)
  207.         {
  208.             keysAvailable.AddLast(key);
  209.         }
  210.         foreach (long time in timers)
  211.         {
  212.             keysTimer.AddLast(time);
  213.         }
  214.         if (keysTimer.Count != keysAvailable.Count)
  215.         {
  216.             throw new ArgumentException("Each key has to have a timer to it's pushing");
  217.         }
  218.     }
  219.  
  220.     /// <summary>
  221.     /// Sets the input for the console. Each input must end with "\n" or "\r\n"
  222.     /// </summary>
  223.     /// <param name="input"></param>
  224.     public void SetIn(string input)
  225.     {
  226.         StringBuilder inputString = new StringBuilder(input);
  227.         for (int i = 0; i < inputString.Length; i++)
  228.         {
  229.             if (inputString[i] == '\n')
  230.             {
  231.                 if (i > 0)
  232.                 {
  233.                     if (inputString[i - 1] != '\r')
  234.                     {
  235.                         inputString.Insert(i, '\r');
  236.                         i++;
  237.                     }
  238.                 }
  239.                 else
  240.                 {
  241.                     inputString.Insert(i, '\r');
  242.                     i++;
  243.                 }
  244.             }
  245.         }
  246.         this.input = inputString.ToString();
  247.     }
  248.  
  249.     /// <summary>
  250.     /// Gets the console generated output
  251.     /// </summary>
  252.     /// <returns></returns>
  253.     public string GetOutput()
  254.     {
  255.         StringBuilder output = new StringBuilder();
  256.         int lastNotNullRow = 0;
  257.         for (int row = 0; row < buffer.GetLength(0); row++)
  258.         {
  259.             for (int col = 0; col < buffer.GetLength(1); col++)
  260.             {
  261.                 if (buffer[row, col] != '\0')
  262.                 {
  263.                     for (int i = lastNotNullRow; i < row; i++)
  264.                     {
  265.                         output.Append("\r\n");
  266.                     }
  267.                     lastNotNullRow = row;
  268.                     output.Append(buffer[row, col]);
  269.                 }
  270.                 else
  271.                 {
  272.                     break;
  273.                 }
  274.             }
  275.         }
  276.  
  277.         if (lastNotNullRow != bufferIndex.Row)
  278.         {
  279.             for (int i = lastNotNullRow; i < bufferIndex.Row; i++)
  280.             {
  281.                 output.Append("\r\n");
  282.             }
  283.         }
  284.  
  285.         return output.ToString();
  286.     }
  287.  
  288.     public string ReadLine()
  289.     {
  290.         string retValue = null;
  291.         if (input == null)
  292.         {
  293.             return null;
  294.         }
  295.         else
  296.         {
  297.             int buffIndex = input.IndexOf('\n');
  298.             if (buffIndex < 0)
  299.             {
  300.                 throw new InvalidOperationException("Invalid input format!");
  301.             }
  302.             else
  303.             {
  304.                 if (buffIndex == 0)
  305.                 {
  306.                     return "";
  307.                 }
  308.                 else
  309.                 {
  310.                     retValue = input.Substring(0, buffIndex - 1);
  311.                     input = input.Substring(buffIndex + 1);
  312.                     return retValue;
  313.                 }
  314.             }
  315.         }
  316.     }
  317.  
  318.     public int Read()
  319.     {
  320.         if (input == null)
  321.         {
  322.             return -1;
  323.         }
  324.         else
  325.         {
  326.             int buffIndex = input.IndexOf('\n');
  327.             if (buffIndex < 0)
  328.             {
  329.                 throw new InvalidOperationException("Invalid input format!");
  330.             }
  331.             else
  332.             {
  333.                 int retValue = (int)input[0];
  334.                 input = input.Substring(1);
  335.                 return retValue;
  336.             }
  337.         }
  338.     }
  339.  
  340.     #region Writes
  341.  
  342.     public void Write(char c)
  343.     {
  344.         WriteInternal(c.ToString());
  345.     }
  346.  
  347.     public void Write(char[] c)
  348.     {
  349.         WriteInternal(c.ToString());
  350.     }
  351.  
  352.     public void Write(string s)
  353.     {
  354.         WriteInternal(s.ToString());
  355.     }
  356.  
  357.     public void Write(int i)
  358.     {
  359.         WriteInternal(i.ToString());
  360.     }
  361.  
  362.     public void Write(uint i)
  363.     {
  364.         WriteInternal(i.ToString());
  365.     }
  366.  
  367.     public void Write(bool b)
  368.     {
  369.         WriteInternal(b.ToString());
  370.     }
  371.  
  372.     public void Write(float f)
  373.     {
  374.         WriteInternal(f.ToString());
  375.     }
  376.  
  377.     public void Write(long l)
  378.     {
  379.         WriteInternal(l.ToString());
  380.     }
  381.  
  382.     public void Write(ulong l)
  383.     {
  384.         WriteInternal(l.ToString());
  385.     }
  386.  
  387.     public void Write(decimal d)
  388.     {
  389.         WriteInternal(d.ToString());
  390.     }
  391.  
  392.     public void Write(double d)
  393.     {
  394.         WriteInternal(d.ToString());
  395.     }
  396.  
  397.     public void Write(byte b)
  398.     {
  399.         WriteInternal(b.ToString());
  400.     }
  401.  
  402.     public void Write(object o)
  403.     {
  404.         WriteInternal(o.ToString());
  405.     }
  406.  
  407.     public void Write(string format, object o)
  408.     {
  409.         WriteInternal(String.Format(format, o));
  410.     }
  411.  
  412.     public void Write(string format, object o1, object o2)
  413.     {
  414.         WriteInternal(String.Format(format, o1, o2));
  415.     }
  416.  
  417.     public void Write(string format, object o1, object o2, object o3)
  418.     {
  419.         WriteInternal(String.Format(format, o1, o2, o3));
  420.     }
  421.  
  422.     public void Write(string format, params object[] o)
  423.     {
  424.         WriteInternal(String.Format(format, o));
  425.     }
  426.  
  427.     #endregion
  428.  
  429.     #region Writelines
  430.  
  431.     public void WriteLine()
  432.     {
  433.         WriteInternal("\n");
  434.     }
  435.  
  436.     public void WriteLine(char c)
  437.     {
  438.         WriteInternal(c.ToString() + "\n");
  439.     }
  440.  
  441.     public void WriteLine(char[] c)
  442.     {
  443.         WriteInternal(c.ToString() + "\n");
  444.     }
  445.  
  446.     public void WriteLine(string s)
  447.     {
  448.         WriteInternal(s.ToString() + "\n");
  449.     }
  450.  
  451.     public void WriteLine(int i)
  452.     {
  453.         WriteInternal(i.ToString() + "\n");
  454.     }
  455.  
  456.     public void WriteLine(uint i)
  457.     {
  458.         WriteInternal(i.ToString() + "\n");
  459.     }
  460.  
  461.     public void WriteLine(bool b)
  462.     {
  463.         WriteInternal(b.ToString() + "\n");
  464.     }
  465.  
  466.     public void WriteLine(float f)
  467.     {
  468.         WriteInternal(f.ToString() + "\n");
  469.     }
  470.  
  471.     public void WriteLine(long l)
  472.     {
  473.         WriteInternal(l.ToString() + "\n");
  474.     }
  475.  
  476.     public void WriteLine(ulong l)
  477.     {
  478.         WriteInternal(l.ToString() + "\n");
  479.     }
  480.  
  481.     public void WriteLine(decimal d)
  482.     {
  483.         WriteInternal(d.ToString() + "\n");
  484.     }
  485.  
  486.     public void WriteLine(double d)
  487.     {
  488.         WriteInternal(d.ToString() + "\n");
  489.     }
  490.  
  491.     public void WriteLine(byte b)
  492.     {
  493.         WriteInternal(b.ToString() + "\n");
  494.     }
  495.  
  496.     public void WriteLine(object o)
  497.     {
  498.         WriteInternal(o.ToString() + "\n");
  499.     }
  500.  
  501.     public void WriteLine(string format, object o)
  502.     {
  503.         WriteInternal(String.Format(format, o) + "\n");
  504.     }
  505.  
  506.     public void WriteLine(string format, object o1, object o2)
  507.     {
  508.         WriteInternal(String.Format(format, o1, o2) + "\n");
  509.     }
  510.  
  511.     public void WriteLine(string format, object o1, object o2, object o3)
  512.     {
  513.         WriteInternal(String.Format(format, o1, o2, o3) + "\n");
  514.     }
  515.  
  516.     public void WriteLine(string format, params object[] o)
  517.     {
  518.         WriteInternal(String.Format(format, o) + "\n");
  519.     }
  520.  
  521.     #endregion
  522.  
  523.  
  524.     private void WriteInternal(string value)
  525.     {
  526.         for (int valueIndex = 0; valueIndex < value.Length; valueIndex++)
  527.         {
  528.             if (value[valueIndex] == '\n')
  529.             {
  530.                 bufferIndex.Row++;
  531.                 bufferIndex.Column = 0;
  532.             }
  533.             else
  534.             {
  535.                 buffer[bufferIndex.Row, bufferIndex.Column] = value[valueIndex];
  536.                 bufferIndex.Column++;
  537.             }
  538.  
  539.             if (bufferIndex.Column >= bufferWidth)
  540.             {
  541.                 bufferIndex.Row++;
  542.                 bufferIndex.Column = 0;
  543.             }
  544.             if (bufferIndex.Row >= bufferHeight)
  545.             {
  546.                 PushBufferUp(1);
  547.             }
  548.         }
  549.     }
  550.  
  551.     private void PushBufferUp(int rows)
  552.     {
  553.         char[,] nextBuffer = new char[bufferHeight, bufferWidth];
  554.         for (int row = 0; row + rows < bufferHeight; row++)
  555.         {
  556.             for (int col = 0; col < bufferWidth; col++)
  557.             {
  558.                 nextBuffer[row, col] = buffer[row + rows, col];
  559.             }
  560.         }
  561.         bufferIndex.Row = windowsHeight - rows;
  562.         bufferIndex.Column = 0;
  563.     }
  564.  
  565.     public void Clear()
  566.     {
  567.         buffer = new char[bufferHeight, bufferWidth];
  568.     }
  569.  
  570.     public ConsoleKeyInfo ReadKey()
  571.     {
  572.         if (keysAvailable.Count == 0)
  573.         {
  574.             throw new InvalidOperationException("No keys available in the console keys buffer");
  575.         }
  576.         else
  577.         {      
  578.             ConsoleKeyInfo key = keysAvailable.First.Value;
  579.             keysAvailable.RemoveFirst();
  580.             if (keysTimer != null)
  581.             {
  582.                 keysTimer.RemoveFirst();
  583.             }
  584.             return key;
  585.         }
  586.     }
  587.  
  588.     /// <summary>
  589.     /// Makes the key countdown timer decrease with the specified ticks
  590.     /// </summary>
  591.     /// <param name="ticks"></param>
  592.     public void KeyTickTimer(int ticks)
  593.     {
  594.         if (keysTimer != null)
  595.         {
  596.             keysTimer.First.Value -= ticks;
  597.             if (keysTimer.First.Value < 0)
  598.             {
  599.                 keysTimer.First.Value = 0;
  600.             }
  601.         }
  602.     }
  603.  
  604.     public void SetCursorPosition(int column, int row)
  605.     {
  606.         if (column >= bufferWidth || row >= bufferHeight ||
  607.             column < 0 || row < 0)
  608.         {
  609.             throw new IndexOutOfRangeException(String.Format("Buffer index is out of range. Column must be between 0 and {0}." +
  610.                 "Row must be between 0 and {1}.", bufferWidth - 1, bufferHeight - 1));
  611.         }
  612.  
  613.         bufferIndex.Row = row;
  614.         bufferIndex.Column = column;
  615.  
  616.         for (int c = 0; c < bufferIndex.Column; c++)
  617.         {
  618.             if (buffer[bufferIndex.Row, c] == '\0')
  619.             {
  620.                 buffer[bufferIndex.Row, c] = ' ';
  621.             }
  622.         }
  623.     }
  624. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement