@softwarefactory-project/re-ansi 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -7,9 +7,11 @@ Ansi code to HTML
7
7
  re-ansi supports:
8
8
 
9
9
  * Cursor movement: `\n`, line erase and `\r` line erase
10
- * SGR parameter: italic and bold
10
+ * SGR parameter: italic, bold, underline and line-through
11
11
  * 3/4 bits [color code][ansi-color-code]
12
12
 
13
+ re-ansi removes styles on new lines, so that a `<br />` can't be nested inside `<span style>` elements.
14
+
13
15
  For details, see the `AnsiCode.parse` function.
14
16
 
15
17
  ## Install
@@ -79,27 +81,36 @@ Make sure to read about [React][reason-react] and [Reason][rescript-lang] too.
79
81
 
80
82
  ## Changes
81
83
 
84
+ ### 0.6.0
85
+
86
+ - Fix support for bright colors.
87
+
88
+ ### 0.5.0
89
+
90
+ - Add support for `[m` and `[K`.
91
+
82
92
  ### 0.4.0
83
93
 
84
- - Add support for underline and line-through text decoration
85
- - Add support for lighter font style
94
+ - Add support for underline and line-through text decoration.
95
+ - Add support for lighter font style.
96
+ - Add initial support for color reset.
86
97
 
87
98
  ### 0.3.0
88
99
 
89
- - Add unique keys to react list elements
90
- - Create a href elements for http links
100
+ - Add unique keys to react list elements.
101
+ - Create a href elements for http links.
91
102
 
92
103
  ### 0.2.1
93
104
 
94
- - Fix Ansi.parse to return the full document instead of the last line
105
+ - Fix Ansi.parse to return the full document instead of the last line.
95
106
 
96
107
  ### 0.2.0
97
108
 
98
- - Fix a recursion limit in Ansi.parse for log bigger than 10MB
109
+ - Fix a recursion limit in Ansi.parse for log bigger than 10MB.
99
110
 
100
111
  ### 0.1.3
101
112
 
102
- - Initial release
113
+ - Initial release.
103
114
 
104
115
  [ansi-color-code]: https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit
105
116
  [reason-react]: https://reasonml.github.io/reason-react/docs/en/components
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softwarefactory-project/re-ansi",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "ANSI code to HTML",
5
5
  "files": ["README.md", "LICENSE", "bsconfig.json", "src"],
6
6
  "main": "./src/Ansi.bs.js",
package/src/Ansi.bs.js CHANGED
@@ -50,6 +50,30 @@ function fourBitColors(code) {
50
50
  }
51
51
  }
52
52
 
53
+ function threeBitColors(code) {
54
+ switch (code) {
55
+ case 0 :
56
+ return "grey";
57
+ case 1 :
58
+ return "red";
59
+ case 2 :
60
+ return "green";
61
+ case 3 :
62
+ return "yellow";
63
+ case 4 :
64
+ return "blue";
65
+ case 5 :
66
+ return "magenta";
67
+ case 6 :
68
+ return "cyan";
69
+ case 7 :
70
+ return "white";
71
+ default:
72
+ console.log("Unknown color value:", code);
73
+ return ;
74
+ }
75
+ }
76
+
53
77
  function combine(css1, css2) {
54
78
  return Object.assign({}, css1, css2);
55
79
  }
