-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathformat.go
More file actions
60 lines (55 loc) · 1.13 KB
/
format.go
File metadata and controls
60 lines (55 loc) · 1.13 KB
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
package main
import (
"fmt"
"strings"
)
func formatAge(secs int64) string {
if secs <= 0 {
return "unknown"
}
if secs < 60 {
return fmt.Sprintf("%ds ago", secs)
}
if secs < 3600 {
return fmt.Sprintf("%dm ago", secs/60)
}
if secs < 86400 {
return fmt.Sprintf("%dh ago", secs/3600)
}
return fmt.Sprintf("%dd ago", secs/86400)
}
func pluralLines(n int) string {
if n == 1 {
return "1 line"
}
return fmt.Sprintf("%d lines", n)
}
// sectionRanges takes sorted line numbers and returns a string like "1-5, 10, 20-25"
func sectionRanges(nums []int) string {
if len(nums) == 0 {
return ""
}
var parts []string
start := nums[0]
prev := nums[0]
for i := 1; i < len(nums); i++ {
if nums[i] == prev+1 {
prev = nums[i]
continue
}
if start == prev {
parts = append(parts, fmt.Sprintf("%d", start))
} else {
parts = append(parts, fmt.Sprintf("%d-%d", start, prev))
}
start = nums[i]
prev = nums[i]
}
if start == prev {
parts = append(parts, fmt.Sprintf("%d", start))
} else {
parts = append(parts, fmt.Sprintf("%d-%d", start, prev))
}
return strings.Join(parts, ", ")
}
const scanBufferSize = 1024 * 1024