forked from rubblelabs/ripple
/
terminal.go
303 lines (283 loc) · 8.1 KB
/
terminal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Utiltities for formatting Ripple data in a terminal
package terminal
import (
"fmt"
"reflect"
"github.com/atticlab/ripple/data"
"github.com/atticlab/ripple/websockets"
"github.com/fatih/color"
)
type Flag uint32
const (
Indent Flag = 1 << iota
DoubleIndent
TripleIndent
ShowLedgerSequence
ShowTransactionId
)
var Default Flag
var (
ledgerStyle = color.New(color.FgRed, color.Underline)
leStyle = color.New(color.FgWhite)
txStyle = color.New(color.FgGreen)
proposalStyle = color.New(color.FgYellow)
validationStyle = color.New(color.FgYellow, color.Bold)
tradeStyle = color.New(color.FgBlue)
balanceStyle = color.New(color.FgMagenta)
pathStyle = color.New(color.FgYellow)
offerStyle = color.New(color.FgYellow)
lineStyle = color.New(color.FgYellow)
infoStyle = color.New(color.FgRed)
)
func defaultUint32(v *uint32) uint32 {
if v != nil {
return *v
}
return 0
}
func BoolSymbol(v bool) string {
if v {
return "✓"
}
return "✗"
}
func MemoSymbol(tx data.Transaction) string {
return BoolSymbol(len(tx.GetBase().Memos) > 0)
}
func SignSymbol(s data.SignerAgent) string {
valid, err := data.CheckSignature(s)
return BoolSymbol(!valid || err != nil)
}
type bundle struct {
color *color.Color
format string
values []interface{}
flag Flag
}
func newLeBundle(v interface{}, flag Flag) (*bundle, error) {
var (
format = "%-11s "
values = []interface{}{v.(data.LedgerEntry).GetLedgerEntryType()}
)
switch le := v.(type) {
case *data.AccountRoot:
format += "%-34s %08X %s"
values = append(values, []interface{}{le.Account, *le.Flags, le.Balance}...)
case *data.LedgerHashes:
format += "%d hashes"
values = append(values, []interface{}{len(*le.Hashes)}...)
case *data.RippleState:
format += "%s %s %s"
values = append(values, []interface{}{le.Balance, le.HighLimit, le.LowLimit}...)
case *data.Offer:
format += "%-34s %-60s %-60s %-18s"
values = append(values, []interface{}{le.Account, le.TakerPays, le.TakerGets, le.Ratio()}...)
case *data.FeeSettings:
format += "%d %d %d %d"
values = append(values, []interface{}{le.BaseFee, le.ReferenceFeeUnits, le.ReserveBase, le.ReserveIncrement}...)
case *data.Amendments:
format += "%s"
values = append(values, []interface{}{le.Amendments}...)
default:
return nil, fmt.Errorf("Unknown Ledger Entry Type")
}
return &bundle{
color: leStyle,
format: format,
values: values,
flag: flag,
}, nil
}
func newTxBundle(v data.Transaction, insert string, flag Flag) (*bundle, error) {
var (
base = v.GetBase()
format = "%s %-11s %-8s %s%s %-34s %-9d "
values = []interface{}{SignSymbol(v), base.GetType(), base.Fee, insert, MemoSymbol(v), base.Account, base.Sequence}
)
if flag&ShowTransactionId > 0 {
txId, err := data.NodeId(v)
if err != nil {
return nil, err
}
format = "%s " + format
values = append([]interface{}{txId}, values...)
}
switch tx := v.(type) {
case *data.Payment:
format += "=> %-34s %-60s %-60s"
values = append(values, []interface{}{tx.Destination, tx.Amount, tx.SendMax}...)
case *data.OfferCreate:
format += "%-9d %-60s %-60s %-18s"
values = append(values, []interface{}{defaultUint32(tx.OfferSequence), tx.TakerPays, tx.TakerGets, tx.Ratio()}...)
case *data.OfferCancel:
format += "%-9d"
values = append(values, tx.OfferSequence)
case *data.AccountSet:
format += "%-9d"
values = append(values, tx.Sequence)
case *data.TrustSet:
format += "%-60s %d %d"
values = append(values, tx.LimitAmount, tx.QualityIn, tx.QualityOut)
}
return &bundle{
color: txStyle,
format: format,
values: values,
flag: flag,
}, nil
}
func newTxmBundle(txm *data.TransactionWithMetaData, flag Flag) (*bundle, error) {
insert := fmt.Sprintf("%s ", txm.MetaData.TransactionResult.Symbol())
if flag&ShowLedgerSequence > 0 {
insert = fmt.Sprintf("%-9d %s", txm.LedgerSequence, insert)
}
b, err := newTxBundle(txm.Transaction, insert, flag)
if err != nil {
return nil, err
}
if len(txm.MetaData.AffectedNodes) == 0 {
// Likely a proposed transaction
b.color = proposalStyle
} else if !txm.MetaData.TransactionResult.Success() {
b.color = infoStyle
}
return b, nil
}
func newBundle(value interface{}, flag Flag) (*bundle, error) {
switch v := value.(type) {
case *data.TransactionWithMetaData:
return newTxmBundle(v, flag)
case data.Transaction:
return newTxBundle(v, "", flag)
case data.LedgerEntry:
return newLeBundle(v, flag)
}
switch v := reflect.Indirect(reflect.ValueOf(value)).Interface().(type) {
case websockets.LedgerStreamMsg:
return &bundle{
color: ledgerStyle,
format: "Ledger %d closed at %s with %d transactions",
values: []interface{}{v.LedgerSequence, v.LedgerTime.String(), v.TxnCount},
flag: flag,
}, nil
case websockets.ServerStreamMsg:
return &bundle{
color: infoStyle,
format: "Server Status: %s (%d/%d)",
values: []interface{}{v.Status, v.LoadFactor, v.LoadBase},
flag: flag,
}, nil
case data.Ledger:
return &bundle{
color: ledgerStyle,
format: "Ledger %d closed at %s",
values: []interface{}{v.LedgerSequence, v.CloseTime.String()},
flag: flag,
}, nil
case data.InnerNode:
return &bundle{
color: leStyle,
format: "%s: %d hashes",
values: []interface{}{v.Type, v.Count()},
flag: flag,
}, nil
case data.Proposal:
return &bundle{
color: proposalStyle,
format: "%s Proposal: %s %s %s %s",
values: []interface{}{SignSymbol(&v), v.PublicKey.NodePublicKey(), v.CloseTime.String(), v.PreviousLedger, v.LedgerHash},
flag: flag,
}, nil
case data.Validation:
return &bundle{
color: validationStyle,
format: "%s Validation: %s %s %s %-8d %08X %s",
values: []interface{}{SignSymbol(&v), v.SigningPubKey.NodePublicKey(), v.SigningTime.String(), v.LedgerHash, v.LedgerSequence, v.Flags, v.Amendments},
flag: flag,
}, nil
case data.Trade:
return &bundle{
color: tradeStyle,
format: "Trade: %-34s => %-34s %22.8f %60s => %-60s",
values: []interface{}{v.Giver, v.Taker, v.Rate(), v.Got, v.Paid},
flag: flag,
}, nil
case data.Balance:
return &bundle{
color: balanceStyle,
format: "Balance: %-34s Currency: %s Balance: %20s Change: %20s",
values: []interface{}{v.Account, v.Currency, v.Balance, v.Change},
flag: flag,
}, nil
case data.Path:
sig, err := v.Signature()
if err != nil {
return nil, err
}
return &bundle{
color: pathStyle,
format: "Path: %08X %s",
values: []interface{}{sig, v.String()},
flag: flag,
}, nil
case data.OrderBookOffer:
return &bundle{
color: offerStyle,
format: "Offer: %34s %8d %s %25s %62s %62s",
values: []interface{}{v.Account, v.Sequence, BoolSymbol(v.Expiration != nil && *v.Expiration > 0), v.Ratio(), v.TakerPays, v.TakerGets},
flag: flag,
}, nil
case data.AccountOffer:
return &bundle{
color: offerStyle,
format: "Offer: %08X %9d %34s %62s %62s",
values: []interface{}{v.Flags, v.Sequence, v.Quality, v.TakerPays, v.TakerGets},
flag: flag,
}, nil
case data.AccountLine:
return &bundle{
color: lineStyle,
format: "Line: %20s %3s %34s %34s %20s %s %s %8d %8d",
values: []interface{}{v.Balance, v.Currency, v.Account, v.Limit, v.LimitPeer, BoolSymbol(v.NoRipple), BoolSymbol(v.NoRipplePeer), v.QualityIn, v.QualityOut},
flag: flag,
}, nil
default:
return &bundle{
color: infoStyle,
format: "%s",
values: []interface{}{v},
flag: flag,
}, nil
}
}
func indent(flag Flag) string {
switch {
case flag&Indent > 0:
return " "
case flag&DoubleIndent > 0:
return " "
case flag&TripleIndent > 0:
return " "
default:
return ""
}
}
func println(value interface{}, flag Flag) (int, error) {
b, err := newBundle(value, flag)
if err != nil {
return 0, err
}
return b.color.Printf(indent(flag)+b.format+"\n", b.values...)
}
func Println(value interface{}, flag Flag) {
if _, err := println(value, flag); err != nil {
infoStyle.Println(err.Error())
}
}
func Sprint(value interface{}, flag Flag) string {
b, err := newBundle(value, flag)
if err != nil {
return fmt.Sprintf("Cannot format: %+v", value)
}
return b.color.SprintfFunc()(indent(flag)+b.format, b.values...)
}