From 754029ea67e0368c090ad72a57d5aabc3e81d327 Mon Sep 17 00:00:00 2001 From: Meir Kriheli Date: Tue, 30 Jul 2024 19:52:31 +0300 Subject: [PATCH] Added implemention selection (Python or Rust) to pybidi cli --- bidi/__init__.py | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/bidi/__init__.py b/bidi/__init__.py index c7838a9..5daf367 100644 --- a/bidi/__init__.py +++ b/bidi/__init__.py @@ -42,6 +42,16 @@ def main(): help="Text encoding (default: utf-8)", ) + parser.add_argument( + "-u", + "--upper-is-rtl", + dest="upper_is_rtl", + default=False, + action="store_true", + help="Treat upper case chars as strong 'R' " + "for debugging (default: False), Ignored in Rust algo", + ) + parser.add_argument( "-d", "--debug", @@ -61,17 +71,37 @@ def main(): help="Override base direction [L|R]", ) + parser.add_argument( + "-r", + "--rust", + dest="use_rust", + action="store_true", + help="Use the Rust unicode-bidi implemention instead of the Python one", + ) + + parser.add_argument( + "-v", "--version", action="version", version=f"pybidi {VERSION}" + ) + options, rest = parser.parse_known_args() lines = rest or sys.stdin + params = { + "encoding": options.encoding, + "base_dir": options.base_dir, + "debug": options.debug, + } + + if options.use_rust: + display_func = get_display + else: + from .algorithm import get_display as get_display_python + display_func = get_display_python + params["upper_is_rtl"] = options.upper_is_rtl + for line in lines: - display = get_display( - line, - options.encoding, - options.base_dir, - options.debug, - ) + display = display_func(line, **params) # adjust the encoding as unicode, to match the output encoding if not isinstance(display, str): display = bytes(display).decode(options.encoding)