wasSharpNET – Blame information for rev 9

Subversion Repositories:
Rev:
Rev Author Line No. Line
9 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. //
5 ///////////////////////////////////////////////////////////////////////////
6  
7 using System;
8 using System.Collections.Generic;
9 using System.Linq;
10  
11 namespace wasSharpNET.Console
12 {
13 public static class ConsoleExtensions
14 {
15 public enum ConsoleTextAlignment
16 {
17 TOP_LEFT,
18 TOP_CENTER,
19 TOP_RIGHT
20 }
21  
22  
23 public static void WriteLine(this object data, ConsoleColor foreground,
24 ConsoleColor background)
25 {
26 var cFG = System.Console.ForegroundColor;
27 var cBG = System.Console.BackgroundColor;
28 System.Console.ForegroundColor = foreground;
29 System.Console.BackgroundColor = background;
30 System.Console.WriteLine(data);
31 System.Console.ForegroundColor = cFG;
32 System.Console.BackgroundColor = cBG;
33 }
34  
35 public static void WriteLine(this object data, ConsoleColor foreground)
36 {
37 var cFG = System.Console.ForegroundColor;
38 System.Console.ForegroundColor = foreground;
39 System.Console.WriteLine(data);
40 System.Console.ForegroundColor = cFG;
41 }
42  
43 public static void Write(this object data, ConsoleColor foreground,
44 ConsoleColor background)
45 {
46 var cFG = System.Console.ForegroundColor;
47 var cBG = System.Console.BackgroundColor;
48 System.Console.ForegroundColor = foreground;
49 System.Console.BackgroundColor = background;
50 System.Console.Write(data);
51 System.Console.ForegroundColor = cFG;
52 System.Console.BackgroundColor = cBG;
53 }
54  
55 public static void WriteLine(this object data, ConsoleTextAlignment alignment)
56 {
57 switch (alignment)
58 {
59 case ConsoleTextAlignment.TOP_CENTER:
60 System.Console.CursorLeft = Math.Max(System.Console.WindowWidth/2 - data.ToString().Length/2, 0);
61 WriteLine(data, System.Console.ForegroundColor, System.Console.BackgroundColor);
62 break;
63 default:
64 throw new NotImplementedException();
65 }
66 }
67  
68 public static void WriteLine(this object data, ConsoleTextAlignment alignment, ConsoleColor foregroundColor)
69 {
70 switch (alignment)
71 {
72 case ConsoleTextAlignment.TOP_CENTER:
73 System.Console.CursorLeft = Math.Max(System.Console.WindowWidth / 2 - data.ToString().Length / 2, 0);
74 WriteLine(data, foregroundColor, System.Console.BackgroundColor);
75 break;
76 default:
77 throw new NotImplementedException();
78 }
79 }
80  
81 public static void Write(this object data, ConsoleTextAlignment alignment)
82 {
83 switch (alignment)
84 {
85 case ConsoleTextAlignment.TOP_CENTER:
86 System.Console.CursorLeft = Math.Max(System.Console.WindowWidth/2 - data.ToString().Length/2, 0);
87 Write(data, System.Console.ForegroundColor, System.Console.BackgroundColor);
88 break;
89 default:
90 throw new NotImplementedException();
91 }
92 }
93  
94 public static void WriteLine(this IEnumerable<object> data, ConsoleTextAlignment alignment)
95 {
96 switch (alignment)
97 {
98 case ConsoleTextAlignment.TOP_CENTER:
99 var textBlock = data.Select(o => o.ToString()).ToArray();
100 var padding = Math.Max(System.Console.WindowWidth/2 - textBlock.Select(o => o.Length).Max()/2, 0);
101 foreach (var line in data)
102 {
103 System.Console.CursorLeft = padding;
104 WriteLine(line, System.Console.ForegroundColor, System.Console.BackgroundColor);
105 }
106 break;
107 default:
108 throw new NotImplementedException();
109 }
110 }
111 }
112 }