printer/templateprinter.go
changeset 120 54b6f2a4098b
parent 93 1cef5da83488
child 124 20d514540f37
equal deleted inserted replaced
119:cf398230331a 120:54b6f2a4098b
    13 	"io"
    13 	"io"
    14 	"os"
    14 	"os"
    15 	"reflect"
    15 	"reflect"
    16 	"strings"
    16 	"strings"
    17 	"text/template"
    17 	"text/template"
       
    18 	"time"
    18 
    19 
    19 	"github.com/m0t0k1ch1/gomif"
    20 	"github.com/m0t0k1ch1/gomif"
    20 	"github.com/mattn/go-isatty"
    21 	"github.com/mattn/go-isatty"
    21 
    22 
    22 	"github.com/McKael/madon"
    23 	"github.com/McKael/madon"
    41 	if tmpl == "" {
    42 	if tmpl == "" {
    42 		return nil, fmt.Errorf("empty template")
    43 		return nil, fmt.Errorf("empty template")
    43 	}
    44 	}
    44 	t, err := template.New("output").Funcs(template.FuncMap{
    45 	t, err := template.New("output").Funcs(template.FuncMap{
    45 		"fromhtml": html2string,
    46 		"fromhtml": html2string,
    46 		"fromunix": unix2string,
    47 		"fromunix": unix2time,
       
    48 		"tolocal":  dateToLocal,
    47 		"color":    ansiColor,
    49 		"color":    ansiColor,
    48 		"trim":     strings.TrimSpace,
    50 		"trim":     strings.TrimSpace,
    49 		"wrap":     wrap,
    51 		"wrap":     wrap,
    50 	}).Parse(tmpl)
    52 	}).Parse(tmpl)
    51 	if err != nil {
    53 	if err != nil {
   167 		width = 10
   169 		width = 10
   168 	}
   170 	}
   169 	doc.ToText(&buf, text, indent, indent+"  ", width)
   171 	doc.ToText(&buf, text, indent, indent+"  ", width)
   170 	return buf.String()
   172 	return buf.String()
   171 }
   173 }
       
   174 
       
   175 // Parse datetime string from RFC3339 (default format in templates because
       
   176 // of implicit conversion to string) and return a local time.
       
   177 func dateToLocal(s string) (time.Time, error) {
       
   178 	t, err := time.Parse(time.RFC3339, s)
       
   179 	if err != nil {
       
   180 		return t, err
       
   181 	}
       
   182 	return t.Local(), err
       
   183 }