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
|
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fs
import (
"time"
)
// FormatFileInfo returns a formatted version of info for human readability.
// Implementations of [FileInfo] can call this from a String method.
// The output for a file named "hello.go", 100 bytes, mode 0o644, created
// January 1, 1970 at noon is
//
// -rw-r--r-- 100 1970-01-01 12:00:00 hello.go
func FormatFileInfo(info FileInfo) string {
name := info.Name()
b := make([]byte, 0, 40+len(name))
b = append(b, info.Mode().String()...)
b = append(b, ' ')
size := info.Size()
var usize uint64
if size >= 0 {
usize = uint64(size)
} else {
b = append(b, '-')
usize = uint64(-size)
}
var buf [20]byte
i := len(buf) - 1
for usize >= 10 {
q := usize / 10
buf[i] = byte('0' + usize - q*10)
i--
usize = q
}
buf[i] = byte('0' + usize)
b = append(b, buf[i:]...)
b = append(b, ' ')
b = append(b, info.ModTime().Format(time.DateTime)...)
b = append(b, ' ')
b = append(b, name...)
if info.IsDir() {
b = append(b, '/')
}
return string(b)
}
// FormatDirEntry returns a formatted version of dir for human readability.
// Implementations of [DirEntry] can call this from a String method.
// The outputs for a directory named subdir and a file named hello.go are:
//
// d subdir/
// - hello.go
func FormatDirEntry(dir DirEntry) string {
name := dir.Name()
b := make([]byte, 0, 5+len(name))
// The Type method does not return any permission bits,
// so strip them from the string.
mode := dir.Type().String()
mode = mode[:len(mode)-9]
b = append(b, mode...)
b = append(b, ' ')
b = append(b, name...)
if dir.IsDir() {
b = append(b, '/')
}
return string(b)
}
|