File: __init__.py

package info (click to toggle)
textual-image 0.8.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,468 kB
  • sloc: python: 1,851; makefile: 2
file content (36 lines) | stat: -rw-r--r-- 1,457 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""Rich renderables to display images in terminal."""

import logging
import sys
from typing import Type

from textual_image.renderable import sixel, tgp
from textual_image.renderable.halfcell import Image as HalfcellImage
from textual_image.renderable.sixel import Image as SixelImage
from textual_image.renderable.tgp import Image as TGPImage
from textual_image.renderable.unicode import Image as UnicodeImage

logger = logging.getLogger(__name__)

is_tty = sys.__stdout__ and sys.__stdout__.isatty()

Image: Type[TGPImage | SixelImage | HalfcellImage | UnicodeImage]

# TGP should be on top, as it performs way better than Sixel.
# However, the only terminal with TGP unicode diacritic support I know of is Kitty.
# Konsole and wezterm report TGP support, but don't work with our placeholder implementation, but do with Sixel.
# As Kitty does *not* support Sixel, this order should be best in terms of compatibility.
if is_tty and sixel.query_terminal_support():
    logger.debug("Sixel support detected")
    Image = SixelImage
elif is_tty and tgp.query_terminal_support():
    logger.debug("Terminal Graphics Protocol support detected")
    Image = TGPImage
elif is_tty:
    logger.debug("Connected to a terminal, using half cell rendering")
    Image = HalfcellImage
else:
    logger.debug("Not connected to a terminal, falling back to unicode")
    Image = UnicodeImage

__all__ = ["Image", "TGPImage", "SixelImage", "HalfcellImage", "UnicodeImage"]