Skip to content

Commit

Permalink
FEATURE: Add loading bar
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjitagawa committed May 3, 2022
1 parent 4463dde commit c239b75
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 20 deletions.
106 changes: 106 additions & 0 deletions WindowsKeyFinder/LoadingBar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace WindowsKeyFinder
{
/// <summary>
/// An ASCII progress bar
/// </summary>
public class ProgressBar : IDisposable, IProgress<double>
{
private const int blockCount = 10;
private readonly TimeSpan animationInterval = TimeSpan.FromSeconds(1.0 / 8);
private const string animation = @"|/-\";

private readonly Timer timer;

private double currentProgress = 0;
private string currentText = string.Empty;
private bool disposed = false;
private int animationIndex = 0;

public ProgressBar()
{
timer = new Timer(TimerHandler);

// A progress bar is only for temporary display in a console window.
// If the console output is redirected to a file, draw nothing.
// Otherwise, we'll end up with a lot of garbage in the target file.
if (!Console.IsOutputRedirected)
{
ResetTimer();
}
}

public void Report(double value)
{
// Make sure value is in [0..1] range
value = Math.Max(0, Math.Min(1, value));
Interlocked.Exchange(ref currentProgress, value);
}

private void TimerHandler(object state)
{
lock (timer)
{
if (disposed) return;

int progressBlockCount = (int)(currentProgress * blockCount);
int percent = (int)(currentProgress * 100);
string text = string.Format("[{0}{1}] {2,3}% {3}",
new string('#', progressBlockCount), new string('-', blockCount - progressBlockCount),
percent,
animation[animationIndex++ % animation.Length]);
UpdateText(text);

ResetTimer();
}
}

private void UpdateText(string text)
{
// Get length of common portion
int commonPrefixLength = 0;
int commonLength = Math.Min(currentText.Length, text.Length);
while (commonPrefixLength < commonLength && text[commonPrefixLength] == currentText[commonPrefixLength])
{
commonPrefixLength++;
}

// Backtrack to the first differing character
StringBuilder outputBuilder = new StringBuilder();
outputBuilder.Append('\b', currentText.Length - commonPrefixLength);

// Output new suffix
outputBuilder.Append(text.Substring(commonPrefixLength));

// If the new text is shorter than the old one: delete overlapping characters
int overlapCount = currentText.Length - text.Length;
if (overlapCount > 0)
{
outputBuilder.Append(' ', overlapCount);
outputBuilder.Append('\b', overlapCount);
}

Console.Write(outputBuilder);
currentText = text;
}

private void ResetTimer()
{
timer.Change(animationInterval, TimeSpan.FromMilliseconds(-1));
}

public void Dispose()
{
lock (timer)
{
disposed = true;
UpdateText(string.Empty);
}
}

}
}
46 changes: 30 additions & 16 deletions WindowsKeyFinder/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;

namespace WindowsKeyFinder
{
Expand All @@ -8,34 +9,47 @@ class Program
static void Main(string[] args)
{

Windows productKey = null;

try
{
productKey = new Windows();
productKey.FindProductKeyClick();

}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}


Windows productKey = new Windows();

productKey.FindProductKeyClick();

Console.ForegroundColor = ConsoleColor.DarkGreen;

Console.Write("Fetching the key from your device... ");
using (var progress = new ProgressBar())
{
for (int i = 0; i <= 200; i++)
{
progress.Report((double)i / 200);
Thread.Sleep(5);
}
}
Console.WriteLine("Done.");


Console.WriteLine("-------------------------------------------------------------------------------------");
Console.WriteLine("|\tWelcome to Windows Product Key Finder! |");
Console.WriteLine("| |");
Console.WriteLine("| |");
Console.WriteLine("| |");
Console.WriteLine("| |");
Console.WriteLine("|\tWelcome to Windows Product Key Finder! |");
Console.WriteLine("|\tHere is your Windows Key: |");
Console.WriteLine("| |");
Console.WriteLine($"| {productKey.Key} |");
Console.WriteLine("| |");
Console.WriteLine("| |");
Console.WriteLine("| |");
Console.WriteLine("| |");
Console.WriteLine("| - Kenji T. |");
Console.WriteLine("-------------------------------------------------------------------------------------");





Console.WriteLine("\n\n");
Console.WriteLine("Link to source code: ");
Console.WriteLine("\thttps://github.com/kenjitagawa/WindowsKeyFinder");
Console.WriteLine("\n");
Console.Write("Press any key to quit.");
Console.ReadKey();

Expand Down
10 changes: 7 additions & 3 deletions WindowsKeyFinder/Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,13 @@ public void FindProductKeyClick()
byte[] id = null;
var regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");

if (regKey != null) id = regKey.GetValue("DigitalProductId") as byte[];

m_productKey = DecodeKey(id);
if (regKey != null)
{
id = regKey.GetValue("DigitalProductId") as byte[];
m_productKey = DecodeKey(id);
}
else
return;
}

}
Expand Down
1 change: 0 additions & 1 deletion WindowsKeyFinder/WindowsKeyFinder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

<ItemGroup>
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageReference Include="Xamarin.Essentials" Version="1.7.1" />
</ItemGroup>

</Project>

0 comments on commit c239b75

Please sign in to comment.