Skip to content

Commit

Permalink
Merge pull request #7 from mainak55512/enhancement
Browse files Browse the repository at this point in the history
concurrency first attempt
  • Loading branch information
mainak55512 authored Aug 24, 2024
2 parents 740e6db + b41d995 commit e42b53b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import (
"flag"
"fmt"
"os"
"sync"

"github.com/olekukonko/tablewriter"
)

func main() {
mu := &sync.RWMutex{}
wg := &sync.WaitGroup{}
var lang = flag.String("ext", "none", "filter based on extention")
flag.Parse()

Expand All @@ -21,8 +24,13 @@ func main() {
return
}
for _, file := range files {
getFileDetails(file, &file_details, &folder_count, &is_git_initialized)
wg.Add(1)
go func(wg *sync.WaitGroup, mu *sync.RWMutex) {
defer wg.Done()
getFileDetails(file, &file_details, &folder_count, &is_git_initialized, mu)
}(wg, mu)
}
wg.Wait()

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"File Type", "File Count", "Number of Lines", "Gap", "comments", "Code"})
Expand Down
21 changes: 13 additions & 8 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/fs"
"os"
"strings"
"sync"
)

type File_details struct {
Expand Down Expand Up @@ -44,8 +45,8 @@ func countLines(file_name string, ext string) (int32, int32, int32, int32) {
return code, gap, comments, (code + gap + comments)
}

func addNewEntry(file fs.DirEntry, ext string, file_details *[]File_details) {
code, gap, comments, line_count := countLines(file.Name(), ext)
func addNewEntry(ext string, file_details *[]File_details, code, gap, comments, line_count int32) {
// code, gap, comments, line_count := countLines(file.Name(), ext)
*file_details = append(*file_details, File_details{
ext: ext,
file_count: 1,
Expand All @@ -56,11 +57,11 @@ func addNewEntry(file fs.DirEntry, ext string, file_details *[]File_details) {
})
}

func updateExistingEntry(file fs.DirEntry, ext string, file_details *[]File_details, check *bool) {
func updateExistingEntry(ext string, file_details *[]File_details, check *bool, code, gap, comments, line_count int32) {
for i := range *file_details {
if (*file_details)[i].ext == ext {
*check = true
code, gap, comments, line_count := countLines(file.Name(), ext)
// code, gap, comments, line_count := countLines(file.Name(), ext)
(*file_details)[i].file_count += 1
(*file_details)[i].code += code
(*file_details)[i].gap += gap
Expand All @@ -71,7 +72,7 @@ func updateExistingEntry(file fs.DirEntry, ext string, file_details *[]File_deta
}
}

func getFileDetails(file fs.DirEntry, file_details *[]File_details, folder_count *int32, is_git_initialized *bool) {
func getFileDetails(file fs.DirEntry, file_details *[]File_details, folder_count *int32, is_git_initialized *bool, mu *sync.RWMutex) {
if file.IsDir() {
if file.Name() == ".git" && *is_git_initialized == false {
*is_git_initialized = true
Expand All @@ -83,15 +84,19 @@ func getFileDetails(file fs.DirEntry, file_details *[]File_details, folder_count
if ext == "" {
return
}

code, gap, comments, line_count := countLines(file.Name(), ext)
mu.Lock()
if len(*file_details) == 0 {
addNewEntry(file, ext, file_details)
addNewEntry(ext, file_details, code, gap, comments, line_count)
} else if len(*file_details) > 0 {
check := false
updateExistingEntry(file, ext, file_details, &check)
updateExistingEntry(ext, file_details, &check, code, gap, comments, line_count)
if check == false {
addNewEntry(file, ext, file_details)
addNewEntry(ext, file_details, code, gap, comments, line_count)
}
}
mu.Unlock()
}

func getTotalCounts(file_details *[]File_details) (int32, int32, int32, int32, int32) {
Expand Down

0 comments on commit e42b53b

Please sign in to comment.