author | convert-repo |
Fri, 05 May 2017 21:55:09 +0000 | |
changeset 69 | 7217174c217a |
parent 51 | 300ac09051a7 |
child 83 | 57afac822019 |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright © 2017 Mikael Berthe <mikael@lilotux.net> |
2 |
// |
|
3 |
// Licensed under the MIT license. |
|
4 |
// Please see the LICENSE file is this directory. |
|
5 |
||
6 |
package printer |
|
7 |
||
8 |
import ( |
|
9 |
"encoding/json" |
|
10 |
"fmt" |
|
11 |
"io" |
|
12 |
"os" |
|
13 |
"reflect" |
|
14 |
"text/template" |
|
15 |
||
40
ad148f60dc87
TemplatePrinter: Handle InstanceStatus lists
Mikael Berthe <mikael@lilotux.net>
parents:
14
diff
changeset
|
16 |
"github.com/m0t0k1ch1/gomif" |
50
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
17 |
"github.com/mattn/go-isatty" |
40
ad148f60dc87
TemplatePrinter: Handle InstanceStatus lists
Mikael Berthe <mikael@lilotux.net>
parents:
14
diff
changeset
|
18 |
|
0 | 19 |
"github.com/McKael/madon" |
49
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
41
diff
changeset
|
20 |
"github.com/McKael/madonctl/printer/colors" |
0 | 21 |
) |
22 |
||
51
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
50
diff
changeset
|
23 |
// ColorMode defines the color behaviour: 0=auto, 1=forced, 2=disabled |
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
50
diff
changeset
|
24 |
var ColorMode int |
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
50
diff
changeset
|
25 |
|
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
50
diff
changeset
|
26 |
// disableColors can be set to true to disable the color template function |
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
50
diff
changeset
|
27 |
var disableColors bool |
50
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
28 |
|
0 | 29 |
// TemplatePrinter represents a Template printer |
30 |
type TemplatePrinter struct { |
|
31 |
rawTemplate string |
|
32 |
template *template.Template |
|
33 |
} |
|
34 |
||
35 |
// NewPrinterTemplate returns a Template ResourcePrinter |
|
36 |
// For TemplatePrinter, the option parameter contains the template string. |
|
37 |
func NewPrinterTemplate(option string) (*TemplatePrinter, error) { |
|
38 |
tmpl := option |
|
41
909c3ddd83f6
Add fromunix filter to templates (converts date from UNIX timestamp)
Mikael Berthe <mikael@lilotux.net>
parents:
40
diff
changeset
|
39 |
t, err := template.New("output").Funcs(template.FuncMap{ |
909c3ddd83f6
Add fromunix filter to templates (converts date from UNIX timestamp)
Mikael Berthe <mikael@lilotux.net>
parents:
40
diff
changeset
|
40 |
"fromhtml": html2string, |
909c3ddd83f6
Add fromunix filter to templates (converts date from UNIX timestamp)
Mikael Berthe <mikael@lilotux.net>
parents:
40
diff
changeset
|
41 |
"fromunix": unix2string, |
50
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
42 |
"color": ansiColor, |
41
909c3ddd83f6
Add fromunix filter to templates (converts date from UNIX timestamp)
Mikael Berthe <mikael@lilotux.net>
parents:
40
diff
changeset
|
43 |
}).Parse(tmpl) |
0 | 44 |
if err != nil { |
45 |
return nil, err |
|
46 |
} |
|
50
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
47 |
|
51
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
50
diff
changeset
|
48 |
// Update disableColors. |
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
50
diff
changeset
|
49 |
// In auto-mode, check if stdout is a TTY. |
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
50
diff
changeset
|
50 |
if ColorMode == 2 || (ColorMode == 0 && !isatty.IsTerminal(os.Stdout.Fd())) { |
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
50
diff
changeset
|
51 |
disableColors = true |
50
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
52 |
} |
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
53 |
|
0 | 54 |
return &TemplatePrinter{ |
55 |
rawTemplate: tmpl, |
|
56 |
template: t, |
|
57 |
}, nil |
|
58 |
} |
|
59 |
||
60 |
// PrintObj sends the object as text to the writer |
|
61 |
// If the writer w is nil, standard output will be used. |
|
62 |
func (p *TemplatePrinter) PrintObj(obj interface{}, w io.Writer, tmpl string) error { |
|
63 |
if w == nil { |
|
64 |
w = os.Stdout |
|
65 |
} |
|
66 |
||
67 |
if p.template == nil { |
|
68 |
return fmt.Errorf("template not built") |
|
69 |
} |
|
70 |
||
71 |
switch ot := obj.(type) { // I wish I knew a better way... |
|
72 |
case []madon.Account, []madon.Application, []madon.Attachment, []madon.Card, |
|
73 |
[]madon.Client, []madon.Context, []madon.Instance, []madon.Mention, |
|
74 |
[]madon.Notification, []madon.Relationship, []madon.Report, |
|
40
ad148f60dc87
TemplatePrinter: Handle InstanceStatus lists
Mikael Berthe <mikael@lilotux.net>
parents:
14
diff
changeset
|
75 |
[]madon.Results, []madon.Status, []madon.StreamEvent, []madon.Tag, |
ad148f60dc87
TemplatePrinter: Handle InstanceStatus lists
Mikael Berthe <mikael@lilotux.net>
parents:
14
diff
changeset
|
76 |
[]*gomif.InstanceStatus: |
0 | 77 |
return p.templateForeach(ot, w) |
78 |
} |
|
79 |
||
80 |
return p.templatePrintSingleObj(obj, w) |
|
81 |
} |
|
82 |
||
83 |
func (p *TemplatePrinter) templatePrintSingleObj(obj interface{}, w io.Writer) error { |
|
84 |
// This code comes from Kubernetes. |
|
85 |
data, err := json.Marshal(obj) |
|
86 |
if err != nil { |
|
87 |
return err |
|
88 |
} |
|
89 |
out := map[string]interface{}{} |
|
90 |
if err := json.Unmarshal(data, &out); err != nil { |
|
91 |
return err |
|
92 |
} |
|
93 |
if err = p.safeExecute(w, out); err != nil { |
|
94 |
return fmt.Errorf("error executing template %q: %v", p.rawTemplate, err) |
|
95 |
} |
|
96 |
return nil |
|
97 |
} |
|
98 |
||
99 |
// safeExecute tries to execute the template, but catches panics and returns an error |
|
100 |
// should the template engine panic. |
|
101 |
// This code comes from Kubernetes. |
|
102 |
func (p *TemplatePrinter) safeExecute(w io.Writer, obj interface{}) error { |
|
103 |
var panicErr error |
|
104 |
// Sorry for the double anonymous function. There's probably a clever way |
|
105 |
// to do this that has the defer'd func setting the value to be returned, but |
|
106 |
// that would be even less obvious. |
|
107 |
retErr := func() error { |
|
108 |
defer func() { |
|
109 |
if x := recover(); x != nil { |
|
110 |
panicErr = fmt.Errorf("caught panic: %+v", x) |
|
111 |
} |
|
112 |
}() |
|
113 |
return p.template.Execute(w, obj) |
|
114 |
}() |
|
115 |
if panicErr != nil { |
|
116 |
return panicErr |
|
117 |
} |
|
118 |
return retErr |
|
119 |
} |
|
120 |
||
121 |
func (p *TemplatePrinter) templateForeach(ol interface{}, w io.Writer) error { |
|
122 |
switch reflect.TypeOf(ol).Kind() { |
|
123 |
case reflect.Slice: |
|
124 |
s := reflect.ValueOf(ol) |
|
125 |
||
126 |
for i := 0; i < s.Len(); i++ { |
|
127 |
o := s.Index(i).Interface() |
|
128 |
if err := p.templatePrintSingleObj(o, w); err != nil { |
|
129 |
return err |
|
130 |
} |
|
131 |
} |
|
132 |
} |
|
133 |
return nil |
|
134 |
} |
|
50
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
135 |
|
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
136 |
func ansiColor(desc string) (string, error) { |
51
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
50
diff
changeset
|
137 |
if disableColors { |
50
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
138 |
return "", nil |
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
139 |
} |
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
140 |
return colors.ANSICodeString(desc) |
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
141 |
} |