Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Console Display Helper
- public class ConsoleDisplayHelper {
- public const char Default_TextLine_Char = '-';
- private static int s_height = Console.WindowHeight;
- private static int s_width = Console.WindowWidth;
- private static int s_textline_width = s_width - 10;
- private int _displayed;
- private string _enterToContinue;
- private List<DisplayLine> _lines;
- private char _textLineCharacter = Default_TextLine_Char;
- public char TextLineCharacter {
- get { return _textLineCharacter; }
- set {
- if(value == char.MaxValue || value == char.MaxValue)
- _textLineCharacter = Default_TextLine_Char;
- else
- _textLineCharacter = value;
- }
- }
- public ConsoleDisplayHelper() {
- this._lines = new List<DisplayLine>();
- }
- public ConsoleDisplayHelper(string enterToContinueText) {
- this._lines = new List<DisplayLine>();
- this._enterToContinue = enterToContinueText;
- }
- public void AddTextLine() {
- this.AddTextLine(TextLineCharacter,s_textline_width);
- }
- public void AddTextLine(char ch) {
- this.AddTextLine(ch,s_textline_width);
- }
- public void AddTextLine(char ch,int width) {
- DisplayLine line = new DisplayLine {
- _body = this.GetTextLine(ch,width)
- };
- this._lines.Add(line);
- }
- public string GetTextLine(char ch,int width) {
- return "".PadLeft(width,ch);
- }
- public void AddLineFormat(string format,params object[] parameters) {
- DisplayLine line = new DisplayLine() {
- _body = string.Format(format,parameters),
- };
- this._lines.Add(line);
- }
- public void AddLine() {
- DisplayLine line = new DisplayLine();
- this._lines.Add(line);
- }
- public void AddLine(string body) {
- DisplayLine line = new DisplayLine {
- _body = body
- };
- this._lines.Add(line);
- }
- public void AddLine(string body,bool central) {
- DisplayLine line = new DisplayLine {
- _body = body,
- _central = central
- };
- this._lines.Add(line);
- }
- public void AddLine(string option,string body) {
- DisplayLine line = new DisplayLine {
- _option = option,
- _body = body
- };
- this._lines.Add(line);
- }
- public void AddSectionHead(string s) {
- this.AddLine();
- this.AddLine();
- this.AddLine(s,true);
- this.AddLine();
- }
- public void Display() {
- int num3 = 0;
- int num4 = s_width / 4;
- int num = 0;
- int count = this._lines.Count;
- while(num < count) {
- DisplayLine line = (DisplayLine)this._lines[num];
- if(line._option != null) {
- int length = line._option.Length;
- if((length > num3) && (length <= num4)) {
- num3 = length;
- }
- }
- num++;
- }
- num3 += 2;
- num = 0;
- count = this._lines.Count;
- while(num < count) {
- DisplayLine line2 = (DisplayLine)this._lines[num];
- string str = line2._option;
- if(str != null) {
- Console.Write(str);
- if(str.Length > num4) {
- this.WriteOneLine(null);
- str = "";
- }
- }
- if(line2._central) {
- this.DisplayWordWrappedString(line2._body,0,(s_width - line2._body.Length) / 2);
- }
- else {
- this.DisplayWordWrappedString(line2._body,(str == null) ? 0 : str.Length,(str == null) ? 0 : num3);
- }
- num++;
- }
- }
- private void DisplayWordWrappedString(string s,int currentOffset,int leftMargin) {
- bool flag = true;
- if(s == null) {
- s = string.Empty;
- }
- foreach(string str in s.Split(new char[] { ' ' })) {
- int length = str.Length;
- if(!flag) {
- length++;
- }
- if((currentOffset + length) >= s_width) {
- this.WriteOneLine(null);
- currentOffset = 0;
- flag = true;
- }
- if(flag) {
- while(currentOffset < leftMargin) {
- Console.Write(' ');
- currentOffset++;
- }
- }
- else {
- Console.Write(' ');
- currentOffset++;
- }
- Console.Write(str);
- currentOffset += str.Length;
- flag = false;
- }
- this.WriteOneLine(null);
- }
- private void WriteOneLine(string s) {
- this._displayed++;
- if((this._enterToContinue != null) && ((this._displayed % (s_height - 1)) == 0)) {
- Console.WriteLine();
- Console.Write(this._enterToContinue);
- Console.ReadLine();
- }
- Console.WriteLine(s);
- }
- [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
- private struct DisplayLine {
- public string _option;
- public string _body;
- public bool _central;
- }
- }
- #endregion
Advertisement
Add Comment
Please, Sign In to add comment