-1

I have this string:

form = '''
- {sha}: {message}
~> {commit_status_change}
=> {result_for_commit}
'''

Then I prepare parameters using click (or any other tool) for coloring:

params = {
    "sha": click.style(sha, bold=True),
    "message": click.style(msg, bold=True),
    "commit_status_change": click.style(change, fg="green"),
    "result_for_commit": click.style(result, fg="green")
}

Lastly, I print the string:

print(form.format(**params))

I want to make => and ~> symbols bold while keeping this neat form. How do I do it using python3?

martineau
  • 119,623
  • 25
  • 170
  • 301
Talos
  • 457
  • 4
  • 15
  • Your title should describe your question in one sentence. What does "Colors in Python format string" have to do with "I want to make => and ~> symbols bold"? [Ask] – Pranav Hosangadi Oct 03 '20 at 12:53
  • 2
    Does this answer your question? [How to print colored text in Python?](https://stackoverflow.com/questions/287871/how-to-print-colored-text-in-python) – mousetail Oct 03 '20 at 12:56
  • @mousetail: I my opinion this is *not* a duplicate of that question. – martineau Oct 03 '20 at 14:55
  • @martineau Just like in that question, the correct solution is to use the ANSI escape codes or any of the python modules that provide them – mousetail Oct 03 '20 at 15:00
  • @Pranav: I have edited the question's title to make it better express the question, however since one could consider making the text bold effectively changing its color, I didn't change that aspect. – martineau Oct 03 '20 at 15:00
  • @mousetail: I think the question is how to use the escape codes in conjunction with `str.format()`. The OP could chime in here anytime and clarify things… – martineau Oct 03 '20 at 15:01
  • same as a color but just BOLD = '\033[1m' but would never search that way – john taylor Oct 03 '20 at 15:17

1 Answers1

2

You can use ANSI escape sequences to make text bold:

form = '''
- {sha}: {message}
\033[01m ~> \033[0m {commit_status_change}
\033[01m => \033[0m {result_for_commit}
'''

On windows you have to call os.system('') to enable escape codes. Also, many terminals do not support bold, it might be better to use a slightly lighter color text instead, or use underline.

mousetail
  • 7,009
  • 4
  • 25
  • 45