-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
209 lines (192 loc) · 7.75 KB
/
MainForm.cs
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
CodeStat -- Calculate statistics for your code
Copyright (C) 2021 iProgramInCpp
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CodeStat
{
public partial class MainForm : Form
{
public bool m_shouldCancel = false;
public MainForm()
{
InitializeComponent();
}
private void ButtonBrowse_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog dlg = new FolderBrowserDialog())
{
if (dlg.ShowDialog() == DialogResult.OK)
{
InputTextPath.Text = dlg.SelectedPath;
}
}
}
private void MainForm_Load(object sender, EventArgs e)
{
}
public bool FilePathHasExtensions(string path, string[] exts)
{
foreach (string ext in exts)
{
if (path.EndsWith("." + ext)) return true;
}
return false;
}
public int FilePathGetExtensionFromList(string path, string[] exts)
{
for (int i=0; i<exts.Length; i++)
{
if (path.EndsWith("." + exts[i])) return i;
}
return -1;
}
public bool m_runningTask = false;
public void GetFiles(ref List<FileThing> files, string path, bool incSubdirs)
{
string extThing = InputTextExtension.Text;
string[] exts = extThing.Split('|');
try
{
string[] filess = Directory.GetFiles(path);
if (filess.Length != 0)
{
progressBar1.Minimum = 0;
progressBar1.Value = 0;
progressBar1.Maximum = filess.Length + 1;
foreach (string file in filess)
{
if (FilePathHasExtensions(file, exts))
{
if (m_shouldCancel) return;
LabelFile.Text = file;
//files.Add(file);
try
{
FileThing t = new FileThing();
t.fileName = file;
t.fileSize = (int)new FileInfo(file).Length;
string[] lines = File.ReadAllLines(file);
t.loc = lines.Length;
t.sloc = 0;
foreach (var line in lines)
if (!string.IsNullOrWhiteSpace(line)/*TODO: add comment support too*/)
t.sloc++;
files.Add(t);
}
catch (Exception ex) { ConsoleLog.Items.Add(ex.Message); }
}
if (m_shouldCancel) return;
progressBar1.Value++;
Application.DoEvents();
}
}
if (incSubdirs)
{
string[] subdirs = Directory.GetDirectories(path);
foreach(string subdir in subdirs)
{
if (m_shouldCancel) return;
GetFiles(ref files, subdir, incSubdirs);
if (m_shouldCancel) return;
Application.DoEvents();
}
}
}
catch (Exception ex) { ConsoleLog.Items.Add(ex.Message); }
Application.DoEvents();
}
void CalculateTotalStats(ref List<FileThing> fileThings)
{
int totalLOC = 0, totalSLOC = 0, totalFileSize = 0;
var exts = InputTextExtension.Text.Split('|');
int extCount = exts.Length;
int[] totalLocForExts = new int[extCount];
int[] totalSLocForExts = new int[extCount];
int[] totalSizeForExts = new int[extCount];
progressBar1.Minimum = 0;
progressBar1.Maximum = fileThings.Count+1;
progressBar1.Value = 0;
foreach (FileThing thing in fileThings)
{
if (m_shouldCancel) return;
LabelFile.Text = thing.fileName;
totalLOC += thing.loc;
totalSLOC += thing.sloc;
totalFileSize += thing.fileSize;
int ext = FilePathGetExtensionFromList(thing.fileName, exts);
if (ext != -1)
{
totalLocForExts[ext] += thing.loc;
totalSLocForExts[ext] += thing.sloc;
totalSizeForExts[ext] += thing.fileSize;
}
progressBar1.Value++;
if (m_shouldCancel) return;
Application.DoEvents();
}
//log everything
StatsList.Items.Add($"Total file size: {totalFileSize}");
StatsList.Items.Add($"Total non-empty LOC: {totalSLOC}");
StatsList.Items.Add($"Total lines of code: {totalLOC}");
StatsList.Items.Add("===================================================================");
for (int i=0; i<extCount; i++)
{
StatsList.Items.Add($"[{exts[i]}] total file size: {totalSizeForExts[i]}");
StatsList.Items.Add($"[{exts[i]}] total non-empty LOC: {totalSLocForExts[i]}");
StatsList.Items.Add($"[{exts[i]}] total lines of code (LOC): {totalLocForExts[i]}");
}
}
private void ButtonCalculate_Click(object sender, EventArgs e)
{
m_runningTask = true;
//get files
List<FileThing> files = new List<FileThing>();
StatsList.Items.Clear();
LabelStep.Text = "Indexing files...";
GetFiles(ref files, InputTextPath.Text, CheckIncludeSubdirs.Checked);
LabelStep.Text = "Calculating total stats...";
CalculateTotalStats(ref files);
LabelStep.Text = "Done!";
LabelFile.Text = "";
ButtonCancel.Enabled = true;
ButtonCancel.Text = "Cancel";
m_shouldCancel = false;
m_runningTask = false;
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
if (m_runningTask)
{
m_shouldCancel = true;
ButtonCancel.Enabled = false;
ButtonCancel.Text = "Cancelling...";
}
}
}
public class FileThing
{
public string fileName;
public int loc, sloc;//lines of code, source lines of code
public int fileSize;
}
}