Skip to content

Commit 18ec6ab

Browse files
committed
Feat: Colored text
1 parent 921aef3 commit 18ec6ab

File tree

5 files changed

+93
-25
lines changed

5 files changed

+93
-25
lines changed

src/CodeOfChaos.CliArgsParser.Library/CodeOfChaos.CliArgsParser.Library.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@
3535

3636
<ItemGroup>
3737
<PackageReference Include="AterraEngine.Unions" Version="3.9.0" />
38+
<PackageReference Include="CodeOfChaos.Ansi" Version="1.0.0" />
3839
</ItemGroup>
3940
</Project>

src/CodeOfChaos.CliArgsParser.Library/Commands/VersionBump/VersionBumpCommand.cs

+25-19
Original file line numberDiff line numberDiff line change
@@ -17,54 +17,60 @@ public partial class VersionBumpCommand : ICommand<VersionBumpParameters> {
1717
// Methods
1818
// -----------------------------------------------------------------------------------------------------------------
1919
public async Task ExecuteAsync(VersionBumpParameters parameters) {
20-
Console.WriteLine("Bumping version...");
20+
Console.WriteLine(ConsoleTextStore.BumpingVersion);
2121
SuccessOrFailure<SemanticVersionDto> bumpResult = await BumpVersion(parameters);
2222
if (bumpResult is { IsFailure: true, AsFailure.Value: var errorBumping }) {
23-
Console.WriteLine(errorBumping);
23+
Console.WriteLine(ConsoleTextStore.CommandEndFailure(errorBumping));
2424
return;
2525
}
2626

2727
SemanticVersionDto updatedVersion = bumpResult.AsSuccess.Value;
2828

29-
Console.WriteLine("Git committing ...");
29+
Console.WriteLine(ConsoleTextStore.GitCommitting);
3030
SuccessOrFailure gitCommitResult = await GitHelpers.TryCreateGitCommit(updatedVersion);
3131
if (gitCommitResult is { IsFailure: true, AsFailure.Value: var errorCommiting }) {
32-
Console.WriteLine(errorCommiting);
32+
Console.WriteLine(ConsoleTextStore.CommandEndFailure(errorCommiting));
3333
return;
3434
}
35+
36+
// Ask the user for extra input to make sure they want to push the current tag.
37+
if (!parameters.Force) {
38+
Console.WriteLine(ConsoleTextStore.QuestionTagAndCommit);
39+
string? input = Console.ReadLine()?.ToLowerInvariant();
40+
if (input is not "y") {
41+
Console.WriteLine(ConsoleTextStore.CommandEndSuccess());
42+
return;
43+
}
44+
}
3545

36-
Console.WriteLine("Git tagging ...");
46+
Console.WriteLine(ConsoleTextStore.GitTagging);
3747
SuccessOrFailure gitTagResult = await GitHelpers.TryCreateGitTag(updatedVersion);
3848
if (gitTagResult is { IsFailure: true, AsFailure.Value: var errorTagging }) {
39-
Console.WriteLine(errorTagging);
49+
Console.WriteLine(ConsoleTextStore.CommandEndFailure(errorTagging));
4050
return;
4151
}
4252

43-
Console.WriteLine($"Version {updatedVersion} committed and tagged successfully.");
53+
Console.WriteLine(ConsoleTextStore.TagSuccessful(updatedVersion));
4454

45-
if (!parameters.PushToRemote) return;
46-
47-
// Ask the user for extra input to make sure they want to push the current tag.
48-
if (!parameters.Force) {
49-
Console.WriteLine("Do you want to push to origin? (y/n)");
50-
string? input = Console.ReadLine()?.ToLowerInvariant();
51-
if (input is not "y") return;
55+
if (!parameters.PushToRemote) {
56+
Console.WriteLine(ConsoleTextStore.CommandEndSuccess());
57+
return;
5258
}
5359

54-
Console.WriteLine("Pushing to origin ...");
60+
Console.WriteLine(ConsoleTextStore.GitPushingToRemote);
5561
SuccessOrFailure pushResult = await GitHelpers.TryPushToOrigin();
5662
if (pushResult is { IsFailure: true, AsFailure.Value: var errorPushing }) {
57-
Console.WriteLine(errorPushing);
63+
Console.WriteLine(ConsoleTextStore.CommandEndFailure(errorPushing));
5864
return;
5965
}
6066

6167
SuccessOrFailure pushTagsResult = await GitHelpers.TryPushTagsToOrigin();
6268
if (pushTagsResult is { IsFailure: true, AsFailure.Value: var errorPushingTags }) {
63-
Console.WriteLine(errorPushingTags);
69+
Console.WriteLine(ConsoleTextStore.CommandEndFailure(errorPushingTags));
6470
return;
6571
}
6672

67-
Console.WriteLine("Pushed to origin successfully.");
73+
Console.WriteLine(ConsoleTextStore.CommandEndSuccess());
6874
}
6975

7076

@@ -104,7 +110,7 @@ private static async Task<SuccessOrFailure<SemanticVersionDto>> BumpVersion(Vers
104110
}
105111

106112
versionElement.Value = versionDto.ToString();
107-
Console.WriteLine($"Updated version of package {projectName} to {versionElement.Value}");
113+
Console.WriteLine(ConsoleTextStore.UpdatedVersion(projectName, versionElement.Value));
108114
}
109115

110116
return versionDto is not null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using CodeOfChaos.Ansi;
5+
6+
namespace CodeOfChaos.CliArgsParser.Library.Shared;
7+
8+
// ---------------------------------------------------------------------------------------------------------------------
9+
// Code
10+
// ---------------------------------------------------------------------------------------------------------------------
11+
public static class ConsoleTextStore {
12+
private static AnsiStringBuilder Builder { get; } = new();
13+
14+
public static string BumpingVersion => Builder.Fore.AppendWhitesmoke("Bumping version ...").ToStringAndClear();
15+
public static string GitCommitting => Builder.Fore.AppendWhitesmoke("Git committing ...").ToStringAndClear();
16+
public static string GitTagging => Builder.Fore.AppendWhitesmoke("Git tagging ...").ToStringAndClear();
17+
public static string GitPushingToRemote => Builder.Fore.AppendWhitesmoke("Pushing to origin ...").ToStringAndClear();
18+
public static string QuestionTagAndCommit => Builder.WithFore(f => f
19+
.AppendWhitesmoke("Do you want to Git tag & push to origin?")
20+
.AppendSlategray(" (y/n)")
21+
).ToStringAndClear();
22+
23+
public static string CommandEndSuccess() => Builder.Fore.AppendGreen("Command completed successfully.").ToStringAndClear();
24+
public static string CommandEndFailure(string? failure) => Builder.Fore.WithFore(f => {
25+
f.AppendMaroon("Command failed");
26+
if (failure is not null) f.AppendMaroon(" : ").AppendWhitesmoke(failure);
27+
}
28+
).ToStringAndClear();
29+
30+
public static string TagSuccessful(SemanticVersionDto versionDto) => Builder.WithFore(f => f
31+
.AppendWhitesmoke("Version ")
32+
.AppendDeepskyblue(versionDto.ToString())
33+
.AppendWhitesmoke(" successfully git tagged and committed.")
34+
).ToStringAndClear();
35+
36+
public static string UpdatedVersion(string projectName, string versionElement) => Builder.WithFore(f => f
37+
.AppendWhitesmoke("Updated version of package ")
38+
.AppendDeepskyblue(projectName)
39+
.AppendWhitesmoke(" to ")
40+
.AppendDeepskyblue(versionElement)
41+
).ToStringAndClear();
42+
}

src/CodeOfChaos.CliArgsParser.Library/Shared/GitHelpers.cs

+21-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Imports
33
// ---------------------------------------------------------------------------------------------------------------------
44
using AterraEngine.Unions;
5+
using CodeOfChaos.Ansi;
56
using System.Diagnostics;
67

78
namespace CodeOfChaos.CliArgsParser.Library.Shared;
@@ -15,9 +16,13 @@ public static async Task<SuccessOrFailure> TryPushToOrigin() {
1516
UseShellExecute = false,
1617
CreateNoWindow = true
1718
};
19+
var builder = new AnsiStringBuilder();
1820

1921
using Process? gitTagProcess = Process.Start(gitTagInfo);
20-
Console.WriteLine(await gitTagProcess?.StandardOutput.ReadToEndAsync()!);
22+
23+
builder.Fore.AppendWhitesmokeLine(await gitTagProcess?.StandardOutput.ReadToEndAsync()!);
24+
Console.WriteLine(builder.ToStringAndClear());
25+
2126
await gitTagProcess.WaitForExitAsync();
2227

2328
if (gitTagProcess.ExitCode != 0) return "Push to origin failed";
@@ -32,9 +37,13 @@ public static async Task<SuccessOrFailure> TryPushTagsToOrigin() {
3237
UseShellExecute = false,
3338
CreateNoWindow = true
3439
};
40+
var builder = new AnsiStringBuilder();
3541

3642
using Process? gitTagProcess = Process.Start(gitTagInfo);
37-
Console.WriteLine(await gitTagProcess?.StandardOutput.ReadToEndAsync()!);
43+
44+
builder.Fore.AppendWhitesmokeLine(await gitTagProcess?.StandardOutput.ReadToEndAsync()!);
45+
Console.WriteLine(builder.ToStringAndClear());
46+
3847
await gitTagProcess.WaitForExitAsync();
3948

4049
if (gitTagProcess.ExitCode != 0) return "Pushing Tags to origin failed";
@@ -49,9 +58,13 @@ public static async Task<SuccessOrFailure> TryCreateGitTag(SemanticVersionDto up
4958
UseShellExecute = false,
5059
CreateNoWindow = true
5160
};
61+
var builder = new AnsiStringBuilder();
5262

5363
using Process? gitTagProcess = Process.Start(gitTagInfo);
54-
Console.WriteLine(await gitTagProcess?.StandardOutput.ReadToEndAsync()!);
64+
65+
builder.Fore.AppendWhitesmokeLine(await gitTagProcess?.StandardOutput.ReadToEndAsync()!);
66+
Console.WriteLine(builder.ToStringAndClear());
67+
5568
await gitTagProcess.WaitForExitAsync();
5669

5770
if (gitTagProcess.ExitCode != 0) return "Git Tagging failed";
@@ -65,9 +78,13 @@ public static async Task<SuccessOrFailure> TryCreateGitCommit(SemanticVersionDto
6578
UseShellExecute = false,
6679
CreateNoWindow = true
6780
};
81+
var builder = new AnsiStringBuilder();
6882

6983
using Process? gitCommitProcess = Process.Start(gitCommitInfo);
70-
Console.WriteLine(await gitCommitProcess?.StandardOutput.ReadToEndAsync()!);
84+
85+
builder.Fore.AppendWhitesmokeLine(await gitCommitProcess?.StandardOutput.ReadToEndAsync()!);
86+
Console.WriteLine(builder.ToStringAndClear());
87+
7188
await gitCommitProcess.WaitForExitAsync();
7289

7390
if (gitCommitProcess.ExitCode != 0) return "Git Commit failed";

src/CodeOfChaos.CliArgsParser.Library/Shared/SemanticVersionDto.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// ---------------------------------------------------------------------------------------------------------------------
22
// Imports
33
// ---------------------------------------------------------------------------------------------------------------------
4+
using CodeOfChaos.Ansi;
45
using CodeOfChaos.CliArgsParser.Library.Commands.VersionBump;
56
using System.Diagnostics.CodeAnalysis;
67
using System.Text.RegularExpressions;
@@ -99,12 +100,13 @@ public override string ToString() => Preview is not null
99100

100101
private static SemanticVersionDto FromInput(string inputText = "Please enter a semantic version:") {
101102
int tries = 0;
103+
var builder = new AnsiStringBuilder();
102104
while (tries <= 5) {
103-
Console.WriteLine(inputText);
105+
Console.WriteLine(builder.Fore.AppendWhitesmoke(inputText).ToStringAndClear());
104106
string? input = Console.ReadLine();
105107
if (input is not null && TryParse(input, out SemanticVersionDto? newVersion)) return newVersion;
106108

107-
Console.WriteLine("Invalid input");
109+
Console.WriteLine(builder.Fore.AppendDarkorange("Invalid input").ToStringAndClear());
108110
tries++;
109111
}
110112

0 commit comments

Comments
 (0)