Browse code

Add colors

Robert Cranston authored on 11/01/2023 14:29:21
Showing 2 changed files

... ...
@@ -148,6 +148,14 @@ Environment variables:
148 148
     is not defined, `pager` if it exists in `$PATH`, else `less` . The value is
149 149
     interpreted by the shell. If the `$LESS` environment variable is unset, it
150 150
     is set to `FR`.
151
+  GLREGISTRY_COLORS
152
+    If standard out is a terminal, the colors used in output of the `enums`,
153
+    `enums-tree`, `params`, `params-tree`, `audit,` and `audit-tree` commands.
154
+    It uses the same format (and defaults) as GREP_COLORS, i.e. a
155
+    colon-separated list of capabilties: `ms` (matching selected), `fn` (file
156
+    name), `ln` (line number), `se` (separators). Added custom capabilities
157
+    are: `ve` (version), `ex` (extension), `un` (unsupported). Defaults to
158
+    `ms=01;31:fn=35:ln=32:se=36:ve=01;34:ex=34:un=01;33`.
151 159
 ```
152 160
 
153 161
 ## References
... ...
@@ -105,6 +105,14 @@ Environment variables:
105 105
     is not defined, `pager` if it exists in `$PATH`, else `less` . The value is
106 106
     interpreted by the shell. If the `$LESS` environment variable is unset, it
107 107
     is set to `FR`.
108
+  GLREGISTRY_COLORS
109
+    If standard out is a terminal, the colors used in output of the `enums`,
110
+    `enums-tree`, `params`, `params-tree`, `audit,` and `audit-tree` commands.
111
+    It uses the same format (and defaults) as GREP_COLORS, i.e. a
112
+    colon-separated list of capabilties: `ms` (matching selected), `fn` (file
113
+    name), `ln` (line number), `se` (separators). Added custom capabilities
114
+    are: `ve` (version), `ex` (extension), `un` (unsupported). Defaults to
115
+    `ms=01;31:fn=35:ln=32:se=36:ve=01;34:ex=34:un=01;33`.
108 116
 """
109 117
 
110 118
 
... ...
@@ -150,6 +158,20 @@ CACHE  = ENV_XDG('CACHE',  os.path.join('~', '.cache'))
150 158
 EDITOR = ENV_PRG('EDITOR', 'vi')
151 159
 PAGER  = ENV_PRG('PAGER',  'less')
152 160
 LESS   = os.environ.get('LESS') or 'FR'
161
+COLORS = collections.defaultdict(str, [
162
+    (color.split('=') + [''])[:2]
163
+    for color in
164
+    filter(None, (
165
+        os.environ.get('GLREGISTRY_COLORS') or
166
+        (lambda x, y: ':'.join([x, x and y]))(
167
+            (
168
+                os.environ.get('GREP_COLORS') or
169
+                'ms=01;31:fn=35:ln=32:se=36'
170
+            ),
171
+            've=01;34:ex=34:un=01;33',
172
+        )
173
+    ).split(':'))
174
+])
153 175
 IN    = lambda a, v, s: f"contains(concat('{s}',@{a},'{s}'),'{s}{v}{s}')"
154 176
 MAYBE = lambda a, v:    f"(@{a}='{v}' or not(@{a}))"
155 177
 TYPES    = "/registry/types"
... ...
@@ -208,9 +230,27 @@ def page(lines):
208 230
         sys.stdout.write(lines)
209 231
 
210 232
 
233
+### `color`
234
+def color(capability, string):
235
+    if not sys.stdout.isatty():
236
+        return string
237
+    return f'\x1b[{COLORS[capability]}m{string}\x1b[m'
238
+
239
+
240
+### `color_supports`
241
+def color_supports(supports):
242
+    for support in supports:
243
+        if support == 'UNSUPPORTED' or support.startswith('<'):
244
+            yield color('un', support)
245
+        elif support.startswith('GL_'):
246
+            yield color('ex', support)
247
+        else:
248
+            yield color('ve', support)
249
+
250
+
211 251
 ### `indentjoin`
212 252
 def indentjoin(indent, sep, parts):
213
-    return ' ' * INDENT * indent + sep.join(map(str, parts))
253
+    return ' ' * INDENT * indent + color('se', sep).join(map(str, parts))
214 254
 
215 255
 
216 256
 ### `removeprefix`
... ...
@@ -455,9 +495,9 @@ def enums(xml, group=None):
455 495
 ### `enums_tree`
456 496
 def enums_tree(xml, group=None):
457 497
     for supports, enums in sorted(enums_(xml, group).items()):
458
-        yield indentjoin(0, ',', supports)
498
+        yield indentjoin(0, ',', color_supports(supports))
459 499
         for enum in sorted(enums):
460
-            yield indentjoin(1, '', [enum])
500
+            yield indentjoin(1, '', [color('ms', enum)])
461 501
 
462 502
 
463 503
 ### `params_`
... ...
@@ -489,13 +529,13 @@ def params(xml, group=None):
489 529
 def params_tree(xml, group=None):
490 530
     for (count, param), occurences in sorted(params_(xml, group).items()):
491 531
         yield indentjoin(0, ':', [
492
-            param,
493
-            -count,
532
+            color('ms', param),
533
+            color('ln', -count),
494 534
         ])
495 535
         for supports_, commands in sorted(occurences.items()):
496
-            yield indentjoin(1, ',', supports_)
536
+            yield indentjoin(1, ',', color_supports(supports_))
497 537
             for command in sorted(commands):
498
-                yield indentjoin(2, '', [command])
538
+                yield indentjoin(2, '', [color('fn', command)])
499 539
 
500 540
 
501 541
 ### `audit_`
... ...
@@ -518,23 +558,23 @@ def audit(xml, path=None):
518 558
         for file, line       in locations
519 559
     ):
520 560
         yield indentjoin(0, ':', [
521
-            file,
522
-            line,
523
-            indentjoin(0, ',', supports),
524
-            name
561
+            color('fn', file),
562
+            color('ln', line),
563
+            indentjoin(0, ',', color_supports(supports)),
564
+            color('ms', name),
525 565
         ])
526 566
 
527 567
 
528 568
 ### `audit_tree`
529 569
 def audit_tree(xml, path=None):
530 570
     for supports, names in sorted(audit_(xml, path).items()):
531
-        yield indentjoin(0, ',', supports)
571
+        yield indentjoin(0, ',', color_supports(supports))
532 572
         for name, locations in sorted(names.items()):
533
-            yield indentjoin(1, '', [name])
573
+            yield indentjoin(1, '', [color('ms', name)])
534 574
             for file, line in sorted(locations):
535 575
                 yield indentjoin(2, ':', [
536
-                    file,
537
-                    line,
576
+                    color('fn', file),
577
+                    color('ln', line),
538 578
                 ])
539 579
 
540 580
 
... ...
@@ -573,11 +613,11 @@ def refs(name):
573 613
 ### `refs_all`
574 614
 def refs_all(name):
575 615
     for support, locations in sorted(refs_(name).items()):
576
-        yield indentjoin(0, ',', [support])
616
+        yield indentjoin(0, ',', color_supports([support]))
577 617
         for name_, url in sorted(locations):
578 618
             yield indentjoin(1, ':', [
579
-                name_,
580
-                url,
619
+                color('ms', name_),
620
+                color('fn', url),
581 621
             ])
582 622
 
583 623