-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFormMain.cs
More file actions
143 lines (116 loc) · 5.25 KB
/
Copy pathFormMain.cs
File metadata and controls
143 lines (116 loc) · 5.25 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
using System.Reflection;
namespace LinesOfCodeCounter;
public partial class FormMain : Form
{
string FolderToExamine { get; set; }
DataGridColorHelper dataGridColorHelper;
HashSet<string> acceptedFileTypes = [".cs", ".py", ".shader", ".cpp", ".h"];
HashSet<string> excludedFilePaths = [@"\bin", @"\obj", @"\Properties", @"\Debug", @"\Release", @"\Plugins", @"\LeanTween", @"\ThirdParty", @"\Third Party", @"\TextMesh Pro"];
HashSet<CodeFile> codeFiles = [];
ProjectHistoryView projectHistoryView;
public FormMain() => InitializeComponent();
void Form1_Load(object sender, EventArgs e) => SetupUI();
void SetupUI()
{
labelFilezCounted.Text = labelTotalLines.Text = labelTotalChars.Text = labelAvgLines.Text = labelAvgChars.Text = string.Empty;
UserInputConverter.FillTextBoxesWithSettings(acceptedFileTypes, excludedFilePaths, richTextBoxAllowedFiles, richTextBoxExcludedFiles);
SetupDataGridView();
projectHistoryView = new ProjectHistoryView(this, panelMenuBar, GetCurrentDataControls(), () => FolderToExamine);
void SetupDataGridView()
{
EnableDoubleBufferedDataGrid();
dataGridColorHelper = new(dataGridView1, this.Font);
dataGridView1.ForeColor = dataGridColorHelper.TextColor;
void EnableDoubleBufferedDataGrid() => typeof(DataGridView).InvokeMember("DoubleBuffered", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty, null, dataGridView1, new object[] { true });
}
}
Control[] GetCurrentDataControls() => new Control[]
{
buttonPickFolder,
labelSelectedFolder,
buttonDoCaluclation,
label2,
label3,
dataGridView1,
richTextBoxAllowedFiles,
richTextBoxExcludedFiles,
labelTotalLines,
labelAvgChars,
labelAvgLines,
labelFilezCounted,
labelTotalChars,
labelTimeTaken,
label4,
label5,
label6,
label7,
label8,
};
void buttonSelectFolder_Click(object sender, EventArgs e)
{
using(FolderBrowserDialog folderBrowser = new())
{
DialogResult result = folderBrowser.ShowDialog();
if(result == DialogResult.OK && !string.IsNullOrWhiteSpace(folderBrowser.SelectedPath))
{
FolderToExamine = folderBrowser.SelectedPath;
labelTimeTaken.Text = "";
}
}
labelSelectedFolder.Text = "Examining Folder: " + FolderToExamine;
projectHistoryView.Refresh();
}
void buttonDoCaluclation_Click(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(FolderToExamine))
return;
UpdateSettings();
if(TryGetResults(out CodeAnalysisResult? result))
{
UpdateUILabels(result!);
projectHistoryView.SaveSnapshot(result!);
projectHistoryView.Refresh();
}
void UpdateSettings()
{
UserInputConverter.ConvertUserInputsToRealSettings(ref acceptedFileTypes, ref excludedFilePaths, richTextBoxAllowedFiles, richTextBoxExcludedFiles);
TimeSpan timeTaken = DataGridViewFiller.GenerateAndFillDataGridView(ref codeFiles, dataGridView1, FolderToExamine, acceptedFileTypes, excludedFilePaths);
labelTimeTaken.Text = $"Analysis finished in {timeTaken.TotalSeconds:F2} seconds.";
}
bool TryGetResults(out CodeAnalysisResult? result)
{
if(codeFiles?.Count <= 0)
{
_ = MessageBox.Show("No matches found. Assure file extensions you want are inlcuded.");
result = null;
return false;
}
result = new CodeAnalysisResult(codeFiles!);
return true;
}
void UpdateUILabels(CodeAnalysisResult result)
{
if(result == null)
return;
labelFilezCounted.Text = result.TotalFiles.ToString("#,0");
labelTotalLines.Text = result.TotalLines.ToString("#,0");
labelTotalChars.Text = result.TotalCharacters.ToString("#,0");
labelAvgLines.Text = result.AverageLinesPerFile.ToString("#,0.##");
labelAvgChars.Text = result.AverageCharactersPerLine.ToString("#,0.##");
}
}
void panelMenuBar_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
_ = Native.ReleaseCapture();
_ = Native.SendMessage(Handle, Native.WM_NCLBUTTONDOWN, Native.HT_CAPTION, 0);
}
}
void buttonQuit_Click(object sender, EventArgs e) => Environment.Exit(0);
void buttonMinimize_Click(object sender, EventArgs e) => this.WindowState = FormWindowState.Minimized;
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) => dataGridColorHelper.DoRowBackColor(e);
void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) => dataGridColorHelper.DoRowHeaderColor(sender, e);
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) => dataGridColorHelper.DoSelectAllColor(e);
void dataGridView1_Paint(object sender, PaintEventArgs e) => dataGridColorHelper.DoColumnHeaderColor(e);
}