Merge branch 'master' of github.com:gogits/gogs

This commit is contained in:
slene 2014-03-16 00:41:35 +08:00
commit 631deb2e7d
3 changed files with 48 additions and 1 deletions

View file

@ -21,6 +21,7 @@ var TemplateFuncs template.FuncMap = map[string]interface{}{
}, },
"str2html": Str2html, "str2html": Str2html,
"TimeSince": TimeSince, "TimeSince": TimeSince,
"FileSize": FileSize,
"Subtract": Subtract, "Subtract": Subtract,
"ActionIcon": ActionIcon, "ActionIcon": ActionIcon,
"ActionDesc": ActionDesc, "ActionDesc": ActionDesc,

View file

@ -8,6 +8,7 @@ import (
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"math"
"strings" "strings"
"time" "time"
) )
@ -80,6 +81,51 @@ func TimeSince(then time.Time) string {
return then.String() return then.String()
} }
const (
Byte = 1
KByte = Byte * 1024
MByte = KByte * 1024
GByte = MByte * 1024
TByte = GByte * 1024
PByte = TByte * 1024
EByte = PByte * 1024
)
var bytesSizeTable = map[string]uint64{
"b": Byte,
"kb": KByte,
"mb": MByte,
"gb": GByte,
"tb": TByte,
"pb": PByte,
"eb": EByte,
}
func logn(n, b float64) float64 {
return math.Log(n) / math.Log(b)
}
func humanateBytes(s uint64, base float64, sizes []string) string {
if s < 10 {
return fmt.Sprintf("%dB", s)
}
e := math.Floor(logn(float64(s), base))
suffix := sizes[int(e)]
val := float64(s) / math.Pow(base, math.Floor(e))
f := "%.0f"
if val < 10 {
f = "%.1f"
}
return fmt.Sprintf(f+"%s", val, suffix)
}
// FileSize calculates the file size and generate user-friendly string.
func FileSize(s int64) string {
sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"}
return humanateBytes(uint64(s), 1024, sizes)
}
// Subtract deals with subtraction of all types of number. // Subtract deals with subtraction of all types of number.
func Subtract(left interface{}, right interface{}) interface{} { func Subtract(left interface{}, right interface{}) interface{} {
var rleft, rright int64 var rleft, rright int64

View file

@ -47,7 +47,7 @@
{{if .IsDir}} {{if .IsDir}}
<a href="/{{$username}}/{{$reponame}}/tree/{{$branchname}}/{{.Path}}">{{.Name}}</a> <a href="/{{$username}}/{{$reponame}}/tree/{{$branchname}}/{{.Path}}">{{.Name}}</a>
{{else}} {{else}}
<a href="#">{{.Name}} - {{.Size}}</a> <a href="#">{{.Name}} - {{FileSize .Size}}</a>
{{end}}</td> {{end}}</td>
<td class="date"><time datetime="{{.Created}}" data-title="true" title="{{.Created}}">{{TimeSince .Created}}</time></td> <td class="date"><time datetime="{{.Created}}" data-title="true" title="{{.Created}}">{{TimeSince .Created}}</time></td>
<td class="text">{{.Message}}</td> <td class="text">{{.Message}}</td>