Route math output to Word equation actions

This commit is contained in:
Edern Deneuville
2026-09-11 14:06:21 +02:00
parent 63dbde759d
commit 23cbbb6269
+24 -9
View File
@@ -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)