Skip to content

Commit

Permalink
Added code to show amount saved / processed
Browse files Browse the repository at this point in the history
  • Loading branch information
deanhume committed Sep 6, 2024
1 parent 0cd4612 commit e48f044
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions ViewMinifier/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ namespace HtmlMinifier
/// </summary>
public class Program
{
public static long totalProcessed = 0;
public static long totalSaved = 0;

static void Main(string[] args)
{
args = new string[1];
args[0] = @"c:\temp\test";

if (args.Length == 0)
{
Console.WriteLine("Please provide folder path or file(s) to process");
Expand All @@ -33,7 +39,14 @@ static void Main(string[] args)
}
}
Console.WriteLine("Minification Complete");
Console.WriteLine("------------------------------------------");
Console.WriteLine("Total Processed: {0}", BytesToString(totalProcessed));
Console.WriteLine("Total Minified: {0}", BytesToString(totalSaved));
Console.WriteLine("Total Saved: {0}", BytesToString(totalProcessed - totalSaved));
Console.WriteLine("------------------------------------------");

}
Console.Read();
}

/// <summary>
Expand Down Expand Up @@ -64,11 +77,18 @@ public static void ProcessFile(Features features, string filePath)
{
Console.WriteLine("Beginning Minification");

// File size before minify
totalProcessed += new FileInfo(filePath).Length;

// Minify contents
string minifiedContents = MinifyHtml(filePath, features);

// Write to the same file
File.WriteAllText(filePath, minifiedContents, new UTF8Encoding(true));

// File size after minify
totalSaved += new FileInfo(filePath).Length;

Console.WriteLine("Minified file : " + filePath);
}

Expand Down Expand Up @@ -105,5 +125,16 @@ public static string MinifyHtml(string filePath, Features features)
return reader.MinifyHtmlCode(features);
}
}

static String BytesToString(long byteCount)
{
string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" }; //Longs run out around EB
if (byteCount == 0)
return "0" + suf[0];
long bytes = Math.Abs(byteCount);
int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
double num = Math.Round(bytes / Math.Pow(1024, place), 1);
return (Math.Sign(byteCount) * num).ToString() + suf[place];
}
}
}

0 comments on commit e48f044

Please sign in to comment.