printer/themeprinter.go
author Mikael Berthe <mikael@lilotux.net>
Wed, 21 Mar 2018 22:47:40 +0100
changeset 214 78fe649d7fc9
parent 194 660233815ca8
child 239 605a00e9d1ab
permissions -rw-r--r--
Version 2.3.0
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/pkg/errors"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
	"github.com/McKael/madon"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
)
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
const themeDirName = "themes"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
// ThemePrinter represents a Theme printer
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
type ThemePrinter struct {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
	name        string
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
	templateDir string
89
3758bb5f0979 ResourcePrinter (templates): Use options to pass color_mode
Mikael Berthe <mikael@lilotux.net>
parents: 87
diff changeset
    27
	colorMode   string
85
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).
89
3758bb5f0979 ResourcePrinter (templates): Use options to pass color_mode
Mikael Berthe <mikael@lilotux.net>
parents: 87
diff changeset
    34
// The "color_mode" option defines the color behaviour: it can be
3758bb5f0979 ResourcePrinter (templates): Use options to pass color_mode
Mikael Berthe <mikael@lilotux.net>
parents: 87
diff changeset
    35
// "auto" (default), "on" (forced), "off" (disabled).
85
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
func NewPrinterTheme(options Options) (*ThemePrinter, error) {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
	name, ok := options["name"]
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
	if !ok || name == "" {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
		return nil, fmt.Errorf("no theme name")
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
	}
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
	if strings.IndexRune(name, '/') >= 0 {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
		// Should we allow that?  (subthemes, etc.)
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
		return nil, fmt.Errorf("invalid theme name")
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
	}
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
	return &ThemePrinter{
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
		name:        name,
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
		templateDir: options["template_directory"],
89
3758bb5f0979 ResourcePrinter (templates): Use options to pass color_mode
Mikael Berthe <mikael@lilotux.net>
parents: 87
diff changeset
    48
		colorMode:   options["color_mode"],
85
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
	}, nil
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
}
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
// PrintObj sends the object as text to the writer
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
// If the writer w is nil, standard output will be used.
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
func (p *ThemePrinter) PrintObj(obj interface{}, w io.Writer, tmpl string) error {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
	if w == nil {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
		w = os.Stdout
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
	}
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
	if p.name == "" {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
		return fmt.Errorf("no theme name") // Should not happen
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
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
	var objType string
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    65
	switch obj.(type) {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
	case []madon.Account, madon.Account, *madon.Account:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
		objType = "account"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    68
	case []madon.Application, madon.Application, *madon.Application:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    69
		objType = "application"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
	case []madon.Attachment, madon.Attachment, *madon.Attachment:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    71
		objType = "attachment"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    72
	case []madon.Card, madon.Card, *madon.Card:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    73
		objType = "card"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    74
	case []madon.Client, madon.Client, *madon.Client:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    75
		objType = "client"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    76
	case []madon.Context, madon.Context, *madon.Context:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    77
		objType = "context"
193
4a1e3b57fd0f Themes & Templates: Add new types
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    78
	case []madon.Emoji, madon.Emoji, *madon.Emoji:
4a1e3b57fd0f Themes & Templates: Add new types
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    79
		objType = "emoji"
85
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    80
	case []madon.Instance, madon.Instance, *madon.Instance:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    81
		objType = "instance"
193
4a1e3b57fd0f Themes & Templates: Add new types
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    82
	case []madon.List, madon.List, *madon.List:
4a1e3b57fd0f Themes & Templates: Add new types
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    83
		objType = "list"
85
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    84
	case []madon.Mention, madon.Mention, *madon.Mention:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    85
		objType = "mention"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    86
	case []madon.Notification, madon.Notification, *madon.Notification:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    87
		objType = "notification"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    88
	case []madon.Relationship, madon.Relationship, *madon.Relationship:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    89
		objType = "relationship"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    90
	case []madon.Report, madon.Report, *madon.Report:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    91
		objType = "report"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    92
	case []madon.Results, madon.Results, *madon.Results:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    93
		objType = "results"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    94
	case []madon.Status, madon.Status, *madon.Status:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    95
		objType = "status"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    96
	case []madon.StreamEvent, madon.StreamEvent, *madon.StreamEvent:
193
4a1e3b57fd0f Themes & Templates: Add new types
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    97
		objType = "stream_event"
85
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    98
	case []madon.Tag, madon.Tag, *madon.Tag:
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    99
		objType = "tag"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   100
	}
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   101
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   102
	var rp *ResourcePrinter
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   103
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   104
	if objType != "" {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   105
		// Check template exists
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   106
		templatePath := filepath.Join(p.templateDir, themeDirName, p.name, objType) + ".tmpl"
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   107
		if _, err := os.Stat(templatePath); err != nil {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   108
			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
   109
		} else {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   110
			t, err := ioutil.ReadFile(templatePath)
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 read template")
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   113
			}
89
3758bb5f0979 ResourcePrinter (templates): Use options to pass color_mode
Mikael Berthe <mikael@lilotux.net>
parents: 87
diff changeset
   114
			o := Options{
3758bb5f0979 ResourcePrinter (templates): Use options to pass color_mode
Mikael Berthe <mikael@lilotux.net>
parents: 87
diff changeset
   115
				"template":   string(t),
3758bb5f0979 ResourcePrinter (templates): Use options to pass color_mode
Mikael Berthe <mikael@lilotux.net>
parents: 87
diff changeset
   116
				"color_mode": p.colorMode,
3758bb5f0979 ResourcePrinter (templates): Use options to pass color_mode
Mikael Berthe <mikael@lilotux.net>
parents: 87
diff changeset
   117
			}
3758bb5f0979 ResourcePrinter (templates): Use options to pass color_mode
Mikael Berthe <mikael@lilotux.net>
parents: 87
diff changeset
   118
			np, err := NewPrinter("template", o)
85
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   119
			if err != nil {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   120
				return errors.Wrap(err, "cannot create template printer")
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
			rp = &np
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   123
		}
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   124
	}
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   125
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   126
	if rp != nil {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   127
		return (*rp).PrintObj(obj, w, "")
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   128
	}
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   129
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   130
	// No resource printer; let's fall back to plain printer
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   131
	// XXX Maybe we should just fail?
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   132
	plainP, err := NewPrinter("plain", nil)
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   133
	if err != nil {
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   134
		return errors.Wrap(err, "cannot create plaintext printer")
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   135
	}
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   136
	return plainP.PrintObj(obj, w, "")
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   137
}