@@ -78,10 +102,15 @@ function int_of_cp(c) {
78
102
 
79
103
  function getColorStyle(colorMode, colorValue) {
80
104
  switch (colorMode) {
105
+ case 3 :
106
+ return {
107
+ TAG: /* Foreground */0,
108
+ _0: colorValue
109
+ };
81
110
  case 0 :
82
111
  case 4 :
83
112
  return {
84
- TAG: /* Background */1,
113
+ TAG: /* Background */2,
85
114
  _0: colorValue
86
115
  };
87
116
  case 1 :
@@ -91,10 +120,9 @@ function getColorStyle(colorMode, colorValue) {
91
120
  case 7 :
92
121
  case 8 :
93
122
  break;
94
- case 3 :
95
123
  case 9 :
96
124
  return {
97
- TAG: /* Foreground */0,
125
+ TAG: /* BrightForeground */1,
98
126
  _0: colorValue
99
127
  };
100
128
  default:
@@ -105,18 +133,27 @@ function getColorStyle(colorMode, colorValue) {
105
133
  }
106
134
 
107
135
  function getColorStyleCss(color) {
108
- if (color.TAG === /* Foreground */0) {
109
- return Belt_Option.flatMap(fourBitColors(color._0), (function (color) {
110
- return {
111
- color: color
112
- };
113
- }));
114
- } else {
115
- return Belt_Option.flatMap(fourBitColors(color._0), (function (background) {
116
- return {
117
- background: background
118
- };
119
- }));
136
+ switch (color.TAG | 0) {
137
+ case /* Foreground */0 :
138
+ return Belt_Option.flatMap(fourBitColors(color._0), (function (color) {
139
+ return {
140
+ color: color
141
+ };
142
+ }));
143
+ case /* BrightForeground */1 :
144
+ return Belt_Option.flatMap(threeBitColors(color._0), (function (color) {
145
+ return {
146
+ color: color,
147
+ fontWeight: "bold"
148
+ };
149
+ }));
150
+ case /* Background */2 :
151
+ return Belt_Option.flatMap(fourBitColors(color._0), (function (background) {
152
+ return {
153
+ background: background
154
+ };
155
+ }));
156
+
120
157
  }
121
158
  }
122
159
 
@@ -296,138 +333,154 @@ function parse(txt, pos) {
296
333
  var match$2 = codePoints.tl;
297
334
  if (!match$2) {
298
335
  return [
299
- 1,
300
- undefined
336
+ length,
337
+ /* Clear */0
301
338
  ];
302
339
  }
303
340
  var style$2 = match$2.hd;
304
341
  var exit$2 = 0;
305
- if (style$2 !== 48) {
306
- if (style$2 !== 49) {
307
- exit$2 = 5;
308
- } else {
309
- var match$3 = match$2.tl;
310
- if (match$3) {
311
- var match$4 = match$3.tl;
312
- if (match$4 && !match$4.tl) {
313
- colorMode = match$3.hd;
314
- colorValue = match$4.hd;
315
- xs = codePoints;
316
- exit$1 = 2;
342
+ var exit$3 = 0;
343
+ var match$3 = match$2.tl;
344
+ if (match$3) {
345
+ if (match$3.hd === 75) {
346
+ return [
347
+ 4,
348
+ /* EraseLine */1
349
+ ];
350
+ }
351
+ exit$3 = 6;
352
+ } else {
353
+ exit$3 = 6;
354
+ }
355
+ if (exit$3 === 6) {
356
+ var switcher = style$2 - 48 | 0;
357
+ if (switcher === 0 || switcher === 1) {
358
+ if (switcher !== 0) {
359
+ var match$4 = match$2.tl;
360
+ if (match$4) {
361
+ var match$5 = match$4.tl;
362
+ if (match$5 && !match$5.tl) {
363
+ colorMode = match$4.hd;
364
+ colorValue = match$5.hd;
365
+ xs = codePoints;
366
+ exit$1 = 2;
367
+ } else {
368
+ exit$2 = 5;
369
+ }
317
370
  } else {
318
371
  exit$2 = 5;
319
372
  }
320
373
  } else {
321
- exit$2 = 5;
322
- }
323
- }
324
- } else {
325
- var match$5 = match$2.tl;
326
- if (!match$5) {
327
- return [
328
- length,
329
- /* Clear */0
330
- ];
331
- }
332
- var style$3 = match$5.hd;
333
- var exit$3 = 0;
334
- if (style$3 !== 48) {
335
- if (style$3 === 75) {
336
- return [
337
- 4,
338
- /* EraseLine */1
339
- ];
340
- }
341
- exit$3 = 6;
342
- } else {
343
- if (!match$5.tl) {
344
- return [
345
- length,
346
- /* Clear */0
347
- ];
348
- }
349
- exit$3 = 6;
350
- }
351
- if (exit$3 === 6) {
352
- var match$6 = match$5.tl;
353
- if (match$6 && match$6.hd === 59) {
354
- var match$7 = match$6.tl;
355
- if (!match$7) {
374
+ var match$6 = match$2.tl;
375
+ if (!match$6) {
356
376
  return [
357
- 1,
358
- undefined
377
+ length,
378
+ /* Clear */0
359
379
  ];
360
380
  }
361
- var match$8 = match$7.tl;
362
- if (match$8) {
363
- var match$9 = match$8.tl;
364
- var colorValue$2 = match$8.hd;
365
- var colorMode$2 = match$7.hd;
366
- if (match$9) {
367
- if (match$9.hd !== 59) {
368
- exit$2 = 5;
369
- } else {
381
+ var style$3 = match$6.hd;
382
+ var exit$4 = 0;
383
+ if (style$3 !== 48) {
384
+ exit$4 = 7;
385
+ } else {
386
+ if (!match$6.tl) {
387
+ return [
388
+ length,
389
+ /* Clear */0
390
+ ];
391
+ }
392
+ exit$4 = 7;
393
+ }
394
+ if (exit$4 === 7) {
395
+ var match$7 = match$6.tl;
396
+ if (match$7 && match$7.hd === 59) {
397
+ var match$8 = match$7.tl;
398
+ if (!match$8) {
399
+ return [
400
+ 1,
401
+ undefined
402
+ ];
403
+ }
404
+ var match$9 = match$8.tl;
405
+ if (match$9) {
370
406
  var match$10 = match$9.tl;
371
- if (!match$10) {
372
- return [
373
- 1,
374
- undefined
375
- ];
376
- }
377
- var cm2$1 = match$10.hd;
378
- var match$11 = match$10.tl;
379
- if (match$11) {
380
- if (match$11.tl) {
381
- if (cm2$1 !== 49) {
382
- exit$2 = 5;
383
- } else {
384
- var match$12 = match$10.tl;
385
- var match$13 = match$12.tl;
386
- if (match$13.tl) {
387
- return [
388
- 1,
389
- undefined
390
- ];
407
+ var colorValue$2 = match$9.hd;
408
+ var colorMode$2 = match$8.hd;
409
+ if (match$10) {
410
+ if (match$10.hd !== 59) {
411
+ exit$2 = 5;
412
+ } else {
413
+ var match$11 = match$10.tl;
414
+ if (!match$11) {
415
+ return [
416
+ 1,
417
+ undefined
418
+ ];
419
+ }
420
+ var cm2$1 = match$11.hd;
421
+ var match$12 = match$11.tl;
422
+ if (match$12) {
423
+ if (match$12.tl) {
424
+ if (cm2$1 !== 49) {
425
+ exit$2 = 5;
426
+ } else {
427
+ var match$13 = match$11.tl;
428
+ var match$14 = match$13.tl;
429
+ if (match$14.tl) {
430
+ return [
431
+ 1,
432
+ undefined
433
+ ];
434
+ }
435
+ style$1 = style$3;
436
+ cm1 = colorMode$2;
437
+ cv1 = colorValue$2;
438
+ cm2 = match$13.hd;
439
+ cv2 = match$14.hd;
440
+ xs$1 = codePoints;
441
+ exit$1 = 4;
442
+ }
443
+ } else {
444
+ style$1 = style$3;
445
+ cm1 = colorMode$2;
446
+ cv1 = colorValue$2;
447
+ cm2 = cm2$1;
448
+ cv2 = match$12.hd;
449
+ xs$1 = codePoints;
450
+ exit$1 = 4;
391
451
  }
392
- style$1 = style$3;
393
- cm1 = colorMode$2;
394
- cv1 = colorValue$2;
395
- cm2 = match$12.hd;
396
- cv2 = match$13.hd;
397
- xs$1 = codePoints;
398
- exit$1 = 4;
452
+ } else {
453
+ exit$2 = 5;
399
454
  }
400
- } else {
401
- style$1 = style$3;
402
- cm1 = colorMode$2;
403
- cv1 = colorValue$2;
404
- cm2 = cm2$1;
405
- cv2 = match$11.hd;
406
- xs$1 = codePoints;
407
- exit$1 = 4;
408
455
  }
409
456
  } else {
410
- exit$2 = 5;
457
+ colorMode$1 = colorMode$2;
458
+ colorValue$1 = colorValue$2;
459
+ style = style$3;
460
+ exit$1 = 3;
411
461
  }
462
+ } else {
463
+ exit$2 = 5;
412
464
  }
413
465
  } else {
414
- colorMode$1 = colorMode$2;
415
- colorValue$1 = colorValue$2;
416
- style = style$3;
417
- exit$1 = 3;
466
+ exit$2 = 5;
418
467
  }
419
- } else {
420
- exit$2 = 5;
421
468
  }
422
- } else {
423
- exit$2 = 5;
469
+
424
470
  }
471
+ } else {
472
+ if (switcher === 27) {
473
+ return [
474
+ 3,
475
+ /* EraseLine */1
476
+ ];
477
+ }
478
+ exit$2 = 5;
425
479
  }
426
-
427
480
  }
428
481
  if (exit$2 === 5) {
429
- var match$14 = match$2.tl;
430
- if (!match$14) {
482
+ var match$15 = match$2.tl;
483
+ if (!match$15) {
431
484
  return [
432
485
  length,
433
486
  Belt_Option.flatMap(get$1(style$2), (function (style) {
@@ -438,26 +491,26 @@ function parse(txt, pos) {
438
491
  }))
439
492
  ];
440
493
  }
441
- var colorValue$3 = match$14.hd;
442
- var exit$4 = 0;
443
- var match$15 = match$14.tl;
444
- if (match$15) {
445
- if (match$15.hd !== 59) {
446
- exit$4 = 6;
494
+ var colorValue$3 = match$15.hd;
495
+ var exit$5 = 0;
496
+ var match$16 = match$15.tl;
497
+ if (match$16) {
498
+ if (match$16.hd !== 59) {
499
+ exit$5 = 6;
447
500
  } else {
448
- var match$16 = match$15.tl;
449
- if (!match$16) {
501
+ var match$17 = match$16.tl;
502
+ if (!match$17) {
450
503
  return [
451
504
  1,
452
505
  undefined
453
506
  ];
454
507
  }
455
- if (match$16.tl) {
456
- exit$4 = 6;
508
+ if (match$17.tl) {
509
+ exit$5 = 6;
457
510
  } else {
458
511
  colorMode$1 = style$2;
459
512
  colorValue$1 = colorValue$3;
460
- style = match$16.hd;
513
+ style = match$17.hd;
461
514
  exit$1 = 3;
462
515
  }
463
516
  }
@@ -467,56 +520,56 @@ function parse(txt, pos) {
467
520
  xs = codePoints;
468
521
  exit$1 = 2;
469
522
  }
470
- if (exit$4 === 6) {
523
+ if (exit$5 === 6) {
471
524
  if (colorValue$3 !== 59) {
472
525
  return [
473
526
  1,
474
527
  undefined
475
528
  ];
476
529
  }
477
- var match$17 = match$14.tl;
478
- var match$18 = match$17.tl;
479
- if (!match$18) {
530
+ var match$18 = match$15.tl;
531
+ var match$19 = match$18.tl;
532
+ if (!match$19) {
480
533
  return [
481
534
  1,
482
535
  undefined
483
536
  ];
484
537
  }
485
- var match$19 = match$18.tl;
486
- var colorValue$4 = match$18.hd;
487
- var colorMode$3 = match$17.hd;
488
- if (match$19) {
489
- if (match$19.hd !== 59) {
538
+ var match$20 = match$19.tl;
539
+ var colorValue$4 = match$19.hd;
540
+ var colorMode$3 = match$18.hd;
541
+ if (match$20) {
542
+ if (match$20.hd !== 59) {
490
543
  return [
491
544
  1,
492
545
  undefined
493
546
  ];
494
547
  }
495
- var match$20 = match$19.tl;
496
- if (!match$20) {
548
+ var match$21 = match$20.tl;
549
+ if (!match$21) {
497
550
  return [
498
551
  1,
499
552
  undefined
500
553
  ];
501
554
  }
502
- var cm2$2 = match$20.hd;
503
- var match$21 = match$20.tl;
504
- if (!match$21) {
555
+ var cm2$2 = match$21.hd;
556
+ var match$22 = match$21.tl;
557
+ if (!match$22) {
505
558
  return [
506
559
  1,
507
560
  undefined
508
561
  ];
509
562
  }
510
- if (match$21.tl) {
563
+ if (match$22.tl) {
511
564
  if (cm2$2 !== 49) {
512
565
  return [
513
566
  1,
514
567
  undefined
515
568
  ];
516
569
  }
517
- var match$22 = match$20.tl;
518
- var match$23 = match$22.tl;
519
- if (match$23.tl) {
570
+ var match$23 = match$21.tl;
571
+ var match$24 = match$23.tl;
572
+ if (match$24.tl) {
520
573
  return [
521
574
  1,
522
575
  undefined
@@ -525,8 +578,8 @@ function parse(txt, pos) {
525
578
  style$1 = style$2;
526
579
  cm1 = colorMode$3;
527
580
  cv1 = colorValue$4;
528
- cm2 = match$22.hd;
529
- cv2 = match$23.hd;
581
+ cm2 = match$23.hd;
582
+ cv2 = match$24.hd;
530
583
  xs$1 = codePoints;
531
584
  exit$1 = 4;
532
585
  } else {
@@ -534,7 +587,7 @@ function parse(txt, pos) {
534
587
  cm1 = colorMode$3;
535
588
  cv1 = colorValue$4;
536
589
  cm2 = cm2$2;
537
- cv2 = match$21.hd;
590
+ cv2 = match$22.hd;
538
591
  xs$1 = codePoints;
539
592
  exit$1 = 4;
540
593
  }
@@ -549,27 +602,14 @@ function parse(txt, pos) {
549
602
  }
550
603
 
551
604
  } else {
552
- var match$24 = codePoints.tl;
553
- if (!match$24) {
554
- return [
555
- 1,
556
- undefined
557
- ];
558
- }
559
- if (match$24.hd !== 27) {
560
- return [
561
- 1,
562
- undefined
563
- ];
564
- }
565
- var match$25 = match$24.tl;
605
+ var match$25 = codePoints.tl;
566
606
  if (!match$25) {
567
607
  return [
568
608
  1,
569
609
  undefined
570
610
  ];
571
611
  }
572
- if (match$25.hd !== 91) {
612
+ if (match$25.hd !== 27) {
573
613
  return [
574
614
  1,
575
615
  undefined
@@ -582,7 +622,7 @@ function parse(txt, pos) {
582
622
  undefined
583
623
  ];
584
624
  }
585
- if (match$26.hd !== 49) {
625
+ if (match$26.hd !== 91) {
586
626
  return [
587
627
  1,
588
628
  undefined
@@ -595,7 +635,7 @@ function parse(txt, pos) {
595
635
  undefined
596
636
  ];
597
637
  }
598
- if (match$27.hd !== 65) {
638
+ if (match$27.hd !== 49) {
599
639
  return [
600
640
  1,
601
641
  undefined
@@ -608,7 +648,7 @@ function parse(txt, pos) {
608
648
  undefined
609
649
  ];
610
650
  }
611
- if (match$28.hd !== 27) {
651
+ if (match$28.hd !== 65) {
612
652
  return [
613
653
  1,
614
654
  undefined
@@ -621,14 +661,27 @@ function parse(txt, pos) {
621
661
  undefined
622
662
  ];
623
663
  }
624
- if (match$29.hd !== 91) {
664
+ if (match$29.hd !== 27) {
625
665
  return [
626
666
  1,
627
667
  undefined
628
668
  ];
629
669
  }
630
670
  var match$30 = match$29.tl;
631
- if (match$30 && match$30.hd === 74) {
671
+ if (!match$30) {
672
+ return [
673
+ 1,
674
+ undefined
675
+ ];
676
+ }
677
+ if (match$30.hd !== 91) {
678
+ return [
679
+ 1,
680
+ undefined
681
+ ];
682
+ }
683
+ var match$31 = match$30.tl;
684
+ if (match$31 && match$31.hd === 74) {
632
685
  return [
633
686
  9,
634
687
  /* EraseLine */1
@@ -702,6 +755,7 @@ function parse(txt, pos) {
702
755
 
703
756
  var AnsiCode = {
704
757
  fourBitColors: fourBitColors,
758
+ threeBitColors: threeBitColors,
705
759
  combine: combine,
706
760
  addWeight: addWeight,
707
761
  addStyle: addStyle,
@@ -771,7 +825,7 @@ function parse$1(txt, length, pos) {
771
825
 
772
826
  }
773
827
  } else {
774
- if (code.TAG === /* HRef */0) {
828
+ if (!code.TAG) {
775
829
  return [
776
830
  pos$1,
777
831
  {
package/src/Ansi.re CHANGED
@@ -59,6 +59,21 @@ module AnsiCode = {
59
59
  None;
60
60
  };
61
61
 
62
+ let threeBitColors = (code: int): option(string) =>
63
+ switch (code) {
64
+ | 00 => "grey"->Some
65
+ | 01 => "red"->Some
66
+ | 02 => "green"->Some
67
+ | 03 => "yellow"->Some
68
+ | 04 => "blue"->Some
69
+ | 05 => "magenta"->Some
70
+ | 06 => "cyan"->Some
71
+ | 07 => "white"->Some
72
+ | _ =>
73
+ Js.log2("Unknown color value:", code);
74
+ None;
75
+ };
76
+
62
77
  // Css utility
63
78
  let combine = (css1, css2) => ReactDOM.Style.combine(css1, css2);
64
79
  let addWeight = fontWeight => ReactDOM.Style.make(~fontWeight, ());
@@ -70,11 +85,12 @@ module AnsiCode = {
70
85
  module ColorCss = {
71
86
  type t =
72
87
  | Foreground(int)
88
+ | BrightForeground(int)
73
89
  | Background(int);
74
90
  let getColorStyle = (colorMode: int, colorValue: int): option(t) =>
75
91
  switch (colorMode) {
76
- | 3
77
- | 9 => colorValue->Foreground->Some
92
+ | 3 => colorValue->Foreground->Some
93
+ | 9 => colorValue->BrightForeground->Some
78
94
  | 4
79
95
  | 0 => colorValue->Background->Some
80
96
  | _ =>
@@ -88,6 +104,10 @@ module AnsiCode = {
88
104
  v
89
105
  ->fourBitColors
90
106
  ->Option.flatMap(color => ReactDOM.Style.make(~color, ())->Some)
107
+ | BrightForeground(v) =>
108
+ v
109
+ ->threeBitColors
110
+ ->Option.flatMap(color => ReactDOM.Style.make(~color, ~fontWeight="bold", ())->Some)
91
111
  | Background(v) =>
92
112
  v
93
113
  ->fourBitColors
@@ -174,12 +194,16 @@ module AnsiCode = {
174
194
  switch (codePoints) {
175
195
  // \n\x0d[1A\x0d[J
176
196
  | [10, 27, 91, 49, 65, 27, 91, 74, ..._] => (9, EraseLine->Some)
177
- // [1K
178
- | [91, 48, 75, ..._] => (4, EraseLine->Some)
197
+ // [_K
198
+ | [91, _, 75, ..._] => (4, EraseLine->Some)
199
+ // [K
200
+ | [91, 75, ..._] => (3, EraseLine->Some)
179
201
  // [00m
180
202
  | [91, 48, 48]
181
203
  // [0m
182
- | [91, 48] => (length, Clear->Some)
204
+ | [91, 48]
205
+ // [m
206
+ | [91] => (length, Clear->Some)
183
207
  // [_m
184
208
  | [91, style] => (
185
209
  length,
package/src/old.js ADDED
File without changes