-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from OussamaM1/implement-tree-view-flag
✨ Implemented option to display directory in a tree view
- Loading branch information
Showing
6 changed files
with
159 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Package service provides functionalities related to directory listing. | ||
package service | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
|
||
"github.com/oussamaM1/treels/module" | ||
) | ||
|
||
const ( | ||
whiteSpaces = " " // Whitespaces | ||
boxUpAndRight = "└── " // BOX DRAWINGS HEAVY UP AND RIGHT | ||
boxLightVertical = "│ " // BOX DRAWINGS LIGHT VERTICAL | ||
boxLightVerticalAndRight = "├── " // BOX DRAWINGS LIGHT VERTICAL AND RIGHT | ||
) | ||
|
||
// Dispatcher func - executes function based on flags | ||
func Dispatcher(options module.Options) { | ||
if options.Flags.ShowTreeView { | ||
TreeDirectory(options, "", true) | ||
} else { | ||
ListDirectory(options) | ||
} | ||
} | ||
|
||
// ListDirectory func - lists the content of the directory. | ||
func ListDirectory(options module.Options) { | ||
CheckDefaultDirectory(&options.Directory) | ||
|
||
// Open and read the directory | ||
files, d, err := readDirectory(options.Directory) | ||
defer closeDirectory(d) | ||
if err != nil { | ||
log.Fatalf("Error reading directory: %s\n", err) | ||
} | ||
|
||
// Print files and directories | ||
fmt.Printf("Files and directories in %s:\n", options.Directory) | ||
for _, file := range files { | ||
if !isHidden(file.Name()) || options.Flags.ShowHidden { | ||
fmt.Println(file.Name()) | ||
} | ||
} | ||
} | ||
|
||
// TreeDirectory func - displays a tree view of the directory. | ||
func TreeDirectory(options module.Options, indent string, isLastFolder bool) { | ||
CheckDefaultDirectory(&options.Directory) | ||
|
||
// Open and read the directory | ||
files, d, err := readDirectory(options.Directory) | ||
defer closeDirectory(d) | ||
if err != nil { | ||
log.Fatalf("Error reading directory: %s\n", err) | ||
} | ||
|
||
// Sort files by name | ||
sort.Slice(files, func(i, j int) bool { | ||
return files[i].Name() < files[j].Name() | ||
}) | ||
|
||
// Print files and directories | ||
lastVisibleIndex := getLastVisibleIndex(files, options.Flags.ShowHidden) | ||
for i, file := range files { | ||
if !isHidden(file.Name()) || options.Flags.ShowHidden { | ||
var prefix, childIndentNext string | ||
if i == lastVisibleIndex && isLastFolder { | ||
prefix = indent + boxUpAndRight | ||
childIndentNext = indent + whiteSpaces | ||
} else if i == lastVisibleIndex { | ||
prefix = indent + boxUpAndRight | ||
childIndentNext = indent + whiteSpaces | ||
} else { | ||
prefix = indent + boxLightVerticalAndRight | ||
childIndentNext = indent + boxLightVertical | ||
} | ||
|
||
fmt.Println(prefix + file.Name()) | ||
|
||
if file.IsDir() { | ||
newDirectory := filepath.Join(options.Directory, file.Name()) | ||
newIsLastFolder := i == lastVisibleIndex && isLastFolder | ||
TreeDirectory(module.Options{Directory: newDirectory, Flags: module.Flags{ShowHidden: options.Flags.ShowHidden}}, childIndentNext, newIsLastFolder) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func getLastVisibleIndex(files []os.FileInfo, showHidden bool) int { | ||
for i := len(files) - 1; i >= 0; i-- { | ||
if !isHidden(files[i].Name()) || showHidden { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Package service - service/util.go | ||
package service | ||
|
||
import ( | ||
"log" | ||
"os" | ||
) | ||
|
||
// CheckDefaultDirectory func - returns current working directory if no directory is specified | ||
func CheckDefaultDirectory(directory *string) { | ||
if *directory == "" { | ||
// Get the current working directory - default value | ||
var err error | ||
*directory, err = os.Getwd() | ||
log.Println(*directory) | ||
if err != nil { | ||
log.Fatalf("Error getting current working directory: %s\n", err) | ||
} | ||
} | ||
} | ||
|
||
// readDirectory func - opens and reads the directory. | ||
func readDirectory(directory string) ([]os.FileInfo, *os.File, error) { | ||
// Open the directory | ||
d, err := os.Open(directory) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
// Read the directory contents | ||
files, err := d.Readdir(-1) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
return files, d, nil | ||
} | ||
|
||
// closeDirectory func - closes the directory. | ||
func closeDirectory(directory *os.File) { | ||
err := directory.Close() | ||
if err != nil { | ||
log.Fatalf("Error while closing directory: %s\n", err) | ||
} | ||
} | ||
|
||
// isHidden func - checks if the file name starts with a dot (hidden file). | ||
func isHidden(name string) bool { | ||
return len(name) > 0 && name[0] == '.' | ||
} |