summaryrefslogtreecommitdiff
path: root/internal/app/view_helpers.go
blob: b023d62a6c636d0a9d82951b563d52480d44c08b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package app

import (
	"fmt"
	"os"
	"strings"
	"time"

	"github.com/charmbracelet/bubbles/table"
)

func videoRow(v video) table.Row {
	duration := "(unknown)"
	if v.Duration > 0 {
		duration = formatDuration(v.Duration)
	}
	age := humanizeAge(v.ModTime)
	path := trimPath(v.Path)
	if v.Err != nil {
		duration = "!" + v.Err.Error()
	}
	return table.Row{v.Name, duration, age, path}
}

func renderProgressBar(done, total, width int) string {
	if width <= 0 || total <= 0 {
		return ""
	}
	if done < 0 {
		done = 0
	}
	if done > total {
		done = total
	}
	filled := int(float64(done) / float64(total) * float64(width))
	if filled > width {
		filled = width
	}
	bar := strings.Repeat("#", filled) + strings.Repeat("-", width-filled)
	return fmt.Sprintf("[%s]", bar)
}

func formatDuration(d time.Duration) string {
	if d <= 0 {
		return "--"
	}
	totalSeconds := int(d.Seconds() + 0.5)
	hours := totalSeconds / 3600
	minutes := (totalSeconds % 3600) / 60
	seconds := totalSeconds % 60
	if hours > 0 {
		return fmt.Sprintf("%d:%02d:%02d", hours, minutes, seconds)
	}
	return fmt.Sprintf("%02d:%02d", minutes, seconds)
}

func humanizeAge(t time.Time) string {
	if t.IsZero() {
		return "--"
	}
	dur := time.Since(t)
	if dur < time.Minute {
		return "just now"
	}
	if dur < time.Hour {
		return fmt.Sprintf("%dm ago", int(dur.Minutes()))
	}
	if dur < 24*time.Hour {
		return fmt.Sprintf("%dh ago", int(dur.Hours()))
	}
	return t.Format("2006-01-02")
}

func trimPath(path string) string {
	home, err := os.UserHomeDir()
	if err == nil && strings.HasPrefix(path, home) {
		return "~" + strings.TrimPrefix(path, home)
	}
	return path
}