Skip to content

Commit 296e48d

Browse files
committed
Used f-strings instead of percent formatting.
1 parent c12ac91 commit 296e48d

File tree

10 files changed

+28
-34
lines changed

10 files changed

+28
-34
lines changed

docs/pages/asking_for_input.rst

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ and returns the text. Just like ``(raw_)input``.
2525
from prompt_toolkit import prompt
2626
2727
text = prompt('Give me some input: ')
28-
print('You said: %s' % text)
28+
print(f'You said: {text}')
2929
3030
.. image:: ../images/hello-world-prompt.png
3131

@@ -86,7 +86,7 @@ base class.
8686
from prompt_toolkit.lexers import PygmentsLexer
8787
8888
text = prompt('Enter HTML: ', lexer=PygmentsLexer(HtmlLexer))
89-
print('You said: %s' % text)
89+
print(f'You said: {text}')
9090
9191
.. image:: ../images/html-input.png
9292

@@ -105,7 +105,7 @@ you can do the following:
105105
style = style_from_pygments_cls(get_style_by_name('monokai'))
106106
text = prompt('Enter HTML: ', lexer=PygmentsLexer(HtmlLexer), style=style,
107107
include_default_pygments_style=False)
108-
print('You said: %s' % text)
108+
print(f'You said: {text}')
109109
110110
We pass ``include_default_pygments_style=False``, because otherwise, both
111111
styles will be merged, possibly giving slightly different colors in the outcome
@@ -266,7 +266,7 @@ a completer that implements that interface.
266266
267267
html_completer = WordCompleter(['<html>', '<body>', '<head>', '<title>'])
268268
text = prompt('Enter HTML: ', completer=html_completer)
269-
print('You said: %s' % text)
269+
print(f'You said: {text}')
270270
271271
:class:`~prompt_toolkit.completion.WordCompleter` is a simple completer that
272272
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:
310310
})
311311
312312
text = prompt('# ', completer=completer)
313-
print('You said: %s' % text)
313+
print(f'You said: {text}')
314314
315315
Whenever there is a ``None`` value in the dictionary, it means that there is no
316316
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
476476
cursor_position=i)
477477
478478
number = int(prompt('Give a number: ', validator=NumberValidator()))
479-
print('You said: %i' % number)
479+
print(f'You said: {number}')
480480
481481
.. image:: ../images/number-validator.png
482482

@@ -515,7 +515,7 @@ follows:
515515
move_cursor_to_end=True)
516516
517517
number = int(prompt('Give a number: ', validator=validator))
518-
print('You said: %i' % number)
518+
print(f'You said: {number}')
519519
520520
We define a function that takes a string, and tells whether it's valid input or
521521
not by returning a boolean.
@@ -592,7 +592,7 @@ Example:
592592
593593
while True:
594594
text = session.prompt('> ', auto_suggest=AutoSuggestFromHistory())
595-
print('You said: %s' % text)
595+
print(f'You said: {text}')
596596
597597
.. image:: ../images/auto-suggestion.png
598598

@@ -627,7 +627,7 @@ of the foreground.
627627
return HTML('This is a <b><style bg="ansired">Toolbar</style></b>!')
628628
629629
text = prompt('> ', bottom_toolbar=bottom_toolbar)
630-
print('You said: %s' % text)
630+
print(f'You said: {text}')
631631
632632
.. image:: ../images/bottom-toolbar.png
633633

@@ -646,7 +646,7 @@ Similar, we could use a list of style/text tuples.
646646
})
647647
648648
text = prompt('> ', bottom_toolbar=bottom_toolbar, style=style)
649-
print('You said: %s' % text)
649+
print(f'You said: {text}')
650650
651651
The default class name is ``bottom-toolbar`` and that will also be used to fill
652652
the background of the toolbar.
@@ -734,7 +734,7 @@ An example of a prompt that prints ``'hello world'`` when :kbd:`Control-T` is pr
734734
event.app.exit()
735735
736736
text = prompt('> ', key_bindings=bindings)
737-
print('You said: %s' % text)
737+
print(f'You said: {text}')
738738
739739
740740
Note that we use
@@ -892,7 +892,7 @@ A default value can be given:
892892
from prompt_toolkit import prompt
893893
import getpass
894894
895-
prompt('What is your name: ', default='%s' % getpass.getuser())
895+
prompt('What is your name: ', default=f"{getpass.getuser()}")
896896
897897
898898
Mouse support
@@ -990,7 +990,7 @@ returns a coroutines and is awaitable.
990990
while True:
991991
with patch_stdout():
992992
result = await session.prompt_async('Say something: ')
993-
print('You said: %s' % result)
993+
print(f'You said: {result}')
994994
995995
The :func:`~prompt_toolkit.patch_stdout.patch_stdout` context manager is
996996
optional, but it's recommended, because other coroutines could print to stdout.

