Skip to content

Commit 8a1ee14

Browse files
committed
Added exclude folder option, currently works only on one folder
1 parent 180536e commit 8a1ee14

File tree

5 files changed

+35
-24
lines changed

5 files changed

+35
-24
lines changed

process/process_flags.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"github.com/mainak55512/stto/utils"
88
)
99

10-
func ProcessCount(count_details *[]utils.OutputStructure, file_details *[]utils.File_details, folder_name *string, is_git_initialized *bool, folder_count *int32, mu *sync.RWMutex, wg *sync.WaitGroup) (utils.TotalCount, error) {
11-
err := ProcessConcurrentWorkers(file_details, folder_count, folder_name, is_git_initialized, mu, wg)
10+
func ProcessCount(count_details *[]utils.OutputStructure, file_details *[]utils.File_details, folder_name *string, is_git_initialized *bool, folder_count *int32, mu *sync.RWMutex, wg *sync.WaitGroup, skipDir string) (utils.TotalCount, error) {
11+
err := ProcessConcurrentWorkers(file_details, folder_count, folder_name, is_git_initialized, mu, wg, skipDir)
1212
if err != nil {
1313
return utils.TotalCount{}, fmt.Errorf("%w", err)
1414
}
@@ -43,12 +43,15 @@ func ProcessCount(count_details *[]utils.OutputStructure, file_details *[]utils.
4343
func ProcessByFlags(count_details *[]utils.OutputStructure, file_details *[]utils.File_details, folder_name *string, is_git_initialized *bool, folder_count *int32, mu *sync.RWMutex, wg *sync.WaitGroup) {
4444

4545
inpFlags := utils.HandleFlags(folder_name)
46-
46+
var skipDir string
47+
if *inpFlags.NDir != "none" {
48+
skipDir = *inpFlags.NDir
49+
}
4750
if *inpFlags.Help == true {
4851
fmt.Println(utils.EmitHelpText())
4952
} else {
5053
if *inpFlags.JSON == true {
51-
_, err := ProcessCount(count_details, file_details, folder_name, is_git_initialized, folder_count, mu, wg)
54+
_, err := ProcessCount(count_details, file_details, folder_name, is_git_initialized, folder_count, mu, wg, skipDir)
5255
if err != nil {
5356
fmt.Println(fmt.Errorf("%w", err))
5457
}
@@ -61,7 +64,7 @@ func ProcessByFlags(count_details *[]utils.OutputStructure, file_details *[]util
6164
}
6265
fmt.Println(jsonOutput)
6366
} else if *inpFlags.YAML == true {
64-
_, err := ProcessCount(count_details, file_details, folder_name, is_git_initialized, folder_count, mu, wg)
67+
_, err := ProcessCount(count_details, file_details, folder_name, is_git_initialized, folder_count, mu, wg, skipDir)
6568
if err != nil {
6669
fmt.Println(fmt.Errorf("%w", err))
6770
}
@@ -74,7 +77,7 @@ func ProcessByFlags(count_details *[]utils.OutputStructure, file_details *[]util
7477
}
7578
fmt.Println(yamlOutput)
7679
} else {
77-
total_counts, err := ProcessCount(count_details, file_details, folder_name, is_git_initialized, folder_count, mu, wg)
80+
total_counts, err := ProcessCount(count_details, file_details, folder_name, is_git_initialized, folder_count, mu, wg, skipDir)
7881
if err != nil {
7982
fmt.Println(fmt.Errorf("%w", err))
8083
}

process/process_workers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func ProcessConcurrentWorkers(
1212
is_git_initialized *bool,
1313
mu *sync.RWMutex,
1414
wg *sync.WaitGroup,
15+
skipDir string,
1516
) error {
1617

1718
// Limited goroutines to 50
@@ -20,7 +21,7 @@ func ProcessConcurrentWorkers(
2021
// Buffered jobs channel
2122
jobs := make(chan utils.File_info, 100)
2223

23-
files, err := utils.GetFiles(is_git_initialized, folder_count, *folder_name)
24+
files, err := utils.GetFiles(is_git_initialized, folder_count, *folder_name, skipDir)
2425

2526
if err != nil {
2627
return err

utils/handle_cmd.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type FlagOptions struct {
1111
JSON *bool
1212
YAML *bool
1313
Sort *bool
14+
NDir *string
1415
}
1516

1617
func HandleFlags(folder_name *string) FlagOptions {
@@ -21,6 +22,7 @@ func HandleFlags(folder_name *string) FlagOptions {
2122
var json = flag.Bool("json", false, "Get output in json format")
2223
var yaml = flag.Bool("yaml", false, "Get output in yaml format")
2324
var sort = flag.Bool("sort", false, "Sort result in descending order")
25+
var nDir = flag.String("excl-dir", "none", "Exclude Directory")
2426

2527
flag.Parse()
2628
if len(flag.Args()) > 0 {
@@ -33,5 +35,6 @@ func HandleFlags(folder_name *string) FlagOptions {
3335
JSON: json,
3436
YAML: yaml,
3537
Sort: sort,
38+
NDir: nDir,
3639
}
3740
}

utils/handle_files.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func GetFiles(
6666
is_git_initialized *bool,
6767
folder_count *int32,
6868
file_directory_name string,
69+
skipDir string,
6970
) ([]File_info, error) {
7071
var files []File_info
7172
folder_location := "."
@@ -85,13 +86,13 @@ func GetFiles(
8586
guardDir <- struct{}{}
8687
wgDir.Add(1)
8788

88-
err := walkDirConcur(folder_location, folder_count, &files, is_git_initialized, wgDir, muDir, guardDir)
89+
err := walkDirConcur(folder_location, folder_count, &files, is_git_initialized, wgDir, muDir, guardDir, skipDir)
8990
wgDir.Wait()
9091

9192
return files, err
9293
}
9394

94-
func walkDirConcur(folder_location string, folder_count *int32, files *[]File_info, is_git_initialized *bool, wgDir *sync.WaitGroup, muDir *sync.RWMutex, guardDir chan struct{}) error {
95+
func walkDirConcur(folder_location string, folder_count *int32, files *[]File_info, is_git_initialized *bool, wgDir *sync.WaitGroup, muDir *sync.RWMutex, guardDir chan struct{}, skipDir string) error {
9596
defer wgDir.Done()
9697

9798
visitFolder := func(
@@ -104,24 +105,26 @@ func walkDirConcur(folder_location string, folder_count *int32, files *[]File_in
104105
if err != nil {
105106
return err
106107
}
107-
// if it is a folder, then increase the folder count
108-
if f.IsDir() && _path != folder_location {
109-
110-
// if folder name is '.git', then
111-
// set is_git_initialized to true
112-
if _path == path.Join(folder_location, ".git") {
113-
muDir.Lock()
114-
if *is_git_initialized == false {
115-
*is_git_initialized = true
116-
}
117-
muDir.Unlock()
108+
// if folder name is '.git', then
109+
// set is_git_initialized to true
110+
if f.IsDir() && _path == path.Join(folder_location, ".git") {
111+
muDir.Lock()
112+
if *is_git_initialized == false {
113+
*is_git_initialized = true
118114
}
115+
muDir.Unlock()
116+
}
117+
if skipDir != "" && _path == skipDir {
118+
return filepath.SkipDir
119+
}
120+
// if it is a folder, then increase the folder count
121+
if f.IsDir() && _path != folder_location && _path != path.Join(folder_location, skipDir) {
119122
muDir.Lock()
120123
*folder_count++
121124
muDir.Unlock()
122125
guardDir <- struct{}{}
123126
wgDir.Add(1)
124-
go walkDirConcur(_path, folder_count, files, is_git_initialized, wgDir, muDir, guardDir)
127+
go walkDirConcur(_path, folder_count, files, is_git_initialized, wgDir, muDir, guardDir, "")
125128
return filepath.SkipDir
126129
}
127130
if f.Type().IsRegular() {

utils/utils.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,18 @@ This emits help text when --help tag is called
5050
*/
5151
func EmitHelpText() string {
5252

53-
versionDetails := `0.1.9`
53+
versionDetails := `0.1.10`
5454
authorDetails := `mainak55512 (mbhattacharjee432@gmail.com)`
55-
flagDetails := "--help\n--ext [list extension name]\n--excl-ext [list of extension name]\n--json\n--yaml\n--sort"
55+
flagDetails := "--help\n--ext [list extension name]\n--excl-ext [list of extension name]\n--excl-dir folder-name\n--json\n--yaml\n--sort"
5656
helpFlagDetails := "--help\tShows the usage details\n\n\tstto --help or,\n\tstto -help\n\n"
5757
extFlagDetails := "--ext\tFilters output based on the given extension\n\n\tstto --ext [extension name] [(optional) folder name] or,\n\tstto -ext [extension name] [(optional) folder name]\n\n"
5858
exclextFlagDetails := "--excl-ext\tFilters out files from output based on the given extension\n\n\tstto --excl-ext [extension name] [(optional) folder name] or,\n\tstto -excl-ext [extension name] [(optional) folder name]\n\n"
59+
exclDirFlagDetails := "--excl-dir\tFilters out folder from output\n\n\tstto --excl-dir [folder name] or,\n\tstto -excl-dir [folder name]\n\n"
5960
jsonFlagDetails := "--json\tEmits output in JSON format\n\n\tstto --json\n\n"
6061
yamlFlagDetails := "--yaml\tEmits output in YAML format\n\n\tstto --yaml\n\n"
6162
sortFlagDetails := "--sort\tSorts output based on descending line count\n\n\tstto --sort\n\n"
6263
generalUsageDetails := "\n\n[General usage]:\n\tstto or,\n\tstto [folder name]"
63-
returnText := "\nSTTO: a simple and quick line of code counter.\nAuthor: " + authorDetails + "\nVersion: " + versionDetails + generalUsageDetails + "\n\n[Flags]:\n" + flagDetails + "\n[Usage]:\n" + helpFlagDetails + extFlagDetails + exclextFlagDetails + jsonFlagDetails + yamlFlagDetails + sortFlagDetails
64+
returnText := "\nSTTO: a simple and quick line of code counter.\nAuthor: " + authorDetails + "\nVersion: " + versionDetails + generalUsageDetails + "\n\n[Flags]:\n" + flagDetails + "\n[Usage]:\n" + helpFlagDetails + extFlagDetails + exclextFlagDetails + exclDirFlagDetails + jsonFlagDetails + yamlFlagDetails + sortFlagDetails
6465

6566
return returnText
6667
}

0 commit comments

Comments
 (0)