Add template command tolocal; update templates and documentation
authorMikael Berthe <mikael@lilotux.net>
Thu, 11 May 2017 18:05:06 +0200
changeset 120 54b6f2a4098b
parent 119 cf398230331a
child 121 10453a1c5698
Add template command tolocal; update templates and documentation
printer/plainprinter.go
printer/templateprinter.go
templates/README.md
templates/ansi-notification.tmpl
templates/ansi-status.tmpl
templates/themes/ansi/notification.tmpl
templates/themes/ansi/status.tmpl
--- 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
 }
--- 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
+}
--- 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:
--- 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}}
--- 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}}
--- 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}}
--- 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}}