Skip to content

Commit

Permalink
Add option to include hidden files and directories
Browse files Browse the repository at this point in the history
  • Loading branch information
zyadtaha committed Jan 24, 2025
1 parent 820ce37 commit ee26bcf
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ 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).
- [ ] an option to include hidden files and directories (e.g., --hidden).
- [x] 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:
Expand Down
3 changes: 2 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,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.BoolVar(&config.IncludeHidden, "hidden", false, "Include hidden files and directories")

flag.Parse()

printer.PrintProjectStructure(config.DirPath, config.OutputPath, config.ExtFilter)
printer.PrintProjectStructure(config.DirPath, config.OutputPath, config.ExtFilter, config.IncludeHidden)
}
19 changes: 10 additions & 9 deletions pkg/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ import (

// Config holds the flag values
type Config struct {
DirPath string
OutputPath string
ExtFilter string
DirPath string
OutputPath string
ExtFilter string
IncludeHidden bool
}

// HandleFlags
func HandleFlags(config Config) {
PrintProjectStructure(config.DirPath, config.OutputPath, config.ExtFilter)
PrintProjectStructure(config.DirPath, config.OutputPath, config.ExtFilter, config.IncludeHidden)
}

// 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, extFilter string) {
func PrintProjectStructure(root string, outputFile string, extFilter string, includeHidden bool) {
absRoot, err := filepath.Abs(root)
if err != nil {
fmt.Println("Error getting absolute path:", err)
Expand All @@ -31,7 +32,7 @@ func PrintProjectStructure(root string, outputFile string, extFilter string) {

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

fmt.Print(output)

Expand All @@ -54,7 +55,7 @@ func writeToFile(output, outputFile string) {
}

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

dir, err := os.Open(currentDir)
Expand All @@ -74,15 +75,15 @@ func getTreeOutput(currentDir string, prefix string, extFilter string) string {

for i, entry := range entries {
// Skip hidden files/directories (those starting with ".")
if strings.HasPrefix(entry.Name(), ".") {
if !includeHidden && strings.HasPrefix(entry.Name(), ".") {
continue
}

isLast := i == len(entries)-1

if entry.IsDir() {
output += fmt.Sprintf("%s%s/\n", prefix+getTreePrefix(isLast), entry.Name())
output += getTreeOutput(filepath.Join(currentDir, entry.Name()), prefix+getIndent(isLast), extFilter)
output += getTreeOutput(filepath.Join(currentDir, entry.Name()), prefix+getIndent(isLast), extFilter, includeHidden)
} else {
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(".", "", "", false)
})

rootName := filepath.Base(tmpDir)
Expand Down

0 comments on commit ee26bcf

Please sign in to comment.