From 296e48d0ba5ce4fa21cb1c927f71f16d6c634566 Mon Sep 17 00:00:00 2001 From: Taha Yasser Adnan Date: Sat, 2 Nov 2024 01:35:25 +0100 Subject: [PATCH 1/5] Used f-strings instead of percent formatting. --- docs/pages/asking_for_input.rst | 26 +++++++++---------- docs/pages/getting_started.rst | 2 +- .../full-screen/simple-demos/line-prefixes.py | 9 ++----- .../progress-bar/a-lot-of-parallel-tasks.py | 2 +- examples/prompts/asyncio-prompt.py | 2 +- ...colored-completions-with-formatted-text.py | 6 ++--- examples/prompts/fancy-zsh-prompt.py | 6 ++--- examples/prompts/get-multiline-input.py | 4 +-- examples/prompts/patch-stdout.py | 2 +- .../prompts/swap-light-and-dark-colors.py | 3 +-- 10 files changed, 28 insertions(+), 34 deletions(-) diff --git a/docs/pages/asking_for_input.rst b/docs/pages/asking_for_input.rst index 20619ac1f..5ad4269d4 100644 --- a/docs/pages/asking_for_input.rst +++ b/docs/pages/asking_for_input.rst @@ -25,7 +25,7 @@ and returns the text. Just like ``(raw_)input``. from prompt_toolkit import prompt text = prompt('Give me some input: ') - print('You said: %s' % text) + print(f'You said: {text}') .. image:: ../images/hello-world-prompt.png @@ -86,7 +86,7 @@ base class. from prompt_toolkit.lexers import PygmentsLexer text = prompt('Enter HTML: ', lexer=PygmentsLexer(HtmlLexer)) - print('You said: %s' % text) + print(f'You said: {text}') .. image:: ../images/html-input.png @@ -105,7 +105,7 @@ you can do the following: style = style_from_pygments_cls(get_style_by_name('monokai')) text = prompt('Enter HTML: ', lexer=PygmentsLexer(HtmlLexer), style=style, include_default_pygments_style=False) - print('You said: %s' % text) + print(f'You said: {text}') We pass ``include_default_pygments_style=False``, because otherwise, both styles will be merged, possibly giving slightly different colors in the outcome @@ -266,7 +266,7 @@ a completer that implements that interface. html_completer = WordCompleter(['', '', '', '']) text = prompt('Enter HTML: ', completer=html_completer) - print('You said: %s' % text) + print(f'You said: {text}') :class:`~prompt_toolkit.completion.WordCompleter` is a simple completer that completes the last word before the cursor with any of the given words. @@ -310,7 +310,7 @@ levels. :class:`~prompt_toolkit.completion.NestedCompleter` solves this issue: }) text = prompt('# ', completer=completer) - print('You said: %s' % text) + print(f'You said: {text}') Whenever there is a ``None`` value in the dictionary, it means that there is no further nested completion at that point. When all values of a dictionary would @@ -476,7 +476,7 @@ takes a :class:`~prompt_toolkit.document.Document` as input and raises cursor_position=i) number = int(prompt('Give a number: ', validator=NumberValidator())) - print('You said: %i' % number) + print(f'You said: {number}') .. image:: ../images/number-validator.png @@ -515,7 +515,7 @@ follows: move_cursor_to_end=True) number = int(prompt('Give a number: ', validator=validator)) - print('You said: %i' % number) + print(f'You said: {number}') We define a function that takes a string, and tells whether it's valid input or not by returning a boolean. @@ -592,7 +592,7 @@ Example: while True: text = session.prompt('> ', auto_suggest=AutoSuggestFromHistory()) - print('You said: %s' % text) + print(f'You said: {text}') .. image:: ../images/auto-suggestion.png @@ -627,7 +627,7 @@ of the foreground. return HTML('This is a <b><style bg="ansired">Toolbar</style></b>!') text = prompt('> ', bottom_toolbar=bottom_toolbar) - print('You said: %s' % text) + print(f'You said: {text}') .. image:: ../images/bottom-toolbar.png @@ -646,7 +646,7 @@ Similar, we could use a list of style/text tuples. }) text = prompt('> ', bottom_toolbar=bottom_toolbar, style=style) - print('You said: %s' % text) + print(f'You said: {text}') The default class name is ``bottom-toolbar`` and that will also be used to fill the background of the toolbar. @@ -734,7 +734,7 @@ An example of a prompt that prints ``'hello world'`` when :kbd:`Control-T` is pr event.app.exit() text = prompt('> ', key_bindings=bindings) - print('You said: %s' % text) + print(f'You said: {text}') Note that we use @@ -892,7 +892,7 @@ A default value can be given: from prompt_toolkit import prompt import getpass - prompt('What is your name: ', default='%s' % getpass.getuser()) + prompt('What is your name: ', default=f"{getpass.getuser()}") Mouse support @@ -990,7 +990,7 @@ returns a coroutines and is awaitable. while True: with patch_stdout(): result = await session.prompt_async('Say something: ') - print('You said: %s' % result) + print(f'You said: {result}') The :func:`~prompt_toolkit.patch_stdout.patch_stdout` context manager is optional, but it's recommended, because other coroutines could print to stdout. diff --git a/docs/pages/getting_started.rst b/docs/pages/getting_started.rst index 06287a080..72d31592e 100644 --- a/docs/pages/getting_started.rst +++ b/docs/pages/getting_started.rst @@ -57,7 +57,7 @@ and returns the text. Just like ``(raw_)input``. from prompt_toolkit import prompt text = prompt('Give me some input: ') - print('You said: %s' % text) + print(f'You said: {text}') Learning `prompt_toolkit` diff --git a/examples/full-screen/simple-demos/line-prefixes.py b/examples/full-screen/simple-demos/line-prefixes.py index 687d43673..bc33be476 100755 --- a/examples/full-screen/simple-demos/line-prefixes.py +++ b/examples/full-screen/simple-demos/line-prefixes.py @@ -33,15 +33,10 @@ def get_line_prefix(lineno, wrap_count): if wrap_count == 0: - return HTML('[%s] <style bg="orange" fg="black">--></style> ') % lineno + return HTML(f'[{lineno}] <style bg="orange" fg="black">--></style> ') text = str(lineno) + "-" + "*" * (lineno // 2) + ": " - return HTML('[%s.%s] <style bg="ansigreen" fg="ansiblack">%s</style>') % ( - lineno, - wrap_count, - text, - ) - + return HTML(f'[{lineno}.{wrap_count}] <style bg="ansigreen" fg="ansiblack">{text}</style>') # Global wrap lines flag. wrap_lines = True diff --git a/examples/progress-bar/a-lot-of-parallel-tasks.py b/examples/progress-bar/a-lot-of-parallel-tasks.py index a20982789..007bdb6ff 100755 --- a/examples/progress-bar/a-lot-of-parallel-tasks.py +++ b/examples/progress-bar/a-lot-of-parallel-tasks.py @@ -39,7 +39,7 @@ def stop_task(label, total, sleep_time): threads = [] for i in range(160): - label = "Task %i" % i + label = f"Task {i}" total = random.randrange(50, 200) sleep_time = random.randrange(5, 20) / 100.0 diff --git a/examples/prompts/asyncio-prompt.py b/examples/prompts/asyncio-prompt.py index 32a1481d9..bd97fb3a9 100755 --- a/examples/prompts/asyncio-prompt.py +++ b/examples/prompts/asyncio-prompt.py @@ -26,7 +26,7 @@ async def print_counter(): try: i = 0 while True: - print("Counter: %i" % i) + print(f"Counter: {i}") i += 1 await asyncio.sleep(3) except asyncio.CancelledError: diff --git a/examples/prompts/auto-completion/colored-completions-with-formatted-text.py b/examples/prompts/auto-completion/colored-completions-with-formatted-text.py index eeff259df..c8c037b4a 100755 --- a/examples/prompts/auto-completion/colored-completions-with-formatted-text.py +++ b/examples/prompts/auto-completion/colored-completions-with-formatted-text.py @@ -97,12 +97,12 @@ def get_completions(self, document, complete_event): family_color = family_colors.get(family, "default") display = HTML( - "%s<b>:</b> <ansired>(<" + f"{animal}<b>:</b> <ansired>(<" + family_color - + ">%s</" + + f">{family}</" + family_color + ">)</ansired>" - ) % (animal, family) + ) else: display = animal diff --git a/examples/prompts/fancy-zsh-prompt.py b/examples/prompts/fancy-zsh-prompt.py index cc31c1f34..11753e121 100755 --- a/examples/prompts/fancy-zsh-prompt.py +++ b/examples/prompts/fancy-zsh-prompt.py @@ -51,9 +51,9 @@ def get_prompt() -> HTML: "<right-part> " "<branch> master<exclamation-mark>!</exclamation-mark> </branch> " " <env> py36 </env> " - " <time>%s</time> " + f" <time>{datetime.datetime.now().isoformat()}</time> " "</right-part>" - ) % (datetime.datetime.now().isoformat(),) + ) used_width = sum( [ @@ -65,7 +65,7 @@ def get_prompt() -> HTML: total_width = get_app().output.get_size().columns padding_size = total_width - used_width - padding = HTML("<padding>%s</padding>") % (" " * padding_size,) + padding = HTML(f"<padding>{" " * padding_size}</padding>") return merge_formatted_text([left_part, padding, right_part, "\n", "# "]) diff --git a/examples/prompts/get-multiline-input.py b/examples/prompts/get-multiline-input.py index cda6fa52c..dd74c633f 100755 --- a/examples/prompts/get-multiline-input.py +++ b/examples/prompts/get-multiline-input.py @@ -17,8 +17,8 @@ def prompt_continuation(width, line_number, wrap_count): if wrap_count > 0: return " " * (width - 3) + "-> " else: - text = ("- %i - " % (line_number + 1)).rjust(width) - return HTML("<strong>%s</strong>") % text + text = (f"- {line_number + 1} - ").rjust(width) + return HTML(f"<strong>{text}</strong>") if __name__ == "__main__": diff --git a/examples/prompts/patch-stdout.py b/examples/prompts/patch-stdout.py index 3a89f3650..7a283b601 100755 --- a/examples/prompts/patch-stdout.py +++ b/examples/prompts/patch-stdout.py @@ -21,7 +21,7 @@ def thread(): i = 0 while running: i += 1 - print("i=%i" % i) + print(f"i={i}") time.sleep(1) t = threading.Thread(target=thread) diff --git a/examples/prompts/swap-light-and-dark-colors.py b/examples/prompts/swap-light-and-dark-colors.py index 4237d7321..f092a86e8 100755 --- a/examples/prompts/swap-light-and-dark-colors.py +++ b/examples/prompts/swap-light-and-dark-colors.py @@ -58,9 +58,8 @@ def bottom_toolbar(): HTML( 'Press <style bg="#222222" fg="#ff8888">[control-t]</style> ' "to swap between dark/light colors. " - '<style bg="ansiblack" fg="ansiwhite">[%s]</style>' + f'<style bg="ansiblack" fg="ansiwhite">[{on}]</style>' ) - % on ) text = prompt( From cd63e8a7269ece850c21c3f855a5cf6f70770a56 Mon Sep 17 00:00:00 2001 From: Taha Yasser Adnan <taha.y.adnan@gmail.com> Date: Sat, 2 Nov 2024 01:53:56 +0100 Subject: [PATCH 2/5] Removed trailing whitespaces. --- examples/full-screen/simple-demos/line-prefixes.py | 1 - .../auto-completion/colored-completions-with-formatted-text.py | 2 +- examples/prompts/fancy-zsh-prompt.py | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/examples/full-screen/simple-demos/line-prefixes.py b/examples/full-screen/simple-demos/line-prefixes.py index bc33be476..db72bcc27 100755 --- a/examples/full-screen/simple-demos/line-prefixes.py +++ b/examples/full-screen/simple-demos/line-prefixes.py @@ -34,7 +34,6 @@ def get_line_prefix(lineno, wrap_count): if wrap_count == 0: return HTML(f'[{lineno}] <style bg="orange" fg="black">--></style> ') - text = str(lineno) + "-" + "*" * (lineno // 2) + ": " return HTML(f'[{lineno}.{wrap_count}] <style bg="ansigreen" fg="ansiblack">{text}</style>') diff --git a/examples/prompts/auto-completion/colored-completions-with-formatted-text.py b/examples/prompts/auto-completion/colored-completions-with-formatted-text.py index c8c037b4a..ff75e68e3 100755 --- a/examples/prompts/auto-completion/colored-completions-with-formatted-text.py +++ b/examples/prompts/auto-completion/colored-completions-with-formatted-text.py @@ -102,7 +102,7 @@ def get_completions(self, document, complete_event): + f">{family}</" + family_color + ">)</ansired>" - ) + ) else: display = animal diff --git a/examples/prompts/fancy-zsh-prompt.py b/examples/prompts/fancy-zsh-prompt.py index 11753e121..e7364a56e 100755 --- a/examples/prompts/fancy-zsh-prompt.py +++ b/examples/prompts/fancy-zsh-prompt.py @@ -65,7 +65,7 @@ def get_prompt() -> HTML: total_width = get_app().output.get_size().columns padding_size = total_width - used_width - padding = HTML(f"<padding>{" " * padding_size}</padding>") + padding = HTML(f"<padding>{" " * padding_size}</padding>") return merge_formatted_text([left_part, padding, right_part, "\n", "# "]) From 16c3f582babfc466e0654cb604378e9e985d9093 Mon Sep 17 00:00:00 2001 From: Taha Yasser Adnan <taha.y.adnan@gmail.com> Date: Sat, 2 Nov 2024 01:56:56 +0100 Subject: [PATCH 3/5] Removed trailing whitespaces, again... --- examples/full-screen/simple-demos/line-prefixes.py | 2 +- examples/prompts/fancy-zsh-prompt.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/full-screen/simple-demos/line-prefixes.py b/examples/full-screen/simple-demos/line-prefixes.py index db72bcc27..9f6e38b53 100755 --- a/examples/full-screen/simple-demos/line-prefixes.py +++ b/examples/full-screen/simple-demos/line-prefixes.py @@ -33,7 +33,7 @@ def get_line_prefix(lineno, wrap_count): if wrap_count == 0: - return HTML(f'[{lineno}] <style bg="orange" fg="black">--></style> ') + return HTML(f'[{lineno}] <style bg="orange" fg="black">--></style> ') text = str(lineno) + "-" + "*" * (lineno // 2) + ": " return HTML(f'[{lineno}.{wrap_count}] <style bg="ansigreen" fg="ansiblack">{text}</style>') diff --git a/examples/prompts/fancy-zsh-prompt.py b/examples/prompts/fancy-zsh-prompt.py index e7364a56e..51d521924 100755 --- a/examples/prompts/fancy-zsh-prompt.py +++ b/examples/prompts/fancy-zsh-prompt.py @@ -53,7 +53,7 @@ def get_prompt() -> HTML: " <env> py36 </env> " f" <time>{datetime.datetime.now().isoformat()}</time> " "</right-part>" - ) + ) used_width = sum( [ From 07fc69924d3440090fa84fa5cb2326b2d7937caa Mon Sep 17 00:00:00 2001 From: Taha Yasser Adnan <taha.y.adnan@gmail.com> Date: Sat, 2 Nov 2024 02:01:24 +0100 Subject: [PATCH 4/5] Formatted 2 files using ruff. --- examples/full-screen/simple-demos/line-prefixes.py | 5 ++++- examples/prompts/swap-light-and-dark-colors.py | 10 ++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/examples/full-screen/simple-demos/line-prefixes.py b/examples/full-screen/simple-demos/line-prefixes.py index 9f6e38b53..15400fd49 100755 --- a/examples/full-screen/simple-demos/line-prefixes.py +++ b/examples/full-screen/simple-demos/line-prefixes.py @@ -35,7 +35,10 @@ def get_line_prefix(lineno, wrap_count): if wrap_count == 0: return HTML(f'[{lineno}] <style bg="orange" fg="black">--></style> ') text = str(lineno) + "-" + "*" * (lineno // 2) + ": " - return HTML(f'[{lineno}.{wrap_count}] <style bg="ansigreen" fg="ansiblack">{text}</style>') + return HTML( + f'[{lineno}.{wrap_count}] <style bg="ansigreen" fg="ansiblack">{text}</style>' + ) + # Global wrap lines flag. wrap_lines = True diff --git a/examples/prompts/swap-light-and-dark-colors.py b/examples/prompts/swap-light-and-dark-colors.py index f092a86e8..9640da7e2 100755 --- a/examples/prompts/swap-light-and-dark-colors.py +++ b/examples/prompts/swap-light-and-dark-colors.py @@ -54,12 +54,10 @@ def bottom_toolbar(): else: on = "on=false" - return ( - HTML( - 'Press <style bg="#222222" fg="#ff8888">[control-t]</style> ' - "to swap between dark/light colors. " - f'<style bg="ansiblack" fg="ansiwhite">[{on}]</style>' - ) + return HTML( + 'Press <style bg="#222222" fg="#ff8888">[control-t]</style> ' + "to swap between dark/light colors. " + f'<style bg="ansiblack" fg="ansiwhite">[{on}]</style>' ) text = prompt( From 04cacee326925f84285b19c611f006dbed7893cc Mon Sep 17 00:00:00 2001 From: Taha Yasser Adnan <taha.y.adnan@gmail.com> Date: Sat, 2 Nov 2024 02:04:38 +0100 Subject: [PATCH 5/5] Fixed a typo in a file I NEVER got near it. --- PROJECTS.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PROJECTS.rst b/PROJECTS.rst index eabfe13ab..a066e0057 100644 --- a/PROJECTS.rst +++ b/PROJECTS.rst @@ -13,7 +13,7 @@ Shells: - `saws <https://github.com/donnemartin/saws>`_: A Supercharged AWS Command Line Interface. - `cycli <https://github.com/nicolewhite/cycli>`_: A Command Line Interface for Cypher. - `crash <https://github.com/crate/crash>`_: Crate command line client. -- `vcli <https://github.com/dbcli/vcli>`_: Vertica client. +- `vcli <https://github.com/dbcli/vcli>`_: Vertical client. - `aws-shell <https://github.com/awslabs/aws-shell>`_: An integrated shell for working with the AWS CLI. - `softlayer-python <https://github.com/softlayer/softlayer-python>`_: A command-line interface to manage various SoftLayer products and services. - `ipython <http://github.com/ipython/ipython/>`_: The IPython REPL @@ -52,7 +52,7 @@ Full screen applications: - `sanctuary-zero <https://github.com/t0xic0der/sanctuary-zero>`_: A secure chatroom with zero logging and total transience. - `Hummingbot <https://github.com/CoinAlpha/hummingbot>`_: A Cryptocurrency Algorithmic Trading Platform - `git-bbb <https://github.com/MrMino/git-bbb>`_: A `git blame` browser. -- `ass <https://github.com/mlang/ass`_: An OpenAI Assistants API client. +- `ass <https://github.com/mlang/ass>`_: An OpenAI Assistants API client. Libraries: