Skip to content

Commit 5fa6916

Browse files
committed
Console Progressbar
Fixes #1 Console menu have issue on large list Fixes #23 Fix ascii issues
1 parent f07af3d commit 5fa6916

File tree

10 files changed

+149
-46
lines changed

10 files changed

+149
-46
lines changed

src/ConsoleR/AsciiArt/AsciiCharacters/AsciiChars.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,14 +396,14 @@ public static string GetAsciiArt(string text) {
396396
private static string ToSingleLine(List<string> asciiChars)
397397
{
398398
var sb = new StringBuilder();
399-
var rowsCount = asciiChars[0].Split(Environment.NewLine).Length;
399+
var rowsCount = asciiChars[0].Split("\r\n").Length;
400400
for(var i = 0; i < rowsCount;i++){
401401
for (var j = 0; j < asciiChars.Count;j++) {
402-
var splitted = asciiChars[j].Split(Environment.NewLine);
402+
var splitted = asciiChars[j].Split("\r\n");
403403
sb.Append(splitted[i]);
404404

405405
}
406-
sb.Append(Environment.NewLine);
406+
sb.Append("\r\n");
407407
}
408408

409409
return sb.ToString();

src/ConsoleR/Confirm/ConsoleConfirm.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static bool Confirm(string message, bool defaultValue) {
1111
}
1212

1313
internal class ConsoleConfirm(string Message) {
14-
private bool? _defaultValue;
14+
private readonly bool? _defaultValue;
1515

1616
internal ConsoleConfirm(string Message, bool defaultValue): this(Message) {
1717

@@ -25,7 +25,7 @@ internal bool GetAnswer() {
2525
}
2626
Console.Write($"{Message} [{confirmActions}]: ");
2727
var result = Console.ReadKey();
28-
Console.WriteLine();
28+
System.Console.WriteLine();
2929
if(result.Key == ConsoleKey.Enter)
3030
{
3131
if(_defaultValue.HasValue)

src/ConsoleR/ConsoleR.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1414
</PropertyGroup>
1515
<PropertyGroup>
16-
<Version>0.3.2</Version>
16+
<Version>0.3.3</Version>
1717
<PackageId>Doroudi.ConsoleR</PackageId>
1818
<Authors>Saeid Doroudi</Authors>
1919
<Description>ConsoleR is set of utilities to make awesome console apps in .Net</Description>

src/ConsoleR/Loading/ConsoleLoading.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/ConsoleR/Menu/ConsoleMenu.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,18 @@ private void Init(bool selectFirst, string[] options)
5151

5252
public void Show()
5353
{
54+
System.Console.SetCursorPosition(0, 0);
5455
System.Console.Clear();
5556
System.Console.WriteLine(_displayText);
56-
if(_showNumbers){
57+
if(_showNumbers) {
5758
System.Console.WriteLine("(Use Arrow keys to navigate up and down to select and Enter to submit or use number to select)");
5859
}
5960
else
6061
System.Console.WriteLine("(Use Arrow keys to navigate up and down to select and Enter to submit)");
6162

6263
for (int i = 0; i < _options.Count; i++)
6364
{
64-
MenuOption? option = _options[i];
65+
var option = _options[i];
6566
System.Console.ForegroundColor = option.Selected ? ConsoleColor.Green : ConsoleColor.White;
6667
var numberSign = _showNumbers ? i + 1 + ")" : "";
6768
System.Console.WriteLine($"{numberSign} {option.Option}");

src/ConsoleR/Password/Password.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static string Password(string prompt)
3030
Write("*");
3131
}
3232
} while (true);
33-
WriteLine();
33+
System.Console.WriteLine();
3434
return password;
3535
}
3636
}

src/ConsoleR/ReadLine/ReadLine.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ public static ConsoleKeyInfo ReadKey()
77
return System.Console.ReadKey();
88
}
99

10+
public static ConsoleKeyInfo ReadKey(string message)
11+
{
12+
Write(message);
13+
return System.Console.ReadKey();
14+
}
15+
1016
public static string? ReadLine(string? defaultValue = null)
1117
{
1218
if (string.IsNullOrEmpty(defaultValue))
1319
return System.Console.ReadLine();
14-
20+
1521
return ReadWithEditableValue(defaultValue);
1622
}
1723

src/ConsoleR/Spinner/Spinner.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
namespace ConsoleR.Loading;
2+
3+
public class Spinner
4+
{
5+
int spinStep = 0;
6+
string _message = "";
7+
private CancellationTokenSource _cancellationSource;
8+
private Task? _task;
9+
static string[] _pattern = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
10+
static string[] _legacyPattern = ["-","\\","|","/"];
11+
private static string[] Pattern => ConsoleHelpers.IsLegacy ? _legacyPattern : _pattern;
12+
13+
public Spinner()
14+
{
15+
_cancellationSource = new CancellationTokenSource();
16+
}
17+
private void Spin()
18+
{
19+
System.Console.SetCursorPosition(0, 0);
20+
Console.Clear();
21+
Console.Write($"{Pattern[spinStep++]} {_message}");
22+
spinStep %= Pattern.Length;
23+
24+
System.Console.SetCursorPosition(System.Console.CursorLeft - _message.Length - 1, System.Console.CursorTop);
25+
}
26+
27+
public async Task Start(Action action, string message = "")
28+
{
29+
_message = message;
30+
System.Console.CursorVisible = false;
31+
_task = Task.Run(async () =>
32+
{
33+
while (!_cancellationSource.IsCancellationRequested)
34+
{
35+
Spin();
36+
await Task.Delay(120).ConfigureAwait(false);
37+
}
38+
});
39+
40+
try
41+
{
42+
action.Invoke();
43+
Stop(_message);
44+
}
45+
catch (Exception ex)
46+
{
47+
Stop(errorMessage:ex.Message);
48+
}
49+
finally
50+
{
51+
System.Console.CursorVisible = true;
52+
}
53+
}
54+
55+
public void Stop(string? message = null, string? errorMessage = null)
56+
{
57+
if (_cancellationSource.IsCancellationRequested)
58+
return;
59+
60+
System.Console.SetCursorPosition(0, 0);
61+
Console.Clear();
62+
if (string.IsNullOrEmpty(errorMessage))
63+
{
64+
Console.Success(message ?? _message, true);
65+
}
66+
else
67+
{
68+
Console.Error(errorMessage, true);
69+
}
70+
71+
_cancellationSource.Cancel();
72+
_task?.Wait();
73+
}
74+
75+
public void SetText(string message)
76+
{
77+
_message = message;
78+
}
79+
}

src/ConsoleR/WriteLine/WriteLine.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,55 @@ namespace ConsoleR;
22

33
public static partial class Console
44
{
5-
6-
75
public static void Clear() {
86
System.Console.Clear();
97
}
108

11-
public static void WriteBool(bool value, string? trueMessage, string? falseMessage) {
9+
public static void WriteBool(bool value, string? trueMessage = null, string? falseMessage = null) {
1210
if(value)
1311
Success(trueMessage ?? "True");
1412
else
1513
Error(falseMessage ?? "False");
1614
}
1715

18-
19-
public static void WriteLine(string message, ConsoleColor? color = null)
16+
public static void WriteLine()
2017
{
21-
DoWriteLine(message,color);
18+
System.Console.WriteLine();
2219
}
23-
24-
public static void WriteLine()
20+
21+
public static void WriteLine(string message, ConsoleColor? color = null)
2522
{
26-
DoWriteLine("");
23+
DoWriteLine(message, color);
2724
}
2825

2926
public static void Error(string message, bool showIcon = false)
3027
{
3128
if (showIcon) {
32-
message = ConsoleHelpers.IsLegacy? "X" : "❌ " + message;
29+
message = (ConsoleHelpers.IsLegacy? "X " : "❌ ") + message;
3330
}
3431
DoWriteLine(message, ConsoleColor.Red);
3532
}
3633

3734
public static void Success(string message, bool showIcon = false)
3835
{
3936
if (showIcon) {
40-
message = ConsoleHelpers.IsLegacy ? "" : "✅ " + message;
37+
message = (ConsoleHelpers.IsLegacy ? "" : "✅ ") + message;
4138
}
4239
DoWriteLine(message, ConsoleColor.Green);
4340
}
4441

4542
public static void Info(string message, bool showIcon = false)
4643
{
4744
if (showIcon) {
48-
message = ConsoleHelpers.IsLegacy ? "i " :"❕" + message;
45+
message = (ConsoleHelpers.IsLegacy ? "i " :"❕") + message;
4946
}
5047
DoWriteLine(message, ConsoleColor.Blue);
5148
}
5249

5350
public static void Warning(string message, bool showIcon = false)
5451
{
5552
if (showIcon) {
56-
message = ConsoleHelpers.IsLegacy ? "! " : "⚠️ " + message;
53+
message = (ConsoleHelpers.IsLegacy ? "! " : "⚠️ ") + message;
5754
}
5855
DoWriteLine(message, ConsoleColor.Yellow);
5956
}

tests/ConsoleR.TestApp/Program.cs

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,80 @@
11
using ConsoleR;
2+
using ConsoleR.Loading;
23
using Console = ConsoleR.Console;
34

4-
var value = Console.ReadLine("ProjectName:", "ConsoleR");
5-
if(!string.IsNullOrEmpty(value))
6-
Console.AsciiArt(value, ConsoleColor.Green);
75

6+
var spinner = new Spinner();
7+
await spinner.Start(() =>
8+
{
9+
int x = 0;
10+
while (x < 10)
11+
{
12+
x++;
13+
Thread.Sleep(100);
14+
}
15+
}, "Starting app");
16+
17+
Console.AsciiArt("ConsoleR", ConsoleColor.Yellow);
18+
Console.WriteLine("\nPress any key to continue");
819
Console.ReadKey();
20+
921
string[] frontEndFrameworks = ["Blazor", "Angular", "Vue", "React", "VanillaJs"];
1022
var selectedItem = Console.Menu("Please Select One beloved frontend framework", true, frontEndFrameworks).Select();
23+
1124
Console.AsciiArt(frontEndFrameworks[selectedItem], GetFrameworkColor(frontEndFrameworks[selectedItem]));
12-
Console.Info("Progress started...");
13-
Console.Warning("It seems there is issue in the system");
14-
Console.Error("Process failed :(");
15-
Console.Info("Retrying...");
16-
Console.ReadLine("Press enter to continue");
25+
26+
await Task.Delay(2000);
27+
Console.Info("Progress started...", true);
28+
await Task.Delay(1000);
29+
Console.Warning("It seems there is issue in the system", true);
30+
await Task.Delay(1000);
31+
Console.Error("Process failed :(", true);
32+
await Task.Delay(1000);
33+
Console.Info("Retrying...", true);
34+
await Task.Delay(1000);
35+
Console.ReadLine("still working on it");
36+
await Task.Delay(1000);
1737
Console.Success("Progress Succeed", showIcon: true);
18-
Console.WriteLine("Wait it is not completed yet", ConsoleColor.Magenta);
38+
await Task.Delay(1000);
39+
Console.WriteLine("Wait it is not completed yet, Check next step", ConsoleColor.Magenta);
1940

41+
Console.WriteLine();
2042
var password = Console.Password("Enter your password:");
2143
Console.Alert($"your password is: {password}", "Password", ConsoleMessageType.Info);
2244

23-
Console.ReadKey();
2445

25-
var result = Console.Confirm("Are you sure to process");
46+
var result = Console.Confirm("Are you sure to process", true);
2647
if (result)
2748
Console.Success("Processing", true);
2849
else
2950
Console.Warning("You cancelled request", true);
3051

31-
Console.ReadKey();
52+
Console.ReadKey("Press any key");
3253

3354
string[] plugins = ["TypeScript", "Linter", "Nuxt", "Vite"];
3455
var selectedItems = Console.Checkbox("Select feature that you want to install:", plugins).Select();
35-
for (int i = 0; i < selectedItems.Length; i++) {
56+
for (int i = 0; i < selectedItems.Length; i++)
57+
{
3658
var plugin = selectedItems[i];
3759
Console.WriteLine(plugin.Option, (ConsoleColor)i);
3860
}
3961

62+
Console.ReadKey("Press any key");
63+
4064
// Table
4165
Person[] people2 = [
4266
new Person("Saeid Doroudi",30, "Tehran"),
4367
new Person("Saman", 25, "Marand"),
4468
new Person("Alice", 35, "Zurich"),
4569
new Person("Alireza", 40, "Tabriz")
4670
];
47-
4871
Console.Table(people2, ConsoleColor.DarkCyan);
4972

50-
ConsoleColor GetFrameworkColor(string framework) {
51-
return framework switch {
73+
Console.ReadKey("Press any key to exit");
74+
ConsoleColor GetFrameworkColor(string framework)
75+
{
76+
return framework switch
77+
{
5278
"Blazor" => ConsoleColor.DarkMagenta,
5379
"Angular" => ConsoleColor.Red,
5480
"Vue" => ConsoleColor.Green,
@@ -60,4 +86,4 @@ ConsoleColor GetFrameworkColor(string framework) {
6086

6187

6288

63-
record Person(string Name,int Age, string City);
89+
record Person(string Name, int Age, string City);

0 commit comments

Comments
 (0)