Skip to content

Commit

Permalink
Merge pull request #3 from GergesHany/exporting_the_directory_structure
Browse files Browse the repository at this point in the history
Add support for exporting the directory structure to a file && update the readme
  • Loading branch information
Ahmedhossamdev authored Jan 18, 2025
2 parents c97151e + 12ab0f7 commit 6cf8088
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 23 deletions.
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ PrintLayout is a command-line tool that prints the directory structure of a spec
## Installation

### Option 1: Go Install

To install PrintLayout, run:

```bash
Expand Down Expand Up @@ -34,10 +35,6 @@ Download the pre-built binary for your operating system from the [Releases page]
```bash
printlayout or printlayout /path/to/your/folder
```





#### Windows

Expand Down Expand Up @@ -87,6 +84,12 @@ To run the project during development without installing it:
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 /path/to/your/folder /path/to/output/file
```

### Run Tests

To run the tests:
Expand All @@ -110,17 +113,19 @@ This will create an executable named printlayout in your project directory.
Here are some ideas for future improvements to the project:

1. Advanced Command-Line Options:
- Add support for limiting the depth of the directory tree (e.g., --depth 2).
- Add an option to include hidden files and directories (e.g., --hidden).
- Add an option to ignore files and directories listed in .gitignore (e.g., --ignore-gitignore).

- [ ] Add support for limiting the depth of the directory tree (e.g., --depth 2).
- [ ] an option to include hidden files and directories (e.g., --hidden).
- [ ] an option to ignore files and directories listed in .gitignore (e.g., --ignore-gitignore).

2. Customizable Output:
- Add support for customizing the tree symbols (e.g., --symbols=ascii for ASCII-only output).
- Add support for exporting the directory structure to a file (e.g., --output tree.txt).

- [ ] support for customizing the tree symbols (e.g., --symbols=ascii for ASCII-only output).
- [x] support for exporting the directory structure to a file (e.g., --output tree.txt).

3. Performance Improvements:
- Optimize the directory traversal for large directories.
- Add support for parallel processing of directories.
- [ ] Optimize the directory traversal for large directories.
- [ ] Add support for parallel processing of directories.

## Contributing

Expand Down
17 changes: 15 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@ import (
)

func main() {

// TODO: Add advanced command-line options (depth, hidden files, etc, ignore files, etc, ignore files that in .gitignore)

// Get the root directory
root := "."
if len(os.Args) > 1 {
root = os.Args[1]
}

printer.PrintProjectStructure(root)
}
// the output file
outputFile := ".";
if len(os.Args) > 2 {
outputFile = os.Args[2]
}

if outputFile == "." {
printer.PrintProjectStructure(root)
} else {
printer.PrintProjectStructureAndAddToDir(root, outputFile)
}
}
54 changes: 44 additions & 10 deletions pkg/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,37 @@ import (
"strings"
)

// PrintProjectStructureAndAddToDir prints the directory structure of the given root directory and writes it to the output file.
func PrintProjectStructureAndAddToDir(root string, outputFile string) {
absRoot, err := filepath.Abs(root)
if err != nil {
fmt.Println("Error getting absolute path:", err)
return
}

absOutputFile, err := filepath.Abs(outputFile)
if err != nil {
fmt.Println("Error getting absolute path:", err)
return
}

rootName := filepath.Base(absRoot)
output := fmt.Sprintf("%s/\n", rootName)

output += getTreeOutput(absRoot, "")

if absOutputFile != "" {
err := os.WriteFile(absOutputFile, []byte(output), 0644)
if err != nil {
fmt.Println("Error writing to file:", err)
}
} else {
fmt.Print(output)
}

fmt.Println(output)
}

// PrintProjectStructure prints the folder structure starting from the specified directory.
func PrintProjectStructure(root string) {
absRoot, err := filepath.Abs(root)
Expand All @@ -18,21 +49,22 @@ func PrintProjectStructure(root string) {

rootName := filepath.Base(absRoot)
fmt.Printf("%s/\n", rootName)

printTree(absRoot, "")
fmt.Print(getTreeOutput(absRoot, ""))
}

// printTree prints the directory tree structure.
func printTree(currentDir string, prefix string) {
// getTreeOutput returns the directory tree structure as a string.
func getTreeOutput(currentDir string, prefix string) string {
var output string

dir, err := os.Open(currentDir)
if err != nil {
return
return output
}
defer dir.Close()

entries, err := dir.Readdir(-1)
if err != nil {
return
return output
}

sort.Slice(entries, func(i, j int) bool {
Expand All @@ -48,12 +80,14 @@ func printTree(currentDir string, prefix string) {
isLast := i == len(entries)-1

if entry.IsDir() {
fmt.Printf("%s%s/\n", prefix+getTreePrefix(isLast), entry.Name())
printTree(filepath.Join(currentDir, entry.Name()), prefix+getIndent(isLast))
output += fmt.Sprintf("%s%s/\n", prefix+getTreePrefix(isLast), entry.Name())
output += getTreeOutput(filepath.Join(currentDir, entry.Name()), prefix+getIndent(isLast))
} else {
fmt.Printf("%s%s\n", prefix+getTreePrefix(isLast), entry.Name())
output += fmt.Sprintf("%s%s\n", prefix+getTreePrefix(isLast), entry.Name())
}
}

return output
}

// getTreePrefix returns the tree prefix for the current entry.
Expand All @@ -70,4 +104,4 @@ func getIndent(isLast bool) string {
return " "
}
return "│ "
}
}

0 comments on commit 6cf8088

Please sign in to comment.