Skip to content

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

Closed

Description

@hugovk

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

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Dec 28, 2024
  2. added a commit that references this issue on Dec 30, 2024
  3. added 2 commits that reference this issue on Dec 30, 2024
  4. added 3 commits that reference this issue on Dec 30, 2024
  5. encukou commented on Jan 3, 2025

    @encukou
    Member

    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).

  6. reopened this on Jan 3, 2025
  7. added a commit that references this issue on Jan 4, 2025
  8. hugovk commented on Jan 4, 2025

    @hugovk
    MemberAuthor

    Thanks, good idea. Please see PR #128498.

  9. encukou commented on Jan 6, 2025

    @encukou
    Member

    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.

  10. added 2 commits that reference this issue on Jan 8, 2025
  11. joey-cramer commented on Jan 16, 2025

    @joey-cramer

    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.

  12. hugovk commented on Jan 16, 2025

    @hugovk
    MemberAuthor

    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!

  13. ZeroIntensity commented on Jan 28, 2025

    @ZeroIntensity
    Member

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

  14. hugovk commented on Jan 29, 2025

    @hugovk
    MemberAuthor

    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!

  15. encukou commented on Jan 31, 2025

    @encukou
    Member

    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?

  16. hugovk commented on Feb 3, 2025

    @hugovk
    MemberAuthor

    Good idea, please see PR #129625.

  17. added a commit that references this issue on Feb 8, 2025
  18. serhiy-storchaka commented on Feb 28, 2025

    @serhiy-storchaka
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions