-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcoverage.html
More file actions
7848 lines (7263 loc) · 335 KB
/
Copy pathcoverage.html
File metadata and controls
7848 lines (7263 loc) · 335 KB
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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>byter: Go Coverage Report</title>
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">github.com/bengarrett/retrotxtgo/byter/byter.go (83.7%)</option>
<option value="file1">github.com/bengarrett/retrotxtgo/cmd/cmd.go (40.0%)</option>
<option value="file2">github.com/bengarrett/retrotxtgo/cmd/example.go (85.7%)</option>
<option value="file3">github.com/bengarrett/retrotxtgo/cmd/example/example.go (86.1%)</option>
<option value="file4">github.com/bengarrett/retrotxtgo/cmd/hex.go (55.6%)</option>
<option value="file5">github.com/bengarrett/retrotxtgo/cmd/hexa/hexa.go (97.2%)</option>
<option value="file6">github.com/bengarrett/retrotxtgo/cmd/info.go (100.0%)</option>
<option value="file7">github.com/bengarrett/retrotxtgo/cmd/internal/dump/dump.go (17.9%)</option>
<option value="file8">github.com/bengarrett/retrotxtgo/cmd/internal/flag/flag.go (8.2%)</option>
<option value="file9">github.com/bengarrett/retrotxtgo/cmd/internal/flag/flags.go (12.5%)</option>
<option value="file10">github.com/bengarrett/retrotxtgo/cmd/internal/format/format.go (100.0%)</option>
<option value="file11">github.com/bengarrett/retrotxtgo/cmd/internal/info/info.go (32.0%)</option>
<option value="file12">github.com/bengarrett/retrotxtgo/cmd/internal/view/view.go (19.3%)</option>
<option value="file13">github.com/bengarrett/retrotxtgo/cmd/lang.go (80.0%)</option>
<option value="file14">github.com/bengarrett/retrotxtgo/cmd/list.go (80.0%)</option>
<option value="file15">github.com/bengarrett/retrotxtgo/cmd/list/list.go (91.7%)</option>
<option value="file16">github.com/bengarrett/retrotxtgo/cmd/online/online.go (42.1%)</option>
<option value="file17">github.com/bengarrett/retrotxtgo/cmd/table.go (66.7%)</option>
<option value="file18">github.com/bengarrett/retrotxtgo/cmd/terminal.go (66.7%)</option>
<option value="file19">github.com/bengarrett/retrotxtgo/cmd/update/update.go (34.4%)</option>
<option value="file20">github.com/bengarrett/retrotxtgo/cmd/version/version.go (46.2%)</option>
<option value="file21">github.com/bengarrett/retrotxtgo/cmd/view.go (87.0%)</option>
<option value="file22">github.com/bengarrett/retrotxtgo/convert/convert.go (82.0%)</option>
<option value="file23">github.com/bengarrett/retrotxtgo/convert/encoding.go (71.2%)</option>
<option value="file24">github.com/bengarrett/retrotxtgo/fsys/fsys.go (80.0%)</option>
<option value="file25">github.com/bengarrett/retrotxtgo/fsys/read.go (85.5%)</option>
<option value="file26">github.com/bengarrett/retrotxtgo/fsys/reader.go (70.7%)</option>
<option value="file27">github.com/bengarrett/retrotxtgo/fsys/zip.go (17.5%)</option>
<option value="file28">github.com/bengarrett/retrotxtgo/info/detail.go (79.2%)</option>
<option value="file29">github.com/bengarrett/retrotxtgo/info/info.go (76.0%)</option>
<option value="file30">github.com/bengarrett/retrotxtgo/internal/mock/mock.go (18.1%)</option>
<option value="file31">github.com/bengarrett/retrotxtgo/internal/save/save.go (80.0%)</option>
<option value="file32">github.com/bengarrett/retrotxtgo/internal/tmp/tmp.go (100.0%)</option>
<option value="file33">github.com/bengarrett/retrotxtgo/logs/execute.go (4.3%)</option>
<option value="file34">github.com/bengarrett/retrotxtgo/logs/logs.go (62.2%)</option>
<option value="file35">github.com/bengarrett/retrotxtgo/main.go (0.0%)</option>
<option value="file36">github.com/bengarrett/retrotxtgo/meta/meta.go (96.2%)</option>
<option value="file37">github.com/bengarrett/retrotxtgo/nl/nl.go (75.4%)</option>
<option value="file38">github.com/bengarrett/retrotxtgo/sample/sample.go (80.4%)</option>
<option value="file39">github.com/bengarrett/retrotxtgo/table/lang.go (89.7%)</option>
<option value="file40">github.com/bengarrett/retrotxtgo/table/list.go (95.2%)</option>
<option value="file41">github.com/bengarrett/retrotxtgo/table/table.go (59.5%)</option>
<option value="file42">github.com/bengarrett/retrotxtgo/term/term.go (59.6%)</option>
<option value="file43">github.com/bengarrett/retrotxtgo/xud/xud.go (54.9%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">not covered</span>
<span class="cov8">covered</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" style="display: none">// Package byter provides functions that manipulate or return byte arrays.
package byter
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"io"
"strings"
"unicode/utf8"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/transform"
)
var (
ErrCharmap = errors.New("the c charmap cannot be nil")
ErrUTF8 = errors.New("string cannot encode to utf-8")
)
const (
SUB = 26 // SUB is the ASCII control code for substitute.
DosSUB = 8594 // DosSub is the Unicode for the right-arrow.
SymbolSUB = 9242 // SymbolSUB is the Unicode for the substitute character.
)
// BOM is the UTF-8 byte order mark prefix.
func BOM() []byte <span class="cov8" title="1">{
const ef, bb, bf = 239, 187, 191
return []byte{ef, bb, bf}
}</span>
// TrimEOF will cut text at the first occurrence of the SUB character.
// The SUB is used by DOS and CP/M as an end-of-file marker.
func TrimEOF(b []byte) []byte <span class="cov8" title="1">{
// ASCII control code
cut := bytes.IndexByte(b, SUB)
if cut > 0 && len(b) > cut </span><span class="cov8" title="1">{
return b[:cut]
}</span>
// UTF-8 symbol for substitute character
<span class="cov8" title="1">s := string(b)
if cut := strings.IndexRune(s, SymbolSUB); cut > 0 </span><span class="cov0" title="0">{
return []byte(s[:cut])
}</span>
// UTF-8 right-arrow which is displayed for the CP-437 substitute character code point 26
<span class="cov8" title="1">if cut := strings.IndexRune(s, DosSUB); cut > 0 </span><span class="cov0" title="0">{
return []byte(s[:cut])
}</span>
<span class="cov8" title="1">return b</span>
}
// MakeBytes generates a 256 character or 8-bit container ready to hold legacy code point values.
func MakeBytes() []byte <span class="cov8" title="1">{
const size = 256
m := make([]byte, size)
for i := range size </span><span class="cov8" title="1">{
m[i] = byte(i)
}</span>
<span class="cov8" title="1">return m</span>
}
// Mark adds a UTF-8 byte order mark to the text if it doesn't already exist.
func Mark(b []byte) []byte <span class="cov8" title="1">{
const size = 3
if len(b) >= size </span><span class="cov8" title="1">{
if t := b[:3]; bytes.Equal(t, BOM()) </span><span class="cov8" title="1">{
return b
}</span>
}
<span class="cov8" title="1">return append(BOM(), b...)</span>
}
// HexDecode decodes a hexadecimal string into bytes.
func HexDecode(s string) ([]byte, error) <span class="cov8" title="1">{
src := []byte(s)
dst := make([]byte, hex.DecodedLen(len(src)))
if _, err := hex.Decode(dst, src); err != nil </span><span class="cov0" title="0">{
return nil, fmt.Errorf("could not decode hexadecimal string: %q: %w", s, err)
}</span>
<span class="cov8" title="1">return dst, nil</span>
}
// HexEncode encodes a string into hexadecimal bytes.
func HexEncode(s string) []byte <span class="cov8" title="1">{
src := []byte(s)
dst := make([]byte, hex.EncodedLen(len(src)))
hex.Encode(dst, src)
return dst
}</span>
// Decode a string using the character map.
func Decode(c *charmap.Charmap, s string) ([]byte, error) <span class="cov8" title="1">{
if c == nil </span><span class="cov0" title="0">{
return nil, ErrCharmap
}</span>
<span class="cov8" title="1">decoder := c.NewDecoder()
reader := transform.NewReader(strings.NewReader(s), decoder)
b, err := io.ReadAll(reader)
if err != nil </span><span class="cov0" title="0">{
return nil, fmt.Errorf("dstring io.readall error: %w", err)
}</span>
<span class="cov8" title="1">return b, nil</span>
}
// Encode the string using the character map.
func Encode(c *charmap.Charmap, s string) ([]byte, error) <span class="cov8" title="1">{
if c == nil </span><span class="cov0" title="0">{
return nil, ErrCharmap
}</span>
<span class="cov8" title="1">b := []byte(s)
if !utf8.Valid(b) </span><span class="cov0" title="0">{
return nil, fmt.Errorf("estring: %w", ErrUTF8)
}</span>
<span class="cov8" title="1">encoder := c.NewEncoder()
reader := transform.NewReader(strings.NewReader(s), encoder)
p, err := io.ReadAll(reader)
if err != nil </span><span class="cov0" title="0">{
return nil, fmt.Errorf("estring io.readall error: %w", err)
}</span>
<span class="cov8" title="1">return p, nil</span>
}
</pre>
<pre class="file" id="file1" style="display: none">// Package cmd handles the terminal interface, user flags and arguments.
package cmd
import (
"errors"
"fmt"
"os"
"strings"
"github.com/bengarrett/retrotxtgo/cmd/example"
"github.com/bengarrett/retrotxtgo/cmd/internal/flag"
"github.com/bengarrett/retrotxtgo/cmd/version"
"github.com/bengarrett/retrotxtgo/logs"
"github.com/bengarrett/retrotxtgo/meta"
"github.com/spf13/cobra"
)
var (
ErrHide = errors.New("could not hide the flag")
ErrUsage = errors.New("command usage could not display")
)
// ID are the cobra command group IDs.
const (
IDcodepage = "idcp" // codepage group
IDfile = "idfile" // file group
IDsample = "idsample" // sample group
)
// Cmd represents the base command when called without any subcommands.
var Cmd = base()
func base() *cobra.Command <span class="cov8" title="1">{
s := "Use " + meta.Name + " to print legacy text on modern terminals."
l := "Text files and art created without Unicode often fail to display on modern systems. " +
"\n" + s
expl := strings.Builder{}
example.Cmd.String(&expl)
return &cobra.Command{
Use: meta.Bin,
Short: s,
Long: l,
Example: expl.String(),
RunE: func(cmd *cobra.Command, _ []string) error </span><span class="cov0" title="0">{
// Do nothing other than print the help.
// This func must remain otherwise root
// command flags are ignored by Cobra.
return flag.Help(cmd)
}</span>,
}
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() error <span class="cov0" title="0">{
// disable the default "completion" command.
Cmd.CompletionOptions.DisableDefaultCmd = true
// hide the cobra introduced "help" command.
// https://github.com/spf13/cobra/issues/587#issuecomment-810159087
Cmd.SetHelpCommand(&cobra.Command{Hidden: true})
// the help "command" is hidden, so it needs to be assigned to a
// group otherwise it will display an empty "Additional Commands:".
Cmd.SetHelpCommandGroupID(IDcodepage)
// hide the cobra errors.
Cmd.SilenceErrors = true // set to false to debug errors
// build the version flag template.
Cmd.Version = meta.String()
s := strings.Builder{}
if err := version.Template(&s); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov0" title="0">Cmd.SetVersionTemplate(s.String())
if errE := Cmd.Execute(); errE != nil </span><span class="cov0" title="0">{
const minArgs = 2
if len(os.Args) < minArgs </span><span class="cov0" title="0">{
if errU := Cmd.Usage(); errU != nil </span><span class="cov0" title="0">{
logs.FatalS(ErrUsage, errU, "rootCmd")
}</span>
}
<span class="cov0" title="0">args := ""
if len(os.Args) > 1 </span><span class="cov0" title="0">{
args = strings.Join(os.Args[1:], " ")
}</span>
<span class="cov0" title="0">return fmt.Errorf("%w: %s %s", errE, meta.Bin, strings.TrimSpace(args))</span>
}
<span class="cov0" title="0">return nil</span>
}
// Tester creates and hides a custom tester flag.
// It is its own function so it can also be applied to unit tests as well as init.
func Tester(c *cobra.Command) *cobra.Command <span class="cov8" title="1">{
c.PersistentFlags().BoolVar(&flag.Cmd.Tester, "tester", false,
"optional in-memory, tester config file")
if err := c.PersistentFlags().MarkHidden("tester"); err != nil </span><span class="cov0" title="0">{
logs.FatalS(ErrHide, err, "tester")
}</span>
<span class="cov8" title="1">return c</span>
}
func init() <span class="cov8" title="1">{
Cmd = Tester(Cmd)
c := &cobra.Group{ID: IDcodepage, Title: "Codepage:"}
f := &cobra.Group{ID: IDfile, Title: "File:"}
s := &cobra.Group{ID: IDsample, Title: "Sample:"}
Cmd.AddGroup(c, f, s)
// create a version flag that only works on root.
Cmd.LocalNonPersistentFlags().BoolP("version", "v", false, "")
}</span>
</pre>
<pre class="file" id="file2" style="display: none">package cmd
import (
"fmt"
"strings"
"github.com/bengarrett/retrotxtgo/cmd/example"
"github.com/bengarrett/retrotxtgo/cmd/list"
"github.com/bengarrett/retrotxtgo/term"
"github.com/spf13/cobra"
)
func ListExample() *cobra.Command <span class="cov8" title="1">{
s := fmt.Sprintf("List the included sample text files available for use with the %s and %s commands",
term.Example("info"), term.Example("view"))
l := fmt.Sprintf("List the included sample text art and documents available for use with the %s and %s commands.",
term.Example("info"), term.Example("view"))
expl := strings.Builder{}
example.Examples.String(&expl)
return &cobra.Command{
Use: "example",
Aliases: []string{"e", "sample", "s"},
GroupID: IDsample,
Short: s,
Long: l,
Example: expl.String(),
RunE: func(cmd *cobra.Command, _ []string) error </span><span class="cov0" title="0">{
return list.Examples(cmd.OutOrStdout())
}</span>,
}
}
func init() <span class="cov8" title="1">{
Cmd.AddCommand(ListExample())
}</span>
</pre>
<pre class="file" id="file3" style="display: none">// Package example provides help usage examples for the cmd package.
package example
import (
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
"strings"
"text/template"
"github.com/bengarrett/retrotxtgo/meta"
"github.com/bengarrett/retrotxtgo/term"
"github.com/gookit/color"
)
// Filenames is the placeholder for the filenames in the help usage examples.
const Filenames = "[filenames]"
// Example is the type for the help usage examples.
type Example int
const (
Cmd Example = iota // Cmd is the example for the root command.
Examples // Examples are the examples for the list examples command.
Table // Table are the examples for the list tables command.
Info // Info is the example for the info command.
View // View is the example for the view command.
Dump // Dump is the example for the dump command.
)
// Example string writes the example usage help.
func (e Example) String(w io.Writer) <span class="cov8" title="1">{
if w == nil </span><span class="cov0" title="0">{
w = io.Discard
}</span>
<span class="cov8" title="1">b := &bytes.Buffer{}
// change example operating system path separator
t := template.Must(template.New("example").Parse(e.result()))
err := t.Execute(b, string(os.PathSeparator))
if err != nil </span><span class="cov0" title="0">{
log.Fatal(err)
}</span>
// color the example text except text following
// the last hash #, which is treated as a comment
<span class="cov8" title="1">const cmmt, sentence = "#", 2
scanner := bufio.NewScanner(b)
cnt := 0
rows := len(strings.Split(b.String(), "\n"))
for scanner.Scan() </span><span class="cov8" title="1">{
cnt++
s := strings.Split(scanner.Text(), cmmt)
l := len(s)
if l < sentence </span><span class="cov8" title="1">{
fmt.Fprint(w, term.Info(scanner.Text()))
if cnt < rows </span><span class="cov8" title="1">{
fmt.Fprintln(w)
}</span>
<span class="cov8" title="1">continue</span>
}
<span class="cov8" title="1">if len(s) < 1 </span><span class="cov0" title="0">{
break</span>
}
// do not the last hash as a comment
<span class="cov8" title="1">ex := strings.Join(s[:l-1], cmmt)
fmt.Fprint(w, term.Info(ex))
fmt.Fprintf(w, "%s%s", color.Secondary.Sprint(cmmt), s[l-1])
if cnt < rows </span><span class="cov8" title="1">{
fmt.Fprintln(w)
}</span>
}
}
func (e Example) result() string <span class="cov8" title="1">{
switch e </span>{
case Cmd:<span class="cov8" title="1">
return cmd()</span>
case Examples:<span class="cov8" title="1">
return examples()</span>
case Table:<span class="cov8" title="1">
return table()</span>
case Info:<span class="cov8" title="1">
return info()</span>
case View:<span class="cov8" title="1">
return view()</span>
case Dump:<span class="cov0" title="0">
return dump()</span>
}
<span class="cov0" title="0">return ""</span>
}
func cmd() string <span class="cov8" title="1">{
s := &strings.Builder{}
fmt.Fprintf(s, " %s info %s\n", meta.Bin, Filenames)
fmt.Fprintf(s, " %s view %s\n", meta.Bin, Filenames)
fmt.Fprintf(s, " %s example\n\n", meta.Bin)
fmt.Fprintf(s, " %s list\n", meta.Bin)
fmt.Fprintf(s, " %s tables\n", meta.Bin)
fmt.Fprintf(s, " %s table [code page names or aliases]\n", meta.Bin)
fmt.Fprintf(s, " %s lang", meta.Bin)
return s.String()
}</span>
func examples() string <span class="cov8" title="1">{
s := &strings.Builder{}
fmt.Fprintf(s, " %s examples\t\t# list the builtin examples\n", meta.Bin)
fmt.Fprintf(s, " %s info ascii\t\t# information on the buildin ascii sample\n", meta.Bin)
fmt.Fprintf(s, " %s info ascii -f json\t# information in json format on the ascii sample\n", meta.Bin)
fmt.Fprintf(s, " %s view ascii\t\t# view the ascii example\n", meta.Bin)
fmt.Fprintf(s, " %s info ansi.rgb\t# information on the 24-bit color ansi example\n", meta.Bin)
fmt.Fprintf(s, " %s view ansi.rgb\t# view the 24-bit color ansi example", meta.Bin)
return s.String()
}</span>
func table() string <span class="cov8" title="1">{
s := &strings.Builder{}
fmt.Fprintf(s, " %s table cp437\n", meta.Bin)
fmt.Fprintf(s, " %s table cp437 latin1 windows-1252\n", meta.Bin)
fmt.Fprintf(s, " %s table iso-8859-15\n", meta.Bin)
fmt.Fprintf(s, " %s list\t# list the supported code page tables\n", meta.Bin)
return s.String()
}</span>
func info() string <span class="cov8" title="1">{
s := &strings.Builder{}
fmt.Fprintf(s, " %s info text.asc logo.jpg # print the information of multiple files\n", meta.Bin)
fmt.Fprintf(s, " %s info file.txt --format json # print the information using a structured syntax\n", meta.Bin)
return s.String()
}</span>
func view() string <span class="cov8" title="1">{
s := &strings.Builder{}
fmt.Fprintf(s, " %s view file.txt -i latin1\n", meta.Bin)
fmt.Fprintf(s, " %s view file1.txt file2.txt --input \"iso-8859-1\"\n", meta.Bin)
fmt.Fprintf(s, " cat file.txt | %s view", meta.Bin)
return s.String()
}</span>
func dump() string <span class="cov0" title="0">{
s := &strings.Builder{}
fmt.Fprintf(s, " %s dump file.txt\n", meta.Bin)
fmt.Fprintf(s, " %s dump file1.txt file2.txt\n", meta.Bin)
fmt.Fprintf(s, " cat file.txt | %s dump", meta.Bin)
return s.String()
}</span>
</pre>
<pre class="file" id="file4" style="display: none">package cmd
import (
"github.com/bengarrett/retrotxtgo/cmd/hexa"
"github.com/bengarrett/retrotxtgo/cmd/internal/flag"
"github.com/spf13/cobra"
)
func Dec() *cobra.Command <span class="cov8" title="1">{
s := "Conversion of decimal to hexadecimal numbers"
l := `Rudimentary decimal to hexadecimal conversions.
Convert one or a series of decimal numbers to their hexadecimal, base 16 values.
For example the number "255" is converted to "FF".
The number "9" is returned as "9".
No prefixes or leading characters are added to the hexadecimal numbers.
Negative signs should not be used as they could be interpreted as
a command flag.
`
return &cobra.Command{
Use: "dec",
Aliases: []string{"d"},
Short: s,
Long: l,
GroupID: IDcodepage,
Example: ` retrotxt dec 0 255 106 161`,
RunE: func(cmd *cobra.Command, args []string) error </span><span class="cov0" title="0">{
if len(args) == 0 </span><span class="cov0" title="0">{
return cmd.Help()
}</span>
<span class="cov0" title="0">const base = 10
if flag.Hex.Raw </span><span class="cov0" title="0">{
return hexa.Parser(cmd.OutOrStdout(), base, args...)
}</span>
<span class="cov0" title="0">return hexa.Writer(cmd.ErrOrStderr(), base, args...)</span>
},
}
}
func Hex() *cobra.Command <span class="cov8" title="1">{
s := "Conversion of hexadecimal to decimal numbers"
l := `Rudimentary hexadecimal to decimal conversions.
Convert one or a series of hexadecimal numbers to their decimal, base 10 values.
For example the hexadecimal number "FF" is converted to "255".
The hexadecimal number "55" is returned as "85".
The hexadecimal number "09" is converted to "9".
Common, case insensitive, hexadecimal prefixes are stripped.
- x prefix (x00)
- # hash prefix, found in CSS (#000)
- $ dollar prefix, found in retro microcomputers ($00)
- U+ unicode prefix, used in unicode (U+0000)
- 0x zero prefix, found in linux and unix C syntax (0x00)
- \x escape prefix, found in programming languages (\x00)
Numeric character reference (NCR) syntax is also supported.
- &#000; decimal NCR syntax
- &#x00; hexadecimal NCR syntax
Any signs are ignored. If a string is not a hexadecimal number then the
value is printed as "invalid".
`
return &cobra.Command{
Use: "hex",
Aliases: []string{"h", "x"},
Short: s,
Long: l,
GroupID: IDcodepage,
Example: ` retrotxt hex 0x00 xff U+006A a1`,
RunE: func(cmd *cobra.Command, args []string) error </span><span class="cov0" title="0">{
if len(args) == 0 </span><span class="cov0" title="0">{
return cmd.Help()
}</span>
<span class="cov0" title="0">const base = 16
if flag.Hex.Raw </span><span class="cov0" title="0">{
return hexa.Parser(cmd.OutOrStdout(), base, args...)
}</span>
<span class="cov0" title="0">return hexa.Writer(cmd.ErrOrStderr(), base, args...)</span>
},
}
}
func DecInit() *cobra.Command <span class="cov8" title="1">{
const s = "raw output only returns the space separated results"
d := Dec()
d.Flags().BoolVarP(&flag.Hex.Raw, "raw", "r", false, s)
return d
}</span>
func HexInit() *cobra.Command <span class="cov8" title="1">{
const s = "raw output only returns the space separated results"
h := Hex()
h.Flags().BoolVarP(&flag.Hex.Raw, "raw", "r", false, s)
return h
}</span>
func init() <span class="cov8" title="1">{
Cmd.AddCommand(DecInit(), HexInit())
}</span>
</pre>
<pre class="file" id="file5" style="display: none">// Package hexa provides rudimental hexadecimal conversion functions.
package hexa
import (
"fmt"
"io"
"math"
"strconv"
"strings"
)
// Base is the number base system for the conversion.
type Base int
const (
Base10 Base = 10 // Base10 is the decimal number base system.
Base16 Base = 16 // Base16 is the hexadecimal number base system.
)
// Hexadecimal prefix identifiers.
const (
X = "X" // common
Hash = "#" // cascading style sheets colors
Doll = "$" // retro microcomputers
U = "U+" // unicode
Zero = "0X" // linux and unix C syntax
Esc = "\\X" // escape
)
// TrimIdent trims the hexadecimal prefix identifiers and
// returns the hexadecimal value as an upper case string.
func TrimIdent(s string) string <span class="cov8" title="1">{
s = strings.ToUpper(s)
s = strings.TrimPrefix(s, X)
s = strings.TrimPrefix(s, Hash)
s = strings.TrimPrefix(s, Doll)
s = strings.TrimPrefix(s, U)
s = strings.TrimPrefix(s, Zero)
s = strings.TrimPrefix(s, Esc)
return s
}</span>
// TrimNCR trims the numeric character reference prefix and suffix,
// and returns a decimal integer as a string.
// If the string is not in NCR syntax, then the original string is returned.
func TrimNCR(s string) string <span class="cov8" title="1">{
const dec, suffix = "&#", ";"
x := strings.ToUpper(s)
if !strings.HasPrefix(x, dec) || !strings.HasSuffix(x, suffix) </span><span class="cov8" title="1">{
return s
}</span>
<span class="cov8" title="1">x = strings.TrimPrefix(x, dec)
x = strings.TrimSuffix(x, suffix)
if x == "" </span><span class="cov0" title="0">{
return s
}</span>
<span class="cov8" title="1">return x</span>
}
// TrimIndents trims the hexadecimal prefix identifiers and
// returns the hexadecimal values as upper case strings.
func TrimIndents(vals ...string) []string <span class="cov8" title="1">{
s := make([]string, len(vals))
for i, val := range vals </span><span class="cov8" title="1">{
s[i] = TrimIdent(val)
}</span>
<span class="cov8" title="1">for i, val := range s </span><span class="cov8" title="1">{
s[i] = TrimNCR(val)
}</span>
<span class="cov8" title="1">return s</span>
}
// Parse converts the provided strings to based unsigned ints.
// If a string is not a valid integer then the value is -1.
func Parse(b Base, vals ...string) []int64 <span class="cov8" title="1">{
n := make([]int64, len(vals))
for i, val := range vals </span><span class="cov8" title="1">{
x, err := strconv.ParseInt(val, int(b), 64)
if err != nil </span><span class="cov8" title="1">{
n[i] = -1
continue</span>
}
<span class="cov8" title="1">x = int64(math.Abs(float64(x))) // remove sign
n[i] = x</span>
}
<span class="cov8" title="1">return n</span>
}
// Parser writes the hexadecimal values of the provided decimal strings.
// Only the results are written and are separated by a space.
// If a string is not a hexadecimal number then the value is printed as "NaN".
func Parser(w io.Writer, b Base, vals ...string) error <span class="cov8" title="1">{
if w == nil </span><span class="cov8" title="1">{
w = io.Discard
}</span>
<span class="cov8" title="1">const pad = " "
sb := &strings.Builder{}
nums := []int64{}
switch b </span>{
case Base10:<span class="cov8" title="1">
nums = Parse(b, vals...)</span>
case Base16:<span class="cov8" title="1">
nums = Parse(b, TrimIndents(vals...)...)</span>
}
<span class="cov8" title="1">for _, x := range nums </span><span class="cov8" title="1">{
if x == -1 </span><span class="cov8" title="1">{
fmt.Fprintf(sb, "NaN%s", pad)
continue</span>
}
<span class="cov8" title="1">switch b </span>{
case Base10:<span class="cov8" title="1">
fmt.Fprintf(sb, "%X%s", x, pad)</span>
case Base16:<span class="cov8" title="1">
fmt.Fprintf(sb, "%d%s", x, pad)</span>
}
}
<span class="cov8" title="1">fmt.Fprint(w, strings.TrimSpace(sb.String()))
fmt.Fprintln(w)
return nil</span>
}
// Writer the hexadecimal values of the provided decimal strings.
// If a string is not a hexadecimal number then the value is printed as "invalid".
func Writer(w io.Writer, b Base, vals ...string) error <span class="cov8" title="1">{
if w == nil </span><span class="cov8" title="1">{
w = io.Discard
}</span>
<span class="cov8" title="1">const pad = " "
sb := &strings.Builder{}
nums := []int64{}
switch b </span>{
case Base10:<span class="cov8" title="1">
nums = Parse(b, vals...)</span>
case Base16:<span class="cov8" title="1">
nums = Parse(b, TrimIndents(vals...)...)</span>
}
<span class="cov8" title="1">for i, x := range nums </span><span class="cov8" title="1">{
if i >= len(vals) </span><span class="cov0" title="0">{
break</span>
}
<span class="cov8" title="1">s := strings.ToUpper(vals[i])
if x == -1 </span><span class="cov8" title="1">{
fmt.Fprintf(sb, "%s = invalid%s", s, pad)
continue</span>
}
<span class="cov8" title="1">switch b </span>{
case Base10:<span class="cov8" title="1">
fmt.Fprintf(sb, "%s = %X%s", s, x, pad)</span>
case Base16:<span class="cov8" title="1">
fmt.Fprintf(sb, "%s = %d%s", s, x, pad)</span>
}
}
<span class="cov8" title="1">fmt.Fprint(w, strings.TrimSpace(sb.String()))
fmt.Fprintln(w)
return nil</span>
}
</pre>
<pre class="file" id="file6" style="display: none">package cmd
import (
"strings"
"github.com/bengarrett/retrotxtgo/cmd/example"
"github.com/bengarrett/retrotxtgo/cmd/internal/flag"
"github.com/bengarrett/retrotxtgo/cmd/internal/format"
"github.com/bengarrett/retrotxtgo/cmd/internal/info"
"github.com/bengarrett/retrotxtgo/term"
"github.com/spf13/cobra"
)
const infoLong = `Discover details and information about any text or text art file.
The info command will return the following information about a text file:
- slug A URL friendly version of the filename.
- filename The filename.
- filetype The file type or function, such as plain text file.
- Unicode Whether the file is readable as Unicode.
- line break The line break type in use, such as CRLF.
- characters The number of characters in the file.
- words The number of words in the file.
- size The file size in a human readable format.
- lines The number of lines in the file determined by the line breaks.
- width The widest line in the file.
- modified The date and time the file was last modified.
- media type The IANA media type, such as text/plain.
- SHA256 check The SHA256 integrity checksum of the file.
- CRC64 The cyclic redundancy check of the file.
- CRC32 The cyclic redundancy check of the file.
- MD5 The MD5 hash of the file.
Art scene files embedded with SAUCE metadata will also return the
following information:
- title The title of the file.
- author The nickname or handle of the file creator.
- group The group or company of the author.
- date The date the file was created.
- original size The size of the file without the SAUCE metadata.
- file type The specific type of the file, such as PNG image.
- data type The media type of the file, such as a bitmap image.
- description The description of file and data type.
- character width The number of characters per line.
- number of lines The number of lines in the file.
- interpretation Additional information about the file types.
- comments Additional comments by the author.
The full SAUCE specification can be found at:
https://www.acid.org/info/sauce/sauce.htm`
func InfoCommand() *cobra.Command <span class="cov8" title="1">{
s := "Information on text files"
expl := strings.Builder{}
example.Info.String(&expl)
return &cobra.Command{
Use: "info " + example.Filenames,
Aliases: []string{"i"},
GroupID: IDfile,
Short: s,
Long: infoLong,
Example: expl.String(),
RunE: func(cmd *cobra.Command, args []string) error </span><span class="cov8" title="1">{
return info.Run(cmd.OutOrStdout(), cmd, args...)
}</span>,
}
}
func InfoInit() *cobra.Command <span class="cov8" title="1">{
ic := InfoCommand()
s := &strings.Builder{}
infos := format.Format().Info
term.Options(s, "print format or syntax", true, true, infos[:]...)
ic.Flags().StringVarP(&flag.Info.Format, "format", "f", "color", s.String())
ic.Flags().BoolVarP(&flag.Info.Checksum, "checksum", "c", false,
"also include redundant checksums such as MD5 and CRC")
return ic
}</span>
func init() <span class="cov8" title="1">{
Cmd.AddCommand(InfoInit())
}</span>
</pre>
<pre class="file" id="file7" style="display: none">package dump
import (
"encoding/hex"
"errors"
"fmt"
"io"
"github.com/bengarrett/retrotxtgo/cmd/internal/flag"
"github.com/bengarrett/retrotxtgo/fsys"
"github.com/bengarrett/retrotxtgo/term"
"github.com/spf13/cobra"
)
var ErrPipeRead = errors.New("could not read text stream from piped stdin (standard input)")
// Run parses the arguments supplied with the view command.
func Run(w io.Writer, cmd *cobra.Command, args ...string) error <span class="cov0" title="0">{
if w == nil </span><span class="cov0" title="0">{
w = io.Discard
}</span>
// piped input from other programs and then exit
<span class="cov0" title="0">ok, err := fsys.IsPipe()
if err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov0" title="0">if ok </span><span class="cov0" title="0">{
return Pipe(w)
}</span>
// read from files or samples
<span class="cov0" title="0">args, c, samp, err := flag.Args(cmd, args...)
if err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov0" title="0">for i, arg := range args </span><span class="cov0" title="0">{
if i == 0 && arg == "" </span><span class="cov0" title="0">{
return nil
}</span>
<span class="cov0" title="0">if i > 0 && i < len(arg) </span><span class="cov0" title="0">{
const page = 76
term.HR(w, page)
}</span>
<span class="cov0" title="0">b, err := flag.ReadArgument(arg, c, samp)
if err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov0" title="0">fmt.Fprint(w, hex.Dump(b))</span>
}
<span class="cov0" title="0">return nil</span>
}
// Pipe parses a standard input (stdin) stream of data.
func Pipe(w io.Writer) error <span class="cov8" title="1">{
if w == nil </span><span class="cov8" title="1">{
w = io.Discard
}</span>
<span class="cov8" title="1">data, err := fsys.ReadPipe()
if err != nil </span><span class="cov8" title="1">{
return fmt.Errorf("%w, %w", ErrPipeRead, err)
}</span>
<span class="cov0" title="0">fmt.Fprint(w, hex.Dump(data))
return nil</span>
}
</pre>
<pre class="file" id="file8" style="display: none">// Package flag provides the command flags handlers.
package flag
import (
"errors"
"fmt"
"os"
"slices"
"strconv"
"strings"