-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDirectoryCountStatistics.cs
More file actions
146 lines (124 loc) · 5.27 KB
/
Copy pathDirectoryCountStatistics.cs
File metadata and controls
146 lines (124 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
namespace CoreEx.CodeGen.Counting;
/// <summary>
/// Provides <see cref="DirectoryInfo"/> count statistics.
/// </summary>
internal class DirectoryCountStatistics(DirectoryInfo directory)
{
/// <summary>
/// Gets the <see cref="DirectoryInfo"/>.
/// </summary>
public DirectoryInfo Directory { get; } = directory;
/// <summary>
/// Gets the file count.
/// </summary>
public int FileCount { get; private set; }
/// <summary>
/// Gets the total file count including children.
/// </summary>
public int TotalFileCount => FileCount + Children.Sum(x => x.TotalFileCount);
/// <summary>
/// Gets the generated file count.
/// </summary>
public int GeneratedFileCount { get; private set; }
/// <summary>
/// Gets the total generated file count including children.
/// </summary>
public int GeneratedTotalFileCount => GeneratedFileCount + Children.Sum(x => x.GeneratedTotalFileCount);
/// <summary>
/// Gets the line count;
/// </summary>
public int LineCount { get; private set; }
/// <summary>
/// Gets the total line count including children.
/// </summary>
public int TotalLineCount => LineCount + Children.Sum(x => x.TotalLineCount);
/// <summary>
/// Gets the generated line count.
/// </summary>
public int GeneratedLineCount { get; private set; }
/// <summary>
/// Gets the total line count including children.
/// </summary>
public int GeneratedTotalLineCount => GeneratedLineCount + Children.Sum(x => x.GeneratedTotalLineCount);
/// <summary>
/// Gets the child <see cref="DirectoryCountStatistics"/> instances.
/// </summary>
public List<DirectoryCountStatistics> Children { get; } = [];
/// <summary>
/// Increments the file count.
/// </summary>
public void IncrementFileCount(bool isGenerated)
{
FileCount++;
if (isGenerated)
GeneratedFileCount++;
}
/// <summary>
/// Increments the line count.
/// </summary>
public void IncrementLineCount(bool isGenerated)
{
LineCount++;
if (isGenerated)
GeneratedLineCount++;
}
/// <summary>
/// Adds a child <see cref="DirectoryCountStatistics"/> instance.
/// </summary>
public DirectoryCountStatistics AddChildDirectory(DirectoryInfo di)
{
var dcs = new DirectoryCountStatistics(di);
Children.Add(dcs);
return dcs;
}
/// <summary>
/// Write the count statistics.
/// </summary>
/// <param name="logger">The <see cref="ILogger"/>.</param>
/// <param name="columnLength">The maximum column length.</param>
/// <param name="indent">The indent size to show hierarchy.</param>
/// <param name="remove">The number of characters to remove from directory </param>
public void Write(ILogger logger, int columnLength, int indent, int remove)
{
if (indent == 0)
{
var hdrAll = string.Format("{0, " + columnLength + "}", "All");
var hdrGen = string.Format("{0, " + (columnLength + 5) + "}", "Generated");
var hdrfiles = string.Format("{0, " + columnLength + "}", "Files");
var hdrlines = string.Format("{0, " + columnLength + "}", "Lines");
logger.LogInformation("{Content}", $"{hdrAll} | {hdrAll} | {hdrGen} | {hdrGen} | Path/");
logger.LogInformation("{Content}", $"{hdrfiles} | {hdrlines} | {hdrfiles} Perc | {hdrlines} Perc | Directory");
int maxLength = 0;
DirectoryDepthAnalysis(this, 0, ref maxLength);
logger.LogInformation("{Content}", new string('-', (columnLength * 4) + 22 + maxLength - remove));
}
var totfiles = string.Format("{0, " + columnLength + "}", TotalFileCount);
var totlines = string.Format("{0, " + columnLength + "}", TotalLineCount);
var totgenFiles = string.Format("{0, " + columnLength + "}", GeneratedTotalFileCount);
var totgenFilesPerc = string.Format("{0, " + 3 + "}", GeneratedTotalFileCount == 0 ? 0 : Math.Round((double)GeneratedTotalFileCount / (double)TotalFileCount * 100.0, 0));
var totgenLines = string.Format("{0, " + columnLength + "}", GeneratedTotalLineCount);
var totgenLinesPerc = string.Format("{0, " + 3 + "}", GeneratedTotalLineCount == 0 ? 0 : Math.Round((double)GeneratedTotalLineCount / (double)TotalLineCount * 100.0, 0));
string spacer;
if (indent == 0)
spacer = string.Empty;
else
spacer = new string(' ', (indent - 1) * 2) + "- ";
logger.LogInformation("{Content}", $"{totfiles} {totlines} {totgenFiles} {totgenFilesPerc}% {totgenLines} {totgenLinesPerc}% {spacer}{Directory.FullName[remove..]}");
foreach (var dcs in Children)
{
if (dcs.TotalFileCount > 0)
dcs.Write(logger, columnLength, indent + 1, remove);
}
}
/// <summary>
/// Performs directory depth and length analysis.
/// </summary>
private static void DirectoryDepthAnalysis(DirectoryCountStatistics dcs, int depth, ref int maxLength)
{
maxLength = Math.Max(maxLength, dcs.Directory.FullName.Length + (depth * 2));
foreach (var d in dcs.Children)
{
DirectoryDepthAnalysis(d, depth + 1, ref maxLength);
}
}
}