Commit 9ecf9ce1 authored by Raymond Hettinger's avatar Raymond Hettinger

Patches #626105:

Replaces the _center function in the calendar
module with the center method for strings.

For situations with uneven padding, the behavior is
slightly different in that the center method puts the
extra space on the right instead of the left.
parent 644991fa
...@@ -122,13 +122,6 @@ def monthcalendar(year, month): ...@@ -122,13 +122,6 @@ def monthcalendar(year, month):
rows.append(row) rows.append(row)
return rows return rows
def _center(str, width):
"""Center a string in a field."""
n = width - len(str)
if n <= 0:
return str
return ' '*((n+1)/2) + str + ' '*((n)/2)
def prweek(theweek, width): def prweek(theweek, width):
"""Print a single week (no newline).""" """Print a single week (no newline)."""
print week(theweek, width), print week(theweek, width),
...@@ -141,7 +134,7 @@ def week(theweek, width): ...@@ -141,7 +134,7 @@ def week(theweek, width):
s = '' s = ''
else: else:
s = '%2i' % day # right-align single-digit days s = '%2i' % day # right-align single-digit days
days.append(_center(s, width)) days.append(s.center(width))
return ' '.join(days) return ' '.join(days)
def weekheader(width): def weekheader(width):
...@@ -152,7 +145,7 @@ def weekheader(width): ...@@ -152,7 +145,7 @@ def weekheader(width):
names = day_abbr names = day_abbr
days = [] days = []
for i in range(_firstweekday, _firstweekday + 7): for i in range(_firstweekday, _firstweekday + 7):
days.append(_center(names[i%7][:width], width)) days.append(names[i%7][:width].center(width))
return ' '.join(days) return ' '.join(days)
def prmonth(theyear, themonth, w=0, l=0): def prmonth(theyear, themonth, w=0, l=0):
...@@ -163,7 +156,7 @@ def month(theyear, themonth, w=0, l=0): ...@@ -163,7 +156,7 @@ def month(theyear, themonth, w=0, l=0):
"""Return a month's calendar string (multi-line).""" """Return a month's calendar string (multi-line)."""
w = max(2, w) w = max(2, w)
l = max(1, l) l = max(1, l)
s = (_center(month_name[themonth] + ' ' + `theyear`, s = ((month_name[themonth] + ' ' + `theyear`).center(
7 * (w + 1) - 1).rstrip() + 7 * (w + 1) - 1).rstrip() +
'\n' * l + weekheader(w).rstrip() + '\n' * l) '\n' * l + weekheader(w).rstrip() + '\n' * l)
for aweek in monthcalendar(theyear, themonth): for aweek in monthcalendar(theyear, themonth):
...@@ -180,8 +173,8 @@ def format3c(a, b, c, colwidth=_colwidth, spacing=_spacing): ...@@ -180,8 +173,8 @@ def format3c(a, b, c, colwidth=_colwidth, spacing=_spacing):
def format3cstring(a, b, c, colwidth=_colwidth, spacing=_spacing): def format3cstring(a, b, c, colwidth=_colwidth, spacing=_spacing):
"""Returns a string formatted from 3 strings, centered within 3 columns.""" """Returns a string formatted from 3 strings, centered within 3 columns."""
return (_center(a, colwidth) + ' ' * spacing + _center(b, colwidth) + return (a.center(colwidth) + ' ' * spacing + b.center(colwidth) +
' ' * spacing + _center(c, colwidth)) ' ' * spacing + c.center(colwidth))
def prcal(year, w=0, l=0, c=_spacing): def prcal(year, w=0, l=0, c=_spacing):
"""Print a year's calendar.""" """Print a year's calendar."""
...@@ -193,7 +186,7 @@ def calendar(year, w=0, l=0, c=_spacing): ...@@ -193,7 +186,7 @@ def calendar(year, w=0, l=0, c=_spacing):
l = max(1, l) l = max(1, l)
c = max(2, c) c = max(2, c)
colwidth = (w + 1) * 7 - 1 colwidth = (w + 1) * 7 - 1
s = _center(`year`, colwidth * 3 + c * 2).rstrip() + '\n' * l s = `year`.center(colwidth * 3 + c * 2).rstrip() + '\n' * l
header = weekheader(w) header = weekheader(w)
header = format3cstring(header, header, header, colwidth, c).rstrip() header = format3cstring(header, header, header, colwidth, c).rstrip()
for q in range(January, January+12, 3): for q in range(January, January+12, 3):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment