summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors <[email protected]>2025-11-13 21:53:58 -0500
committers <[email protected]>2025-11-13 21:53:58 -0500
commit239936e87183a10a33ce593709eb16c92a04af98 (patch)
tree7230f6328610a8c1718b84e24dbc15735a5ab8b6
parent344a6f6415c3c1b593677adec3b8844e0839971b (diff)
downloaddborg-239936e87183a10a33ce593709eb16c92a04af98.tar.gz
dborg-239936e87183a10a33ce593709eb16c92a04af98.zip
refactor: improve file display formatting and metadata handling
-rw-r--r--internal/formatter/buckets.go98
-rw-r--r--internal/formatter/buckets_test.go30
2 files changed, 98 insertions, 30 deletions
diff --git a/internal/formatter/buckets.go b/internal/formatter/buckets.go
index 9672b9d..2cc6b78 100644
--- a/internal/formatter/buckets.go
+++ b/internal/formatter/buckets.go
@@ -5,6 +5,7 @@ import (
"fmt"
"sort"
"strings"
+ "time"
"git.db.org.ai/dborg/internal/models"
"git.db.org.ai/dborg/internal/utils"
@@ -346,23 +347,51 @@ func formatSingleFile(file map[string]any, index int) {
if name, ok := file["file"].(string); ok && name != "" {
fileName = name
- } else if u, ok := file["url"].(string); ok && u != "" {
+ } else if name, ok := file["filename"].(string); ok && name != "" {
+ fileName = name
+ }
+
+ if u, ok := file["url"].(string); ok && u != "" {
url = u
- fileName = extractFileNameFromURL(u)
+ if fileName == "unknown" {
+ fileName = extractFileNameFromURL(u)
+ }
}
fmt.Printf(" %s %s\n", Gray(fmt.Sprintf("%d.", index)), formatFileName(fileName))
if url != "" {
- fmt.Printf(" %s: %s\n", Cyan("URL"), Dim(url))
+ fmt.Printf(" %s: %s\n", Cyan("URL"), Blue(url))
}
if size, ok := file["size"].(float64); ok && size > 0 {
fmt.Printf(" %s: %s\n", Cyan("Size"), formatFileSize(int64(size)))
}
- if modified, ok := file["lastModified"].(string); ok && modified != "" {
- fmt.Printf(" %s: %s\n", Cyan("Modified"), Dim(modified))
+ if modified, ok := file["lastModified"].(float64); ok && modified > 0 {
+ fmt.Printf(" %s: %s\n", Cyan("Modified"), formatLastModified(int64(modified)))
+ }
+
+ var metadata []string
+
+ if bucketType, ok := file["type"].(string); ok && bucketType != "" {
+ metadata = append(metadata, strings.ToUpper(bucketType))
+ }
+
+ if container, ok := file["container"].(string); ok && container != "" {
+ metadata = append(metadata, fmt.Sprintf("Container: %s", container))
+ }
+
+ if bucketId, ok := file["bucketId"].(float64); ok && bucketId > 0 {
+ metadata = append(metadata, fmt.Sprintf("Bucket ID: %d", int64(bucketId)))
+ }
+
+ if fileId, ok := file["id"].(string); ok && fileId != "" {
+ metadata = append(metadata, fmt.Sprintf("File ID: %s", fileId))
+ }
+
+ if len(metadata) > 0 {
+ fmt.Printf(" %s: %s\n", Cyan("Info"), Gray(strings.Join(metadata, " • ")))
}
}
@@ -393,23 +422,6 @@ func formatFileName(name string) string {
return Gray("(unnamed file)")
}
- parts := strings.Split(name, "/")
- if len(parts) > 1 {
- path := strings.Join(parts[:len(parts)-1], "/")
- file := parts[len(parts)-1]
-
- if file == "" {
- file = "(directory)"
- }
-
- file = decodeURLEncoding(file)
-
- ext := getFileExtension(file)
- color := getExtensionColor(ext)
-
- return Gray(path+"/") + Colorize(file, color)
- }
-
name = decodeURLEncoding(name)
ext := getFileExtension(name)
color := getExtensionColor(ext)
@@ -468,3 +480,45 @@ func formatFileSize(bytes int64) string {
return Colorize(FormatBytes(bytes), color)
}
+
+func formatLastModified(timestamp int64) string {
+ t := time.Unix(timestamp, 0)
+ now := time.Now()
+
+ duration := now.Sub(t)
+ days := int(duration.Hours() / 24)
+
+ var timeStr string
+ var color string
+
+ switch {
+ case days == 0:
+ timeStr = "Today"
+ color = ColorGreen
+ case days == 1:
+ timeStr = "Yesterday"
+ color = ColorGreen
+ case days < 7:
+ timeStr = fmt.Sprintf("%d days ago", days)
+ color = ColorGreen
+ case days < 30:
+ weeks := days / 7
+ timeStr = fmt.Sprintf("%d weeks ago", weeks)
+ color = ColorCyan
+ case days < 365:
+ months := days / 30
+ timeStr = fmt.Sprintf("%d months ago", months)
+ color = ColorYellow
+ default:
+ years := days / 365
+ if years == 1 {
+ timeStr = "1 year ago"
+ } else {
+ timeStr = fmt.Sprintf("%d years ago", years)
+ }
+ color = ColorRed
+ }
+
+ dateStr := t.Format("2006-01-02 15:04:05")
+ return Colorize(timeStr, color) + Gray(fmt.Sprintf(" (%s)", dateStr))
+}
diff --git a/internal/formatter/buckets_test.go b/internal/formatter/buckets_test.go
index eabddf0..98c2397 100644
--- a/internal/formatter/buckets_test.go
+++ b/internal/formatter/buckets_test.go
@@ -130,21 +130,35 @@ func TestFormatBucketFilesResults(t *testing.T) {
"files": []any{
map[string]any{
"bucket": "test-bucket.s3.amazonaws.com",
+ "bucketId": float64(123),
+ "filename": "report.pdf",
+ "fullPath": "documents/report.pdf",
"file": "documents/report.pdf",
"url": "https://test-bucket.s3.amazonaws.com/documents/report.pdf",
"size": float64(1024000),
- "lastModified": "2024-01-01T00:00:00Z",
+ "lastModified": float64(1704067200),
+ "type": "aws",
+ "id": "12345",
},
map[string]any{
- "bucket": "test-bucket.s3.amazonaws.com",
- "file": "images/logo.png",
- "url": "https://test-bucket.s3.amazonaws.com/images/logo.png",
- "size": float64(50000),
+ "bucket": "test-bucket.s3.amazonaws.com",
+ "bucketId": float64(123),
+ "filename": "logo.png",
+ "fullPath": "images/logo.png",
+ "file": "images/logo.png",
+ "url": "https://test-bucket.s3.amazonaws.com/images/logo.png",
+ "size": float64(50000),
+ "lastModified": float64(1577836800),
+ "type": "aws",
},
map[string]any{
- "bucket": "another-bucket.s3.amazonaws.com",
- "file": "data.json",
- "url": "https://another-bucket.s3.amazonaws.com/data.json",
+ "bucket": "storage.blob.core.windows.net",
+ "bucketId": float64(456),
+ "filename": "data.json",
+ "file": "data.json",
+ "url": "https://storage.blob.core.windows.net/container/data.json",
+ "type": "azure",
+ "container": "container",
},
},
},