Skip to content

Commit

Permalink
Merge pull request #7 from Ahmedhossamdev/feat/new--ext-flag
Browse files Browse the repository at this point in the history
Add file extension filtering to directory structure printing
  • Loading branch information
Ahmedhossamdev authored Jan 18, 2025
2 parents c244f33 + 85c98dd commit 820ce37
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
15 changes: 9 additions & 6 deletions pkg/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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())
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestPrintProjectStructure(t *testing.T) {
os.Chdir(tmpDir)

output := captureOutput(func() {
PrintProjectStructure(".", "")
PrintProjectStructure(".", "", "")
})

rootName := filepath.Base(tmpDir)
Expand Down

0 comments on commit 820ce37

Please sign in to comment.