Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
torshinalexey committed Apr 22, 2023
1 parent a4cbaec commit 7de459d
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,27 @@ func printHeader(w io.Writer, d time.Time) {

// printMonth prints a calendar month to the given writer for the given date.
func printMonth(w io.Writer, d time.Time) {
var b strings.Builder
b.Grow(100)
// Iterate over each day in the month.
for day := d.AddDate(0, 0, -d.Day()+1); day.Month() == d.Month(); day = day.AddDate(0, 0, 1) {
curDay := day.Day()
// If this is the first day of the month and it is not a Monday, add padding to align the first week.
if day.AddDate(0, 0, -1).Month() != day.Month() && day.Weekday() != time.Monday {
fmt.Fprint(w, strings.Repeat(" ", int(day.Weekday())-1))
b.WriteString(strings.Repeat(" ", int(day.Weekday())-1))
}
// Print the day number with an asterisk if it is the current day.
if day.Day() == d.Day() {
fmt.Fprintf(w, "%.2d*", day.Day())
if curDay == d.Day() {
b.WriteString(fmt.Sprintf("%.2d*", curDay))
continue
}
// Print the day number with linebreak if it is Sunday.
// Print the day number with line break if it is Sunday.
if day.Weekday() == time.Sunday {
fmt.Fprintf(w, "%.2d\n", day.Day())
b.WriteString(fmt.Sprintf("%.2d\n", curDay))
continue
}
// Print the day number with a trailing space.
fmt.Fprintf(w, "%.2d ", day.Day())
b.WriteString(fmt.Sprintf("%.2d ", curDay))
}
fmt.Fprint(w, b.String())
}

0 comments on commit 7de459d

Please sign in to comment.