Skip to content

Commit

Permalink
add features and edit readmefile
Browse files Browse the repository at this point in the history
- Se agrego la flag --emojis
- Se agrego la flag --details
- Se agrego la flag --tree
  • Loading branch information
Wilovy09 committed Jun 12, 2024
1 parent f0a6a3c commit 87c2c07
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 21 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ alias ls='~/GoLS'

## Flags

| Flag | Description | Values | Default |
|--------|-----------------------------|---------|-------- |
| -a | Show hidden files | boolean | false |
| -depth | How deep the search will be | int | 0 |
| Flag | Description | Values | Default |
|----------|-----------------------------|---------|-------- |
| -a | Show hidden files | boolean | false |
| -depth | How deep the search will be | int | 0 |
| -details | Show de file/dir details | bollean | false |
| -tree | Show the files in tree view | boolean | false |
| -emoji | Show emojis in the view | boolean | false |
146 changes: 129 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import (
"flag"
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
"syscall"
"time"
)

func ReadFolder(path string, showAll bool, maxDepth int) {
readFolderRecursive(path, showAll, 0, maxDepth, "")
func ReadFolder(showDetails bool, path string, showAll bool, maxDepth int, showEmojis bool) {
readFolderRecursive(showDetails, path, showAll, 0, maxDepth, "", showEmojis)
}

func readFolderRecursive(path string, showAll bool, depth, maxDepth int, prefix string) {
func readFolderRecursive(showDetails bool, path string, showAll bool, depth, maxDepth int, prefix string, showEmojis bool) {
if maxDepth >= 0 && depth > maxDepth {
return
}
Expand All @@ -28,41 +31,139 @@ func readFolderRecursive(path string, showAll bool, depth, maxDepth int, prefix
continue
}

var fileType string
fileName := f.Name()
filePath := filepath.Join(path, fileName)
var details string
if showDetails {
details = getDetails(filePath)
}

if f.IsDir() {
fileType = "πŸ“"
} else {
fileType = "πŸ“„"
if showEmojis {
fileName = fmt.Sprintf("πŸ“%s", fileName)
} else {
fileName = fileName + "/"
}
} else if showEmojis {
fileName = fmt.Sprintf("πŸ“„%s", fileName)
}

if depth == 0 {
if i == len(files)-1 {
fmt.Printf("%s└──%s%s\n", prefix, fileType, f.Name())
fmt.Printf("%s%s└──%s\n", details, prefix, fileName)
if f.IsDir() {
readFolderRecursive(filepath.Join(path, f.Name()), showAll, depth+1, maxDepth, prefix+" ")
readFolderRecursive(showDetails, filePath, showAll, depth+1, maxDepth, prefix+" ", showEmojis)
}
} else {
fmt.Printf("%sβ”œβ”€β”€%s%s\n", prefix, fileType, f.Name())
fmt.Printf("%s%sβ”œβ”€β”€%s\n", details, prefix, fileName)
if f.IsDir() {
readFolderRecursive(filepath.Join(path, f.Name()), showAll, depth+1, maxDepth, prefix+"β”‚ ")
readFolderRecursive(showDetails, filePath, showAll, depth+1, maxDepth, prefix+"β”‚ ", showEmojis)
}
}
} else {
if i == len(files)-1 {
fmt.Printf("%s└──%s%s\n", prefix, fileType, f.Name())
fmt.Printf("%s%s└──%s\n", details, prefix, fileName)
if f.IsDir() {
readFolderRecursive(filepath.Join(path, f.Name()), showAll, depth+1, maxDepth, prefix+" ")
readFolderRecursive(showDetails, filePath, showAll, depth+1, maxDepth, prefix+" ", showEmojis)
}
} else {
fmt.Printf("%sβ”œβ”€β”€%s%s\n", prefix, fileType, f.Name())
fmt.Printf("%s%sβ”œβ”€β”€%s\n", details, prefix, fileName)
if f.IsDir() {
readFolderRecursive(filepath.Join(path, f.Name()), showAll, depth+1, maxDepth, prefix+"β”‚ ")
readFolderRecursive(showDetails, filePath, showAll, depth+1, maxDepth, prefix+"β”‚ ", showEmojis)
}
}
}
}
}

func ListFiles(showDetails bool, path string, showAll bool, showEmojis bool) {
files, err := os.ReadDir(path)
if err != nil {
fmt.Printf("Error al leer la carpeta: %s : %v\n", path, err)
return
}

var maxNameLen int
for _, f := range files {
if !showAll && strings.HasPrefix(f.Name(), ".") {
continue
}

fileName := f.Name()
if f.IsDir() {
if showEmojis {
fileName = fmt.Sprintf("πŸ“%s", fileName)
} else {
fileName = fileName + "/"
}
} else if showEmojis {
fileName = fmt.Sprintf("πŸ“„%s", fileName)
}

if len(fileName) > maxNameLen {
maxNameLen = len(fileName)
}
}

colWidth := maxNameLen + 2
numCols := 80 / colWidth
if numCols == 0 {
numCols = 1
}

var details []string
for _, f := range files {
if !showAll && strings.HasPrefix(f.Name(), ".") {
continue
}

fileName := f.Name()
filePath := filepath.Join(path, fileName)
if f.IsDir() {
if showEmojis {
fileName = fmt.Sprintf("πŸ“%s", fileName)
} else {
fileName = fileName + "/"
}
} else if showEmojis {
fileName = fmt.Sprintf("πŸ“„%s", fileName)
}

if showDetails {
details = append(details, fmt.Sprintf("%s %s", getDetails(filePath), fileName))
} else {
details = append(details, fileName)
}
}

for i := 0; i < len(details); i += numCols {
end := i + numCols
if end > len(details) {
end = len(details)
}
fmt.Println(strings.Join(details[i:end], strings.Repeat(" ", colWidth)))
}
}


func getDetails(filePath string) string {
info, err := os.Stat(filePath)
if err != nil {
return "Error obteniendo detalles"
}

sys := info.Sys().(*syscall.Stat_t)
uid := sys.Uid
u, err := user.LookupId(fmt.Sprint(uid))
if err != nil {
return "Error obteniendo usuario"
}

perm := info.Mode().Perm()
modTime := info.ModTime().Format(time.RFC3339)
return fmt.Sprintf("%s %s %s", perm, u.Username, modTime)
}

func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] [PATH]\n", os.Args[0])
Expand All @@ -72,13 +173,24 @@ func main() {
path := "."
showAll := flag.Bool("a", false, "Show all files and folders, including those beginning with '.'.")
maxDepth := flag.Int("depth", 0, "Maximum depth for folder traversal. Use -1 for unlimited depth.")
showDetails := flag.Bool("details", false, "Show file details (permissions, creator, date).")
showTree := flag.Bool("tree", false, "Show files and folders in a tree view.")
showEmojis := flag.Bool("emoji", false, "Show emojis for files and folders.")

flag.Parse()

if flag.NArg() > 0 {
path = flag.Arg(0)
}

fmt.Printf("πŸ“%s\n", path)
ReadFolder(path, *showAll, *maxDepth)
if *showTree {
if *showEmojis {
fmt.Printf("πŸ“%s\n", path)
} else {
fmt.Printf("%s/\n", path)
}
ReadFolder(*showDetails, path, *showAll, *maxDepth, *showEmojis)
} else {
ListFiles(*showDetails, path, *showAll, *showEmojis)
}
}

0 comments on commit 87c2c07

Please sign in to comment.