forked from yob/graval
-
Notifications
You must be signed in to change notification settings - Fork 8
/
listformatter.go
65 lines (52 loc) · 1.36 KB
/
listformatter.go
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
package graval
import (
"bytes"
"os"
"strconv"
"strings"
"github.com/jehiah/go-strftime"
)
type listFormatter struct {
files []os.FileInfo
}
func newListFormatter(files []os.FileInfo) *listFormatter {
f := new(listFormatter)
f.files = files
return f
}
// Short returns a string that lists the collection of files by name only,
// one per line
func (formatter *listFormatter) Short() string {
var buffer bytes.Buffer
for _, file := range formatter.files {
buffer.WriteString(file.Name())
buffer.WriteString("\r\n")
}
buffer.WriteString("\r\n")
return buffer.String()
}
// Detailed returns a string that lists the collection of files with extra
// detail, one per line
func (formatter *listFormatter) Detailed() string {
var buffer bytes.Buffer
for _, file := range formatter.files {
buffer.WriteString(file.Mode().String())
buffer.WriteString(" 1 owner group ")
buffer.WriteString(lpad(strconv.Itoa(int(file.Size())), 12))
buffer.WriteString(strftime.Format(" %b %d %H:%M ", file.ModTime()))
buffer.WriteString(file.Name())
buffer.WriteString("\r\n")
}
buffer.WriteString("\r\n")
return buffer.String()
}
func lpad(input string, length int) (result string) {
if len(input) < length {
result = strings.Repeat(" ", length-len(input)) + input
} else if len(input) == length {
result = input
} else {
result = input[0:length]
}
return
}