├── .editorconfig ├── .gitignore ├── FluentConsole.sln ├── FluentConsole ├── Colors.Generated.cs ├── Colors.Generated.tt ├── FluentConsole.Interfaces.cs ├── FluentConsole.cs ├── FluentConsole.csproj ├── FluentConsole.nuspec ├── Internal │ ├── ColorScope.cs │ ├── JetBrainsAnnotations.cs │ └── MainSyntax.cs └── Properties │ └── AssemblyInfo.cs ├── FluentConsoleDemo ├── App.config ├── FluentConsoleDemo.csproj ├── Program.cs └── Properties │ └── AssemblyInfo.cs ├── LICENSE.txt └── README.md /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_style = space 6 | indent_size = 4 7 | trim_trailing_whitespace = true 8 | insert_final_newline = false 9 | 10 | [*.nuspec] 11 | indent_size = 2 12 | 13 | [*.md] 14 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | bin/ 3 | obj/ 4 | *.suo 5 | *.user 6 | *.snk 7 | *.nupkg 8 | \#packages/ 9 | _ReSharper.* 10 | -------------------------------------------------------------------------------- /FluentConsole.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2012 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentConsole", "FluentConsole\FluentConsole.csproj", "{6EAB1403-E32F-406B-9EF6-4F998DD7A29A}" 5 | EndProject 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentConsoleDemo", "FluentConsoleDemo\FluentConsoleDemo.csproj", "{437F02CC-8523-49E4-8734-8B42AEEBB652}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {6EAB1403-E32F-406B-9EF6-4F998DD7A29A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {6EAB1403-E32F-406B-9EF6-4F998DD7A29A}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {6EAB1403-E32F-406B-9EF6-4F998DD7A29A}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {6EAB1403-E32F-406B-9EF6-4F998DD7A29A}.Release|Any CPU.Build.0 = Release|Any CPU 18 | {437F02CC-8523-49E4-8734-8B42AEEBB652}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {437F02CC-8523-49E4-8734-8B42AEEBB652}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {437F02CC-8523-49E4-8734-8B42AEEBB652}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {437F02CC-8523-49E4-8734-8B42AEEBB652}.Release|Any CPU.Build.0 = Release|Any CPU 22 | EndGlobalSection 23 | GlobalSection(SolutionProperties) = preSolution 24 | HideSolutionNode = FALSE 25 | EndGlobalSection 26 | EndGlobal 27 | -------------------------------------------------------------------------------- /FluentConsole/Colors.Generated.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | using System; 4 | using JetBrains.Annotations; 5 | 6 | partial class FluentConsole { 7 | partial interface IMainSyntax { 8 | /// 9 | /// Sets foreground color to for future calls to .Text and .Line. 10 | /// Only applies to calls made on instance it returns. 11 | /// 12 | [NotNull] 13 | IAfterColorSyntax Black { get; } 14 | 15 | /// 16 | /// Sets foreground color to for future calls to .Text and .Line. 17 | /// Only applies to calls made on instance it returns. 18 | /// 19 | [NotNull] 20 | IAfterColorSyntax DarkBlue { get; } 21 | 22 | /// 23 | /// Sets foreground color to for future calls to .Text and .Line. 24 | /// Only applies to calls made on instance it returns. 25 | /// 26 | [NotNull] 27 | IAfterColorSyntax DarkGreen { get; } 28 | 29 | /// 30 | /// Sets foreground color to for future calls to .Text and .Line. 31 | /// Only applies to calls made on instance it returns. 32 | /// 33 | [NotNull] 34 | IAfterColorSyntax DarkCyan { get; } 35 | 36 | /// 37 | /// Sets foreground color to for future calls to .Text and .Line. 38 | /// Only applies to calls made on instance it returns. 39 | /// 40 | [NotNull] 41 | IAfterColorSyntax DarkRed { get; } 42 | 43 | /// 44 | /// Sets foreground color to for future calls to .Text and .Line. 45 | /// Only applies to calls made on instance it returns. 46 | /// 47 | [NotNull] 48 | IAfterColorSyntax DarkMagenta { get; } 49 | 50 | /// 51 | /// Sets foreground color to for future calls to .Text and .Line. 52 | /// Only applies to calls made on instance it returns. 53 | /// 54 | [NotNull] 55 | IAfterColorSyntax DarkYellow { get; } 56 | 57 | /// 58 | /// Sets foreground color to for future calls to .Text and .Line. 59 | /// Only applies to calls made on instance it returns. 60 | /// 61 | [NotNull] 62 | IAfterColorSyntax Gray { get; } 63 | 64 | /// 65 | /// Sets foreground color to for future calls to .Text and .Line. 66 | /// Only applies to calls made on instance it returns. 67 | /// 68 | [NotNull] 69 | IAfterColorSyntax DarkGray { get; } 70 | 71 | /// 72 | /// Sets foreground color to for future calls to .Text and .Line. 73 | /// Only applies to calls made on instance it returns. 74 | /// 75 | [NotNull] 76 | IAfterColorSyntax Blue { get; } 77 | 78 | /// 79 | /// Sets foreground color to for future calls to .Text and .Line. 80 | /// Only applies to calls made on instance it returns. 81 | /// 82 | [NotNull] 83 | IAfterColorSyntax Green { get; } 84 | 85 | /// 86 | /// Sets foreground color to for future calls to .Text and .Line. 87 | /// Only applies to calls made on instance it returns. 88 | /// 89 | [NotNull] 90 | IAfterColorSyntax Cyan { get; } 91 | 92 | /// 93 | /// Sets foreground color to for future calls to .Text and .Line. 94 | /// Only applies to calls made on instance it returns. 95 | /// 96 | [NotNull] 97 | IAfterColorSyntax Red { get; } 98 | 99 | /// 100 | /// Sets foreground color to for future calls to .Text and .Line. 101 | /// Only applies to calls made on instance it returns. 102 | /// 103 | [NotNull] 104 | IAfterColorSyntax Magenta { get; } 105 | 106 | /// 107 | /// Sets foreground color to for future calls to .Text and .Line. 108 | /// Only applies to calls made on instance it returns. 109 | /// 110 | [NotNull] 111 | IAfterColorSyntax Yellow { get; } 112 | 113 | /// 114 | /// Sets foreground color to for future calls to .Text and .Line. 115 | /// Only applies to calls made on instance it returns. 116 | /// 117 | [NotNull] 118 | IAfterColorSyntax White { get; } 119 | 120 | } 121 | 122 | /// 123 | /// Sets foreground color to for future calls to .Text and .Line. 124 | /// Only applies to calls made on instance it returns. 125 | /// 126 | [NotNull] 127 | public static IAfterColorSyntax Black { 128 | get { return instance.Black; } 129 | } 130 | 131 | /// 132 | /// Sets foreground color to for future calls to .Text and .Line. 133 | /// Only applies to calls made on instance it returns. 134 | /// 135 | [NotNull] 136 | public static IAfterColorSyntax DarkBlue { 137 | get { return instance.DarkBlue; } 138 | } 139 | 140 | /// 141 | /// Sets foreground color to for future calls to .Text and .Line. 142 | /// Only applies to calls made on instance it returns. 143 | /// 144 | [NotNull] 145 | public static IAfterColorSyntax DarkGreen { 146 | get { return instance.DarkGreen; } 147 | } 148 | 149 | /// 150 | /// Sets foreground color to for future calls to .Text and .Line. 151 | /// Only applies to calls made on instance it returns. 152 | /// 153 | [NotNull] 154 | public static IAfterColorSyntax DarkCyan { 155 | get { return instance.DarkCyan; } 156 | } 157 | 158 | /// 159 | /// Sets foreground color to for future calls to .Text and .Line. 160 | /// Only applies to calls made on instance it returns. 161 | /// 162 | [NotNull] 163 | public static IAfterColorSyntax DarkRed { 164 | get { return instance.DarkRed; } 165 | } 166 | 167 | /// 168 | /// Sets foreground color to for future calls to .Text and .Line. 169 | /// Only applies to calls made on instance it returns. 170 | /// 171 | [NotNull] 172 | public static IAfterColorSyntax DarkMagenta { 173 | get { return instance.DarkMagenta; } 174 | } 175 | 176 | /// 177 | /// Sets foreground color to for future calls to .Text and .Line. 178 | /// Only applies to calls made on instance it returns. 179 | /// 180 | [NotNull] 181 | public static IAfterColorSyntax DarkYellow { 182 | get { return instance.DarkYellow; } 183 | } 184 | 185 | /// 186 | /// Sets foreground color to for future calls to .Text and .Line. 187 | /// Only applies to calls made on instance it returns. 188 | /// 189 | [NotNull] 190 | public static IAfterColorSyntax Gray { 191 | get { return instance.Gray; } 192 | } 193 | 194 | /// 195 | /// Sets foreground color to for future calls to .Text and .Line. 196 | /// Only applies to calls made on instance it returns. 197 | /// 198 | [NotNull] 199 | public static IAfterColorSyntax DarkGray { 200 | get { return instance.DarkGray; } 201 | } 202 | 203 | /// 204 | /// Sets foreground color to for future calls to .Text and .Line. 205 | /// Only applies to calls made on instance it returns. 206 | /// 207 | [NotNull] 208 | public static IAfterColorSyntax Blue { 209 | get { return instance.Blue; } 210 | } 211 | 212 | /// 213 | /// Sets foreground color to for future calls to .Text and .Line. 214 | /// Only applies to calls made on instance it returns. 215 | /// 216 | [NotNull] 217 | public static IAfterColorSyntax Green { 218 | get { return instance.Green; } 219 | } 220 | 221 | /// 222 | /// Sets foreground color to for future calls to .Text and .Line. 223 | /// Only applies to calls made on instance it returns. 224 | /// 225 | [NotNull] 226 | public static IAfterColorSyntax Cyan { 227 | get { return instance.Cyan; } 228 | } 229 | 230 | /// 231 | /// Sets foreground color to for future calls to .Text and .Line. 232 | /// Only applies to calls made on instance it returns. 233 | /// 234 | [NotNull] 235 | public static IAfterColorSyntax Red { 236 | get { return instance.Red; } 237 | } 238 | 239 | /// 240 | /// Sets foreground color to for future calls to .Text and .Line. 241 | /// Only applies to calls made on instance it returns. 242 | /// 243 | [NotNull] 244 | public static IAfterColorSyntax Magenta { 245 | get { return instance.Magenta; } 246 | } 247 | 248 | /// 249 | /// Sets foreground color to for future calls to .Text and .Line. 250 | /// Only applies to calls made on instance it returns. 251 | /// 252 | [NotNull] 253 | public static IAfterColorSyntax Yellow { 254 | get { return instance.Yellow; } 255 | } 256 | 257 | /// 258 | /// Sets foreground color to for future calls to .Text and .Line. 259 | /// Only applies to calls made on instance it returns. 260 | /// 261 | [NotNull] 262 | public static IAfterColorSyntax White { 263 | get { return instance.White; } 264 | } 265 | 266 | } 267 | 268 | namespace FluentConsoleInternal { 269 | partial class MainSyntax { 270 | public FluentConsole.IAfterColorSyntax Black { 271 | get { return Color(ConsoleColor.Black); } 272 | } 273 | 274 | public FluentConsole.IAfterColorSyntax DarkBlue { 275 | get { return Color(ConsoleColor.DarkBlue); } 276 | } 277 | 278 | public FluentConsole.IAfterColorSyntax DarkGreen { 279 | get { return Color(ConsoleColor.DarkGreen); } 280 | } 281 | 282 | public FluentConsole.IAfterColorSyntax DarkCyan { 283 | get { return Color(ConsoleColor.DarkCyan); } 284 | } 285 | 286 | public FluentConsole.IAfterColorSyntax DarkRed { 287 | get { return Color(ConsoleColor.DarkRed); } 288 | } 289 | 290 | public FluentConsole.IAfterColorSyntax DarkMagenta { 291 | get { return Color(ConsoleColor.DarkMagenta); } 292 | } 293 | 294 | public FluentConsole.IAfterColorSyntax DarkYellow { 295 | get { return Color(ConsoleColor.DarkYellow); } 296 | } 297 | 298 | public FluentConsole.IAfterColorSyntax Gray { 299 | get { return Color(ConsoleColor.Gray); } 300 | } 301 | 302 | public FluentConsole.IAfterColorSyntax DarkGray { 303 | get { return Color(ConsoleColor.DarkGray); } 304 | } 305 | 306 | public FluentConsole.IAfterColorSyntax Blue { 307 | get { return Color(ConsoleColor.Blue); } 308 | } 309 | 310 | public FluentConsole.IAfterColorSyntax Green { 311 | get { return Color(ConsoleColor.Green); } 312 | } 313 | 314 | public FluentConsole.IAfterColorSyntax Cyan { 315 | get { return Color(ConsoleColor.Cyan); } 316 | } 317 | 318 | public FluentConsole.IAfterColorSyntax Red { 319 | get { return Color(ConsoleColor.Red); } 320 | } 321 | 322 | public FluentConsole.IAfterColorSyntax Magenta { 323 | get { return Color(ConsoleColor.Magenta); } 324 | } 325 | 326 | public FluentConsole.IAfterColorSyntax Yellow { 327 | get { return Color(ConsoleColor.Yellow); } 328 | } 329 | 330 | public FluentConsole.IAfterColorSyntax White { 331 | get { return Color(ConsoleColor.White); } 332 | } 333 | 334 | } 335 | } -------------------------------------------------------------------------------- /FluentConsole/Colors.Generated.tt: -------------------------------------------------------------------------------- 1 | <#@ template debug="false" hostspecific="false" language="C#" #> 2 | <#@ assembly name="System.Core" #> 3 | <#@ import namespace="System" #> 4 | <#@ import namespace="System.Linq" #> 5 | <#@ import namespace="System.Text" #> 6 | <#@ import namespace="System.Collections.Generic" #> 7 | <#@ output extension=".cs" #> 8 | 9 | <# var colors = Enum.GetNames(typeof(ConsoleColor)); #> 10 | 11 | using System; 12 | using JetBrains.Annotations; 13 | 14 | partial class FluentConsole { 15 | partial interface IMainSyntax { 16 | <# 17 | foreach (var color in colors) { 18 | #> 19 | /// 20 | /// Sets foreground color to for future calls to .Text and .Line. 21 | /// Only applies to calls made on instance it returns. 22 | /// 23 | [NotNull] 24 | IAfterColorSyntax <#= color #> { get; } 25 | 26 | <# 27 | } 28 | #> 29 | } 30 | 31 | <# 32 | foreach (var color in colors) { 33 | #> 34 | /// 35 | /// Sets foreground color to for future calls to .Text and .Line. 36 | /// Only applies to calls made on instance it returns. 37 | /// 38 | [NotNull] 39 | public static IAfterColorSyntax <#= color #> { 40 | get { return instance.<#= color #>; } 41 | } 42 | 43 | <# 44 | } 45 | #> 46 | } 47 | 48 | namespace FluentConsoleInternal { 49 | partial class MainSyntax { 50 | <# 51 | foreach (var color in colors) { 52 | #> 53 | public FluentConsole.IAfterColorSyntax <#= color #> { 54 | get { return Color(ConsoleColor.<#= color #>); } 55 | } 56 | 57 | <# 58 | } 59 | #> 60 | } 61 | } -------------------------------------------------------------------------------- /FluentConsole/FluentConsole.Interfaces.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using JetBrains.Annotations; 5 | 6 | // ReSharper disable CheckNamespace 7 | // ReSharper disable MethodOverloadWithOptionalParameter 8 | 9 | partial class FluentConsole { 10 | /// 11 | /// Main fluent interface. 12 | /// 13 | [EditorBrowsable(EditorBrowsableState.Never)] 14 | public partial interface IMainSyntax : ITextSyntax { 15 | /// 16 | /// Writes a new line to the console (same as ). 17 | /// 18 | [NotNull] 19 | IMainSyntax NewLine(); 20 | 21 | /// 22 | /// Sets the foreground color for future calls to .Text and .Line. 23 | /// Only applies to calls made on instance it returns. 24 | /// 25 | [NotNull] 26 | IAfterColorSyntax Color(ConsoleColor color); 27 | 28 | /// 29 | /// Applies to the current instance and returns its result. 30 | /// May be useful to set the color conditionally, for example. 31 | /// 32 | TSyntax With([NotNull, InstantHandle] Func transform); 33 | } 34 | 35 | /// 36 | /// Fluent interface used to write text on the console. 37 | /// 38 | [EditorBrowsable(EditorBrowsableState.Never)] 39 | public interface ITextSyntax { 40 | /// 41 | /// Writes an object to the console (same as ). 42 | /// 43 | [NotNull] 44 | IMainSyntax Text(object value); 45 | 46 | /// 47 | /// Writes a string to the console (same as ). 48 | /// 49 | [NotNull] 50 | IMainSyntax Text(string value); 51 | 52 | /// 53 | /// Writes a formatted string to the console (same as ). 54 | /// 55 | [NotNull] 56 | [StringFormatMethod("format")] 57 | IMainSyntax Text(string format, params object[] args); 58 | 59 | /// 60 | /// Writes an object to the console (same as ). 61 | /// 62 | [NotNull] 63 | IMainSyntax Line(object value); 64 | 65 | /// 66 | /// Writes a string to the console (same as ). 67 | /// 68 | [NotNull] 69 | IMainSyntax Line(string value); 70 | 71 | /// 72 | /// Writes a formatted string to the console (same as ). 73 | /// 74 | [NotNull] 75 | [StringFormatMethod("format")] 76 | IMainSyntax Line(string format, params object[] args); 77 | } 78 | 79 | /// 80 | /// Fluent interface with only methods available after call. 81 | /// 82 | [EditorBrowsable(EditorBrowsableState.Never)] 83 | public interface IAfterColorSyntax : ITextSyntax { 84 | /// 85 | /// Applies current color selection to the background instead of the foreground. 86 | /// 87 | [NotNull] 88 | IMainSyntax Background { get; } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /FluentConsole/FluentConsole.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using FluentConsoleInternal; 4 | using JetBrains.Annotations; 5 | 6 | // ReSharper disable CheckNamespace 7 | // ReSharper disable MethodOverloadWithOptionalParameter 8 | 9 | /// 10 | /// Provides fluent API over console methods (mostly color-related). 11 | /// 12 | public static partial class FluentConsole { 13 | private static readonly MainSyntax instance = new MainSyntax(); 14 | 15 | /// 16 | /// Returns an instance of the FluentConsole syntax. 17 | /// Always returns the same instance. Rarely needed. 18 | /// 19 | [NotNull] 20 | public static IMainSyntax Instance { 21 | get { return instance; } 22 | } 23 | 24 | /// 25 | /// Writes an object to the console (same as ). 26 | /// 27 | [NotNull] 28 | public static IMainSyntax Text(object value) { 29 | return instance.Text(value); 30 | } 31 | 32 | /// 33 | /// Writes a string to the console (same as ). 34 | /// 35 | [NotNull] 36 | public static IMainSyntax Text(string value) { 37 | return instance.Text(value); 38 | } 39 | 40 | /// 41 | /// Writes a formatted string to the console (same as ). 42 | /// 43 | [NotNull] 44 | [StringFormatMethod("format")] 45 | public static IMainSyntax Text(string format, params object[] args) { 46 | return instance.Text(format, args); 47 | } 48 | 49 | /// 50 | /// Writes an object to the console (same as ). 51 | /// 52 | [NotNull] 53 | public static IMainSyntax Line(object value) { 54 | return instance.Line(value); 55 | } 56 | 57 | /// 58 | /// Writes a string to the console (same as ). 59 | /// 60 | [NotNull] 61 | public static IMainSyntax Line(string value) { 62 | return instance.Line(value); 63 | } 64 | 65 | /// 66 | /// Writes a formatted string to the console (same as ). 67 | /// 68 | [NotNull] 69 | [StringFormatMethod("format")] 70 | public static IMainSyntax Line(string format, params object[] args) { 71 | return instance.Line(format, args); 72 | } 73 | 74 | /// 75 | /// Writes a new line to the console (same as ). 76 | /// 77 | [NotNull] 78 | public static IMainSyntax NewLine() { 79 | return instance.NewLine(); 80 | } 81 | 82 | /// 83 | /// Sets the foreground color for future calls to .Text and .Line. 84 | /// Only applies to calls made on instance it returns. 85 | /// 86 | [NotNull] 87 | public static IAfterColorSyntax Color(ConsoleColor color) { 88 | return instance.Color(color); 89 | } 90 | 91 | /// 92 | /// Applies to and returns its result. 93 | /// May be useful to set the color conditionally, for example. 94 | /// 95 | public static TSyntax With([NotNull, InstantHandle] Func transform) { 96 | return transform(Instance); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /FluentConsole/FluentConsole.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {6EAB1403-E32F-406B-9EF6-4F998DD7A29A} 8 | Library 9 | Properties 10 | FluentConsole 11 | FluentConsole 12 | v3.5 13 | 512 14 | Client 15 | 16 | 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | bin\Debug\FluentConsole.XML 25 | 26 | 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | bin\Release\FluentConsole.XML 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | True 46 | True 47 | Colors.Generated.tt 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | TextTemplatingFileGenerator 59 | Colors.Generated.cs 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 74 | -------------------------------------------------------------------------------- /FluentConsole/FluentConsole.nuspec: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $id$ 5 | $version$ 6 | $title$ 7 | Andrey Shchekin 8 | https://github.com/ashmind/FluentConsole 9 | https://github.com/ashmind/FluentConsole/blob/master/LICENSE.txt 10 | false 11 | $description$ 12 | Added missing ReSharper annotations. 13 | console 14 | 15 | -------------------------------------------------------------------------------- /FluentConsole/Internal/ColorScope.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | // ReSharper disable CheckNamespace 4 | namespace FluentConsoleInternal { 5 | // ReSharper enable CheckNamespace 6 | internal class ColorScope : MainSyntax, FluentConsole.IAfterColorSyntax { 7 | private readonly ConsoleColor? foreground; 8 | private readonly ConsoleColor? background; 9 | private ConsoleColor? savedForeground; 10 | private ConsoleColor? savedBackground; 11 | 12 | public ColorScope(ConsoleColor? foreground, ConsoleColor? background) { 13 | this.foreground = foreground; 14 | this.background = background; 15 | } 16 | 17 | public FluentConsole.IMainSyntax Background { 18 | get { return new ColorScope(null, this.foreground); } 19 | } 20 | 21 | protected override void BeforeText() { 22 | this.savedBackground = Console.BackgroundColor; 23 | if (this.foreground != null) { 24 | this.savedForeground = Console.ForegroundColor; 25 | Console.ForegroundColor = this.foreground.Value; 26 | } 27 | 28 | if (this.background != null) { 29 | this.savedBackground = Console.BackgroundColor; 30 | Console.BackgroundColor = this.background.Value; 31 | } 32 | } 33 | 34 | protected override void AfterText() { 35 | if (this.savedForeground != null) 36 | Console.ForegroundColor = this.savedForeground.Value; 37 | 38 | if (this.savedBackground != null) 39 | Console.BackgroundColor = this.savedBackground.Value; 40 | } 41 | 42 | public override FluentConsole.IAfterColorSyntax Color(ConsoleColor color) { 43 | return new ColorScope(color, this.background); 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /FluentConsole/Internal/JetBrainsAnnotations.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2007-2012 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | using System; 18 | 19 | // ReSharper disable CheckNamespace 20 | namespace JetBrains.Annotations { 21 | // ReSharper restore CheckNamespace 22 | /// 23 | /// Indicates that the marked method builds string by format pattern and (optional) arguments. 24 | /// Parameter, which contains format string, should be given in constructor. 25 | /// The format string should be in -like form 26 | /// 27 | /// 28 | /// 29 | /// [StringFormatMethod("message")] 30 | /// public void ShowError(string message, params object[] args) 31 | /// { 32 | /// //Do something 33 | /// } 34 | /// public void Foo() 35 | /// { 36 | /// ShowError("Failed: {0}"); // Warning: Non-existing argument in format string 37 | /// } 38 | /// 39 | /// 40 | [AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] 41 | internal sealed class StringFormatMethodAttribute : Attribute { 42 | /// 43 | /// Initializes new instance of StringFormatMethodAttribute 44 | /// 45 | /// Specifies which parameter of an annotated method should be treated as format-string 46 | public StringFormatMethodAttribute(string formatParameterName) { 47 | FormatParameterName = formatParameterName; 48 | } 49 | 50 | /// 51 | /// Gets format parameter name 52 | /// 53 | public string FormatParameterName { get; private set; } 54 | } 55 | 56 | /// 57 | /// Indicates that the function argument should be string literal and match one of the parameters 58 | /// of the caller function. 59 | /// For example, ReSharper annotates the parameter of . 60 | /// 61 | /// 62 | /// 63 | /// public void Foo(string param) 64 | /// { 65 | /// if (param == null) 66 | /// throw new ArgumentNullException("par"); // Warning: Cannot resolve symbol 67 | /// } 68 | /// 69 | /// 70 | [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)] 71 | internal sealed class InvokerParameterNameAttribute : Attribute { } 72 | 73 | /// 74 | /// Indicates that the value of the marked element could be null sometimes, 75 | /// so the check for null is necessary before its usage. 76 | /// 77 | /// 78 | /// 79 | /// [CanBeNull] 80 | /// public object Test() 81 | /// { 82 | /// return null; 83 | /// } 84 | /// 85 | /// public void UseTest() 86 | /// { 87 | /// var p = Test(); 88 | /// var s = p.ToString(); // Warning: Possible 'System.NullReferenceException' 89 | /// } 90 | /// 91 | /// 92 | [AttributeUsage(AttributeTargets.Method | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.Delegate | AttributeTargets.Field, AllowMultiple = false, Inherited = true)] 93 | internal sealed class CanBeNullAttribute : Attribute { } 94 | 95 | /// 96 | /// Indicates that the value of the marked element could never be null 97 | /// 98 | /// 99 | /// 100 | /// [NotNull] 101 | /// public object Foo() 102 | /// { 103 | /// return null; // Warning: Possible 'null' assignment 104 | /// } 105 | /// 106 | /// 107 | [AttributeUsage(AttributeTargets.Method | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.Delegate | AttributeTargets.Field, AllowMultiple = false, Inherited = true)] 108 | internal sealed class NotNullAttribute : Attribute { } 109 | 110 | /// 111 | /// Describes dependency between method input and output. 112 | /// 113 | /// 114 | ///

