feat: refactor terminal width handling in FZF scripts for improved consistency

This commit is contained in:
Benexl
2025-12-02 13:30:03 +03:00
parent f4958cc0cc
commit c8c4e1b2c0
7 changed files with 56 additions and 22 deletions

View File

@@ -5,12 +5,26 @@ Lightweight stdlib-only utilities to replace Rich dependency in preview scripts.
Provides RGB color formatting, table rendering, and markdown stripping. Provides RGB color formatting, table rendering, and markdown stripping.
""" """
import os
import re import re
import shutil import shutil
import textwrap import textwrap
import unicodedata import unicodedata
def get_terminal_width() -> int:
"""
Get terminal width, prioritizing FZF preview environment variables.
Returns:
Terminal width in columns
"""
fzf_cols = os.environ.get("FZF_PREVIEW_COLUMNS")
if fzf_cols:
return int(fzf_cols)
return shutil.get_terminal_size((80, 24)).columns
def display_width(text: str) -> int: def display_width(text: str) -> int:
""" """
Calculate the actual display width of text, accounting for wide characters. Calculate the actual display width of text, accounting for wide characters.
@@ -72,7 +86,7 @@ def print_rule(sep_color: str) -> None:
Args: Args:
sep_color: Color as 'R,G,B' string sep_color: Color as 'R,G,B' string
""" """
width = shutil.get_terminal_size((80, 24)).columns width = get_terminal_width()
r, g, b = parse_color(sep_color) r, g, b = parse_color(sep_color)
print(rgb_color(r, g, b, "" * width)) print(rgb_color(r, g, b, "" * width))
@@ -94,7 +108,7 @@ def print_table_row(
key_styled = rgb_color(r, g, b, key, bold=True) key_styled = rgb_color(r, g, b, key, bold=True)
# Get actual terminal width # Get actual terminal width
term_width = shutil.get_terminal_size((80, 24)).columns term_width = get_terminal_width()
# Calculate display widths accounting for wide characters # Calculate display widths accounting for wide characters
key_display_width = display_width(key) key_display_width = display_width(key)
@@ -183,6 +197,6 @@ def wrap_text(text: str, width: int | None = None) -> str:
Wrapped text Wrapped text
""" """
if width is None: if width is None:
width = shutil.get_terminal_size((80, 24)).columns width = get_terminal_width()
return textwrap.fill(text, width=width) return textwrap.fill(text, width=width)

View File

@@ -1,12 +1,17 @@
import sys import sys
import shutil from _ansi_utils import (
from _ansi_utils import print_rule, print_table_row, strip_markdown, wrap_text print_rule,
print_table_row,
strip_markdown,
wrap_text,
get_terminal_width,
)
HEADER_COLOR = sys.argv[1] HEADER_COLOR = sys.argv[1]
SEPARATOR_COLOR = sys.argv[2] SEPARATOR_COLOR = sys.argv[2]
# Get terminal dimensions # Get terminal dimensions
term_width = shutil.get_terminal_size((80, 24)).columns term_width = get_terminal_width()
# Print title centered # Print title centered
print("{ANIME_TITLE}".center(term_width)) print("{ANIME_TITLE}".center(term_width))

View File

@@ -1,12 +1,17 @@
import sys import sys
import shutil from _ansi_utils import (
from _ansi_utils import print_rule, print_table_row, strip_markdown, wrap_text print_rule,
print_table_row,
strip_markdown,
wrap_text,
get_terminal_width,
)
HEADER_COLOR = sys.argv[1] HEADER_COLOR = sys.argv[1]
SEPARATOR_COLOR = sys.argv[2] SEPARATOR_COLOR = sys.argv[2]
# Get terminal dimensions # Get terminal dimensions
term_width = shutil.get_terminal_size((80, 24)).columns term_width = get_terminal_width()
# Print title centered # Print title centered
print("{CHARACTER_NAME}".center(term_width)) print("{CHARACTER_NAME}".center(term_width))

View File

@@ -1,12 +1,11 @@
import sys import sys
import shutil from _ansi_utils import print_rule, print_table_row, get_terminal_width
from _ansi_utils import print_rule, print_table_row
HEADER_COLOR = sys.argv[1] HEADER_COLOR = sys.argv[1]
SEPARATOR_COLOR = sys.argv[2] SEPARATOR_COLOR = sys.argv[2]
# Get terminal dimensions # Get terminal dimensions
term_width = shutil.get_terminal_size((80, 24)).columns term_width = get_terminal_width()
# Print title centered # Print title centered
print("{TITLE}".center(term_width)) print("{TITLE}".center(term_width))

View File

@@ -1,12 +1,17 @@
import sys import sys
import shutil from _ansi_utils import (
from _ansi_utils import print_rule, print_table_row, strip_markdown, wrap_text print_rule,
print_table_row,
strip_markdown,
wrap_text,
get_terminal_width,
)
HEADER_COLOR = sys.argv[1] HEADER_COLOR = sys.argv[1]
SEPARATOR_COLOR = sys.argv[2] SEPARATOR_COLOR = sys.argv[2]
# Get terminal dimensions # Get terminal dimensions
term_width = shutil.get_terminal_size((80, 24)).columns term_width = get_terminal_width()
# Print title centered # Print title centered
print("{TITLE}".center(term_width)) print("{TITLE}".center(term_width))

View File

@@ -244,12 +244,13 @@ def fzf_image_preview(file_path: str):
def fzf_text_info_render(): def fzf_text_info_render():
"""Renders the text-based info via the cached python script.""" """Renders the text-based info via the cached python script."""
import shutil # Get terminal dimensions from FZF environment or fallback
cols, lines = get_terminal_dimensions()
# Print simple separator line # Print simple separator line with proper width
width = shutil.get_terminal_size((80, 24)).columns
r, g, b = map(int, SEPARATOR_COLOR.split(",")) r, g, b = map(int, SEPARATOR_COLOR.split(","))
print(f"\x1b[38;2;{r};{g};{b}m" + "" * width + "\x1b[0m") separator = f"\x1b[38;2;{r};{g};{b}m" + ("" * cols) + "\x1b[0m"
print(separator, flush=True)
if PREVIEW_MODE == "text" or PREVIEW_MODE == "full": if PREVIEW_MODE == "text" or PREVIEW_MODE == "full":
preview_info_path = INFO_CACHE_DIR / f"{hash_id}.py" preview_info_path = INFO_CACHE_DIR / f"{hash_id}.py"

View File

@@ -1,12 +1,17 @@
import sys import sys
import shutil from _ansi_utils import (
from _ansi_utils import print_rule, print_table_row, strip_markdown, wrap_text print_rule,
print_table_row,
strip_markdown,
wrap_text,
get_terminal_width,
)
HEADER_COLOR = sys.argv[1] HEADER_COLOR = sys.argv[1]
SEPARATOR_COLOR = sys.argv[2] SEPARATOR_COLOR = sys.argv[2]
# Get terminal dimensions # Get terminal dimensions
term_width = shutil.get_terminal_size((80, 24)).columns term_width = get_terminal_width()
# Print title centered # Print title centered
print("{REVIEWER_NAME}".center(term_width)) print("{REVIEWER_NAME}".center(term_width))