author | Mikael Berthe <mikael@lilotux.net> |
Sat, 04 Feb 2023 13:17:17 +0100 | |
changeset 268 | 4dd196a4ee7c |
parent 265 | 05c40b36d3b2 |
permissions | -rw-r--r-- |
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
239
diff
changeset
|
1 |
// Copyright © 2017-2023 Mikael Berthe <mikael@lilotux.net> |
85 | 2 |
// |
3 |
// Licensed under the MIT license. |
|
4 |
// Please see the LICENSE file is this directory. |
|
5 |
||
6 |
package printer |
|
7 |
||
8 |
import ( |
|
9 |
"fmt" |
|
10 |
"io" |
|
11 |
"io/ioutil" |
|
12 |
"os" |
|
13 |
"path/filepath" |
|
14 |
"strings" |
|
15 |
||
16 |
"github.com/pkg/errors" |
|
17 |
||
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
239
diff
changeset
|
18 |
"github.com/McKael/madon/v3" |
85 | 19 |
) |
20 |
||
21 |
const themeDirName = "themes" |
|
22 |
||
23 |
// ThemePrinter represents a Theme printer |
|
24 |
type ThemePrinter struct { |
|
25 |
name string |
|
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 | 28 |
} |
29 |
||
30 |
// NewPrinterTheme returns a Theme ResourcePrinter |
|
31 |
// For ThemePrinter, the options parameter contains the name of the theme |
|
32 |
// and the template base directory (themes are assumed to be in the "themes" |
|
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 | 36 |
func NewPrinterTheme(options Options) (*ThemePrinter, error) { |
37 |
name, ok := options["name"] |
|
38 |
if !ok || name == "" { |
|
39 |
return nil, fmt.Errorf("no theme name") |
|
40 |
} |
|
41 |
if strings.IndexRune(name, '/') >= 0 { |
|
42 |
// Should we allow that? (subthemes, etc.) |
|
43 |
return nil, fmt.Errorf("invalid theme name") |
|
44 |
} |
|
45 |
return &ThemePrinter{ |
|
46 |
name: name, |
|
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 | 49 |
}, nil |
50 |
} |
|
51 |
||
52 |
// PrintObj sends the object as text to the writer |
|
53 |
// If the writer w is nil, standard output will be used. |
|
54 |
func (p *ThemePrinter) PrintObj(obj interface{}, w io.Writer, tmpl string) error { |
|
55 |
if w == nil { |
|
56 |
w = os.Stdout |
|
57 |
} |
|
58 |
||
59 |
if p.name == "" { |
|
60 |
return fmt.Errorf("no theme name") // Should not happen |
|
61 |
} |
|
62 |
||
63 |
var objType string |
|
64 |
||
65 |
switch obj.(type) { |
|
66 |
case []madon.Account, madon.Account, *madon.Account: |
|
67 |
objType = "account" |
|
68 |
case []madon.Application, madon.Application, *madon.Application: |
|
69 |
objType = "application" |
|
70 |
case []madon.Attachment, madon.Attachment, *madon.Attachment: |
|
71 |
objType = "attachment" |
|
72 |
case []madon.Card, madon.Card, *madon.Card: |
|
73 |
objType = "card" |
|
74 |
case []madon.Client, madon.Client, *madon.Client: |
|
75 |
objType = "client" |
|
76 |
case []madon.Context, madon.Context, *madon.Context: |
|
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 | 80 |
case []madon.Instance, madon.Instance, *madon.Instance: |
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 | 84 |
case []madon.Mention, madon.Mention, *madon.Mention: |
85 |
objType = "mention" |
|
86 |
case []madon.Notification, madon.Notification, *madon.Notification: |
|
87 |
objType = "notification" |
|
88 |
case []madon.Relationship, madon.Relationship, *madon.Relationship: |
|
89 |
objType = "relationship" |
|
90 |
case []madon.Report, madon.Report, *madon.Report: |
|
91 |
objType = "report" |
|
92 |
case []madon.Results, madon.Results, *madon.Results: |
|
93 |
objType = "results" |
|
94 |
case []madon.Status, madon.Status, *madon.Status: |
|
95 |
objType = "status" |
|
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 | 98 |
case []madon.Tag, madon.Tag, *madon.Tag: |
99 |
objType = "tag" |
|
100 |
} |
|
101 |
||
102 |
var rp *ResourcePrinter |
|
103 |
||
104 |
if objType != "" { |
|
105 |
// Check template exists |
|
106 |
templatePath := filepath.Join(p.templateDir, themeDirName, p.name, objType) + ".tmpl" |
|
107 |
if _, err := os.Stat(templatePath); err != nil { |
|
108 |
fmt.Fprintf(os.Stderr, "Warning: theme template not found, falling back to plaintext printer\n") // XXX |
|
109 |
} else { |
|
110 |
t, err := ioutil.ReadFile(templatePath) |
|
111 |
if err != nil { |
|
112 |
return errors.Wrap(err, "cannot read template") |
|
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 | 119 |
if err != nil { |
120 |
return errors.Wrap(err, "cannot create template printer") |
|
121 |
} |
|
122 |
rp = &np |
|
123 |
} |
|
124 |
} |
|
125 |
||
126 |
if rp != nil { |
|
127 |
return (*rp).PrintObj(obj, w, "") |
|
128 |
} |
|
129 |
||
130 |
// No resource printer; let's fall back to plain printer |
|
131 |
// XXX Maybe we should just fail? |
|
132 |
plainP, err := NewPrinter("plain", nil) |
|
133 |
if err != nil { |
|
134 |
return errors.Wrap(err, "cannot create plaintext printer") |
|
135 |
} |
|
136 |
return plainP.PrintObj(obj, w, "") |
|
137 |
} |