overall: host a minimal copy of ainsicolors

This commit is contained in:
uael
2023-02-23 20:16:33 +00:00
parent fbc7cf02a3
commit d21da78aa3
47 changed files with 151 additions and 50 deletions

View File

@@ -24,12 +24,12 @@
# -----------------------------------------------------------------------------
from __future__ import annotations
import struct
from colors import color
from pyee import EventEmitter
from typing import Dict, Type
from bumble.core import UUID, name_or_number
from bumble.hci import HCI_Object, key_with_value
from bumble.colors import color
# -----------------------------------------------------------------------------

View File

@@ -20,7 +20,6 @@ import asyncio
import struct
import time
import logging
from colors import color
from pyee import EventEmitter
from typing import Dict, Type
@@ -40,6 +39,7 @@ from .a2dp import (
VendorSpecificMediaCodecInformation,
)
from . import sdp
from .colors import color
# -----------------------------------------------------------------------------
# Logging

103
bumble/colors.py Normal file
View File

@@ -0,0 +1,103 @@
# Copyright (c) 2012 Giorgos Verigakis <verigak@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
from functools import partial
from typing import List, Optional, Union
# ANSI color names. There is also a "default"
COLORS = ('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white')
# ANSI style names
STYLES = (
'none',
'bold',
'faint',
'italic',
'underline',
'blink',
'blink2',
'negative',
'concealed',
'crossed',
)
ColorSpec = Union[str, int]
def _join(*values: ColorSpec) -> str:
return ';'.join(str(v) for v in values)
def _color_code(spec: ColorSpec, base: int) -> str:
if isinstance(spec, str):
spec = spec.strip().lower()
if spec == 'default':
return _join(base + 9)
elif spec in COLORS:
return _join(base + COLORS.index(spec))
elif isinstance(spec, int) and 0 <= spec <= 255:
return _join(base + 8, 5, spec)
else:
raise ValueError('Invalid color spec "%s"' % spec)
def color(
s: str,
fg: Optional[ColorSpec] = None,
bg: Optional[ColorSpec] = None,
style: Optional[str] = None,
) -> str:
codes: List[ColorSpec] = []
if fg:
codes.append(_color_code(fg, 30))
if bg:
codes.append(_color_code(bg, 40))
if style:
for style_part in style.split('+'):
if style_part in STYLES:
codes.append(STYLES.index(style_part))
else:
raise ValueError('Invalid style "%s"' % style_part)
if codes:
return '\x1b[{0}m{1}\x1b[0m'.format(_join(*codes), s)
else:
return s
# Foreground color shortcuts
black = partial(color, fg='black')
red = partial(color, fg='red')
green = partial(color, fg='green')
yellow = partial(color, fg='yellow')
blue = partial(color, fg='blue')
magenta = partial(color, fg='magenta')
cyan = partial(color, fg='cyan')
white = partial(color, fg='white')
# Style shortcuts
bold = partial(color, style='bold')
none = partial(color, style='none')
faint = partial(color, style='faint')
italic = partial(color, style='italic')
underline = partial(color, style='underline')
blink = partial(color, style='blink')
blink2 = partial(color, style='blink2')
negative = partial(color, style='negative')
concealed = partial(color, style='concealed')
crossed = partial(color, style='crossed')

View File

