Skip to content

Run on specific directory added, optimized garbage collection, output style changed #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"mainak55512/stto/utils"
"os"
"path"
"runtime"
"runtime/debug"
"sync"
Expand All @@ -14,7 +15,8 @@ import (

func main() {

debug.SetGCPercent(-1)
// Optimizing GC
debug.SetGCPercent(1000)

// Limiting os threads to available cpu
runtime.GOMAXPROCS(runtime.NumCPU())
Expand All @@ -35,7 +37,13 @@ func main() {
var file_details []utils.File_details
var folder_count int32
var is_git_initialized bool = false
files, err := utils.GetFiles(&is_git_initialized, &folder_count)
var folder_name string = ""
if len(flag.Args()) > 0 {
folder_name = flag.Args()[0]
}

files, err := utils.GetFiles(&is_git_initialized, &folder_count, folder_name)

if err != nil {
fmt.Println("Error:", err)
return
Expand All @@ -57,7 +65,6 @@ func main() {
}
wg.Wait()

runtime.GC()
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{
"File Type",
Expand All @@ -67,6 +74,7 @@ func main() {
"comments",
"Code",
})
table.SetFooterAlignment(2)

// if not extension is provided via --ext flag
if *lang == "none" {
Expand All @@ -80,22 +88,31 @@ func main() {
fmt.Sprint(item.Code),
})
}
table.Render()

total_files,
total_lines,
total_gaps,
total_comments,
total_code := utils.GetTotalCounts(&file_details)

table.SetFooter([]string{
"Total:",
fmt.Sprint(total_files),
fmt.Sprint(total_lines),
fmt.Sprint(total_gaps),
fmt.Sprint(total_comments),
fmt.Sprint(total_code),
})
table.Render()

pwd, e := os.Getwd()

if e != nil {
fmt.Println(e)
os.Exit(1)
}
fmt.Println("\nStats:\n=======")
fmt.Println("Present working directory: ", pwd)

fmt.Println("Target directory: ", path.Join(pwd, folder_name))

// total subdirectories are folder_count-1,
// as present working directory is not a subdirectory
Expand All @@ -104,16 +121,6 @@ func main() {
folder_count-1,
is_git_initialized,
)
fmt.Printf(
"\nTotal files:\t%10d\tTotal lines:\t%10d\n"+
"Total gaps:\t%10d\tTotal comments:\t%10d\n"+
"Total code:\t%10d\n",
total_files,
total_lines,
total_gaps,
total_comments,
total_code,
)
} else {

// will be set to true if atleast one file
Expand Down
18 changes: 14 additions & 4 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"bufio"
"os"
"path"
"path/filepath"
"strings"
"sync"
Expand Down Expand Up @@ -273,20 +274,29 @@ If not folder, it will return the path and extension of the file.
func GetFiles(
is_git_initialized *bool,
folder_count *int32,
file_directory_name string,
) ([]file_info, error) {
var files []file_info
err := filepath.Walk(".", func(
path string,
folder_location := "."
if file_directory_name != "" {
folder_location = path.Join(folder_location, file_directory_name)
}
err := filepath.Walk(folder_location, func(
_path string,
f os.FileInfo,
err error,
) error {

// Handling the error is there is any during file read
if err != nil {
return err
}
// if it is a folder, then increase the folder count
if f.IsDir() {

// if folder name is '.git', then
// set is_git_initialized to true
if path == ".git" && *is_git_initialized == false {
if _path == path.Join(folder_location, ".git") && *is_git_initialized == false {
*is_git_initialized = true
}
*folder_count++
Expand All @@ -300,7 +310,7 @@ func GetFiles(
}
if _, exists := lookup_map[ext]; exists {
files = append(files, file_info{
path: path,
path: _path,
ext: ext,
})
}
Expand Down