Skip to content

Commit

Permalink
Display full help when animated in a small terminal
Browse files Browse the repository at this point in the history
Signed-off-by: guillemdb <guillem@fragile.tech>
  • Loading branch information
Guillemdb committed Sep 15, 2024
1 parent a6ceb23 commit 67f7666
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/hydraclick/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def cli():

@cli.command(short_help="test_stuff.")
@hydra_command()
def nothing(args, **kwargs): # noqa: ARG001
def nothing(args, **kwargs):
"""Test function that does nothing."""


Expand Down
53 changes: 47 additions & 6 deletions src/hydraclick/terminal_effects.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import shutil
from typing import Optional, Callable
import os
import sys

import click
from click import Context, Option
Expand All @@ -18,20 +20,59 @@ def get_no_terminal_efects() -> bool:
NO_TERMINAL_EFFECTS = get_no_terminal_efects()


def display_terminal_effect(value):
"""Display the terminal effect."""
# from terminaltexteffects.effects.effect_random_sequence import RandomSequence
from terminaltexteffects.effects.effect_print import Print # noqa: PLC0415
def config_effect(effect):
"""Configure the terminal effect."""
from terminaltexteffects.utils.graphics import Color # noqa: PLC0415

effect = Print(value)
# effect.effect_config.speed = 0.025
effect.effect_config.print_speed = 5
effect.effect_config.print_head_return_speed = 3
effect.effect_config.final_gradient_stops = (Color("00ffae"), Color("00D1FF"), Color("FFFFFF"))
return effect


def remove_lines(num_lines: int):
"""Remove the last `num_lines` printed lines from the terminal."""
for _ in range(num_lines):
# Move the cursor up one line
sys.stdout.write("\x1b[1A")
# Clear the entire line
sys.stdout.write("\x1b[2K")
sys.stdout.flush()


def count_wrapped_lines(text: str, terminal_width: int):
"""Count the number of lines that the text will take when wrapped."""
lines = text.splitlines()
total_lines = 0
for line in lines:
if terminal_width > 0:
num_terminal_lines = (len(line) + terminal_width - 1) // terminal_width
else:
num_terminal_lines = 1
total_lines += max(num_terminal_lines, 1)
return total_lines


def display_terminal_effect(value, effect_cls=None):
"""Display the terminal effect."""
from terminaltexteffects.effects.effect_print import Print # noqa: PLC0415

effect_cls = effect_cls or Print
effect = effect_cls(value)
effect = config_effect(effect)
with effect.terminal_output() as terminal:
for frame in effect:
terminal.print(frame)
terminal_width = shutil.get_terminal_size().columns
n_lines_last_rendered_frame = count_wrapped_lines(frame, terminal_width)
remove_lines(n_lines_last_rendered_frame)
last_effect = effect_cls(value)
last_effect = config_effect(last_effect)
last_effect.terminal_config.ignore_terminal_dimensions = True
last_frame = list(last_effect)[-1]
sys.stdout.write(last_frame.lstrip())
sys.stdout.write("\n")
sys.stdout.flush()


_TERMINAL_EFFECT = display_terminal_effect
Expand Down

0 comments on commit 67f7666

Please sign in to comment.