github.com/fawick/restic@v0.1.1-0.20171126184616-c02923fbfc79/cmd/restic/format.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "time" 8 9 "github.com/restic/restic/internal/restic" 10 ) 11 12 func formatBytes(c uint64) string { 13 b := float64(c) 14 15 switch { 16 case c > 1<<40: 17 return fmt.Sprintf("%.3f TiB", b/(1<<40)) 18 case c > 1<<30: 19 return fmt.Sprintf("%.3f GiB", b/(1<<30)) 20 case c > 1<<20: 21 return fmt.Sprintf("%.3f MiB", b/(1<<20)) 22 case c > 1<<10: 23 return fmt.Sprintf("%.3f KiB", b/(1<<10)) 24 default: 25 return fmt.Sprintf("%dB", c) 26 } 27 } 28 29 func formatSeconds(sec uint64) string { 30 hours := sec / 3600 31 sec -= hours * 3600 32 min := sec / 60 33 sec -= min * 60 34 if hours > 0 { 35 return fmt.Sprintf("%d:%02d:%02d", hours, min, sec) 36 } 37 38 return fmt.Sprintf("%d:%02d", min, sec) 39 } 40 41 func formatPercent(numerator uint64, denominator uint64) string { 42 if denominator == 0 { 43 return "" 44 } 45 46 percent := 100.0 * float64(numerator) / float64(denominator) 47 48 if percent > 100 { 49 percent = 100 50 } 51 52 return fmt.Sprintf("%3.2f%%", percent) 53 } 54 55 func formatRate(bytes uint64, duration time.Duration) string { 56 sec := float64(duration) / float64(time.Second) 57 rate := float64(bytes) / sec / (1 << 20) 58 return fmt.Sprintf("%.2fMiB/s", rate) 59 } 60 61 func formatDuration(d time.Duration) string { 62 sec := uint64(d / time.Second) 63 return formatSeconds(sec) 64 } 65 66 func formatNode(prefix string, n *restic.Node, long bool) string { 67 if !long { 68 return filepath.Join(prefix, n.Name) 69 } 70 71 switch n.Type { 72 case "file": 73 return fmt.Sprintf("%s %5d %5d %6d %s %s", 74 n.Mode, n.UID, n.GID, n.Size, n.ModTime.Format(TimeFormat), filepath.Join(prefix, n.Name)) 75 case "dir": 76 return fmt.Sprintf("%s %5d %5d %6d %s %s", 77 n.Mode|os.ModeDir, n.UID, n.GID, n.Size, n.ModTime.Format(TimeFormat), filepath.Join(prefix, n.Name)) 78 case "symlink": 79 return fmt.Sprintf("%s %5d %5d %6d %s %s -> %s", 80 n.Mode|os.ModeSymlink, n.UID, n.GID, n.Size, n.ModTime.Format(TimeFormat), filepath.Join(prefix, n.Name), n.LinkTarget) 81 default: 82 return fmt.Sprintf("<Node(%s) %s>", n.Type, n.Name) 83 } 84 }