TermUI.Capabilities.Fallbacks (TermUI v0.2.0)

View Source

Graceful degradation utilities for terminal capabilities.

Provides fallback chains for:

  • Colors: true-color → 256-color → 16-color → monochrome
  • Characters: Unicode box-drawing → ASCII art

Summary

Functions

Converts a 256-color index to the nearest 16-color ANSI index.

Returns the appropriate color based on terminal capabilities.

Converts an RGB color to the nearest 16-color ANSI index.

Converts an RGB color to the nearest 256-color palette index.

Converts a string containing Unicode to ASCII-safe version.

Converts a Unicode character to its ASCII fallback.

Functions

color_256_to_16(index)

@spec color_256_to_16(0..255) :: 0..15

Converts a 256-color index to the nearest 16-color ANSI index.

Returns an integer 0-15.

degrade_color(r, g, b, color_mode)

@spec degrade_color(
  non_neg_integer(),
  non_neg_integer(),
  non_neg_integer(),
  TermUI.Capabilities.color_mode()
) ::
  {:rgb, non_neg_integer(), non_neg_integer(), non_neg_integer()}
  | {:index_256, 0..255}
  | {:index_16, 0..15}
  | :none

Returns the appropriate color based on terminal capabilities.

Automatically degrades RGB to 256 to 16 based on capability.

rgb_to_16(r, g, b)

@spec rgb_to_16(non_neg_integer(), non_neg_integer(), non_neg_integer()) :: 0..15

Converts an RGB color to the nearest 16-color ANSI index.

Returns an integer 0-15.

rgb_to_256(r, g, b)

@spec rgb_to_256(non_neg_integer(), non_neg_integer(), non_neg_integer()) :: 0..255

Converts an RGB color to the nearest 256-color palette index.

Returns an integer 0-255.

string_to_ascii(string)

@spec string_to_ascii(String.t()) :: String.t()

Converts a string containing Unicode to ASCII-safe version.

Replaces all known Unicode characters with their ASCII fallbacks.

unicode_to_ascii(char)

@spec unicode_to_ascii(String.t()) :: String.t()

Converts a Unicode character to its ASCII fallback.

Returns the original character if no fallback is defined.