printer/themeprinter.go
author Mikael Berthe <mikael@lilotux.net>
Sun, 07 May 2017 16:43:51 +0200
changeset 87 cd1c3f1610e4
parent 85 a4464c0b0c36
child 89 3758bb5f0979
permissions -rw-r--r--
Themes: Template for "instance --stats" is "instancestats.tmpl"
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
85
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
// Copyright © 2017 Mikael Berthe <mikael@lilotux.net>
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
//
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
// Licensed under the MIT license.
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
// Please see the LICENSE file is this directory.
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
package printer
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
import (
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
	"fmt"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
	"io"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
	"io/ioutil"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
	"os"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
	"path/filepath"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
	"strings"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
	"github.com/m0t0k1ch1/gomif"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
	"github.com/pkg/errors"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
	"github.com/McKael/madon"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
)
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
const themeDirName = "themes"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
// ThemePrinter represents a Theme printer
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
type ThemePrinter struct {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
	name        string
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
	templateDir string
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
}
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
// NewPrinterTheme returns a Theme ResourcePrinter
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
// For ThemePrinter, the options parameter contains the name of the theme
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
// and the template base directory (themes are assumed to be in the "themes"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
// subdirectory).
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
func NewPrinterTheme(options Options) (*ThemePrinter, error) {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
	name, ok := options["name"]
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
	if !ok || name == "" {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
		return nil, fmt.Errorf("no theme name")
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
	}
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
	if strings.IndexRune(name, '/') >= 0 {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
		// Should we allow that?  (subthemes, etc.)
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
		return nil, fmt.Errorf("invalid theme name")
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
	}
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
	return &ThemePrinter{
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
		name:        name,
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
		templateDir: options["template_directory"],
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
	}, nil
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
}
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
// PrintObj sends the object as text to the writer
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
// If the writer w is nil, standard output will be used.
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
func (p *ThemePrinter) PrintObj(obj interface{}, w io.Writer, tmpl string) error {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
	if w == nil {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
		w = os.Stdout
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
	}
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
	if p.name == "" {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
		return fmt.Errorf("no theme name") // Should not happen
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
	}
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
	var objType string
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
	switch obj.(type) {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
	case []madon.Account, madon.Account, *madon.Account:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
		objType = "account"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    65
	case []madon.Application, madon.Application, *madon.Application:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
		objType = "application"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
	case []madon.Attachment, madon.Attachment, *madon.Attachment:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    68
		objType = "attachment"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    69
	case []madon.Card, madon.Card, *madon.Card:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
		objType = "card"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    71
	case []madon.Client, madon.Client, *madon.Client:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    72
		objType = "client"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    73
	case []madon.Context, madon.Context, *madon.Context:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    74
		objType = "context"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    75
	case []madon.Instance, madon.Instance, *madon.Instance:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    76
		objType = "instance"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    77
	case []madon.Mention, madon.Mention, *madon.Mention:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    78
		objType = "mention"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    79
	case []madon.Notification, madon.Notification, *madon.Notification:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    80
		objType = "notification"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    81
	case []madon.Relationship, madon.Relationship, *madon.Relationship:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    82
		objType = "relationship"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    83
	case []madon.Report, madon.Report, *madon.Report:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    84
		objType = "report"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    85
	case []madon.Results, madon.Results, *madon.Results:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    86
		objType = "results"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    87
	case []madon.Status, madon.Status, *madon.Status:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    88
		objType = "status"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    89
	case []madon.StreamEvent, madon.StreamEvent, *madon.StreamEvent:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    90
		objType = "streamEvent"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    91
	case []madon.Tag, madon.Tag, *madon.Tag:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    92
		objType = "tag"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    93
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    94
	case []*gomif.InstanceStatus, *gomif.InstanceStatus:
87
cd1c3f1610e4 Themes: Template for "instance --stats" is "instancestats.tmpl"
Mikael Berthe <mikael@lilotux.net>
parents: 85
diff changeset
    95
		objType = "instancestats"
85
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    96
	}
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    97
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    98
	var rp *ResourcePrinter
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    99
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   100
	if objType != "" {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   101
		// Check template exists
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   102
		templatePath := filepath.Join(p.templateDir, themeDirName, p.name, objType) + ".tmpl"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   103
		if _, err := os.Stat(templatePath); err != nil {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   104
			fmt.Fprintf(os.Stderr, "Warning: theme template not found, falling back to plaintext printer\n") // XXX
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   105
		} else {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   106
			t, err := ioutil.ReadFile(templatePath)
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   107
			if err != nil {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   108
				return errors.Wrap(err, "cannot read template")
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   109
			}
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   110
			np, err := NewPrinter("template", Options{"template": string(t)})
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   111
			if err != nil {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   112
				return errors.Wrap(err, "cannot create template printer")
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   113
			}
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   114
			rp = &np
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   115
		}
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   116
	}
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   117
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   118
	if rp != nil {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   119
		return (*rp).PrintObj(obj, w, "")
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   120
	}
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   121
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   122
	// No resource printer; let's fall back to plain printer
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   123
	// XXX Maybe we should just fail?
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   124
	plainP, err := NewPrinter("plain", nil)
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   125
	if err != nil {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   126
		return errors.Wrap(err, "cannot create plaintext printer")
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   127
	}
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   128
	return plainP.PrintObj(obj, w, "")
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   129
}