Skip to content

Commit

Permalink
Merge pull request #2 from Ahmedhossamdev/feat/Add-simple-cli
Browse files Browse the repository at this point in the history
Add command-line support for specifying root directory in project structure printing
  • Loading branch information
Ahmedhossamdev authored Jan 17, 2025
2 parents 0456fa9 + 0a1e570 commit 0d612b1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
9 changes: 8 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ package main

import (
"PrintLayout/pkg/printer"
"os"
)

func main() {
printer.PrintProjectStructure()
// TODO: Add advanced command-line options (depth, hidden files, etc, ignore files, etc, ignore files that in .gitignore)
root := "."
if len(os.Args) > 1 {
root = os.Args[1]
}

printer.PrintProjectStructure(root)
}
14 changes: 6 additions & 8 deletions pkg/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import (
"strings"
)

// PrintProjectStructure prints the folder structure starting from the current directory.
func PrintProjectStructure() {
root, err := filepath.Abs(".")
// PrintProjectStructure prints the folder structure starting from the specified directory.
func PrintProjectStructure(root string) {
absRoot, err := filepath.Abs(root)
if err != nil {
fmt.Println("Error getting absolute path:", err)
return
}

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

printTree(root, "")
printTree(absRoot, "")
}

// printTree prints the directory tree structure.
Expand All @@ -30,12 +30,11 @@ func printTree(currentDir string, prefix string) {
}
defer dir.Close()

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

// Sort entries alphabetically
sort.Slice(entries, func(i, j int) bool {
return entries[i].Name() < entries[j].Name()
})
Expand All @@ -50,7 +49,6 @@ func printTree(currentDir string, prefix string) {

if entry.IsDir() {
fmt.Printf("%s%s/\n", prefix+getTreePrefix(isLast), entry.Name())
// Recursively print the contents of the directory
printTree(filepath.Join(currentDir, entry.Name()), prefix+getIndent(isLast))
} else {
fmt.Printf("%s%s\n", prefix+getTreePrefix(isLast), entry.Name())
Expand Down
11 changes: 1 addition & 10 deletions pkg/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,23 @@ import (

// TestPrintProjectStructure tests the PrintProjectStructure function.
func TestPrintProjectStructure(t *testing.T) {
// Create a temporary directory for testing
tmpDir := t.TempDir()

// Create the folder structure dynamically
createTestProjectStructure(t, tmpDir)

// Change the working directory to the temporary directory
oldDir, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get current working directory: %v", err)
}
defer os.Chdir(oldDir) // Restore the original working directory
os.Chdir(tmpDir)

// Capture the output
output := captureOutput(func() {
PrintProjectStructure()
PrintProjectStructure(".")
})

// Get the base name of the temporary directory
rootName := filepath.Base(tmpDir)

// Define the expected output based on the created structure
expected := rootName + "/\n" +
"├── cmd/\n" +
"│ └── main.go\n" +
Expand All @@ -44,17 +38,14 @@ func TestPrintProjectStructure(t *testing.T) {
" └── printer/\n" +
" ├── printer.go\n" +
" └── printer_test.go\n"
// Normalize the output and expected strings
output = strings.TrimSpace(output)
expected = strings.TrimSpace(expected)

// Compare the output
if output != expected {
t.Errorf("Unexpected output:\nGot:\n%s\nExpected:\n%s", output, expected)
}
}

// createTestProjectStructure creates a sample project structure for testing.
// createTestProjectStructure creates a sample project structure for testing.
func createTestProjectStructure(t *testing.T, root string) {
// Define the directories to create
Expand Down
Binary file added printlayout
Binary file not shown.

0 comments on commit 0d612b1

Please sign in to comment.