# HG changeset patch # User Mikael Berthe # Date 1494518706 -7200 # Node ID 54b6f2a4098b7926f46b46a209c15b889cabaade # Parent cf398230331a3ce9c26c50083b4570b3f4bb9613 Add template command tolocal; update templates and documentation diff -r cf398230331a -r 54b6f2a4098b printer/plainprinter.go --- a/printer/plainprinter.go Thu May 11 18:05:06 2017 +0200 +++ b/printer/plainprinter.go Thu May 11 18:05:06 2017 +0200 @@ -127,17 +127,17 @@ return h // Failed: return initial string } -// unix2string convert a UNIX timestamp to a string -func unix2string(ts interface{}) string { +// unix2time convert a UNIX timestamp to a time.Time +func unix2time(ts interface{}) (time.Time, error) { switch t := ts.(type) { case int64: - return time.Unix(t, 0).String() + return time.Unix(t, 0), nil case int: - return time.Unix(int64(t), 0).String() + return time.Unix(int64(t), 0), nil case float64: - return time.Unix(int64(t), 0).String() + return time.Unix(int64(t), 0), nil } - return fmt.Sprintf("%v", ts) + return time.Time{}, fmt.Errorf("invalid timestamp type") } func indentedPrint(w io.Writer, indent string, title, skipIfEmpty bool, label string, format string, args ...interface{}) { @@ -301,7 +301,7 @@ indentedPrint(w, indent, true, false, "User token", "%s", s.AccessToken) indentedPrint(w, indent, false, true, "Type", "%s", s.TokenType) if s.CreatedAt != 0 { - indentedPrint(w, indent, false, true, "Timestamp", "%v", unix2string(s.CreatedAt)) + indentedPrint(w, indent, false, true, "Timestamp", "%v", time.Unix(s.CreatedAt, 0)) } indentedPrint(w, indent, false, true, "Scope", "%s", s.Scope) return nil @@ -316,6 +316,6 @@ indentedPrint(w, indent, false, false, "Statuses", "%d", is.Statuses) indentedPrint(w, indent, false, false, "Open Registrations", "%v", is.OpenRegistrations) indentedPrint(w, indent, false, false, "Up", "%v", is.Up) - indentedPrint(w, indent, false, false, "Date", "%s", unix2string(is.Date)) + indentedPrint(w, indent, false, false, "Date", "%s", time.Unix(is.Date, 0)) return nil } diff -r cf398230331a -r 54b6f2a4098b printer/templateprinter.go --- a/printer/templateprinter.go Thu May 11 18:05:06 2017 +0200 +++ b/printer/templateprinter.go Thu May 11 18:05:06 2017 +0200 @@ -15,6 +15,7 @@ "reflect" "strings" "text/template" + "time" "github.com/m0t0k1ch1/gomif" "github.com/mattn/go-isatty" @@ -43,7 +44,8 @@ } t, err := template.New("output").Funcs(template.FuncMap{ "fromhtml": html2string, - "fromunix": unix2string, + "fromunix": unix2time, + "tolocal": dateToLocal, "color": ansiColor, "trim": strings.TrimSpace, "wrap": wrap, @@ -169,3 +171,13 @@ doc.ToText(&buf, text, indent, indent+" ", width) return buf.String() } + +// Parse datetime string from RFC3339 (default format in templates because +// of implicit conversion to string) and return a local time. +func dateToLocal(s string) (time.Time, error) { + t, err := time.Parse(time.RFC3339, s) + if err != nil { + return t, err + } + return t.Local(), err +} diff -r cf398230331a -r 54b6f2a4098b templates/README.md --- a/templates/README.md Thu May 11 18:05:06 2017 +0200 +++ b/templates/README.md Thu May 11 18:05:06 2017 +0200 @@ -40,11 +40,12 @@ ### Template development Here's a list of available commands (please check the Go template documentation for the built-in functions): -- fromunix UNIXTIMESTAMP -- fromhtml HTMLTEXT -- wrap TEXT -- trim TEXT -- color COLORSPEC\ +- fromunix UNIXTIMESTAMP *converts from UNIX timestampto date* +- tolocal TEXTRFC3339DATE *converts a RFC3339 date string to a local time* +- fromhtml HTMLTEXT *converts HTML to plain text* +- wrap TEXT *rewrap text, with indent and max width* +- trim TEXT *trims text whitespace* +- color COLORSPEC *sends an ANSI color code sequence*\ COLORSPEC is a string with the following format: `[FGCOLOR][,BGCOLOR[,STYLE]]` or `reset`. Examples: diff -r cf398230331a -r 54b6f2a4098b templates/ansi-notification.tmpl --- a/templates/ansi-notification.tmpl Thu May 11 18:05:06 2017 +0200 +++ b/templates/ansi-notification.tmpl Thu May 11 18:05:06 2017 +0200 @@ -1,6 +1,6 @@ - Notification ID: {{color "red"}}{{printf "%.0f" .id}}{{color "reset"}} Type: {{color ",,bold"}}{{.type}}{{color "reset"}} - Timestamp: {{.created_at}} + Timestamp: {{.created_at | tolocal}} {{- with .url}} URL: {{.url}}{{end}} {{- with .account}} @@ -8,13 +8,13 @@ {{- with .status}} - Status ID: {{color "red"}}{{printf "%.0f" .id}}{{color "reset"}} {{color "magenta"}}@{{.account.acct}}{{color "reset"}} Name: {{color ",,bold"}}{{.account.display_name}}{{color "reset"}} - Date: {{.created_at}} + Date: {{.created_at | tolocal}} URL: {{.url}} {{- if .reblog }}{{with .reblog}} {{color ",,bold"}}Reblogged from: {{color "magenta"}}@{{.account.acct}}{{color "reset"}} ID: {{printf "%.0f" .id}} Name: {{color ",,bold"}}{{.account.display_name}}{{color "reset"}} - Date: {{.created_at}} + Date: {{.created_at | tolocal}} {{- if gt .in_reply_to_id 0.0}} Replying to: {{printf "%.0f" .in_reply_to_id}}{{end}} {{- if .sensitive}} diff -r cf398230331a -r 54b6f2a4098b templates/ansi-status.tmpl --- a/templates/ansi-status.tmpl Thu May 11 18:05:06 2017 +0200 +++ b/templates/ansi-status.tmpl Thu May 11 18:05:06 2017 +0200 @@ -1,12 +1,12 @@ - Status ID: {{color "red"}}{{printf "%.0f" .id}}{{color "reset"}} {{color "magenta"}}@{{.account.acct}}{{color "reset"}} Name: {{color ",,bold"}}{{.account.display_name}}{{color "reset"}} - Date: {{.created_at}} + Date: {{.created_at | tolocal}} URL: {{.url}} {{- if .reblog }}{{with .reblog}} {{color ",,bold"}}Reblogged from: {{color "magenta"}}@{{.account.acct}}{{color "reset"}} ID: {{printf "%.0f" .id}} Name: {{color ",,bold"}}{{.account.display_name}}{{color "reset"}} - Date: {{.created_at}} + Date: {{.created_at | tolocal}} {{- if gt .in_reply_to_id 0.0}} Replying to: {{printf "%.0f" .in_reply_to_id}}{{end}} {{- if .sensitive}} diff -r cf398230331a -r 54b6f2a4098b templates/themes/ansi/notification.tmpl --- a/templates/themes/ansi/notification.tmpl Thu May 11 18:05:06 2017 +0200 +++ b/templates/themes/ansi/notification.tmpl Thu May 11 18:05:06 2017 +0200 @@ -1,6 +1,6 @@ - Notification ID: {{color "red"}}{{printf "%.0f" .id}}{{color "reset"}} Type: {{color ",,bold"}}{{.type}}{{color "reset"}} - Timestamp: {{.created_at}} + Timestamp: {{.created_at | tolocal}} {{- with .url}} URL: {{.url}}{{end}} {{- with .account}} @@ -8,13 +8,13 @@ {{- with .status}} - Status ID: {{color "red"}}{{printf "%.0f" .id}}{{color "reset"}} {{color "magenta"}}@{{.account.acct}}{{color "reset"}} Name: {{color ",,bold"}}{{.account.display_name}}{{color "reset"}} - Date: {{.created_at}} + Date: {{.created_at | tolocal}} URL: {{.url}} {{- if .reblog }}{{with .reblog}} {{color ",,bold"}}Reblogged from: {{color "magenta"}}@{{.account.acct}}{{color "reset"}} ID: {{printf "%.0f" .id}} Name: {{color ",,bold"}}{{.account.display_name}}{{color "reset"}} - Date: {{.created_at}} + Date: {{.created_at | tolocal}} {{- if gt .in_reply_to_id 0.0}} Replying to: {{printf "%.0f" .in_reply_to_id}}{{end}} {{- if .sensitive}} diff -r cf398230331a -r 54b6f2a4098b templates/themes/ansi/status.tmpl --- a/templates/themes/ansi/status.tmpl Thu May 11 18:05:06 2017 +0200 +++ b/templates/themes/ansi/status.tmpl Thu May 11 18:05:06 2017 +0200 @@ -1,12 +1,12 @@ - Status ID: {{color "red"}}{{printf "%.0f" .id}}{{color "reset"}} {{color "magenta"}}@{{.account.acct}}{{color "reset"}} Name: {{color ",,bold"}}{{.account.display_name}}{{color "reset"}} - Date: {{.created_at}} + Date: {{.created_at | tolocal}} URL: {{.url}} {{- if .reblog }}{{with .reblog}} {{color ",,bold"}}Reblogged from: {{color "magenta"}}@{{.account.acct}}{{color "reset"}} ID: {{printf "%.0f" .id}} Name: {{color ",,bold"}}{{.account.display_name}}{{color "reset"}} - Date: {{.created_at}} + Date: {{.created_at | tolocal}} {{- if gt .in_reply_to_id 0.0}} Replying to: {{printf "%.0f" .in_reply_to_id}}{{end}} {{- if .sensitive}}