Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight today in colour in calendar's CLI output #128317

Closed
hugovk opened this issue Dec 28, 2024 · 10 comments
Closed

Highlight today in colour in calendar's CLI output #128317

hugovk opened this issue Dec 28, 2024 · 10 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@hugovk
Copy link
Member

hugovk commented Dec 28, 2024

Feature or enhancement

In Python 3.13 we added colour output to the new REPL, tracebacks and doctest, and in 3.14 to unittest and test.regrtest, which can also be controlled with the PYTHON_COLORS, NO_COLOR and FORCE_COLOR environment variables:

On macOS, the cal utility does something similar, but highlights today.

The calendar module has a CLI to print out a calendar to the terminal, but with no highlighting. Let's add it.

Linked PRs

@hugovk hugovk added the type-feature A feature request or enhancement label Dec 28, 2024
@picnixz picnixz added the stdlib Python modules in the Lib dir label Dec 28, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Dec 30, 2024
…H-128353)

(cherry picked from commit 6dbace3)

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Dec 30, 2024
…H-128353)

(cherry picked from commit 6dbace3)

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
hugovk added a commit that referenced this issue Dec 30, 2024
) (#128357)

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
hugovk added a commit that referenced this issue Dec 30, 2024
) (#128358)

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
hugovk added a commit that referenced this issue Jan 3, 2025
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
@hugovk hugovk closed this as completed Jan 3, 2025
@encukou
Copy link
Member

encukou commented Jan 3, 2025

Thank you!
I hate to spoil the fun, but this breaks non-interactive usage (e.g. ./python -m calendar | less).
IMO, the default should look at sys.stdout.isatty() (not stderr). , and be off for formatweek (edit: oh, it already is).

@encukou encukou reopened this Jan 3, 2025
WolframAlph pushed a commit to WolframAlph/cpython that referenced this issue Jan 4, 2025
@hugovk
Copy link
Member Author

hugovk commented Jan 4, 2025

Thanks, good idea. Please see PR #128498.

@encukou
Copy link
Member

encukou commented Jan 6, 2025

Did you consider putting the feature in a private _CLIDemoCalendar class, which would override formatday?

(As I said I'd hate to spoil the fun. Please do take my opinions just as input for consideration.)

This feature adds some complexity to the API, but doesn't generalize to any other use cases -- for example, highlight weekends/holidays, not just the current day. Or highlight in HTML. Or highlight today in a pure text calendar, for example:

     January 2025
 Mo Tu We Th Fr Sa Su
        1  2  3  4  5
[ 6] 7  8  9 10 11 12
 13 14 15 16 17 18 19
 20 21 22 23 24 25 26
 27 28 29 30 31

calendar has a rather powerful (though perhaps dated) mechanism for configuration: subclassing and overriding methods like formatday. This feature adds an alternate mechanism, and I'm not sure it meshes well with the current API.

srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Jan 8, 2025
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Jan 8, 2025
@joey-cramer
Copy link

Hi @hugovk,

I hope this is the right place to comment with my idea.

I am interested in if there are any plans or interest in adding week number to the python calendar?

According to this document (https://docs.python.org/3/library/time.html) %V for week is already a defined entity.

ISO 8601 week number (as a decimal number [01,53]). The first week of the year is the one that contains the first Thursday of the year. Weeks start on Monday.

@hugovk
Copy link
Member Author

hugovk commented Jan 16, 2025

Hello! I think it could be useful. There's some potential complexity as some countries decide which is week 1 differently, but if we can follow ISO 8601 that would simplify things.

The calendar module docs (https://docs.python.org/3/library/calendar.html) says ISO 8601 is used for other calculations:

Zero and negative years are interpreted as prescribed by the ISO 8601 standard.

The calendar module also allows you to define set a different weekday as the first one od the week, we'd need to check how that affects things.

https://docs.python.org/3/library/calendar.html#calendar.setfirstweekday

Please could you post a proposal to the ideas forum?

https://discuss.python.org/c/ideas/6

Thanks!

@ZeroIntensity
Copy link
Member

Using Hugo's dashboard: is this all done now?

@hugovk
Copy link
Member Author

hugovk commented Jan 29, 2025

Using Hugo's dashboard:

🎉

is this all done now?

Thanks for the reminder, not yet: I was planning on giving Petr's suggestion a shot: #128317 (comment).

Did you consider putting the feature in a private _CLIDemoCalendar class, which would override formatday?

I hadn't, something like this? hugovk@8856aeb. It overrides formatday, formatmonth and formatyear.

There's quite a bit of duplication between each class's formatmonth and formatyear. We could deduplicate formatmonth like hugovk@05a5099, and perhaps formatyear like hugovk@738844f, but I'm not sure about those.

What do you think?

(As I said I'd hate to spoil the fun. Please do take my opinions just as input for consideration.)

Thank you, it's very welcome!

@encukou
Copy link
Member

encukou commented Jan 31, 2025

Thanks for considering it!

The first commit does seem cleaner to me than putting terminal colours directly in TextCalendar.
Perhaps add an __init__ with self.highlight_day = highlight_day?

@hugovk
Copy link
Member Author

hugovk commented Feb 3, 2025

Good idea, please see PR #129625.

hugovk added a commit that referenced this issue Feb 8, 2025
Co-authored-by: Petr Viktorin <encukou@gmail.com>
@serhiy-storchaka
Copy link
Member

This change caused a regression. See #130665.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

6 participants