diff --git a/README.md b/README.md index 6b5047c..6730293 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,18 @@ To print the directory structure of a specific folder: printlayout -dir /path/to/your/folder ``` +To print the directory structure and filter by a specific file extension (e.g., .go, .js, .rb): + +```bash +printlayout -ext .js +``` + +To print the directory structure of a specific folder, filter by a specific file extension, and save the output to a file: + +```bash +printlayout -dir /path/to/your/folder -ext .rb -output output.txt +``` + ### Example Output ```bash @@ -81,13 +93,7 @@ printLayout/ To run the project during development without installing it: ```bash -go run ./cmd/main.go /path/to/your/folder -``` - -### Run the project and export the output to a file - -```bash -go run ./cmd/main.go -dir /path/to/your/folder -output /path/to/output/file +go run ./cmd/main.go ``` ### Run Tests diff --git a/cmd/main.go b/cmd/main.go index 31df782..e39773f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -10,8 +10,9 @@ func main() { flag.StringVar(&config.DirPath, "dir", ".", "Directory path to print the structure of") flag.StringVar(&config.OutputPath, "output", "", "Output file path") + flag.StringVar(&config.ExtFilter, "ext", "", "File extension filter (e.g., .go, .js)") flag.Parse() - printer.PrintProjectStructure(config.DirPath, config.OutputPath) + printer.PrintProjectStructure(config.DirPath, config.OutputPath, config.ExtFilter) } diff --git a/pkg/printer/printer.go b/pkg/printer/printer.go index 6a32a50..803e30c 100644 --- a/pkg/printer/printer.go +++ b/pkg/printer/printer.go @@ -12,16 +12,17 @@ import ( type Config struct { DirPath string OutputPath string + ExtFilter string } // HandleFlags func HandleFlags(config Config) { - PrintProjectStructure(config.DirPath, config.OutputPath) + PrintProjectStructure(config.DirPath, config.OutputPath, config.ExtFilter) } // PrintProjectStructure prints the directory structure of the given root directory. // It always prints the structure to the console and writes to the output file if provided. -func PrintProjectStructure(root string, outputFile string) { +func PrintProjectStructure(root string, outputFile string, extFilter string) { absRoot, err := filepath.Abs(root) if err != nil { fmt.Println("Error getting absolute path:", err) @@ -30,7 +31,7 @@ func PrintProjectStructure(root string, outputFile string) { rootName := filepath.Base(absRoot) output := fmt.Sprintf("%s/\n", rootName) - output += getTreeOutput(absRoot, "") + output += getTreeOutput(absRoot, "", extFilter) fmt.Print(output) @@ -53,7 +54,7 @@ func writeToFile(output, outputFile string) { } // getTreeOutput returns the directory tree structure as a string. -func getTreeOutput(currentDir string, prefix string) string { +func getTreeOutput(currentDir string, prefix string, extFilter string) string { var output string dir, err := os.Open(currentDir) @@ -81,9 +82,11 @@ func getTreeOutput(currentDir string, prefix string) string { if entry.IsDir() { output += fmt.Sprintf("%s%s/\n", prefix+getTreePrefix(isLast), entry.Name()) - output += getTreeOutput(filepath.Join(currentDir, entry.Name()), prefix+getIndent(isLast)) + output += getTreeOutput(filepath.Join(currentDir, entry.Name()), prefix+getIndent(isLast), extFilter) } else { - output += fmt.Sprintf("%s%s\n", prefix+getTreePrefix(isLast), entry.Name()) + if extFilter == "" || strings.HasSuffix(entry.Name(), extFilter) { + output += fmt.Sprintf("%s%s\n", prefix+getTreePrefix(isLast), entry.Name()) + } } } diff --git a/pkg/printer/printer_test.go b/pkg/printer/printer_test.go index 8d1f385..0dadb95 100644 --- a/pkg/printer/printer_test.go +++ b/pkg/printer/printer_test.go @@ -22,7 +22,7 @@ func TestPrintProjectStructure(t *testing.T) { os.Chdir(tmpDir) output := captureOutput(func() { - PrintProjectStructure(".", "") + PrintProjectStructure(".", "", "") }) rootName := filepath.Base(tmpDir)