Integrate colorama for coloured output #164
No reviewers
Labels
No Label
bug
data loss
design finalized
good first issue
new feature area
question / support
security
waiting for response
No Milestone
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: borgmatic-collective/borgmatic#164
Loading…
Reference in New Issue
No description provided.
Delete Branch ":feature/coloured-output-first-step"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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.
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!
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
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 afterfrom borgmatic.config
.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.
Point taken. I filed witten/borgmatic#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'
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
It'd be good to have a simple automated test or three for this function, given that it's pure logic.
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'
Nice to have the auto-detection here!
@ -0,0 +64,4 @@
'''
Give red coloured text.
'''
return color_text(colorama.Fore.RED, msg)
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`.
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).Thanks for the review. Comments addressed 👍
Awesome, thank you!
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