Skip to content

Commit

Permalink
Fix type hinting (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
ziegenberg committed Oct 28, 2021
1 parent 9985a8d commit 0c1e1e8
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions ansi2html/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@
import optparse
import re
import sys
from typing import Dict, Iterator, List, Optional, Set, Tuple, Union
from collections import OrderedDict
from typing import Iterator, List, Optional, Set, Tuple, Union

import pkg_resources

try:
from collections import OrderedDict
except ImportError:
from ordereddict import OrderedDict # type: ignore

from ansi2html.style import SCHEME, get_styles

if sys.version_info >= (3, 8):
from typing import TypedDict
else:
from typing_extensions import TypedDict


ANSI_FULL_RESET = 0
ANSI_INTENSITY_INCREASED = 1
ANSI_INTENSITY_REDUCED = 2
Expand Down Expand Up @@ -246,6 +248,14 @@ class CursorMoveUp:
pass


class Attributes(TypedDict):
dark_bg: bool
line_wrap: bool
font_size: str
body: str
styles: Set[str]


class Ansi2HTMLConverter:
"""Convert Ansi color codes to CSS+HTML
Expand Down Expand Up @@ -282,7 +292,7 @@ def __init__(
self.output_encoding = output_encoding
self.scheme = scheme
self.title = title
self._attrs: Optional[Dict[str, Union[bool, str, Set[str]]]] = None
self._attrs: Attributes
self.hyperref = False

if inline:
Expand Down Expand Up @@ -419,7 +429,7 @@ def _handle_ansi_code(

# Special cursor-moving code. The only supported one.
if command == "A":
yield CursorMoveUp # type: ignore
yield CursorMoveUp()
continue

try:
Expand Down Expand Up @@ -521,7 +531,12 @@ def _collapse_cursor(
if final_parts:
final_parts.pop()

while final_parts and "\n" not in final_parts[-1]: # type: ignore
while final_parts and (
isinstance(final_parts[-1], OSC_Link)
or (
isinstance(final_parts[-1], str) and "\n" not in final_parts[-1]
)
):
final_parts.pop()

continue
Expand All @@ -533,7 +548,7 @@ def _collapse_cursor(

def prepare(
self, ansi: str = "", ensure_trailing_newline: bool = False
) -> Dict[str, Union[bool, str, Set[str]]]:
) -> Attributes:
"""Load the contents of 'ansi' into this object"""

body, styles = self.apply_regex(ansi)
Expand All @@ -551,7 +566,7 @@ def prepare(

return self._attrs

def attrs(self) -> Dict[str, Union[bool, str, Set[str]]]:
def attrs(self) -> Attributes:
"""Prepare attributes for the template"""
if not self._attrs:
raise Exception("Method .prepare not yet called.")
Expand All @@ -567,15 +582,15 @@ def convert(
"""
attrs = self.prepare(ansi, ensure_trailing_newline=ensure_trailing_newline)
if not full:
return attrs["body"] # type: ignore
return attrs["body"]
if self.latex:
_template = _latex_template
else:
_template = _html_template
all_styles = get_styles(self.dark_bg, self.line_wrap, self.scheme)
backgrounds = all_styles[:6]
used_styles = filter(
lambda e: e.klass.lstrip(".") in attrs["styles"], all_styles # type: ignore
lambda e: e.klass.lstrip(".") in attrs["styles"], all_styles
)

return _template % {
Expand Down

0 comments on commit 0c1e1e8

Please sign in to comment.