Integrate colorama for coloured output #164

Merged
witten merged 5 commits from :feature/coloured-output-first-step into master 2019-05-13 19:50:37 +00:00
Contributor

Working on witten/borgmatic#125.

This would be a first step to enable a custom logger for all output that is controlled by the existing logging implementation. This does not cover the raising of ValueErrors or the output from Borg itself.

Just getting this up early to see if we agree on a few things. If using colorama is OK, adding it as a dependency, the custom logger implementation and colours used etc.

I could continue on this PR if we're agreed that this is the right direction or we could merge it as I work onwards to cover the rest of the outputting.

Working on https://projects.torsion.org/witten/borgmatic/issues/125. This would be a first step to enable a custom logger for all output that is controlled by the existing logging implementation. This does not cover the raising of ValueErrors or the output from Borg itself. Just getting this up early to see if we agree on a few things. If using colorama is OK, adding it as a dependency, the custom logger implementation and colours used etc. I could continue on this PR if we're agreed that this is the right direction or we could merge it as I work onwards to cover the rest of the outputting.
Owner

I think taking a dependency on colorama is fine. It doesn't pull in any other dependencies or require C library compilation. And it has a compatible license. It hasn't seen a release since last year, but I imagine that's because the library is pretty much "done".

I also think that this PR is a good first step towards colored output. It doesn't have to color absolutely everything out of the gate (e.g. Borg output) to be useful. So I do think it'd be better to merge this iteration prior to expanding its scope. First though I do have a few bits of minor feedback I'll include in a review. Thanks!

I think taking a dependency on colorama is fine. It doesn't pull in any other dependencies or require C library compilation. And it has a compatible license. It hasn't seen a release since last year, but I imagine that's because the library is pretty much "done". I also think that this PR is a good first step towards colored output. It doesn't have to color absolutely everything out of the gate (e.g. Borg output) to be useful. So I do think it'd be better to merge this iteration prior to expanding its scope. First though I do have a few bits of minor feedback I'll include in a review. Thanks!
witten reviewed 2019-05-13 00:11:54 +00:00
witten left a comment
Owner

Cool!

Cool!
@ -22,10 +23,10 @@ from borgmatic.commands import hook
from borgmatic.config import checks, collect, convert, validate
from borgmatic.signals import configure_signals
from borgmatic.verbosity import verbosity_to_log_level
from borgmatic.logger import should_do_markup, get_logger
Owner

Super minor: I tend to alpha order imports within each group, just to make visual scanning easier. E.g., from borgmatic.logger would go on the line after from borgmatic.config.

Super minor: I tend to alpha order imports within each group, just to make visual scanning easier. E.g., `from borgmatic.logger` would go on the line after `from borgmatic.config`.
Author
Contributor

I'll fix. I'd recommend integrating https://isort.readthedocs.io/en/latest/ so that the tooling mandates and saves you time checking it yourself. Here's a configuration I use myself that plays well with Black: https://git.coop/decentral1se/pypkgtemplate/blob/master/%7B%7Bcookiecutter.package%7D%7D/setup.cfg#L7.

I'll fix. I'd recommend integrating https://isort.readthedocs.io/en/latest/ so that the tooling mandates and saves you time checking it yourself. Here's a configuration I use myself that plays well with Black: https://git.coop/decentral1se/pypkgtemplate/blob/master/%7B%7Bcookiecutter.package%7D%7D/setup.cfg#L7.
Owner

Point taken. I filed witten/borgmatic#169 for that.

Point taken. I filed https://projects.torsion.org/witten/borgmatic/issues/169 for that.
@ -170,2 +171,4 @@
help='Go through the motions, but do not actually write to any repositories',
)
common_group.add_argument(
'-nc', '--no-colour', dest='no_colour', action='store_true', help='Disable coloured output'
Owner

May seem silly, but I think I'd prefer American spelling for this. I don't have any good reason except perhaps it's just more common among users: https://en.wikipedia.org/wiki/Comparison_of_American_and_British_English#Demographics

But if you like, you can include flags for both (e.g. --no-color and --no-colour) to be more inclusive!

May seem silly, but I think I'd prefer American spelling for this. I don't have any good reason except perhaps it's just more common among users: https://en.wikipedia.org/wiki/Comparison_of_American_and_British_English#Demographics But if you like, you can include flags for both (e.g. `--no-color` and `--no-colour`) to be more inclusive!
@ -0,0 +18,4 @@
if arg in ('yes', 'on', '1', 'true', 1):
return True
return False
Owner

It'd be good to have a simple automated test or three for this function, given that it's pure logic.

It'd be good to have a simple automated test or three for this function, given that it's pure logic.
Author
Contributor

Makes sense! Also, to note, I took this implementation from the Ansible utility, so it is well battle tested for this use case.

Makes sense! Also, to note, I took this implementation from the Ansible utility, so it is well battle tested for this use case.
@ -0,0 +33,4 @@
if py_colors is not None:
return to_bool(py_colors)
return sys.stdout.isatty() and os.environ.get('TERM') != 'dumb'
Owner

Nice to have the auto-detection here!

Nice to have the auto-detection here!
@ -0,0 +64,4 @@
'''
Give red coloured text.
'''
return color_text(colorama.Fore.RED, msg)
Owner

Given that these *_text() functions are only used in one place, I might be inclined to just inline them there. Do not feel strongly though.

Given that these `*_text()` functions are only used in one place, I might be inclined to just inline them there. Do not feel strongly though.
@ -182,0 +183,4 @@
Borgmatic uses [colorama](https://pypi.org/project/colorama/) to produce
coloured terminal output by default. It can be disable by passing `--no-colour`
or by setting the environment variable `PY_COLORS=False`.
Owner

Given that borgmatic is used so often from cron jobs, a reader's first reaction might be: "Wait.. Does that mean I need to specify --no-colour if I want to call borgmatic from a cron job?" To get ahead of that concern, it might be good to state here that the "by default" color won't happen on a non-interactive terminal (like a cron job).

Given that borgmatic is used so often from cron jobs, a reader's first reaction might be: "Wait.. Does that mean I need to specify `--no-colour` if I want to call borgmatic from a cron job?" To get ahead of that concern, it might be good to state here that the "by default" color won't happen on a non-interactive terminal (like a cron job).
Author
Contributor

Thanks for the review. Comments addressed 👍

Thanks for the review. Comments addressed :thumbsup:
Owner

Awesome, thank you!

Awesome, thank you!
witten closed this pull request 2019-05-13 19:50:37 +00:00
decentral1se deleted branch feature/coloured-output-first-step 2019-05-13 20:39:06 +00:00
Owner

In playing around with this, I found the coloring does not appear to affect critical/error messages that get logged by logging.handle(). So I implemented support for that too: 37362150fe

In playing around with this, I found the coloring does not appear to affect critical/error messages that get logged by `logging.handle()`. So I implemented support for that too: https://projects.torsion.org/witten/borgmatic/commit/37362150fe4a29449298c7b6c690ffee23060899
Sign in to join this conversation.
No reviewers
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: borgmatic-collective/borgmatic#164
No description provided.