Example #1
0
// FormatField formats a list of fields with the given template.
//
// It's a callback from the template, where the list of fields to display is set.
// With this method, we can translate titles, align data and allow custom
// templating for the user.
//
func FormatField(template *cdtype.Template, list []string, data interface{}) (string, error) {
	buf := bytes.NewBuffer(nil)
	tw := &tabwriter.Writer{}

	tw.Init(buf, 0, 4, 1, '\t', 0)
	needNL := false

	for _, key := range list {
		if needNL {
			fmt.Fprintln(tw)
		}
		needNL = true
		title := ""
		switch key {
		case "tempReal":
			title = tran.Splug("Temperature")

		case "tempFelt":
			title = tran.Splug("Feels like")

		case "wind":
			title = tran.Splug("Wind")

		case "humidity":
			title = tran.Splug("Humidity")

		case "pressure":
			title = tran.Splug("Pressure")

		case "sun":
			title = tran.Splug("Sunrise") + " - " + tran.Splug("Sunset")

		case "tempDay":
			title = tran.Splug("Temperature")

		case "precipitation":
			title = tran.Splug("Precipitation probability")
		}

		fmt.Fprint(tw, title+":\t")
		e := template.ExecuteTemplate(tw, key, data)
		if e != nil {
			return "", e
		}
	}
	tw.Flush()
	return strings.Trim(buf.String(), " \n"), nil
}
Example #2
0
// FormatTemplate formats the given data object with the given template function name.
//
func FormatTemplate(template *cdtype.Template, funcName string, data interface{}) (string, error) {
	text, e := template.ToString(funcName, data)
	return strings.Trim(text, " \n"), e
}