| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io/fs" |
| 6 |
"os" |
| 7 |
"path/filepath" |
| 8 |
"strings" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/olekukonko/tablewriter" |
| 12 |
"github.com/olekukonko/tablewriter/tw" |
| 13 |
) |
| 14 |
|
| 15 |
const ( |
| 16 |
folder = "📁" |
| 17 |
file = "📄" |
| 18 |
baseDir = "../" |
| 19 |
indentStr = " " |
| 20 |
) |
| 21 |
|
| 22 |
func main() { |
| 23 |
table := tablewriter.NewTable(os.Stdout, tablewriter.WithTrimSpace(tw.Off)) |
| 24 |
table.Header([]string{"Tree", "Size", "Permissions", "Modified"}) |
| 25 |
err := filepath.WalkDir(baseDir, func(path string, d fs.DirEntry, err error) error { |
| 26 |
if err != nil { |
| 27 |
return err |
| 28 |
} |
| 29 |
|
| 30 |
if d.Name() == "." || d.Name() == ".." { |
| 31 |
return nil |
| 32 |
} |
| 33 |
|
| 34 |
// Calculate relative path depth |
| 35 |
relPath, err := filepath.Rel(baseDir, path) |
| 36 |
if err != nil { |
| 37 |
return err |
| 38 |
} |
| 39 |
|
| 40 |
depth := 0 |
| 41 |
if relPath != "." { |
| 42 |
depth = len(strings.Split(relPath, string(filepath.Separator))) - 1 |
| 43 |
} |
| 44 |
|
| 45 |
indent := strings.Repeat(indentStr, depth) |
| 46 |
|
| 47 |
var name string |
| 48 |
if d.IsDir() { |
| 49 |
name = fmt.Sprintf("%s%s %s", indent, folder, d.Name()) |
| 50 |
} else { |
| 51 |
name = fmt.Sprintf("%s%s %s", indent, file, d.Name()) |
| 52 |
} |
| 53 |
|
| 54 |
info, err := d.Info() |
| 55 |
if err != nil { |
| 56 |
return err |
| 57 |
} |
| 58 |
|
| 59 |
table.Append([]string{ |
| 60 |
name, |
| 61 |
Size(info.Size()).String(), |
| 62 |
info.Mode().String(), |
| 63 |
Time(info.ModTime()).Format(), |
| 64 |
}) |
| 65 |
|
| 66 |
return nil |
| 67 |
}) |
| 68 |
if err != nil { |
| 69 |
fmt.Fprintf(os.Stdout, "Error: %v\n", err) |
| 70 |
return |
| 71 |
} |
| 72 |
|
| 73 |
table.Render() |
| 74 |
} |
| 75 |
|
| 76 |
const ( |
| 77 |
KB = 1024 |
| 78 |
MB = KB * 1024 |
| 79 |
GB = MB * 1024 |
| 80 |
TB = GB * 1024 |
| 81 |
) |
| 82 |
|
| 83 |
type Size int64 |
| 84 |
|
| 85 |
func (s Size) String() string { |
| 86 |
switch { |
| 87 |
case s < KB: |
| 88 |
return fmt.Sprintf("%d B", s) |
| 89 |
case s < MB: |
| 90 |
return fmt.Sprintf("%.2f KB", float64(s)/KB) |
| 91 |
case s < GB: |
| 92 |
return fmt.Sprintf("%.2f MB", float64(s)/MB) |
| 93 |
case s < TB: |
| 94 |
return fmt.Sprintf("%.2f GB", float64(s)/GB) |
| 95 |
default: |
| 96 |
return fmt.Sprintf("%.2f TB", float64(s)/TB) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
type Time time.Time |
| 101 |
|
| 102 |
func (t Time) Format() string { |
| 103 |
now := time.Now() |
| 104 |
diff := now.Sub(time.Time(t)) |
| 105 |
|
| 106 |
if diff.Seconds() < 60 { |
| 107 |
return "just now" |
| 108 |
} else if diff.Minutes() < 60 { |
| 109 |
return fmt.Sprintf("%d minutes ago", int(diff.Minutes())) |
| 110 |
} else if diff.Hours() < 24 { |
| 111 |
return fmt.Sprintf("%d hours ago", int(diff.Hours())) |
| 112 |
} else if diff.Hours() < 24*7 { |
| 113 |
return fmt.Sprintf("%d days ago", int(diff.Hours()/24)) |
| 114 |
} else { |
| 115 |
return time.Time(t).Format("Jan 2, 2006") |
| 116 |
} |
| 117 |
} |