@@ -20,7 +20,7 @@ import asyncio
import itertools
import random
import struct
from colors import color
from bumble.colors import color
from bumble.core import BT_CENTRAL_ROLE, BT_PERIPHERAL_ROLE
from bumble.hci import (

View File

@@ -25,8 +25,7 @@ from contextlib import asynccontextmanager, AsyncExitStack
from dataclasses import dataclass
from typing import Any, ClassVar, Dict, List, Optional, Tuple, Union
from colors import color
from .colors import color
from .att import ATT_CID, ATT_DEFAULT_MTU, ATT_PDU
from .gatt import Characteristic, Descriptor, Service
from .hci import (

View File

@@ -29,8 +29,8 @@ import functools
import logging
import struct
from typing import Optional, Sequence
from colors import color
from .colors import color
from .core import UUID, get_dict_key_by_value
from .att import Attribute

View File

@@ -27,9 +27,9 @@ import asyncio
import logging
import struct
from colors import color
from pyee import EventEmitter
from .colors import color
from .hci import HCI_Constant
from .att import (
ATT_ATTRIBUTE_NOT_FOUND_ERROR,

View File

@@ -29,8 +29,8 @@ from collections import defaultdict
import struct
from typing import List, Tuple, Optional
from pyee import EventEmitter
from colors import color
from .colors import color
from .core import UUID
from .att import (
ATT_ATTRIBUTE_NOT_FOUND_ERROR,

View File

@@ -20,9 +20,9 @@ import struct
import collections
import logging
import functools
from colors import color
from typing import Dict, Type, Union
from .colors import color
from .core import (
BT_BR_EDR_TRANSPORT,
AdvertisingData,

View File

@@ -16,8 +16,8 @@
# Imports
# -----------------------------------------------------------------------------
import logging
from colors import color
from .colors import color
from .att import ATT_CID, ATT_PDU
from .smp import SMP_CID, SMP_Command
from .core import name_or_number

View File

@@ -18,7 +18,8 @@
import logging
import asyncio
import collections
from colors import color
from .colors import color
# -----------------------------------------------------------------------------

View File

@@ -20,8 +20,7 @@ import collections
import logging
import struct
from colors import color
from bumble.colors import color
from bumble.l2cap import L2CAP_PDU
from .hci import (

View File

@@ -25,8 +25,8 @@ import logging
import os
import json
from typing import Optional
from colors import color
from .colors import color
from .hci import Address

View File

@@ -21,10 +21,10 @@ import logging
import struct
from collections import deque
from colors import color
from pyee import EventEmitter
from typing import Dict, Type
from .colors import color
from .core import BT_CENTRAL_ROLE, InvalidStateError, ProtocolError
from .hci import (
HCI_LE_Connection_Update_Command,

View File

@@ -19,9 +19,9 @@ import logging
import asyncio
from functools import partial
from colors import color
import websockets
from bumble.colors import color
from bumble.hci import (
Address,
HCI_SUCCESS,

View File

@@ -18,10 +18,10 @@
import logging
import asyncio
from colors import color
from pyee import EventEmitter
from . import core
from .colors import color
from .core import BT_BR_EDR_TRANSPORT, InvalidStateError, ProtocolError
# -----------------------------------------------------------------------------

View File

@@ -18,11 +18,10 @@
from __future__ import annotations
import logging
import struct
from colors import color
import colors
from typing import Dict, List, Type
from . import core
from .colors import color
from .core import InvalidStateError
from .hci import HCI_Object, name_or_number, key_with_value
@@ -506,7 +505,7 @@ class ServiceAttribute:
def to_string(self, with_colors=False):
if with_colors:
return (
f'Attribute(id={colors.color(self.id_name(self.id),"magenta")},'
f'Attribute(id={color(self.id_name(self.id),"magenta")},'
f'value={self.value})'
)

View File

@@ -29,8 +29,8 @@ import secrets
from typing import Dict, Optional, Type
from pyee import EventEmitter
from colors import color
from .colors import color
from .hci import Address, HCI_LE_Enable_Encryption_Command, HCI_Object, key_with_value
from .core import (
BT_BR_EDR_TRANSPORT,

View File

@@ -18,9 +18,9 @@
import struct
import asyncio
import logging
from colors import color
from .. import hci
from ..colors import color
# -----------------------------------------------------------------------------

View File

@@ -22,10 +22,10 @@ import time
import usb.core
import usb.util
from colors import color
from .common import Transport, ParserSource
from .. import hci
from ..colors import color
# -----------------------------------------------------------------------------

View File

@@ -23,10 +23,10 @@ import ctypes
import platform
import usb1
from colors import color
from .common import Transport, ParserSource
from .. import hci
from ..colors import color
# -----------------------------------------------------------------------------

View File

@@ -22,9 +22,9 @@ import collections
import sys
from typing import Awaitable, TypeVar
from functools import wraps
from colors import color
from pyee import EventEmitter
from .colors import color
# -----------------------------------------------------------------------------
# Logging