docs/pages/getting_started.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ and returns the text. Just like ``(raw_)input``.
5757
from prompt_toolkit import prompt
5858
5959
text = prompt('Give me some input: ')
60-
print('You said: %s' % text)
60+
print(f'You said: {text}')
6161
6262
6363
Learning `prompt_toolkit`

examples/full-screen/simple-demos/line-prefixes.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,10 @@
3333

3434
def get_line_prefix(lineno, wrap_count):
3535
if wrap_count == 0:
36-
return HTML('[%s] <style bg="orange" fg="black">--&gt;</style> ') % lineno
36+
return HTML(f'[{lineno}] <style bg="orange" fg="black">--&gt;</style> ')
3737

3838
text = str(lineno) + "-" + "*" * (lineno // 2) + ": "
39-
return HTML('[%s.%s] <style bg="ansigreen" fg="ansiblack">%s</style>') % (
40-
lineno,
41-
wrap_count,
42-
text,
43-
)
44-
39+
return HTML(f'[{lineno}.{wrap_count}] <style bg="ansigreen" fg="ansiblack">{text}</style>')
4540

4641
# Global wrap lines flag.
4742
wrap_lines = True

examples/progress-bar/a-lot-of-parallel-tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def stop_task(label, total, sleep_time):
3939
threads = []
4040

4141
for i in range(160):
42-
label = "Task %i" % i
42+
label = f"Task {i}"
4343
total = random.randrange(50, 200)
4444
sleep_time = random.randrange(5, 20) / 100.0
4545

examples/prompts/asyncio-prompt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async def print_counter():
2626
try:
2727
i = 0
2828
while True:
29-
print("Counter: %i" % i)
29+
print(f"Counter: {i}")
3030
i += 1
3131
await asyncio.sleep(3)
3232
except asyncio.CancelledError:

examples/prompts/auto-completion/colored-completions-with-formatted-text.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ def get_completions(self, document, complete_event):
9797
family_color = family_colors.get(family, "default")
9898

9999
display = HTML(
100-
"%s<b>:</b> <ansired>(<"
100+
f"{animal}<b>:</b> <ansired>(<"
101101
+ family_color
102-
+ ">%s</"
102+
+ f">{family}</"
103103
+ family_color
104104
+ ">)</ansired>"
105-
) % (animal, family)
105+
)
106106
else:
107107
display = animal
108108

examples/prompts/fancy-zsh-prompt.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ def get_prompt() -> HTML:
5151
"<right-part> "
5252
"<branch> master<exclamation-mark>!</exclamation-mark> </branch> "
5353
" <env> py36 </env> "
54-
" <time>%s</time> "
54+
f" <time>{datetime.datetime.now().isoformat()}</time> "
5555
"</right-part>"
56-
) % (datetime.datetime.now().isoformat(),)
56+
)
5757

5858
used_width = sum(
5959
[
@@ -65,7 +65,7 @@ def get_prompt() -> HTML:
6565
total_width = get_app().output.get_size().columns
6666
padding_size = total_width - used_width
6767

68-
padding = HTML("<padding>%s</padding>") % (" " * padding_size,)
68+
padding = HTML(f"<padding>{" " * padding_size}</padding>")
6969

7070
return merge_formatted_text([left_part, padding, right_part, "\n", "# "])
7171

examples/prompts/get-multiline-input.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def prompt_continuation(width, line_number, wrap_count):
1717
if wrap_count > 0:
1818
return " " * (width - 3) + "-> "
1919
else:
20-
text = ("- %i - " % (line_number + 1)).rjust(width)
21-
return HTML("<strong>%s</strong>") % text
20+
text = (f"- {line_number + 1} - ").rjust(width)
21+
return HTML(f"<strong>{text}</strong>")
2222

2323

2424
if __name__ == "__main__":

examples/prompts/patch-stdout.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def thread():
2121
i = 0
2222
while running:
2323
i += 1
24-
print("i=%i" % i)
24+
print(f"i={i}")
2525
time.sleep(1)
2626

2727
t = threading.Thread(target=thread)

examples/prompts/swap-light-and-dark-colors.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ def bottom_toolbar():
5858
HTML(
5959
'Press <style bg="#222222" fg="#ff8888">[control-t]</style> '
6060
"to swap between dark/light colors. "
61-
'<style bg="ansiblack" fg="ansiwhite">[%s]</style>'
61+
f'<style bg="ansiblack" fg="ansiwhite">[{on}]</style>'
6262
)
63-
% on
6463
)
6564

6665
text = prompt(

0 commit comments

Comments
 (0)