From 23cbbb6269c6b49f5123a617e3872e2efeae5001 Mon Sep 17 00:00:00 2001 From: Edern Deneuville Date: Fri, 11 Sep 2026 14:06:21 +0200 Subject: [PATCH] Route math output to Word equation actions --- src/ai_typewriter/app.py | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/ai_typewriter/app.py b/src/ai_typewriter/app.py index 7b0012e..aaadbea 100644 --- a/src/ai_typewriter/app.py +++ b/src/ai_typewriter/app.py @@ -11,12 +11,22 @@ import keyboard from .ai_client import AIClientError, ask_ai from .clipboard_capture import capture_clipboard from .config import AppConfig, load_config -from .key_stepper import KeyStepper -from .math_format import format_math_text +from .key_stepper import KeyStepper, TypeAction +from .math_format import format_math_text, split_word_equation_segments LOG = logging.getLogger("ai_typewriter") +def prepare_type_payload(answer: str, mode: str) -> str | list[TypeAction]: + if mode == "word_equation": + return [TypeAction(segment.kind, segment.value) for segment in split_word_equation_segments(answer)] + return format_math_text(answer, mode) + + +def payload_length(payload: str | list[TypeAction]) -> int: + return len(payload) + + class AITypewriterApp: def __init__(self, config: AppConfig) -> None: self.config = config @@ -44,15 +54,16 @@ class AITypewriterApp: LOG.info("Texte capturé: %r", selected) started_at = time.perf_counter() answer = ask_ai(selected, self.config) - answer_to_type = format_math_text(answer, self.config.math_text_format) + answer_to_type = prepare_type_payload(answer, self.config.math_text_format) + typed_units = payload_length(answer_to_type) elapsed = time.perf_counter() - started_at LOG.info( - "Réponse reçue en %.2f s: %d caractères (%d après formatage %s). Premier caractère écrit automatiquement, %d restants au clavier.", + "Réponse reçue en %.2f s: %d caractères (%d unités à injecter en mode %s). Premier caractère/action écrit automatiquement, %d restants au clavier.", elapsed, len(answer), - len(answer_to_type), + typed_units, self.config.math_text_format, - max(len(answer_to_type) - 1, 0), + max(typed_units - 1, 0), ) if self._stepper is not None: self._stepper.stop() @@ -88,10 +99,14 @@ def main(argv: list[str] | None = None) -> int: try: started_at = time.perf_counter() answer = ask_ai(args.ask, config) - answer_to_type = format_math_text(answer, config.math_text_format) + answer_to_type = prepare_type_payload(answer, config.math_text_format) + typed_units = payload_length(answer_to_type) elapsed = time.perf_counter() - started_at - LOG.info("Réponse générée en %.2f s: %d caractères (%d après formatage %s).", elapsed, len(answer), len(answer_to_type), config.math_text_format) - print(answer_to_type) + LOG.info("Réponse générée en %.2f s: %d caractères (%d unités en mode %s).", elapsed, len(answer), typed_units, config.math_text_format) + if isinstance(answer_to_type, str): + print(answer_to_type) + else: + print("".join(f"[Alt+= {action.value}]" if action.kind == "equation" else action.value for action in answer_to_type)) return 0 except AIClientError as exc: LOG.error("%s", exc)