andrew4582

ConsoleDisplayHelper

Jun 29th, 2011
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.96 KB | None | 0 0
  1. #region Console Display Helper
  2.  
  3.     public class ConsoleDisplayHelper {
  4.  
  5.         public const char Default_TextLine_Char = '-';
  6.  
  7.         private static int s_height = Console.WindowHeight;
  8.         private static int s_width = Console.WindowWidth;
  9.         private static int s_textline_width = s_width - 10;
  10.  
  11.         private int _displayed;
  12.         private string _enterToContinue;
  13.         private List<DisplayLine> _lines;
  14.         private char _textLineCharacter = Default_TextLine_Char;
  15.  
  16.         public char TextLineCharacter {
  17.             get { return _textLineCharacter; }
  18.             set {
  19.                 if(value == char.MaxValue || value == char.MaxValue)
  20.                     _textLineCharacter = Default_TextLine_Char;
  21.                 else
  22.                     _textLineCharacter = value;
  23.             }
  24.         }
  25.  
  26.         public ConsoleDisplayHelper() {
  27.             this._lines = new List<DisplayLine>();
  28.         }
  29.  
  30.         public ConsoleDisplayHelper(string enterToContinueText) {
  31.             this._lines = new List<DisplayLine>();
  32.             this._enterToContinue = enterToContinueText;
  33.         }
  34.  
  35.         public void AddTextLine() {
  36.             this.AddTextLine(TextLineCharacter,s_textline_width);
  37.         }
  38.  
  39.         public void AddTextLine(char ch) {
  40.             this.AddTextLine(ch,s_textline_width);
  41.         }
  42.  
  43.         public void AddTextLine(char ch,int width) {
  44.             DisplayLine line = new DisplayLine {
  45.                 _body = this.GetTextLine(ch,width)
  46.             };
  47.             this._lines.Add(line);
  48.         }
  49.  
  50.         public string GetTextLine(char ch,int width) {
  51.             return "".PadLeft(width,ch);
  52.         }
  53.  
  54.         public void AddLineFormat(string format,params object[] parameters) {
  55.             DisplayLine line = new DisplayLine() {
  56.                 _body = string.Format(format,parameters),
  57.             };
  58.             this._lines.Add(line);
  59.         }
  60.  
  61.         public void AddLine() {
  62.             DisplayLine line = new DisplayLine();
  63.             this._lines.Add(line);
  64.         }
  65.  
  66.         public void AddLine(string body) {
  67.             DisplayLine line = new DisplayLine {
  68.                 _body = body
  69.             };
  70.             this._lines.Add(line);
  71.         }
  72.  
  73.         public void AddLine(string body,bool central) {
  74.             DisplayLine line = new DisplayLine {
  75.                 _body = body,
  76.                 _central = central
  77.             };
  78.             this._lines.Add(line);
  79.         }
  80.  
  81.         public void AddLine(string option,string body) {
  82.             DisplayLine line = new DisplayLine {
  83.                 _option = option,
  84.                 _body = body
  85.             };
  86.             this._lines.Add(line);
  87.         }
  88.  
  89.         public void AddSectionHead(string s) {
  90.             this.AddLine();
  91.             this.AddLine();
  92.             this.AddLine(s,true);
  93.             this.AddLine();
  94.         }
  95.  
  96.         public void Display() {
  97.             int num3 = 0;
  98.             int num4 = s_width / 4;
  99.             int num = 0;
  100.             int count = this._lines.Count;
  101.             while(num < count) {
  102.                 DisplayLine line = (DisplayLine)this._lines[num];
  103.                 if(line._option != null) {
  104.                     int length = line._option.Length;
  105.                     if((length > num3) && (length <= num4)) {
  106.                         num3 = length;
  107.                     }
  108.                 }
  109.                 num++;
  110.             }
  111.             num3 += 2;
  112.             num = 0;
  113.             count = this._lines.Count;
  114.             while(num < count) {
  115.                 DisplayLine line2 = (DisplayLine)this._lines[num];
  116.                 string str = line2._option;
  117.                 if(str != null) {
  118.                     Console.Write(str);
  119.                     if(str.Length > num4) {
  120.                         this.WriteOneLine(null);
  121.                         str = "";
  122.                     }
  123.                 }
  124.                 if(line2._central) {
  125.                     this.DisplayWordWrappedString(line2._body,0,(s_width - line2._body.Length) / 2);
  126.                 }
  127.                 else {
  128.                     this.DisplayWordWrappedString(line2._body,(str == null) ? 0 : str.Length,(str == null) ? 0 : num3);
  129.                 }
  130.                 num++;
  131.             }
  132.         }
  133.  
  134.         private void DisplayWordWrappedString(string s,int currentOffset,int leftMargin) {
  135.             bool flag = true;
  136.             if(s == null) {
  137.                 s = string.Empty;
  138.             }
  139.             foreach(string str in s.Split(new char[] { ' ' })) {
  140.                 int length = str.Length;
  141.                 if(!flag) {
  142.                     length++;
  143.                 }
  144.                 if((currentOffset + length) >= s_width) {
  145.                     this.WriteOneLine(null);
  146.                     currentOffset = 0;
  147.                     flag = true;
  148.                 }
  149.                 if(flag) {
  150.                     while(currentOffset < leftMargin) {
  151.                         Console.Write(' ');
  152.                         currentOffset++;
  153.                     }
  154.                 }
  155.                 else {
  156.                     Console.Write(' ');
  157.                     currentOffset++;
  158.                 }
  159.                 Console.Write(str);
  160.                 currentOffset += str.Length;
  161.                 flag = false;
  162.             }
  163.             this.WriteOneLine(null);
  164.         }
  165.  
  166.         private void WriteOneLine(string s) {
  167.             this._displayed++;
  168.             if((this._enterToContinue != null) && ((this._displayed % (s_height - 1)) == 0)) {
  169.                 Console.WriteLine();
  170.                 Console.Write(this._enterToContinue);
  171.                 Console.ReadLine();
  172.             }
  173.             Console.WriteLine(s);
  174.         }
  175.  
  176.         [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
  177.         private struct DisplayLine {
  178.             public string _option;
  179.             public string _body;
  180.             public bool _central;
  181.         }
  182.     }
  183.     #endregion
Advertisement
Add Comment
Please, Sign In to add comment