Skip to content

Commit ffa0cfd

Browse files
authored
Merge pull request #23 from mainak55512/enhancement
Run on specific directory added, optimized garbage collection, output style changed
2 parents 8eac92c + 2e0c245 commit ffa0cfd

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

main.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"mainak55512/stto/utils"
77
"os"
8+
"path"
89
"runtime"
910
"runtime/debug"
1011
"sync"
@@ -14,7 +15,8 @@ import (
1415

1516
func main() {
1617

17-
debug.SetGCPercent(-1)
18+
// Optimizing GC
19+
debug.SetGCPercent(1000)
1820

1921
// Limiting os threads to available cpu
2022
runtime.GOMAXPROCS(runtime.NumCPU())
@@ -35,7 +37,13 @@ func main() {
3537
var file_details []utils.File_details
3638
var folder_count int32
3739
var is_git_initialized bool = false
38-
files, err := utils.GetFiles(&is_git_initialized, &folder_count)
40+
var folder_name string = ""
41+
if len(flag.Args()) > 0 {
42+
folder_name = flag.Args()[0]
43+
}
44+
45+
files, err := utils.GetFiles(&is_git_initialized, &folder_count, folder_name)
46+
3947
if err != nil {
4048
fmt.Println("Error:", err)
4149
return
@@ -57,7 +65,6 @@ func main() {
5765
}
5866
wg.Wait()
5967

60-
runtime.GC()
6168
table := tablewriter.NewWriter(os.Stdout)
6269
table.SetHeader([]string{
6370
"File Type",
@@ -67,6 +74,7 @@ func main() {
6774
"comments",
6875
"Code",
6976
})
77+
table.SetFooterAlignment(2)
7078

7179
// if not extension is provided via --ext flag
7280
if *lang == "none" {
@@ -80,22 +88,31 @@ func main() {
8088
fmt.Sprint(item.Code),
8189
})
8290
}
83-
table.Render()
8491

8592
total_files,
8693
total_lines,
8794
total_gaps,
8895
total_comments,
8996
total_code := utils.GetTotalCounts(&file_details)
9097

98+
table.SetFooter([]string{
99+
"Total:",
100+
fmt.Sprint(total_files),
101+
fmt.Sprint(total_lines),
102+
fmt.Sprint(total_gaps),
103+
fmt.Sprint(total_comments),
104+
fmt.Sprint(total_code),
105+
})
106+
table.Render()
107+
91108
pwd, e := os.Getwd()
92109

93110
if e != nil {
94111
fmt.Println(e)
95112
os.Exit(1)
96113
}
97-
fmt.Println("\nStats:\n=======")
98-
fmt.Println("Present working directory: ", pwd)
114+
115+
fmt.Println("Target directory: ", path.Join(pwd, folder_name))
99116

100117
// total subdirectories are folder_count-1,
101118
// as present working directory is not a subdirectory
@@ -104,16 +121,6 @@ func main() {
104121
folder_count-1,
105122
is_git_initialized,
106123
)
107-
fmt.Printf(
108-
"\nTotal files:\t%10d\tTotal lines:\t%10d\n"+
109-
"Total gaps:\t%10d\tTotal comments:\t%10d\n"+
110-
"Total code:\t%10d\n",
111-
total_files,
112-
total_lines,
113-
total_gaps,
114-
total_comments,
115-
total_code,
116-
)
117124
} else {
118125

119126
// will be set to true if atleast one file

utils/utils.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package utils
33
import (
44
"bufio"
55
"os"
6+
"path"
67
"path/filepath"
78
"strings"
89
"sync"
@@ -273,20 +274,29 @@ If not folder, it will return the path and extension of the file.
273274
func GetFiles(
274275
is_git_initialized *bool,
275276
folder_count *int32,
277+
file_directory_name string,
276278
) ([]file_info, error) {
277279
var files []file_info
278-
err := filepath.Walk(".", func(
279-
path string,
280+
folder_location := "."
281+
if file_directory_name != "" {
282+
folder_location = path.Join(folder_location, file_directory_name)
283+
}
284+
err := filepath.Walk(folder_location, func(
285+
_path string,
280286
f os.FileInfo,
281287
err error,
282288
) error {
283289

290+
// Handling the error is there is any during file read
291+
if err != nil {
292+
return err
293+
}
284294
// if it is a folder, then increase the folder count
285295
if f.IsDir() {
286296

287297
// if folder name is '.git', then
288298
// set is_git_initialized to true
289-
if path == ".git" && *is_git_initialized == false {
299+
if _path == path.Join(folder_location, ".git") && *is_git_initialized == false {
290300
*is_git_initialized = true
291301
}
292302
*folder_count++
@@ -300,7 +310,7 @@ func GetFiles(
300310
}
301311
if _, exists := lookup_map[ext]; exists {
302312
files = append(files, file_info{
303-
path: path,
313+
path: _path,
304314
ext: ext,
305315
})
306316
}

0 commit comments

Comments
 (0)