-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
gh-148352: Add more colour to calendar CLI output
#148354
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
Open
hugovk
wants to merge
6
commits into
python:main
Choose a base branch
from
hugovk:3.15-calendar-colour
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9e95c43
Add more colour to calendar
hugovk 1fe0d93
Use ArgumentDefaultsHelpFormatter for better help
hugovk 3343a36
Re-order calendar what's new
hugovk 0843e5b
Docs
hugovk 4b202f0
Improve docs
hugovk cab1a52
Update NEWS
hugovk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -686,28 +686,61 @@ def __init__(self, highlight_day=None, *args, **kwargs): | |
| super().__init__(*args, **kwargs) | ||
| self.highlight_day = highlight_day | ||
|
|
||
| def formatweek(self, theweek, width, *, highlight_day=None): | ||
| def _get_theme(self): | ||
| from _colorize import get_theme | ||
|
|
||
| return get_theme(tty_file=sys.stdout) | ||
|
|
||
| def formatday(self, day, weekday, width, *, highlight_day=None): | ||
| """ | ||
| Returns a single week in a string (no newline). | ||
| Returns a formatted day. | ||
| """ | ||
| if highlight_day: | ||
| from _colorize import get_colors | ||
|
|
||
| ansi = get_colors() | ||
| highlight = f"{ansi.BLACK}{ansi.BACKGROUND_YELLOW}" | ||
| reset = ansi.RESET | ||
| if day == 0: | ||
| s = '' | ||
| else: | ||
| highlight = reset = "" | ||
| s = f'{day:2}' | ||
| s = s.center(width) | ||
| if day == highlight_day: | ||
| theme = self._get_theme().calendar | ||
| s = f"{theme.highlight}{s}{theme.reset}" | ||
| return s | ||
|
|
||
| def formatweek(self, theweek, width, *, highlight_day=None): | ||
| """ | ||
| Returns a single week in a string (no newline). | ||
| """ | ||
| return ' '.join( | ||
| ( | ||
| f"{highlight}{self.formatday(d, wd, width)}{reset}" | ||
| if d == highlight_day | ||
| else self.formatday(d, wd, width) | ||
| ) | ||
| self.formatday(d, wd, width, highlight_day=highlight_day) | ||
| for (d, wd) in theweek | ||
| ) | ||
|
|
||
| def formatweekheader(self, width): | ||
| """ | ||
| Return a header for a week. | ||
| """ | ||
| header = super().formatweekheader(width) | ||
| theme = self._get_theme().calendar | ||
| return f"{theme.weekday}{header}{theme.reset}" | ||
|
|
||
| def formatmonthname(self, theyear, themonth, width, withyear=True): | ||
| """ | ||
| Return a formatted month name. | ||
| """ | ||
| name = super().formatmonthname(theyear, themonth, width, withyear) | ||
| theme = self._get_theme().calendar | ||
| if ( | ||
| self.highlight_day | ||
| and self.highlight_day.year == theyear | ||
| and self.highlight_day.month == themonth | ||
| ): | ||
| color = theme.highlight | ||
| name_only = name.strip() | ||
| colored_name = f"{color}{name_only}{theme.reset}" | ||
| return name.replace(name_only, colored_name, 1) | ||
| else: | ||
| color = theme.header | ||
| return f"{color}{name}{theme.reset}" | ||
|
|
||
| def formatmonth(self, theyear, themonth, w=0, l=0): | ||
| """ | ||
| Return a month's calendar string (multi-line). | ||
|
|
@@ -742,7 +775,9 @@ def formatyear(self, theyear, w=2, l=1, c=6, m=3): | |
| colwidth = (w + 1) * 7 - 1 | ||
| v = [] | ||
| a = v.append | ||
| a(repr(theyear).center(colwidth*m+c*(m-1)).rstrip()) | ||
| theme = self._get_theme().calendar | ||
| year = repr(theyear).center(colwidth*m+c*(m-1)).rstrip() | ||
| a(f"{theme.header}{year}{theme.reset}") | ||
| a('\n'*l) | ||
| header = self.formatweekheader(w) | ||
| for (i, row) in enumerate(self.yeardays2calendar(theyear, m)): | ||
|
|
@@ -843,28 +878,30 @@ def timegm(tuple): | |
|
|
||
| def main(args=None): | ||
| import argparse | ||
| parser = argparse.ArgumentParser(color=True) | ||
| parser = argparse.ArgumentParser( | ||
| formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
| ) | ||
| textgroup = parser.add_argument_group('text only arguments') | ||
| htmlgroup = parser.add_argument_group('html only arguments') | ||
| textgroup.add_argument( | ||
| "-w", "--width", | ||
| type=int, default=2, | ||
| help="width of date column (default 2)" | ||
| help="width of date column" | ||
| ) | ||
| textgroup.add_argument( | ||
| "-l", "--lines", | ||
| type=int, default=1, | ||
| help="number of lines for each week (default 1)" | ||
| help="number of lines for each week" | ||
| ) | ||
| textgroup.add_argument( | ||
| "-s", "--spacing", | ||
| type=int, default=6, | ||
| help="spacing between months (default 6)" | ||
| help="spacing between months" | ||
| ) | ||
| textgroup.add_argument( | ||
| "-m", "--months", | ||
| type=int, default=3, | ||
| help="months per row (default 3)" | ||
| help="months per row" | ||
| ) | ||
| htmlgroup.add_argument( | ||
| "-c", "--css", | ||
|
|
@@ -879,7 +916,7 @@ def main(args=None): | |
| parser.add_argument( | ||
| "-e", "--encoding", | ||
| default=None, | ||
| help="encoding to use for output (default utf-8)" | ||
| help="encoding to use for output" | ||
| ) | ||
| parser.add_argument( | ||
| "-t", "--type", | ||
|
|
@@ -890,7 +927,7 @@ def main(args=None): | |
| parser.add_argument( | ||
| "-f", "--first-weekday", | ||
| type=int, default=0, | ||
| help="weekday (0 is Monday, 6 is Sunday) to start each week (default 0)" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why stop telling the user the defaults? I think it adds clarity in certain situations :) up to you however
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| help="weekday (0 is Monday, 6 is Sunday) to start each week" | ||
| ) | ||
| parser.add_argument( | ||
| "year", | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2026-04-10-20-23-22.gh-issue-148352.lrec3W.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add more colour to :mod:`calendar`'s CLI output. Patch by Hugo van Kemenade. | ||
hugovk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.


Uh oh!
There was an error while loading. Please reload this page.