xref: /aosp_15_r20/external/pigweed/pw_cli/py/pw_cli/branding.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2020 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Facilities for accessing the current Pigweed branding"""
15
16import operator
17from pathlib import Path
18
19import pw_cli.env
20import pw_cli.color
21
22_memoized_banner: str | None = None
23
24# This is the default banner for Pigweed.
25_PIGWEED_BANNER = '''
26 ▒█████▄   █▓  ▄███▒  ▒█    ▒█ ░▓████▒ ░▓████▒ ▒▓████▄
27  ▒█░  █░ ░█▒ ██▒ ▀█▒ ▒█░ █ ▒█  ▒█   ▀  ▒█   ▀  ▒█  ▀█▌
28  ▒█▄▄▄█░ ░█▒ █▓░ ▄▄░ ▒█░ █ ▒█  ▒███    ▒███    ░█   █▌
29  ▒█▀     ░█░ ▓█   █▓ ░█░ █ ▒█  ▒█   ▄  ▒█   ▄  ░█  ▄█▌
30  ▒█      ░█░ ░▓███▀   ▒█▓▀▓█░ ░▓████▒ ░▓████▒ ▒▓████▀
31'''
32
33
34def banner() -> str:
35    global _memoized_banner  # pylint: disable=global-statement
36    if _memoized_banner is not None:
37        return _memoized_banner
38
39    parsed_env = pw_cli.env.pigweed_environment()
40
41    # Take the banner from the file PW_BRANDING_BANNER; or use the default.
42    banner_filename = parsed_env.PW_BRANDING_BANNER
43    _memoized_banner = (
44        Path(banner_filename).read_text(encoding='utf-8', errors='replace')
45        if banner_filename
46        else _PIGWEED_BANNER
47    )
48
49    # Color the banner if requested.
50    banner_color = parsed_env.PW_BRANDING_BANNER_COLOR
51    if banner_color != '':
52        set_color = operator.attrgetter(banner_color)(pw_cli.color.colors())
53        _memoized_banner = '\n'.join(
54            set_color(line) for line in _memoized_banner.splitlines()
55        )
56
57    return _memoized_banner
58