Add crosshairs feature
Type of changes
- [x] New feature
Checklist
- [x] I've run the latest black with default args on new code.
- [x] I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate.
- [x] I've added tests for new code.
- [x] I accept that @willmcgugan may be pedantic in the code review.
Description
This introduces a Crosshairs renderable that draws a cross in the terminal composed of box characters.
E.g.,
from rich import print
from rich.crosshairs import Crosshairs
print(Crosshairs(1, 3))
would produce output similar to
│
│
│
─┼──────────...
│
...
I have 2 questions regarding this:
-
the test cases I wrote include some edge cases that were written out in a big string that is then split. I think this is a very reasonable way to test the 9 edge cases for the
Crosshairsbecause the output is meant to be very visual; does anyone oppose to having the test cases generated like this? (See this string and the slight parsing.) -
for the lines that only contain the vertical bar
│, should they contain trailing whitespace to match the length of the horizontal bar of the cross, or do those lines end at│?
That seems a very good way of expressing the test expectations. It's nice to be able to visually compare.
I think, yes the crosshairs should have trailing whitespace.
BTW, sorry to add this late in the day, but the crosshair class should take a StyleType so you can tweak the colors of the background / foreground.
BTW, sorry to add this late in the day, but the crosshair class should take a
StyleTypeso you can tweak the colors of the background / foreground.
Alright, on it.
At this point, I am just uncomfortable with something: my __rich_console__ does not use the arguments options, and I realise that might be OK, but I do not have enough experience with the console protocol to know for sure.
I only need to have access to the console size and to build the style, but I can do that with the argument console.
Finally, I have to write a test that makes sure Crosshairs instances can be styled. I saw some style tests in other files that encode the result with the explicit ANSI codes that are expected. Is this the only way to go about doing this?
It's fine not to use options. Not all renderables require it.
Re testing the ANSI codes. You could test at a slightly higher level. Perhaps test the Segment objects have the expected Style. Converting Styles to ANSI codes has been so thoroughly tested there probably isn't any benefit in test that again.
Re testing the ANSI codes. You could test at a slightly higher level. Perhaps test the Segment objects have the expected Style. [...]
Meanwhile, I already wrote a test with the explicit ANSI codes, which I'll assume was too much trouble, but also fine now that it is done. Correct me if I'm wrong.
Other than that, this PR is ready for review.
Thanks! Will do a review shortly.
Codecov Report
Merging #2446 (ff6a4bd) into master (19e518f) will decrease coverage by
0.05%. The diff coverage is97.38%.
@@ Coverage Diff @@
## master #2446 +/- ##
==========================================
- Coverage 98.71% 98.65% -0.06%
==========================================
Files 73 73
Lines 7771 7799 +28
==========================================
+ Hits 7671 7694 +23
- Misses 100 105 +5
| Flag | Coverage Δ | |
|---|---|---|
| unittests | 98.65% <97.38%> (-0.06%) |
:arrow_down: |
Flags with carried forward coverage won't be shown. Click here to find out more.
| Impacted Files | Coverage Δ | |
|---|---|---|
| rich/default_styles.py | 100.00% <ø> (ø) |
|
| rich/logging.py | 100.00% <ø> (ø) |
|
| rich/progress.py | 92.76% <ø> (ø) |
|
| rich/scope.py | 100.00% <ø> (ø) |
|
| rich/segment.py | 98.72% <93.10%> (+<0.01%) |
:arrow_up: |
| rich/crosshairs.py | 93.75% <93.75%> (ø) |
|
| rich/_inspect.py | 100.00% <100.00%> (ø) |
|
| rich/box.py | 100.00% <100.00%> (ø) |
|
| rich/cells.py | 96.05% <100.00%> (-3.95%) |
:arrow_down: |
| rich/color.py | 100.00% <100.00%> (ø) |
|
| ... and 6 more |
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.
The build error looks like it is the same build error as in #2450, again with Py 3.11...
Also, not sure how I managed to decrease the coverage of the file rich/cells.py. I didn't add code to that file and I didn't change any tests that already existed.