Function Definition Table syntax:

115 | /// 116 | /// FDT ::= FDTRow [;FDTRow]* 117 | /// FDTRow ::= Input => Output | Output <= Input 118 | /// Input ::= ParameterName: Value [, Input]* 119 | /// Output ::= [ParameterName: Value]* {halt|stop|void|nothing|Value} 120 | /// Value ::= true | false | null | notnull | canbenull 121 | /// 122 | /// If method has single input parameter, it's name could be omitted.
123 | /// Using halt (or void/nothing, which is the same) for method output means that the methos doesn't return normally.
124 | /// canbenull annotation is only applicable for output parameters.
125 | /// You can use multiple [ContractAnnotation] for each FDT row, or use single attribute with rows separated by semicolon.
126 | ///
127 | /// 128 | /// 129 | /// 130 | /// [ContractAnnotation("=> halt")] 131 | /// public void TerminationMethod() 132 | /// 133 | /// 134 | /// [ContractAnnotation("halt <= condition: false")] 135 | /// public void Assert(bool condition, string text) // Regular Assertion method 136 | /// 137 | /// 138 | /// [ContractAnnotation("s:null => true")] 139 | /// public bool IsNullOrEmpty(string s) // String.IsNullOrEmpty 140 | /// 141 | /// 142 | /// // A method that returns null if the parameter is null, and not null if the parameter is not null 143 | /// [ContractAnnotation("null => null; notnull => notnull")] 144 | /// public object Transform(object data) 145 | /// 146 | /// 147 | /// [ContractAnnotation("s:null=>false; =>true,result:notnull; =>false, result:null")] 148 | /// public bool TryParse(string s, out Person result) 149 | /// 150 | /// 151 | /// 152 | [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)] 153 | internal sealed class ContractAnnotationAttribute : Attribute { 154 | public ContractAnnotationAttribute([NotNull] string fdt) 155 | : this(fdt, false) { 156 | } 157 | 158 | public ContractAnnotationAttribute([NotNull] string fdt, bool forceFullStates) { 159 | FDT = fdt; 160 | ForceFullStates = forceFullStates; 161 | } 162 | 163 | public string FDT { get; private set; } 164 | public bool ForceFullStates { get; private set; } 165 | } 166 | 167 | /// 168 | /// Tells code analysis engine if the parameter is completely handled when the invoked method is on stack. 169 | /// If the parameter is a delegate, indicates that delegate is executed while the method is executed. 170 | /// If the parameter is an enumerable, indicates that it is enumerated while the method is executed. 171 | /// 172 | [AttributeUsage(AttributeTargets.Parameter, Inherited = true)] 173 | internal sealed class InstantHandleAttribute : Attribute { } 174 | 175 | 176 | /// 177 | /// Indicates that a method does not make any observable state changes. 178 | /// The same as System.Diagnostics.Contracts.PureAttribute 179 | /// 180 | /// 181 | /// 182 | /// [Pure] 183 | /// private int Multiply(int x, int y) 184 | /// { 185 | /// return x*y; 186 | /// } 187 | /// 188 | /// public void Foo() 189 | /// { 190 | /// const int a=2, b=2; 191 | /// Multiply(a, b); // Waring: Return value of pure method is not used 192 | /// } 193 | /// 194 | /// 195 | [AttributeUsage(AttributeTargets.Method, Inherited = true)] 196 | internal sealed class PureAttribute : Attribute { } 197 | } -------------------------------------------------------------------------------- /FluentConsole/Internal/MainSyntax.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using JetBrains.Annotations; 3 | 4 | // ReSharper disable CheckNamespace 5 | namespace FluentConsoleInternal { 6 | // ReSharper restore CheckNamespace 7 | internal partial class MainSyntax : FluentConsole.IMainSyntax { 8 | protected virtual void BeforeText() { } 9 | protected virtual void AfterText() { } 10 | 11 | public FluentConsole.IMainSyntax Text(object value) { 12 | BeforeText(); 13 | try { 14 | Console.Write(value); 15 | } 16 | finally { 17 | AfterText(); 18 | } 19 | return this; 20 | } 21 | 22 | public FluentConsole.IMainSyntax Text(string value) { 23 | BeforeText(); 24 | try { 25 | Console.Write(value); 26 | } 27 | finally { 28 | AfterText(); 29 | } 30 | return this; 31 | } 32 | 33 | [StringFormatMethod("format")] 34 | // ReSharper disable MethodOverloadWithOptionalParameter 35 | public FluentConsole.IMainSyntax Text(string format, params object[] args) { 36 | // ReSharper restore MethodOverloadWithOptionalParameter 37 | BeforeText(); 38 | try { 39 | Console.Write(format, args); 40 | } 41 | finally { 42 | AfterText(); 43 | } 44 | return this; 45 | } 46 | 47 | public FluentConsole.IMainSyntax Line(object value) { 48 | BeforeText(); 49 | try { 50 | Console.WriteLine(value); 51 | } 52 | finally { 53 | AfterText(); 54 | } 55 | return this; 56 | } 57 | 58 | public FluentConsole.IMainSyntax Line(string value) { 59 | BeforeText(); 60 | try { 61 | Console.WriteLine(value); 62 | } 63 | finally { 64 | AfterText(); 65 | } 66 | return this; 67 | } 68 | 69 | [StringFormatMethod("format")] 70 | // ReSharper disable MethodOverloadWithOptionalParameter 71 | public FluentConsole.IMainSyntax Line(string format, params object[] args) { 72 | // ReSharper restore MethodOverloadWithOptionalParameter 73 | BeforeText(); 74 | try { 75 | Console.WriteLine(format, args); 76 | } 77 | finally { 78 | AfterText(); 79 | } 80 | return this; 81 | } 82 | 83 | public FluentConsole.IMainSyntax NewLine() { 84 | Console.WriteLine(); 85 | return this; 86 | } 87 | 88 | public virtual FluentConsole.IAfterColorSyntax Color(ConsoleColor color) { 89 | return new ColorScope(color, null); 90 | } 91 | 92 | public TSyntax With(Func transform) { 93 | return transform(this); 94 | } 95 | } 96 | } -------------------------------------------------------------------------------- /FluentConsole/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("FluentConsole")] 8 | [assembly: AssemblyDescription(@" 9 | A fluent API that aims to simplify colorful console output. 10 | FluentConsole.White.Text(""Hello "").Green.Text(""world!"")")] 11 | [assembly: AssemblyCopyright("Copyright © Andrey Shchekin 2014")] 12 | 13 | // Setting ComVisible to false makes the types in this assembly not visible 14 | // to COM components. If you need to access a type in this assembly from 15 | // COM, set the ComVisible attribute to true on that type. 16 | [assembly: ComVisible(false)] 17 | 18 | // The following GUID is for the ID of the typelib if this project is exposed to COM 19 | [assembly: Guid("f9919ae9-d8bb-4a65-b72c-fed9723bb5a8")] 20 | 21 | // Version information for an assembly consists of the following four values: 22 | // 23 | // Major Version 24 | // Minor Version 25 | // Build Number 26 | // Revision 27 | // 28 | // You can specify all the values or you can default the Build and Revision Numbers 29 | // by using the '*' as shown below: 30 | // [assembly: AssemblyVersion("1.0.*")] 31 | [assembly: AssemblyVersion("0.8.3.0")] 32 | [assembly: AssemblyFileVersion("0.8.3.0")] -------------------------------------------------------------------------------- /FluentConsoleDemo/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /FluentConsoleDemo/FluentConsoleDemo.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {437F02CC-8523-49E4-8734-8B42AEEBB652} 8 | Exe 9 | Properties 10 | FluentConsoleDemo 11 | FluentConsoleDemo 12 | v4.5 13 | 512 14 | 15 | 16 | AnyCPU 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | AnyCPU 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | {6eab1403-e32f-406b-9ef6-4f998dd7a29a} 53 | FluentConsole 54 | 55 | 56 | 57 | 64 | -------------------------------------------------------------------------------- /FluentConsoleDemo/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace FluentConsoleDemo { 6 | public class Program { 7 | public static void Main(string[] args) { 8 | FluentConsole 9 | .White.Background.Black.Line("Black"); 10 | 11 | FluentConsole 12 | .Cyan.Line("Cyan") 13 | .DarkBlue.Line("DarkBlue") 14 | .DarkCyan.Line("DarkCyan") 15 | .DarkGray.Line("DarkGray") 16 | .DarkGreen.Line("DarkGreen") 17 | .DarkMagenta.Line("DarkMagenta") 18 | .DarkRed.Line("DarkRed") 19 | .DarkYellow.Line("DarkYellow") 20 | .Gray.Line("Gray") 21 | .Green.Line("Green") 22 | .Magenta.Line("Magenta") 23 | .Red.Line("Red") 24 | .White.Line("White") 25 | .Yellow.Line("Yellow"); 26 | 27 | Console.ReadKey(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /FluentConsoleDemo/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("FluentConsoleDemo")] 8 | [assembly: AssemblyCopyright("Copyright © Andrey Shchekin 2014")] 9 | 10 | // Setting ComVisible to false makes the types in this assembly not visible 11 | // to COM components. If you need to access a type in this assembly from 12 | // COM, set the ComVisible attribute to true on that type. 13 | [assembly: ComVisible(false)] 14 | 15 | // The following GUID is for the ID of the typelib if this project is exposed to COM 16 | [assembly: Guid("9c95a00a-082b-4bc9-82c0-4ae453d7324f")] 17 | 18 | // Version information for an assembly consists of the following four values: 19 | // 20 | // Major Version 21 | // Minor Version 22 | // Build Number 23 | // Revision 24 | // 25 | // You can specify all the values or you can default the Build and Revision Numbers 26 | // by using the '*' as shown below: 27 | // [assembly: AssemblyVersion("1.0.*")] 28 | [assembly: AssemblyVersion("1.0.0.0")] 29 | [assembly: AssemblyFileVersion("1.0.0.0")] 30 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, Andrey Shchekin 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 18 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 21 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Usage 2 | 3 | ### Static 4 | 5 | FluentConsole 6 | .White.Background 7 | .Green.Line("Hello there!"); 8 | 9 | FluentConsole 10 | .White.Text("Fluent") 11 | .Red.Text("Console"); 12 | 13 | ### Instance 14 | 15 | var console = FluentConsole.Instance; 16 | 17 | console.Blue.Line(1); 18 | console.Cyan.Line(2); 19 | 20 | ### Tips 21 | 22 | #### Conditional 23 | 24 | FluentConsole 25 | .White.Text("Result: ") 26 | .With(c => result.IsSuccess ? c.Green : c.Red) 27 | .Line(result.Code) 28 | 29 | ## NuGet 30 | https://www.nuget.org/packages/FluentConsole/ 31 | 32 | Install-Package FluentConsole 33 | --------------------------------------------------------------------------------