author | Mikael Berthe <mikael@lilotux.net> |
Tue, 23 Aug 2022 22:33:28 +0200 | |
changeset 259 | db4911b0c721 |
parent 241 | e77dad242f4c |
child 265 | 05c40b36d3b2 |
child 267 | 5b91a65ba95a |
permissions | -rw-r--r-- |
185 | 1 |
// Copyright © 2017-2018 Mikael Berthe <mikael@lilotux.net> |
0 | 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 |
"os" |
|
12 |
"reflect" |
|
13 |
"time" |
|
14 |
||
239
605a00e9d1ab
Switch to Go modules (and bump Go version requirement)
Mikael Berthe <mikael@lilotux.net>
parents:
234
diff
changeset
|
15 |
"github.com/McKael/madon/v2" |
126
7d712d2bde73
Much improved html2text (fromhtml)
Mikael Berthe <mikael@lilotux.net>
parents:
121
diff
changeset
|
16 |
"github.com/McKael/madonctl/printer/html2text" |
0 | 17 |
) |
18 |
||
19 |
// PlainPrinter is the default "plain text" printer |
|
20 |
type PlainPrinter struct { |
|
21 |
Indent string |
|
22 |
NoSubtitles bool |
|
23 |
} |
|
24 |
||
25 |
// NewPrinterPlain returns a plaintext ResourcePrinter |
|
26 |
// For PlainPrinter, the option parameter contains the indent prefix. |
|
83
57afac822019
Use a map for ResourcePrinter options
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
27 |
func NewPrinterPlain(options Options) (*PlainPrinter, error) { |
0 | 28 |
indentInc := " " |
83
57afac822019
Use a map for ResourcePrinter options
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
29 |
if i, ok := options["indent"]; ok { |
57afac822019
Use a map for ResourcePrinter options
Mikael Berthe <mikael@lilotux.net>
parents:
77
diff
changeset
|
30 |
indentInc = i |
0 | 31 |
} |
32 |
return &PlainPrinter{Indent: indentInc}, nil |
|
33 |
} |
|
34 |
||
35 |
// PrintObj sends the object as text to the writer |
|
36 |
// If the writer w is nil, standard output will be used. |
|
37 |
// For PlainPrinter, the option parameter contains the initial indent. |
|
38 |
func (p *PlainPrinter) PrintObj(obj interface{}, w io.Writer, initialIndent string) error { |
|
39 |
if w == nil { |
|
40 |
w = os.Stdout |
|
41 |
} |
|
42 |
switch o := obj.(type) { |
|
43 |
case []madon.Account, []madon.Attachment, []madon.Card, []madon.Context, |
|
192 | 44 |
[]madon.Emoji, []madon.Instance, []madon.InstancePeer, |
45 |
[]madon.List, []madon.Mention, []madon.Notification, |
|
0 | 46 |
[]madon.Relationship, []madon.Report, []madon.Results, |
192 | 47 |
[]madon.Status, []madon.StreamEvent, []madon.Tag, |
210
94be15243195
PlainPrinter: Add WeekActivity format
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
48 |
[]madon.WeekActivity, []madon.DomainName: |
0 | 49 |
return p.plainForeach(o, w, initialIndent) |
152
4397b8c740e5
PlainPrinter: support for madon.DomainName
Mikael Berthe <mikael@lilotux.net>
parents:
150
diff
changeset
|
50 |
case *madon.DomainName: |
4397b8c740e5
PlainPrinter: support for madon.DomainName
Mikael Berthe <mikael@lilotux.net>
parents:
150
diff
changeset
|
51 |
return p.plainPrintDomainName(o, w, initialIndent) |
4397b8c740e5
PlainPrinter: support for madon.DomainName
Mikael Berthe <mikael@lilotux.net>
parents:
150
diff
changeset
|
52 |
case madon.DomainName: |
4397b8c740e5
PlainPrinter: support for madon.DomainName
Mikael Berthe <mikael@lilotux.net>
parents:
150
diff
changeset
|
53 |
return p.plainPrintDomainName(&o, w, initialIndent) |
0 | 54 |
case *madon.Account: |
55 |
return p.plainPrintAccount(o, w, initialIndent) |
|
56 |
case madon.Account: |
|
57 |
return p.plainPrintAccount(&o, w, initialIndent) |
|
58 |
case *madon.Attachment: |
|
59 |
return p.plainPrintAttachment(o, w, initialIndent) |
|
60 |
case madon.Attachment: |
|
61 |
return p.plainPrintAttachment(&o, w, initialIndent) |
|
62 |
case *madon.Card: |
|
63 |
return p.plainPrintCard(o, w, initialIndent) |
|
64 |
case madon.Card: |
|
65 |
return p.plainPrintCard(&o, w, initialIndent) |
|
66 |
case *madon.Context: |
|
67 |
return p.plainPrintContext(o, w, initialIndent) |
|
68 |
case madon.Context: |
|
69 |
return p.plainPrintContext(&o, w, initialIndent) |
|
192 | 70 |
case *madon.Emoji: |
71 |
return p.plainPrintEmoji(o, w, initialIndent) |
|
72 |
case madon.Emoji: |
|
73 |
return p.plainPrintEmoji(&o, w, initialIndent) |
|
0 | 74 |
case *madon.Instance: |
75 |
return p.plainPrintInstance(o, w, initialIndent) |
|
76 |
case madon.Instance: |
|
77 |
return p.plainPrintInstance(&o, w, initialIndent) |
|
192 | 78 |
case *madon.InstancePeer: |
79 |
return p.plainPrintInstancePeer(o, w, initialIndent) |
|
80 |
case madon.InstancePeer: |
|
81 |
return p.plainPrintInstancePeer(&o, w, initialIndent) |
|
82 |
case *madon.List: |
|
83 |
return p.plainPrintList(o, w, initialIndent) |
|
84 |
case madon.List: |
|
85 |
return p.plainPrintList(&o, w, initialIndent) |
|
0 | 86 |
case *madon.Notification: |
87 |
return p.plainPrintNotification(o, w, initialIndent) |
|
88 |
case madon.Notification: |
|
89 |
return p.plainPrintNotification(&o, w, initialIndent) |
|
90 |
case *madon.Relationship: |
|
91 |
return p.plainPrintRelationship(o, w, initialIndent) |
|
92 |
case madon.Relationship: |
|
93 |
return p.plainPrintRelationship(&o, w, initialIndent) |
|
94 |
case *madon.Report: |
|
95 |
return p.plainPrintReport(o, w, initialIndent) |
|
96 |
case madon.Report: |
|
97 |
return p.plainPrintReport(&o, w, initialIndent) |
|
98 |
case *madon.Results: |
|
99 |
return p.plainPrintResults(o, w, initialIndent) |
|
100 |
case madon.Results: |
|
101 |
return p.plainPrintResults(&o, w, initialIndent) |
|
102 |
case *madon.Status: |
|
103 |
return p.plainPrintStatus(o, w, initialIndent) |
|
104 |
case madon.Status: |
|
105 |
return p.plainPrintStatus(&o, w, initialIndent) |
|
106 |
case *madon.UserToken: |
|
107 |
return p.plainPrintUserToken(o, w, initialIndent) |
|
108 |
case madon.UserToken: |
|
109 |
return p.plainPrintUserToken(&o, w, initialIndent) |
|
210
94be15243195
PlainPrinter: Add WeekActivity format
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
110 |
case *madon.WeekActivity: |
94be15243195
PlainPrinter: Add WeekActivity format
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
111 |
return p.plainPrintWeekActivity(o, w, initialIndent) |
94be15243195
PlainPrinter: Add WeekActivity format
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
112 |
case madon.WeekActivity: |
94be15243195
PlainPrinter: Add WeekActivity format
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
113 |
return p.plainPrintWeekActivity(&o, w, initialIndent) |
0 | 114 |
} |
115 |
// TODO: Mention |
|
116 |
// TODO: StreamEvent |
|
117 |
// TODO: Tag |
|
118 |
||
119 |
return fmt.Errorf("PlainPrinter not yet implemented for %T (try json or yaml...)", obj) |
|
120 |
} |
|
121 |
||
122 |
func (p *PlainPrinter) plainForeach(ol interface{}, w io.Writer, ii string) error { |
|
123 |
switch reflect.TypeOf(ol).Kind() { |
|
124 |
case reflect.Slice: |
|
125 |
s := reflect.ValueOf(ol) |
|
126 |
||
127 |
for i := 0; i < s.Len(); i++ { |
|
128 |
o := s.Index(i).Interface() |
|
129 |
if err := p.PrintObj(o, w, ii); err != nil { |
|
130 |
return err |
|
131 |
} |
|
132 |
} |
|
133 |
} |
|
134 |
return nil |
|
135 |
} |
|
136 |
||
137 |
func html2string(h string) string { |
|
126
7d712d2bde73
Much improved html2text (fromhtml)
Mikael Berthe <mikael@lilotux.net>
parents:
121
diff
changeset
|
138 |
t, err := html2text.Textify(h) |
0 | 139 |
if err == nil { |
140 |
return t |
|
141 |
} |
|
142 |
return h // Failed: return initial string |
|
143 |
} |
|
144 |
||
120
54b6f2a4098b
Add template command tolocal; update templates and documentation
Mikael Berthe <mikael@lilotux.net>
parents:
84
diff
changeset
|
145 |
// unix2time convert a UNIX timestamp to a time.Time |
54b6f2a4098b
Add template command tolocal; update templates and documentation
Mikael Berthe <mikael@lilotux.net>
parents:
84
diff
changeset
|
146 |
func unix2time(ts interface{}) (time.Time, error) { |
41
909c3ddd83f6
Add fromunix filter to templates (converts date from UNIX timestamp)
Mikael Berthe <mikael@lilotux.net>
parents:
38
diff
changeset
|
147 |
switch t := ts.(type) { |
909c3ddd83f6
Add fromunix filter to templates (converts date from UNIX timestamp)
Mikael Berthe <mikael@lilotux.net>
parents:
38
diff
changeset
|
148 |
case int64: |
120
54b6f2a4098b
Add template command tolocal; update templates and documentation
Mikael Berthe <mikael@lilotux.net>
parents:
84
diff
changeset
|
149 |
return time.Unix(t, 0), nil |
41
909c3ddd83f6
Add fromunix filter to templates (converts date from UNIX timestamp)
Mikael Berthe <mikael@lilotux.net>
parents:
38
diff
changeset
|
150 |
case int: |
120
54b6f2a4098b
Add template command tolocal; update templates and documentation
Mikael Berthe <mikael@lilotux.net>
parents:
84
diff
changeset
|
151 |
return time.Unix(int64(t), 0), nil |
41
909c3ddd83f6
Add fromunix filter to templates (converts date from UNIX timestamp)
Mikael Berthe <mikael@lilotux.net>
parents:
38
diff
changeset
|
152 |
case float64: |
120
54b6f2a4098b
Add template command tolocal; update templates and documentation
Mikael Berthe <mikael@lilotux.net>
parents:
84
diff
changeset
|
153 |
return time.Unix(int64(t), 0), nil |
41
909c3ddd83f6
Add fromunix filter to templates (converts date from UNIX timestamp)
Mikael Berthe <mikael@lilotux.net>
parents:
38
diff
changeset
|
154 |
} |
120
54b6f2a4098b
Add template command tolocal; update templates and documentation
Mikael Berthe <mikael@lilotux.net>
parents:
84
diff
changeset
|
155 |
return time.Time{}, fmt.Errorf("invalid timestamp type") |
41
909c3ddd83f6
Add fromunix filter to templates (converts date from UNIX timestamp)
Mikael Berthe <mikael@lilotux.net>
parents:
38
diff
changeset
|
156 |
} |
909c3ddd83f6
Add fromunix filter to templates (converts date from UNIX timestamp)
Mikael Berthe <mikael@lilotux.net>
parents:
38
diff
changeset
|
157 |
|
0 | 158 |
func indentedPrint(w io.Writer, indent string, title, skipIfEmpty bool, label string, format string, args ...interface{}) { |
159 |
prefix := indent |
|
160 |
if title { |
|
161 |
prefix += "- " |
|
162 |
} else { |
|
163 |
prefix += " " |
|
164 |
} |
|
165 |
value := fmt.Sprintf(format, args...) |
|
166 |
if !title && skipIfEmpty && len(value) == 0 { |
|
167 |
return |
|
168 |
} |
|
169 |
fmt.Fprintf(w, "%s%s: %s\n", prefix, label, value) |
|
170 |
} |
|
171 |
||
152
4397b8c740e5
PlainPrinter: support for madon.DomainName
Mikael Berthe <mikael@lilotux.net>
parents:
150
diff
changeset
|
172 |
func (p *PlainPrinter) plainPrintDomainName(d *madon.DomainName, w io.Writer, indent string) error { |
4397b8c740e5
PlainPrinter: support for madon.DomainName
Mikael Berthe <mikael@lilotux.net>
parents:
150
diff
changeset
|
173 |
indentedPrint(w, indent, true, false, "Domain Name", "%s", string(*d)) |
4397b8c740e5
PlainPrinter: support for madon.DomainName
Mikael Berthe <mikael@lilotux.net>
parents:
150
diff
changeset
|
174 |
return nil |
4397b8c740e5
PlainPrinter: support for madon.DomainName
Mikael Berthe <mikael@lilotux.net>
parents:
150
diff
changeset
|
175 |
} |
4397b8c740e5
PlainPrinter: support for madon.DomainName
Mikael Berthe <mikael@lilotux.net>
parents:
150
diff
changeset
|
176 |
|
0 | 177 |
func (p *PlainPrinter) plainPrintAccount(a *madon.Account, w io.Writer, indent string) error { |
178 |
indentedPrint(w, indent, true, false, "Account ID", "%d (%s)", a.ID, a.Username) |
|
179 |
indentedPrint(w, indent, false, false, "User ID", "%s", a.Acct) |
|
180 |
indentedPrint(w, indent, false, false, "Display name", "%s", a.DisplayName) |
|
181 |
indentedPrint(w, indent, false, false, "Creation date", "%v", a.CreatedAt.Local()) |
|
182 |
indentedPrint(w, indent, false, false, "URL", "%s", a.URL) |
|
183 |
indentedPrint(w, indent, false, false, "Statuses count", "%d", a.StatusesCount) |
|
184 |
indentedPrint(w, indent, false, false, "Followers count", "%d", a.FollowersCount) |
|
185 |
indentedPrint(w, indent, false, false, "Following count", "%d", a.FollowingCount) |
|
186 |
if a.Locked { |
|
187 |
indentedPrint(w, indent, false, false, "Locked", "%v", a.Locked) |
|
188 |
} |
|
234
f5cd55622194
Display new Account fields in plaintext and templates
Mikael Berthe <mikael@lilotux.net>
parents:
229
diff
changeset
|
189 |
if a.Bot { |
f5cd55622194
Display new Account fields in plaintext and templates
Mikael Berthe <mikael@lilotux.net>
parents:
229
diff
changeset
|
190 |
indentedPrint(w, indent, false, false, "Bot", "%v", a.Bot) |
f5cd55622194
Display new Account fields in plaintext and templates
Mikael Berthe <mikael@lilotux.net>
parents:
229
diff
changeset
|
191 |
} |
0 | 192 |
indentedPrint(w, indent, false, true, "User note", "%s", html2string(a.Note)) // XXX too long? |
207
56afbe03a7d4
Printers: Display moved accounts (PlainPrinter + templates)
Mikael Berthe <mikael@lilotux.net>
parents:
206
diff
changeset
|
193 |
if a.Moved != nil { |
56afbe03a7d4
Printers: Display moved accounts (PlainPrinter + templates)
Mikael Berthe <mikael@lilotux.net>
parents:
206
diff
changeset
|
194 |
m := a.Moved |
56afbe03a7d4
Printers: Display moved accounts (PlainPrinter + templates)
Mikael Berthe <mikael@lilotux.net>
parents:
206
diff
changeset
|
195 |
indentedPrint(w, indent+p.Indent, true, false, "Moved to account ID", "%d (%s)", m.ID, m.Username) |
56afbe03a7d4
Printers: Display moved accounts (PlainPrinter + templates)
Mikael Berthe <mikael@lilotux.net>
parents:
206
diff
changeset
|
196 |
indentedPrint(w, indent+p.Indent, false, false, "New user ID", "%s", m.Acct) |
56afbe03a7d4
Printers: Display moved accounts (PlainPrinter + templates)
Mikael Berthe <mikael@lilotux.net>
parents:
206
diff
changeset
|
197 |
indentedPrint(w, indent+p.Indent, false, false, "New display name", "%s", m.DisplayName) |
56afbe03a7d4
Printers: Display moved accounts (PlainPrinter + templates)
Mikael Berthe <mikael@lilotux.net>
parents:
206
diff
changeset
|
198 |
} |
234
f5cd55622194
Display new Account fields in plaintext and templates
Mikael Berthe <mikael@lilotux.net>
parents:
229
diff
changeset
|
199 |
if a.Source != nil { |
f5cd55622194
Display new Account fields in plaintext and templates
Mikael Berthe <mikael@lilotux.net>
parents:
229
diff
changeset
|
200 |
s := a.Source |
f5cd55622194
Display new Account fields in plaintext and templates
Mikael Berthe <mikael@lilotux.net>
parents:
229
diff
changeset
|
201 |
if s.Privacy != nil { |
f5cd55622194
Display new Account fields in plaintext and templates
Mikael Berthe <mikael@lilotux.net>
parents:
229
diff
changeset
|
202 |
indentedPrint(w, indent, false, true, "Default Privacy", "%s", *s.Privacy) |
f5cd55622194
Display new Account fields in plaintext and templates
Mikael Berthe <mikael@lilotux.net>
parents:
229
diff
changeset
|
203 |
} |
f5cd55622194
Display new Account fields in plaintext and templates
Mikael Berthe <mikael@lilotux.net>
parents:
229
diff
changeset
|
204 |
if s.Language != nil { |
f5cd55622194
Display new Account fields in plaintext and templates
Mikael Berthe <mikael@lilotux.net>
parents:
229
diff
changeset
|
205 |
indentedPrint(w, indent, false, true, "Default Language", "%s", *s.Language) |
f5cd55622194
Display new Account fields in plaintext and templates
Mikael Berthe <mikael@lilotux.net>
parents:
229
diff
changeset
|
206 |
} |
f5cd55622194
Display new Account fields in plaintext and templates
Mikael Berthe <mikael@lilotux.net>
parents:
229
diff
changeset
|
207 |
if s.Sensitive != nil { |
f5cd55622194
Display new Account fields in plaintext and templates
Mikael Berthe <mikael@lilotux.net>
parents:
229
diff
changeset
|
208 |
indentedPrint(w, indent, false, true, "Sensitive by default", "%v", *s.Sensitive) |
f5cd55622194
Display new Account fields in plaintext and templates
Mikael Berthe <mikael@lilotux.net>
parents:
229
diff
changeset
|
209 |
} |
f5cd55622194
Display new Account fields in plaintext and templates
Mikael Berthe <mikael@lilotux.net>
parents:
229
diff
changeset
|
210 |
} |
f5cd55622194
Display new Account fields in plaintext and templates
Mikael Berthe <mikael@lilotux.net>
parents:
229
diff
changeset
|
211 |
if a.Fields != nil { |
f5cd55622194
Display new Account fields in plaintext and templates
Mikael Berthe <mikael@lilotux.net>
parents:
229
diff
changeset
|
212 |
for _, f := range *a.Fields { |
f5cd55622194
Display new Account fields in plaintext and templates
Mikael Berthe <mikael@lilotux.net>
parents:
229
diff
changeset
|
213 |
indentedPrint(w, indent, false, false, ". Metadata field", |
f5cd55622194
Display new Account fields in plaintext and templates
Mikael Berthe <mikael@lilotux.net>
parents:
229
diff
changeset
|
214 |
"[%s] » %s", f.Name, html2string(f.Value)) |
f5cd55622194
Display new Account fields in plaintext and templates
Mikael Berthe <mikael@lilotux.net>
parents:
229
diff
changeset
|
215 |
} |
f5cd55622194
Display new Account fields in plaintext and templates
Mikael Berthe <mikael@lilotux.net>
parents:
229
diff
changeset
|
216 |
} |
0 | 217 |
return nil |
218 |
} |
|
219 |
||
220 |
func (p *PlainPrinter) plainPrintAttachment(a *madon.Attachment, w io.Writer, indent string) error { |
|
221 |
indentedPrint(w, indent, true, false, "Attachment ID", "%d", a.ID) |
|
222 |
indentedPrint(w, indent, false, false, "Type", "%s", a.Type) |
|
223 |
indentedPrint(w, indent, false, false, "Local URL", "%s", a.URL) |
|
203
71455784278b
PlainPrinter: Fix attachment output
Mikael Berthe <mikael@lilotux.net>
parents:
194
diff
changeset
|
224 |
if a.RemoteURL != nil { |
71455784278b
PlainPrinter: Fix attachment output
Mikael Berthe <mikael@lilotux.net>
parents:
194
diff
changeset
|
225 |
indentedPrint(w, indent, false, true, "Remote URL", "%s", *a.RemoteURL) |
71455784278b
PlainPrinter: Fix attachment output
Mikael Berthe <mikael@lilotux.net>
parents:
194
diff
changeset
|
226 |
} |
0 | 227 |
indentedPrint(w, indent, false, true, "Preview URL", "%s", a.PreviewURL) |
203
71455784278b
PlainPrinter: Fix attachment output
Mikael Berthe <mikael@lilotux.net>
parents:
194
diff
changeset
|
228 |
if a.TextURL != nil { |
71455784278b
PlainPrinter: Fix attachment output
Mikael Berthe <mikael@lilotux.net>
parents:
194
diff
changeset
|
229 |
indentedPrint(w, indent, false, true, "Text URL", "%s", *a.TextURL) |
71455784278b
PlainPrinter: Fix attachment output
Mikael Berthe <mikael@lilotux.net>
parents:
194
diff
changeset
|
230 |
} |
71455784278b
PlainPrinter: Fix attachment output
Mikael Berthe <mikael@lilotux.net>
parents:
194
diff
changeset
|
231 |
if a.Description != nil { |
71455784278b
PlainPrinter: Fix attachment output
Mikael Berthe <mikael@lilotux.net>
parents:
194
diff
changeset
|
232 |
indentedPrint(w, indent, false, true, "Description", "%s", *a.Description) |
71455784278b
PlainPrinter: Fix attachment output
Mikael Berthe <mikael@lilotux.net>
parents:
194
diff
changeset
|
233 |
} |
0 | 234 |
return nil |
235 |
} |
|
236 |
||
237 |
func (p *PlainPrinter) plainPrintCard(c *madon.Card, w io.Writer, indent string) error { |
|
238 |
indentedPrint(w, indent, true, false, "Card title", "%s", c.Title) |
|
239 |
indentedPrint(w, indent, false, true, "Description", "%s", c.Description) |
|
240 |
indentedPrint(w, indent, false, true, "URL", "%s", c.URL) |
|
241 |
indentedPrint(w, indent, false, true, "Image", "%s", c.Image) |
|
242 |
return nil |
|
243 |
} |
|
244 |
||
245 |
func (p *PlainPrinter) plainPrintContext(c *madon.Context, w io.Writer, indent string) error { |
|
67
c72b15bec8d9
Fix status context (sync with Madon 1.4.3)
Mikael Berthe <mikael@lilotux.net>
parents:
58
diff
changeset
|
246 |
indentedPrint(w, indent, true, false, "Context", "%d relative(s)", len(c.Ancestors)+len(c.Descendants)) |
0 | 247 |
if len(c.Ancestors) > 0 { |
248 |
indentedPrint(w, indent, false, false, "Ancestors", "") |
|
249 |
p.PrintObj(c.Ancestors, w, indent+p.Indent) |
|
250 |
} |
|
67
c72b15bec8d9
Fix status context (sync with Madon 1.4.3)
Mikael Berthe <mikael@lilotux.net>
parents:
58
diff
changeset
|
251 |
if len(c.Descendants) > 0 { |
c72b15bec8d9
Fix status context (sync with Madon 1.4.3)
Mikael Berthe <mikael@lilotux.net>
parents:
58
diff
changeset
|
252 |
indentedPrint(w, indent, false, false, "Descendants", "") |
c72b15bec8d9
Fix status context (sync with Madon 1.4.3)
Mikael Berthe <mikael@lilotux.net>
parents:
58
diff
changeset
|
253 |
p.PrintObj(c.Descendants, w, indent+p.Indent) |
0 | 254 |
} |
255 |
return nil |
|
256 |
} |
|
257 |
||
192 | 258 |
func (p *PlainPrinter) plainPrintEmoji(e *madon.Emoji, w io.Writer, indent string) error { |
259 |
indentedPrint(w, indent, true, false, "Emoji shortcode", "%s", e.ShortCode) |
|
260 |
indentedPrint(w, indent, false, false, "URL", "%s", e.URL) |
|
261 |
return nil |
|
262 |
} |
|
263 |
||
0 | 264 |
func (p *PlainPrinter) plainPrintInstance(i *madon.Instance, w io.Writer, indent string) error { |
265 |
indentedPrint(w, indent, true, false, "Instance title", "%s", i.Title) |
|
266 |
indentedPrint(w, indent, false, true, "Description", "%s", html2string(i.Description)) |
|
267 |
indentedPrint(w, indent, false, true, "URL", "%s", i.URI) |
|
268 |
indentedPrint(w, indent, false, true, "Email", "%s", i.Email) |
|
18
7a4b57b3e66a
Display instance version, if field is set
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
269 |
indentedPrint(w, indent, false, true, "Version", "%s", i.Version) |
192 | 270 |
if i.ContactAccount != nil { |
271 |
c := i.ContactAccount |
|
272 |
indentedPrint(w, indent+p.Indent, true, false, "Contact account ID", "%d (%s)", c.ID, c.Username) |
|
273 |
indentedPrint(w, indent+p.Indent, false, false, "Contact user ID", "%s", c.Acct) |
|
274 |
indentedPrint(w, indent+p.Indent, false, false, "Contact display name", "%s", c.DisplayName) |
|
275 |
} |
|
276 |
return nil |
|
277 |
} |
|
278 |
||
279 |
func (p *PlainPrinter) plainPrintInstancePeer(i *madon.InstancePeer, w io.Writer, indent string) error { |
|
280 |
indentedPrint(w, indent, true, false, "Peer", "%s", *i) |
|
281 |
return nil |
|
282 |
} |
|
283 |
||
284 |
func (p *PlainPrinter) plainPrintList(l *madon.List, w io.Writer, indent string) error { |
|
285 |
indentedPrint(w, indent, true, false, "List ID", "%d", l.ID) |
|
286 |
indentedPrint(w, indent, false, false, "Title", "%s", l.Title) |
|
0 | 287 |
return nil |
288 |
} |
|
289 |
||
290 |
func (p *PlainPrinter) plainPrintNotification(n *madon.Notification, w io.Writer, indent string) error { |
|
291 |
indentedPrint(w, indent, true, false, "Notification ID", "%d", n.ID) |
|
292 |
indentedPrint(w, indent, false, false, "Type", "%s", n.Type) |
|
293 |
indentedPrint(w, indent, false, false, "Timestamp", "%v", n.CreatedAt.Local()) |
|
294 |
if n.Account != nil { |
|
295 |
indentedPrint(w, indent+p.Indent, true, false, "Account", "(%d) @%s - %s", |
|
296 |
n.Account.ID, n.Account.Acct, n.Account.DisplayName) |
|
297 |
} |
|
298 |
if n.Status != nil { |
|
299 |
p.plainPrintStatus(n.Status, w, indent+p.Indent) |
|
300 |
} |
|
301 |
return nil |
|
302 |
} |
|
303 |
||
304 |
func (p *PlainPrinter) plainPrintRelationship(r *madon.Relationship, w io.Writer, indent string) error { |
|
145
0f6b8411ad36
Sync with Madon library 1.6: Some calls return a Relationship entity
Mikael Berthe <mikael@lilotux.net>
parents:
126
diff
changeset
|
305 |
indentedPrint(w, indent, true, false, "Account ID", "%d", r.ID) |
0 | 306 |
indentedPrint(w, indent, false, false, "Following", "%v", r.Following) |
206
102b34bb2c28
Printers: Add Relationship.showing_reblogs
Mikael Berthe <mikael@lilotux.net>
parents:
203
diff
changeset
|
307 |
//indentedPrint(w, indent, false, false, "Showing reblogs", "%v", r.ShowingReblogs) |
0 | 308 |
indentedPrint(w, indent, false, false, "Followed-by", "%v", r.FollowedBy) |
309 |
indentedPrint(w, indent, false, false, "Blocking", "%v", r.Blocking) |
|
310 |
indentedPrint(w, indent, false, false, "Muting", "%v", r.Muting) |
|
192 | 311 |
indentedPrint(w, indent, false, false, "Muting notifications", "%v", r.MutingNotifications) |
228
360929ee6aef
Display endorsement in relationship entities
Mikael Berthe <mikael@lilotux.net>
parents:
210
diff
changeset
|
312 |
indentedPrint(w, indent, false, false, "Endorsed", "%v", r.Endorsed) |
0 | 313 |
indentedPrint(w, indent, false, false, "Requested", "%v", r.Requested) |
314 |
return nil |
|
315 |
} |
|
316 |
||
317 |
func (p *PlainPrinter) plainPrintReport(r *madon.Report, w io.Writer, indent string) error { |
|
318 |
indentedPrint(w, indent, true, false, "Report ID", "%d", r.ID) |
|
319 |
indentedPrint(w, indent, false, false, "Action taken", "%s", r.ActionTaken) |
|
320 |
return nil |
|
321 |
} |
|
322 |
||
323 |
func (p *PlainPrinter) plainPrintResults(r *madon.Results, w io.Writer, indent string) error { |
|
324 |
indentedPrint(w, indent, true, false, "Results", "%d account(s), %d status(es), %d hashtag(s)", |
|
325 |
len(r.Accounts), len(r.Statuses), len(r.Hashtags)) |
|
326 |
if len(r.Accounts) > 0 { |
|
327 |
indentedPrint(w, indent, false, false, "Accounts", "") |
|
328 |
p.PrintObj(r.Accounts, w, indent+p.Indent) |
|
329 |
} |
|
330 |
if len(r.Statuses) > 0 { |
|
331 |
indentedPrint(w, indent, false, false, "Statuses", "") |
|
332 |
p.PrintObj(r.Statuses, w, indent+p.Indent) |
|
333 |
} |
|
334 |
if len(r.Hashtags) > 0 { |
|
335 |
indentedPrint(w, indent, false, false, "Hashtags", "") |
|
336 |
for _, tag := range r.Hashtags { |
|
229
d4c099a67c86
Sync 'Results' type with madon update
Mikael Berthe <mikael@lilotux.net>
parents:
228
diff
changeset
|
337 |
indentedPrint(w, indent+p.Indent, true, false, "Tag", "%s", tag.Name) |
d4c099a67c86
Sync 'Results' type with madon update
Mikael Berthe <mikael@lilotux.net>
parents:
228
diff
changeset
|
338 |
indentedPrint(w, indent+p.Indent, false, true, "URL", "%s", tag.URL) |
0 | 339 |
} |
340 |
} |
|
341 |
return nil |
|
342 |
} |
|
343 |
||
344 |
func (p *PlainPrinter) plainPrintStatus(s *madon.Status, w io.Writer, indent string) error { |
|
345 |
indentedPrint(w, indent, true, false, "Status ID", "%d", s.ID) |
|
121
10453a1c5698
PlainPrinter: Display username of a status author
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
346 |
if s.Account != nil { |
10453a1c5698
PlainPrinter: Display username of a status author
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
347 |
author := s.Account.Acct |
10453a1c5698
PlainPrinter: Display username of a status author
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
348 |
if s.Account.DisplayName != "" { |
10453a1c5698
PlainPrinter: Display username of a status author
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
349 |
author += " (" + s.Account.DisplayName + ")" |
10453a1c5698
PlainPrinter: Display username of a status author
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
350 |
} |
10453a1c5698
PlainPrinter: Display username of a status author
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
351 |
indentedPrint(w, indent, false, false, "From", "%s", author) |
10453a1c5698
PlainPrinter: Display username of a status author
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
352 |
} |
192 | 353 |
if s.Pinned { |
354 |
indentedPrint(w, indent, false, false, "Pinned", "%v", s.Pinned) |
|
355 |
} |
|
162
3118ab97c8df
PlainPrinter: Display when a message is private
Mikael Berthe <mikael@lilotux.net>
parents:
152
diff
changeset
|
356 |
if s.Visibility == "private" { |
3118ab97c8df
PlainPrinter: Display when a message is private
Mikael Berthe <mikael@lilotux.net>
parents:
152
diff
changeset
|
357 |
indentedPrint(w, indent, false, false, "Private", "true") |
3118ab97c8df
PlainPrinter: Display when a message is private
Mikael Berthe <mikael@lilotux.net>
parents:
152
diff
changeset
|
358 |
} |
0 | 359 |
indentedPrint(w, indent, false, false, "Timestamp", "%v", s.CreatedAt.Local()) |
360 |
||
361 |
if s.Reblog != nil { |
|
121
10453a1c5698
PlainPrinter: Display username of a status author
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
362 |
if s.Reblog.Account != nil { |
10453a1c5698
PlainPrinter: Display username of a status author
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
363 |
indentedPrint(w, indent, false, false, "Reblogged from", "%s", s.Reblog.Account.Username) |
10453a1c5698
PlainPrinter: Display username of a status author
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
364 |
} |
0 | 365 |
return p.plainPrintStatus(s.Reblog, w, indent+p.Indent) |
366 |
} |
|
367 |
||
77
23b2b6c5643e
PlainPrinter: Display when 'sensitive' (aka NSFW) is set
Mikael Berthe <mikael@lilotux.net>
parents:
67
diff
changeset
|
368 |
if s.Sensitive { |
23b2b6c5643e
PlainPrinter: Display when 'sensitive' (aka NSFW) is set
Mikael Berthe <mikael@lilotux.net>
parents:
67
diff
changeset
|
369 |
indentedPrint(w, indent, false, false, "Sensitive (NSFW)", "%v", s.Sensitive) |
23b2b6c5643e
PlainPrinter: Display when 'sensitive' (aka NSFW) is set
Mikael Berthe <mikael@lilotux.net>
parents:
67
diff
changeset
|
370 |
} |
23b2b6c5643e
PlainPrinter: Display when 'sensitive' (aka NSFW) is set
Mikael Berthe <mikael@lilotux.net>
parents:
67
diff
changeset
|
371 |
|
0 | 372 |
indentedPrint(w, indent, false, false, "Contents", "%s", html2string(s.Content)) |
172
0d651f070319
Update types to reflect Mastodon 2.0.0 API changes
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
373 |
if s.InReplyToID != nil && *s.InReplyToID > 0 { |
0d651f070319
Update types to reflect Mastodon 2.0.0 API changes
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
374 |
indentedPrint(w, indent, false, false, "In-Reply-To", "%d", *s.InReplyToID) |
0 | 375 |
} |
376 |
if s.Reblogged { |
|
377 |
indentedPrint(w, indent, false, false, "Reblogged", "%v", s.Reblogged) |
|
378 |
} |
|
379 |
indentedPrint(w, indent, false, false, "URL", "%s", s.URL) |
|
58
5a0cb1e65e65
PlainPrinter: Display attachment URLs in statuses
Mikael Berthe <mikael@lilotux.net>
parents:
41
diff
changeset
|
380 |
// Display minimum details of attachments |
5a0cb1e65e65
PlainPrinter: Display attachment URLs in statuses
Mikael Berthe <mikael@lilotux.net>
parents:
41
diff
changeset
|
381 |
//return p.PrintObj(s.MediaAttachments, w, indent+p.Indent) |
5a0cb1e65e65
PlainPrinter: Display attachment URLs in statuses
Mikael Berthe <mikael@lilotux.net>
parents:
41
diff
changeset
|
382 |
for _, a := range s.MediaAttachments { |
5a0cb1e65e65
PlainPrinter: Display attachment URLs in statuses
Mikael Berthe <mikael@lilotux.net>
parents:
41
diff
changeset
|
383 |
indentedPrint(w, indent+p.Indent, true, false, "Attachment ID", "%d", a.ID) |
150
7f46bcaf240c
Sync with Madon library update
Mikael Berthe <mikael@lilotux.net>
parents:
148
diff
changeset
|
384 |
if a.TextURL != nil && *a.TextURL != "" { |
7f46bcaf240c
Sync with Madon library update
Mikael Berthe <mikael@lilotux.net>
parents:
148
diff
changeset
|
385 |
indentedPrint(w, indent+p.Indent, true, false, "Text URL", "%s", *a.TextURL) |
58
5a0cb1e65e65
PlainPrinter: Display attachment URLs in statuses
Mikael Berthe <mikael@lilotux.net>
parents:
41
diff
changeset
|
386 |
} else if a.URL != "" { |
5a0cb1e65e65
PlainPrinter: Display attachment URLs in statuses
Mikael Berthe <mikael@lilotux.net>
parents:
41
diff
changeset
|
387 |
indentedPrint(w, indent+p.Indent, false, false, "URL", "%s", a.URL) |
150
7f46bcaf240c
Sync with Madon library update
Mikael Berthe <mikael@lilotux.net>
parents:
148
diff
changeset
|
388 |
} else if a.RemoteURL != nil { |
7f46bcaf240c
Sync with Madon library update
Mikael Berthe <mikael@lilotux.net>
parents:
148
diff
changeset
|
389 |
indentedPrint(w, indent+p.Indent, false, false, "Remote URL", "%s", *a.RemoteURL) |
58
5a0cb1e65e65
PlainPrinter: Display attachment URLs in statuses
Mikael Berthe <mikael@lilotux.net>
parents:
41
diff
changeset
|
390 |
} |
203
71455784278b
PlainPrinter: Fix attachment output
Mikael Berthe <mikael@lilotux.net>
parents:
194
diff
changeset
|
391 |
if a.Description != nil && *a.Description != "" { |
241
e77dad242f4c
Fix format bug in plainprinter
Mikael Berthe <mikael@lilotux.net>
parents:
239
diff
changeset
|
392 |
indentedPrint(w, indent+p.Indent, false, true, "Description", "%s", *a.Description) |
203
71455784278b
PlainPrinter: Fix attachment output
Mikael Berthe <mikael@lilotux.net>
parents:
194
diff
changeset
|
393 |
} |
58
5a0cb1e65e65
PlainPrinter: Display attachment URLs in statuses
Mikael Berthe <mikael@lilotux.net>
parents:
41
diff
changeset
|
394 |
} |
0 | 395 |
return nil |
396 |
} |
|
397 |
||
398 |
func (p *PlainPrinter) plainPrintUserToken(s *madon.UserToken, w io.Writer, indent string) error { |
|
399 |
indentedPrint(w, indent, true, false, "User token", "%s", s.AccessToken) |
|
400 |
indentedPrint(w, indent, false, true, "Type", "%s", s.TokenType) |
|
401 |
if s.CreatedAt != 0 { |
|
120
54b6f2a4098b
Add template command tolocal; update templates and documentation
Mikael Berthe <mikael@lilotux.net>
parents:
84
diff
changeset
|
402 |
indentedPrint(w, indent, false, true, "Timestamp", "%v", time.Unix(s.CreatedAt, 0)) |
0 | 403 |
} |
404 |
indentedPrint(w, indent, false, true, "Scope", "%s", s.Scope) |
|
405 |
return nil |
|
406 |
} |
|
210
94be15243195
PlainPrinter: Add WeekActivity format
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
407 |
|
94be15243195
PlainPrinter: Add WeekActivity format
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
408 |
func (p *PlainPrinter) plainPrintWeekActivity(a *madon.WeekActivity, w io.Writer, indent string) error { |
94be15243195
PlainPrinter: Add WeekActivity format
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
409 |
indentedPrint(w, indent, true, false, "Activity week", "%v", a.Week.Format("2006-01-02")) |
94be15243195
PlainPrinter: Add WeekActivity format
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
410 |
indentedPrint(w, indent, false, true, "Weekly logins", "%d", a.Logins) |
94be15243195
PlainPrinter: Add WeekActivity format
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
411 |
indentedPrint(w, indent, false, true, "Weekly statuses", "%d", a.Statuses) |
94be15243195
PlainPrinter: Add WeekActivity format
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
412 |
indentedPrint(w, indent, false, true, "Weekly registrations", "%d", a.Registrations) |
94be15243195
PlainPrinter: Add WeekActivity format
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
413 |
return nil |
94be15243195
PlainPrinter: Add WeekActivity format
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
414 |
} |