Просмотр исходного кода

Display timestamps relative to the current date

This is not perfect yet (by far), but it's good enough for a demo.
Lucas Stadler лет назад: 8
Родитель
Сommit
d834b6feca
1 измененных файлов с 29 добавлено и 3 удалено
  1. 29 3
      quit.go

+ 29 - 3
quit.go

2
2
3
import (
3
import (
4
	"bytes"
4
	"bytes"
5
	"fmt"
5
	"html/template"
6
	"html/template"
6
	"io"
7
	"io"
7
	"log"
8
	"log"
182
	return fc.commit.Author().Name
183
	return fc.commit.Author().Name
183
}
184
}
184
185
185
func (fc *FancyCommit) Date() time.Time {
186
	return fc.commit.Author().When
186
func (fc *FancyCommit) Date() *FancyTime {
187
	return &FancyTime{fc.commit.Author().When}
188
}
189
190
type FancyTime struct {
191
	time.Time
192
}
193
194
func (fc FancyTime) PrettyString() string {
195
	now := time.Now()
196
	diff := time.Since(fc.Time)
197
	switch {
198
	case diff < time.Minute:
199
		return fmt.Sprintf("%d seconds ago", diff/time.Second)
200
	case diff < time.Hour:
201
		return fmt.Sprintf("%d minutes ago", diff/time.Minute)
202
	case now.AddDate(0, 0, -1).Before(fc.Time):
203
		return fmt.Sprintf("%d hours ago", diff/time.Hour)
204
	case now.AddDate(0, 0, -7).Before(fc.Time):
205
		return fmt.Sprintf("%d days ago", diff/(24*time.Hour))
206
	case now.AddDate(0, -1, 0).Before(fc.Time):
207
		return fmt.Sprintf("%d weeks ago", diff/(7*24*time.Hour))
208
	case now.AddDate(-1, 0, 0).Before(fc.Time):
209
		return fmt.Sprintf("%d months ago", diff/(30*7*24*time.Hour))
210
	default:
211
		return fmt.Sprintf("%d years ago", diff/(365*7*24*time.Hour))
212
	}
187
}
213
}
188
214
189
func (fc *FancyCommit) Id(n int) string {
215
func (fc *FancyCommit) Id(n int) string {
325
			<div class="commit-author">{{ .Repo.Commit.Author }}</div>
351
			<div class="commit-author">{{ .Repo.Commit.Author }}</div>
326
			<div class="commit-summary" title="{{ .Repo.Commit.Description }}">{{ .Repo.Commit.Summary }}</div>
352
			<div class="commit-summary" title="{{ .Repo.Commit.Description }}">{{ .Repo.Commit.Summary }}</div>
327
			<div class="commit-id" data-commit="{{ .Repo.Commit.Id 0 }}"><a href="#">{{ .Repo.Commit.Id 10 }}</a></div>
353
			<div class="commit-id" data-commit="{{ .Repo.Commit.Id 0 }}"><a href="#">{{ .Repo.Commit.Id 10 }}</a></div>
328
			<time class="commit-date">{{ .Repo.Commit.Date }}</time>
354
			<time class="commit-date" title="{{ .Repo.Commit.Date }}">{{ .Repo.Commit.Date.PrettyString }}</time>
329
		</section>
355
		</section>
330
356
331
		<table id="files">
357
		<table id="files">