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
 
... ...
@@ -151,6 +159,26 @@ CACHE  = ENV_XDG('CACHE',  os.path.join('~', '.cache'))
151 159
 EDITOR = ENV_PRG('EDITOR', 'vi')
152 160
 PAGER  = ENV_PRG('PAGER',  'less')
153 161
 LESS   = os.environ.get('LESS', 'FR')
162
+COLORS = collections.defaultdict(
163
+    str,
164
+    [
165
+        (color.split('=') + [''])[:2]
166
+        for color in
167
+        filter(
168
+            None,
169
+            os.environ.get(
170
+                'GLREGISTRY_COLORS',
171
+                (lambda x, y: ':'.join([x, x and y]))(
172
+                    os.environ.get(
173
+                        'GREP_COLORS',
174
+                        'ms=01;31:fn=35:ln=32:se=36',
175
+                    ),
176
+                    've=01;34:ex=34:un=01;33',
177
+                ),
178
+            ).split(':'),
179
+        )
180
+    ],
181
+)
154 182
 IN    = lambda a, v, s: f"contains(concat('{s}',@{a},'{s}'),'{s}{v}{s}')"
155 183
 MAYBE = lambda a, v:    f"(@{a}='{v}' or not(@{a}))"
156 184
 TYPES    = "/registry/types"
... ...
@@ -209,9 +237,27 @@ def page(lines):
209 237
         sys.stdout.write(lines)
210 238
 
211 239
 
240
+### `color`
241
+def color(capability, string):
242
+    if not sys.stdout.isatty():
243
+        return string
244
+    return f'\x1b[{COLORS[capability]}m{string}\x1b[m'
245
+
246
+
247
+### `color_supports`
248
+def color_supports(supports):
249
+    for support in supports:
250
+        if support == 'UNSUPPORTED' or support.startswith('<'):
251
+            yield color('un', support)
252
+        elif support.startswith('GL_'):
253
+            yield color('ex', support)
254
+        else:
255
+            yield color('ve', support)
256
+
257
+
212 258
 ### `indentjoin`
213 259
 def indentjoin(indent, sep, parts):
214
-    return ' ' * INDENT * indent + sep.join(map(str, parts))
260
+    return ' ' * INDENT * indent + color('se', sep).join(map(str, parts))
215 261
 
216 262
 
217 263
 ### `removeprefix`
... ...
@@ -456,9 +502,9 @@ def enums(xml, group=None):
456 502
 ### `enums_tree`
457 503
 def enums_tree(xml, group=None):
458 504
     for supports, enums in sorted(enums_(xml, group).items()):
459
-        yield indentjoin(0, ',', supports)
505
+        yield indentjoin(0, ',', color_supports(supports))
460 506
         for enum in sorted(enums):
461
-            yield indentjoin(1, '', [enum])
507
+            yield indentjoin(1, '', [color('ms', enum)])
462 508
 
463 509
 
464 510
 ### `params_`
... ...
@@ -490,13 +536,13 @@ def params(xml, group=None):
490 536
 def params_tree(xml, group=None):
491 537
     for (count, param), occurences in sorted(params_(xml, group).items()):
492 538
         yield indentjoin(0, ':', [
493
-            param,
494
-            -count,
539
+            color('ms', param),
540
+            color('ln', -count),
495 541
         ])
496 542
         for supports_, commands in sorted(occurences.items()):
497
-            yield indentjoin(1, ',', supports_)
543
+            yield indentjoin(1, ',', color_supports(supports_))
498 544
             for command in sorted(commands):
499
-                yield indentjoin(2, '', [command])
545
+                yield indentjoin(2, '', [color('fn', command)])
500 546
 
501 547
 
502 548
 ### `audit_`
... ...
@@ -519,23 +565,23 @@ def audit(xml, path=None):
519 565
         for file, line       in locations
520 566
     ):
521 567
         yield indentjoin(0, ':', [
522
-            file,
523
-            line,
524
-            indentjoin(0, ',', supports),
525
-            name
568
+            color('fn', file),
569
+            color('ln', line),
570
+            indentjoin(0, ',', color_supports(supports)),
571
+            color('ms', name),
526 572
         ])
527 573
 
528 574
 
529 575
 ### `audit_tree`
530 576
 def audit_tree(xml, path=None):
531 577
     for supports, names in sorted(audit_(xml, path).items()):
532
-        yield indentjoin(0, ',', supports)
578
+        yield indentjoin(0, ',', color_supports(supports))
533 579
         for name, locations in sorted(names.items()):
534
-            yield indentjoin(1, '', [name])
580
+            yield indentjoin(1, '', [color('ms', name)])
535 581
             for file, line in sorted(locations):
536 582
                 yield indentjoin(2, ':', [
537
-                    file,
538
-                    line,
583
+                    color('fn', file),
584
+                    color('ln', line),
539 585
                 ])
540 586
 
541 587
 
... ...
@@ -574,11 +620,11 @@ def refs(name):
574 620
 ### `refs_all`
575 621
 def refs_all(name):
576 622
     for support, locations in sorted(refs_(name).items()):
577
-        yield indentjoin(0, ',', [support])
623
+        yield indentjoin(0, ',', color_supports([support]))
578 624
         for name_, url in sorted(locations):
579 625
             yield indentjoin(1, ':', [
580
-                name_,
581
-                url,
626
+                color('ms', name_),
627
+                color('fn', url),
582 628
             ])
583 629
 
584 630