/
resultset.go
775 lines (717 loc) · 23.6 KB
/
resultset.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
// Copyright © 2015-2017,2020-2022 Phil Pennock.
// All rights reserved, except as granted under license.
// Licensed per file LICENSE.txt
package resultset
import (
"encoding/json"
"fmt"
"io"
"os"
"strconv"
"strings"
"unicode/utf16"
"golang.org/x/net/idna"
"github.com/philpennock/character/entities"
"github.com/philpennock/character/internal/runemanip"
"github.com/philpennock/character/internal/table"
"github.com/philpennock/character/sources"
"github.com/philpennock/character/unicode"
)
// CanTable is the interface for the caller to determine if we have
// table-support loaded at all. It mostly just avoids propagating imports of
// the table shim into every place which is already creating results.
func CanTable() bool {
return table.Supported()
}
type selector int
// These constants dictate what is being added to a ResultSet.
const (
_ITEM selector = iota
_STRSEQ
_ERROR
_DIVIDER
)
type printItem uint
// These constants dictate what attribute of a rune should be printed.
const (
PRINT_RUNE printItem = iota
PRINT_RUNE_ISOLATED
PRINT_RUNE_PRESENT_TEXT
PRINT_RUNE_PRESENT_EMOJI
PRINT_RUNE_PRESENT_LEFT
PRINT_RUNE_PRESENT_RIGHT
PRINT_RUNE__RENDERERS // items before this render the rune itself
PRINT_RUNE_DEC
PRINT_RUNE_HEX // raw hex
PRINT_RUNE_JSON // surrogate pair in JSON syntax
PRINT_RUNE_UTF8ENC // URL format
PRINT_RUNE_PUNY
PRINT_RUNE_WIDTH // best guess, terminal display cell width
PRINT_NAME
PRINT_BLOCK
PRINT_HTML_ENTITIES
PRINT_XML_ENTITIES
PRINT_PART_OF // when we decomposed from input
)
type fieldSetSelector uint
// These constants determine which columns will appear in a table.
const (
FIELD_SET_DEFAULT fieldSetSelector = iota
FIELD_SET_NET
FIELD_SET_DEBUG
)
// These are used to influence how runes are rendered
type runeRenderBiasType uint
const (
runeRenderUnspecified runeRenderBiasType = iota
runeRenderText
runeRenderEmoji
runeRenderLeft
runeRenderRight
)
type errorItem struct {
input string
err error
}
// Originally we stored unicode.CharInfo directly but as of the point where
// we support normalization handling, we also want to record where an item came
// from, because it might not be obvious from what was typed at the
// command-line.
// strseq is used only for _STRSEQ types
type charItem struct {
unicode unicode.CharInfo
partOf rune
strseq string
}
// fixedWidthCell satisfies an interface used by the tabular table provider, to
// let us override the width. [types.go TerminalCellWidther -- not asserting here
// so as to maintain table provider abstraction]
//
// The table providers take various approaches to determining width; tabular uses
// go-runewidth which is Pretty Good and far better than most, but we are a tool
// all about Unicode and can sometimes do a little better in some corner cases.
type fixedWidthCell struct {
s string
w int
}
func (fwc fixedWidthCell) String() string { return fwc.s }
func (fwc fixedWidthCell) TerminalCellWidth() int { return fwc.w }
type tcWidther interface {
TerminalCellWidth() int
}
// A ResultSet is the collection of unicode characters (or near facsimiles thereof)
// about which we wish to see data. Various front-end commands just figure out which
// characters are being asked about and throw them in the ResultSet, then at the end
// the ResultSet is asked to print itself in an appropriate format, which might be
// in a table, as lines, or in other ways.
// For convenience, errors are also accumulated here. If emitting tables, the errors
// will be in a separate second table, but otherwise they're interleaved with
// the real characters in the correct order (but to stderr (probably), so if
// the streams diverge then this might not be reconstructible).
type ResultSet struct {
sources *sources.Sources
items []charItem
errors []errorItem
which []selector
OutputStream io.Writer
ErrorStream io.Writer
// This is subject to change; do we want fully selectable sets of fields,
// just pre-canned, something else? For now ... let's keep it simple.
fields fieldSetSelector
// runeBias is used by the flags-control in this package, to select
// UTS#51 presentation selectors to emit after a character, to try to bias how
// it is shown.
runeBias runeRenderBiasType
}
// New creates a ResultSet.
// We now make ResultSet an exported type, ugh, so this stutters when used.
// Most usage should never do that.
func New(s *sources.Sources, sizeHint int) *ResultSet {
r := &ResultSet{
sources: s,
items: make([]charItem, 0, sizeHint),
errors: make([]errorItem, 0, 3),
which: make([]selector, 0, sizeHint),
}
if ResultCmdFlags.Text {
r.runeBias = runeRenderText
}
if ResultCmdFlags.Emoji {
r.runeBias = runeRenderEmoji
}
if ResultCmdFlags.Left {
r.runeBias = runeRenderLeft
}
if ResultCmdFlags.Right {
r.runeBias = runeRenderRight
}
return r
}
// SelectFieldsNet says to use the network fields, not the default fields.
// This API call is very much subject to change.
func (rs *ResultSet) SelectFieldsNet() {
rs.fields = FIELD_SET_NET
}
// SelectFieldsDebug says to show some internal diagnostics, not the default fields.
// This API call is very much subject to change.
func (rs *ResultSet) SelectFieldsDebug() {
rs.fields = FIELD_SET_DEBUG
}
// RunePrintType returns PRINT_RUNE or one of its variants, appropriate to
// handle command-line-chosen variant selectors accordingly.
func (rs *ResultSet) RunePrintType() printItem {
switch rs.runeBias {
case runeRenderUnspecified:
return PRINT_RUNE
case runeRenderText:
return PRINT_RUNE_PRESENT_TEXT
case runeRenderEmoji:
return PRINT_RUNE_PRESENT_EMOJI
case runeRenderLeft:
return PRINT_RUNE_PRESENT_LEFT
case runeRenderRight:
return PRINT_RUNE_PRESENT_RIGHT
default:
panic("unhandled internal runeBias")
}
}
// AddError records, in-sequence, that we got an error at this point.
func (rs *ResultSet) AddError(input string, e error) {
rs.errors = append(rs.errors, errorItem{input, e})
rs.which = append(rs.which, _ERROR)
}
// AddCharInfo is used for recording character information as an item in the result set.
func (rs *ResultSet) AddCharInfo(ci unicode.CharInfo) {
rs.items = append(rs.items, charItem{unicode: ci})
rs.which = append(rs.which, _ITEM)
}
// AddStringSequence is used where we have some combining sequence to be rendered as one
func (rs *ResultSet) AddStringSequence(s string) {
rs.items = append(rs.items, charItem{strseq: s})
rs.which = append(rs.which, _STRSEQ)
}
// AddCharInfoDerivedFrom is used when the character was decomposed by us, so
// that we can display original input if requested.
func (rs *ResultSet) AddCharInfoDerivedFrom(ci unicode.CharInfo, from rune) {
rs.items = append(rs.items, charItem{unicode: ci, partOf: from})
rs.which = append(rs.which, _ITEM)
}
// AddDivider is use between words.
func (rs *ResultSet) AddDivider() {
rs.which = append(rs.which, _DIVIDER)
}
// ErrorCount sums the number of errors in the entire ResultSet.
func (rs *ResultSet) ErrorCount() int {
return len(rs.errors)
}
func (rs *ResultSet) fixStreams() {
if rs.OutputStream == nil {
rs.OutputStream = os.Stdout
}
if rs.ErrorStream == nil {
rs.ErrorStream = os.Stderr
}
}
// PrintPlain shows just characters, but with full errors interleaved too.
// One character or error per line.
func (rs *ResultSet) PrintPlain(what printItem) {
rs.fixStreams()
var ii, ei int
var s selector
for _, s = range rs.which {
switch s {
case _ITEM:
fmt.Fprintf(rs.OutputStream, "%s\n", rs.RenderCharInfoItem(rs.items[ii], what))
ii++
case _STRSEQ:
fmt.Fprintf(rs.OutputStream, "%s\n", rs.items[ii].strseq)
ii++
case _ERROR:
fmt.Fprintf(rs.ErrorStream, "looking up %q: %s\n", rs.errors[ei].input, rs.errors[ei].err)
ei++
case _DIVIDER:
fmt.Fprintln(rs.OutputStream)
default:
fmt.Fprintf(rs.ErrorStream, "internal error, unhandled item to print, of type %v", s)
}
}
}
// StringPlain returns the characters as chars in a word, dividers as a space.
func (rs *ResultSet) StringPlain(what printItem) string {
rs.fixStreams()
out := make([]string, 0, len(rs.which))
var ii, ei int
var s selector
for _, s = range rs.which {
switch s {
case _ITEM:
item := rs.RenderCharInfoItem(rs.items[ii], what)
if itemS, ok := item.(fmt.Stringer); ok {
out = append(out, itemS.String())
} else if itemS, ok := item.(string); ok {
out = append(out, itemS)
} else {
// shouldn't happen (but can't have a primitive type satisfy an interface)
out = append(out, fmt.Sprintf("%v", item))
}
ii++
case _STRSEQ:
// We don't double-print, the items in this should already have been emitted
ii++
case _ERROR:
fmt.Fprintf(rs.ErrorStream, "looking up %q: %s\n", rs.errors[ei].input, rs.errors[ei].err)
ei++
case _DIVIDER:
out = append(out, " ")
default:
fmt.Fprintf(rs.ErrorStream, "internal error, unhandled item to print, of type %v", s)
}
}
return strings.Join(out, "")
}
// LenTotalCount yields how many rows are in the resultset, including dividers and errors
func (rs *ResultSet) LenTotalCount() int {
return len(rs.which)
}
// LenItemCount yields how many successful items are in the table
func (rs *ResultSet) LenItemCount() int {
return len(rs.items)
}
// RenderCharInfoItem converts a charItem and an attribute selector into a string or Stringer
func (rs *ResultSet) RenderCharInfoItem(ci charItem, what printItem) interface{} {
// Exceptional cases first:
//
// We use 0 as a special-case for things like combinations, where there's only a name:
if ci.unicode.Number == 0 {
if ci.strseq != "" {
switch what {
case PRINT_RUNE, PRINT_RUNE_PRESENT_TEXT, PRINT_RUNE_PRESENT_EMOJI, PRINT_RUNE_PRESENT_LEFT, PRINT_RUNE_PRESENT_RIGHT:
return ci.strseq
case PRINT_NAME:
return "(derived sequence)"
default:
return " "
}
}
if what != PRINT_NAME {
return " "
}
}
// Some other substitutions:
if what < PRINT_RUNE__RENDERERS && !strconv.IsGraphic(ci.unicode.Number) {
// We need controls to not be printed, such as 0x98 "START OF STRING"
// The first 20 points are easy, there are replacements for them.
// strconv.IsGraphic is false for codepoints in newer Unicode than Go
// stdlib handles, so we can't just default to the replacement
// character.
// We probably need to encode "control" etc as bits in a bitfield in ci.unicode.
// For now, let's use a heuristic: all the "real" control characters are in
// "Basic Latin" or "Latin-1 Supplement" and Unicode won't be assigning new
// code-points there which Go won't recognise as non-graphic.
switch {
case ci.unicode.Number < 0x20:
return string(rune(0x2400 + ci.unicode.Number)) // 'Control Pictures' block
case ci.unicode.Number <= 0xFF:
return string(rune(0xFFFD)) // "REPLACEMENT CHARACTER"
}
}
// Normal handling:
switch what {
case PRINT_RUNE:
s := string(ci.unicode.Number)
if w, override := runemanip.DisplayCellWidth(s); override {
return fixedWidthCell{s: s, w: w}
}
return s
case PRINT_RUNE_ISOLATED: // BROKEN
sInner := string(ci.unicode.Number)
// FIXME: None of these are actually working
sOuter := fmt.Sprintf("%c%c%c",
0x202A, // LEFT-TO-RIGHT EMBEDDING
// 0x202D, // LEFT-TO-RIGHT OVERRIDE
// 0x2066, // LEFT-TO-RIGHT ISOLATE
ci.unicode.Number,
// 0x2069, // POP DIRECTIONAL ISOLATE
0x202C, // POP DIRECTIONAL FORMATTING
)
if w, override := runemanip.DisplayCellWidth(sInner); override {
return fixedWidthCell{s: sOuter, w: w}
}
return sOuter
// These next four demonstrate that we need a bit of a design rethink here, because
// "2.11 Order of Emoji ZWJ Sequences" of
// <http://unicode.org/reports/tr51/> demonstrates that we've got a cascade
// of modifiers we could be applying:
//
// > When representing emoji ZWJ sequences for an individual person, the following order should be used:
// > Order Category Section
// > 1 Base Section 1.4.1 Emoji Characters
// > 2 Emoji modifier or emoji presentation selector Section 2.4 Diversity
// > 3 Hair component Section 2.8 Hair Component
// > 4 Color Section 2.9, Color
// > 5 Gender sign or object Section 2.3.1, Gender-Neutral Emoji
// > 6 Direction indicator Section 2.10, Emoji Glyph Facing Direction
//
// Fortunately for me right now, the Emoji Glyph Facing Direction sequences
// end with the emoji presentation selector so I don't need to rewrite for
// the bits we support here.
case PRINT_RUNE_PRESENT_TEXT: // text presentation selector, UTS#51 §1.4.3 ED-8
s := string(ci.unicode.Number)
w, _ := runemanip.DisplayCellWidth(s)
if unicode.Emojiable(ci.unicode.Number) {
s = s + "\uFE0E" // VARIATION SELECTOR-15
}
return fixedWidthCell{s: s, w: w}
case PRINT_RUNE_PRESENT_EMOJI: // emoji presentation selector, UTS#51 §1.4.3 ED-9
s := string(ci.unicode.Number)
w, _ := runemanip.DisplayCellWidth(s)
if unicode.Emojiable(ci.unicode.Number) {
s = s + "\uFE0F" // VARIATION SELECTOR-16
}
return fixedWidthCell{s: s, w: w}
case PRINT_RUNE_PRESENT_LEFT: // emoji glyph facing direction UTS#51 §2.10
s := string(ci.unicode.Number)
w, _ := runemanip.DisplayCellWidth(s)
if unicode.Emojiable(ci.unicode.Number) {
s = s + "\u200D\u2B05\uFE0F" // ZERO WIDTH JOINER, LEFTWARDS BLACK ARROW, VARIATION SELECTOR-16
}
return fixedWidthCell{s: s, w: w}
case PRINT_RUNE_PRESENT_RIGHT: // emoji glyph facing direction UTS#51 §2.10
s := string(ci.unicode.Number)
w, _ := runemanip.DisplayCellWidth(s)
if unicode.Emojiable(ci.unicode.Number) {
s = s + "\u200D\u27A1\uFE0F" // ZERO WIDTH JOINER, BLACK RIGHTWARDS ARROW, VARIATION SELECTOR-16
}
return fixedWidthCell{s: s, w: w}
case PRINT_RUNE_HEX:
return strconv.FormatUint(uint64(ci.unicode.Number), 16)
case PRINT_RUNE_DEC:
return strconv.FormatUint(uint64(ci.unicode.Number), 10)
case PRINT_RUNE_UTF8ENC:
bb := []byte(string(ci.unicode.Number))
var s string
for i := range bb {
s += fmt.Sprintf("%%%X", bb[i])
}
return s
case PRINT_RUNE_JSON:
r1, r2 := utf16.EncodeRune(ci.unicode.Number)
if r1 == 0xFFFD && r2 == 0xFFFD {
if ci.unicode.Number <= 0xFFFF {
return fmt.Sprintf("\\u%04X", ci.unicode.Number)
}
return "?"
}
return fmt.Sprintf("\\u%04X\\u%04X", r1, r2)
case PRINT_RUNE_PUNY:
p, err := idna.ToASCII(string(ci.unicode.Number))
if err != nil {
return ""
}
return p
case PRINT_RUNE_WIDTH:
// If we supported color etc, then this would be a good opportunity to
// use the override return bool to color red or something.
width, _ := runemanip.DisplayCellWidth(string(ci.unicode.Number))
return strconv.FormatUint(uint64(width), 10)
case PRINT_NAME:
if ci.unicode.NameWidth == 0 {
return ci.unicode.Name
}
return fixedWidthCell{s: ci.unicode.Name, w: ci.unicode.NameWidth}
case PRINT_BLOCK:
return rs.sources.UBlocks.Lookup(ci.unicode.Number)
case PRINT_HTML_ENTITIES:
eList, ok := entities.HTMLEntitiesReverse[ci.unicode.Number]
if !ok {
return ""
}
return "&" + strings.Join(eList, "; &") + ";"
case PRINT_XML_ENTITIES:
eList, ok := entities.XMLEntitiesReverse[ci.unicode.Number]
if !ok {
return ""
}
return "&" + strings.Join(eList, "; &") + ";"
case PRINT_PART_OF:
if ci.partOf == 0 {
return ""
}
return string(ci.partOf)
default:
panic(fmt.Sprintf("unhandled item to print: %v", what))
}
}
// JItem is how a character is represented in JSON output.
type JItem struct {
Display string `json:"display"`
DisplayText string `json:"displayText"`
DisplayEmoji string `json:"displayEmoji"`
DisplayLeft string `json:"displayLeft"`
DisplayRight string `json:"displayRight"`
Name string `json:"name"`
Hex string `json:"hex"`
Dec string `json:"decimal"`
Block string `json:"block"`
VIMDigraphs []string `json:"vimDigraphs,omitempty"`
X11Digraphs []string `json:"x11Digraphs,omitempty"`
HTMLEntities []string `json:"htmlEntities,omitempty"`
XMLEntities []string `json:"xmlEntities,omitempty"`
UTF8 string `json:"utf8"`
JSONEscape string `json:"jsonEscape"`
RenderWidth int `json:"renderWidth"`
Puny string `json:"puny"`
PartOf string `json:"part-of,omitempty"`
}
// JInfo is something which might be shown instead of JItem.
type JInfo struct {
Comment string `json:"comment"`
}
// JCombination is something which might be shown instead of JItem
type JCombination struct {
Display string `json:"display"`
Derived bool `json:"derived"`
}
// S converts to a string, for JSON
func S(x interface{}) string {
switch s := x.(type) {
case string:
return s
case fixedWidthCell:
return s.s
case fmt.Stringer:
return s.String()
default:
return ""
}
}
// JItemWidth converts to a width, for JSON
func JItemWidth(x interface{}) int {
switch s := x.(type) {
case string:
width, _ := runemanip.DisplayCellWidth(s)
return width
case tcWidther:
return s.TerminalCellWidth()
case fmt.Stringer:
width, _ := runemanip.DisplayCellWidth(s.String())
return width
default:
return 0
}
}
// JSONEntry constructs a probably-JItem struct for JSON rendering of a character.
func (rs *ResultSet) JSONEntry(ci charItem) interface{} {
html, _ := entities.HTMLEntitiesReverse[ci.unicode.Number]
xml, _ := entities.XMLEntitiesReverse[ci.unicode.Number]
if ci.unicode.Number == 0 {
if ci.unicode.Name != "" {
return &JInfo{Comment: ci.unicode.Name}
}
if ci.strseq != "" {
return &JCombination{Display: ci.strseq, Derived: true}
}
}
return &JItem{
Display: S(rs.RenderCharInfoItem(ci, PRINT_RUNE)),
DisplayText: S(rs.RenderCharInfoItem(ci, PRINT_RUNE_PRESENT_TEXT)),
DisplayEmoji: S(rs.RenderCharInfoItem(ci, PRINT_RUNE_PRESENT_EMOJI)),
DisplayLeft: S(rs.RenderCharInfoItem(ci, PRINT_RUNE_PRESENT_LEFT)),
DisplayRight: S(rs.RenderCharInfoItem(ci, PRINT_RUNE_PRESENT_RIGHT)),
Name: S(rs.RenderCharInfoItem(ci, PRINT_NAME)),
Hex: S(rs.RenderCharInfoItem(ci, PRINT_RUNE_HEX)),
Dec: S(rs.RenderCharInfoItem(ci, PRINT_RUNE_DEC)),
Block: S(rs.RenderCharInfoItem(ci, PRINT_BLOCK)),
VIMDigraphs: rs.sources.Vim.DigraphsSliceFor(ci.unicode.Number),
X11Digraphs: rs.sources.X11.DigraphsSliceFor(ci.unicode.Number),
HTMLEntities: html,
XMLEntities: xml,
UTF8: S(rs.RenderCharInfoItem(ci, PRINT_RUNE_UTF8ENC)),
JSONEscape: S(rs.RenderCharInfoItem(ci, PRINT_RUNE_JSON)),
RenderWidth: JItemWidth(rs.RenderCharInfoItem(ci, PRINT_RUNE)),
Puny: S(rs.RenderCharInfoItem(ci, PRINT_RUNE_PUNY)),
PartOf: S(rs.RenderCharInfoItem(ci, PRINT_PART_OF)),
}
}
// PrintJSON shows everything we know about each result, in JSON.
// Rather than array of char/div/error, we have two top-level arrays
// of results and a divider is represented by a nil item. Not sure
// how friendly that is for arbitrary input, but it helps with syncing I think
// (but errors inline might help more).
//
// Note that this is distinct from using a JSON table renderer for the normal
// tables and is instead emitting completely different output.
func (rs *ResultSet) PrintJSON() {
rs.fixStreams()
type JError struct {
Input string `json:"input"`
Error string `json:"error"`
}
var output struct {
Characters []interface{} `json:"characters,omitempty"`
Errors []JError `json:"errors,omitempty"`
}
if len(rs.items) > 0 {
output.Characters = make([]interface{}, 0, len(rs.items))
ii := 0
for _, s := range rs.which {
switch s {
case _ITEM, _STRSEQ:
output.Characters = append(output.Characters, rs.JSONEntry(rs.items[ii]))
ii++
case _ERROR:
// skip, handled below
case _DIVIDER:
output.Characters = append(output.Characters, nil)
}
}
}
if len(rs.errors) > 0 {
output.Errors = make([]JError, 0, len(rs.errors))
for _, ei := range rs.errors {
output.Errors = append(output.Errors, JError{ei.input, ei.err.Error()})
}
}
b, err := json.MarshalIndent(&output, "", " ")
if err != nil {
fmt.Fprintln(rs.ErrorStream, err)
return
}
rs.OutputStream.Write(b)
rs.OutputStream.Write([]byte{'\n'})
}
// PrintTables provides much more verbose details about the contents of
// a ResultSet, in a structured terminal table.
func (rs *ResultSet) PrintTables() {
rs.fixStreams()
if len(rs.items) > 0 {
t := table.New()
t.AddHeaders(rs.detailsHeaders()...)
ii := 0
for _, s := range rs.which {
switch s {
case _ITEM, _STRSEQ:
t.AddRow(rs.detailsFor(rs.items[ii])...)
ii++
case _ERROR:
// skip, print in separate table below
case _DIVIDER:
t.AddSeparator()
}
}
for _, props := range rs.detailsColumnProperties() {
if props.align != table.UNSET {
t.AlignColumn(props.column, props.align)
}
if props.skipable {
t.SetSkipableColumn(props.column)
}
}
fmt.Fprint(rs.OutputStream, t.Render())
}
if len(rs.errors) > 0 {
t := table.New()
t.AddHeaders("Problem Input", "Error")
for _, ei := range rs.errors {
t.AddRow(ei.input, ei.err)
}
fmt.Fprint(rs.ErrorStream, t.Render())
}
}
func (rs *ResultSet) detailsHeaders() []interface{} {
switch rs.fields {
case FIELD_SET_DEFAULT:
return []interface{}{
"C", "Name", "Hex", "Dec", "Block", "Vim", "X11", "HTML", "XML",
}
case FIELD_SET_NET:
return []interface{}{
"C", "Name", "Hex", "UTF-8", "JSON", "Punycode", "Of",
}
case FIELD_SET_DEBUG:
return []interface{}{
"C", "Width", "Hex", "Name", "C-Type",
}
}
return nil
}
type columnProperties struct {
column int // 1-based
align table.Alignment
skipable bool
}
func (rs *ResultSet) detailsColumnProperties() []columnProperties {
switch rs.fields {
case FIELD_SET_DEFAULT:
return []columnProperties{
{3, table.RIGHT, false}, // Hex
{4, table.RIGHT, false}, // Dec
{6, table.UNSET, true}, // Vim
{7, table.UNSET, true}, // X11
{8, table.UNSET, true}, // HTML
{9, table.UNSET, true}, // XML
}
case FIELD_SET_NET:
return []columnProperties{
{3, table.RIGHT, false}, // Hex
{4, table.RIGHT, false}, // Dec
{7, table.UNSET, true}, // Of
}
case FIELD_SET_DEBUG:
return []columnProperties{
{3, table.RIGHT, false}, // Hex
}
}
return nil
}
func (rs *ResultSet) detailsFor(ci charItem) []interface{} {
runeDisplay := PRINT_RUNE // should be PRINT_RUNE_ISOLATED
switch rs.runeBias {
case runeRenderUnspecified:
// no action, want PRINT_RUNE
case runeRenderText:
runeDisplay = PRINT_RUNE_PRESENT_TEXT
case runeRenderEmoji:
runeDisplay = PRINT_RUNE_PRESENT_EMOJI
case runeRenderLeft:
runeDisplay = PRINT_RUNE_PRESENT_LEFT
case runeRenderRight:
runeDisplay = PRINT_RUNE_PRESENT_RIGHT
}
switch rs.fields {
case FIELD_SET_DEFAULT:
return []interface{}{
rs.RenderCharInfoItem(ci, runeDisplay),
rs.RenderCharInfoItem(ci, PRINT_NAME),
rs.RenderCharInfoItem(ci, PRINT_RUNE_HEX),
rs.RenderCharInfoItem(ci, PRINT_RUNE_DEC),
rs.RenderCharInfoItem(ci, PRINT_BLOCK),
// We might put Info in here, to match old Perl script behaviour
rs.sources.Vim.DigraphsFor(ci.unicode.Number),
rs.sources.X11.DigraphsFor(ci.unicode.Number),
rs.RenderCharInfoItem(ci, PRINT_HTML_ENTITIES),
rs.RenderCharInfoItem(ci, PRINT_XML_ENTITIES),
// PRINT_PART_OF is almost always empty and while important, it
// annoys me, so I've removed it from my most-common view.
}
case FIELD_SET_NET:
return []interface{}{
rs.RenderCharInfoItem(ci, runeDisplay),
rs.RenderCharInfoItem(ci, PRINT_NAME),
rs.RenderCharInfoItem(ci, PRINT_RUNE_HEX),
rs.RenderCharInfoItem(ci, PRINT_RUNE_UTF8ENC),
rs.RenderCharInfoItem(ci, PRINT_RUNE_JSON),
rs.RenderCharInfoItem(ci, PRINT_RUNE_PUNY),
rs.RenderCharInfoItem(ci, PRINT_PART_OF),
}
case FIELD_SET_DEBUG:
return []interface{}{
rs.RenderCharInfoItem(ci, runeDisplay),
rs.RenderCharInfoItem(ci, PRINT_RUNE_WIDTH),
rs.RenderCharInfoItem(ci, PRINT_RUNE_HEX),
rs.RenderCharInfoItem(ci, PRINT_NAME),
func(c charItem) string { t := rs.RenderCharInfoItem(ci, runeDisplay); return fmt.Sprintf("%T", t) }(ci),
}
}
return nil
}