Class: Fasti::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/fasti/formatter.rb

Overview

Handles calendar formatting and display with color-coded output.

This class provides various calendar display formats (month, quarter, year) with ANSI color coding for holidays, weekends, and the current date. Holiday detection is handled by the Calendar class using the holidays gem.

## Styling

  • Holidays: Bold text

  • Sundays: Bold text

  • Today: Inverted background/text colors (combined with above styles)

Examples:

Basic month formatting

formatter = Formatter.new
calendar = Calendar.new(2024, 6, country: :jp)
puts formatter.format_month(calendar)

Year view

formatter = Formatter.new
puts formatter.format_year(2024, start_of_week: :sunday, country: :jp)

Instance Method Summary collapse

Constructor Details

#initialize(styles: {}) ⇒ Formatter

Creates a new formatter instance.

Examples:

Formatter.new(styles: config.style)

Parameters:

  • styles (Hash<Symbol, Style>) (defaults to: {})

    Styles for different day types Keys can be :sunday, :monday, …, :saturday, :holiday, :today



30
31
32
33
# File 'lib/fasti/formatter.rb', line 30

def initialize(styles: {})
  @styles = styles
  @style_cache = {}
end

Instance Method Details

#format_month(calendar) ⇒ String

Formats a single month calendar with headers and color coding.

Displays the month/year header, day abbreviations, and calendar grid with appropriate color coding for holidays, weekends, and today.

Examples:

calendar = Calendar.new(2024, 6, country: :jp)
formatter.format_month(calendar)
# Output:
#      June 2024
#
# Su Mo Tu We Th Fr Sa
#                    1
#  2  3  4  5  6  7  8
# ...

Parameters:

  • calendar (Calendar)

    The calendar to format

Returns:

  • (String)

    Formatted calendar string with ANSI color codes



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fasti/formatter.rb', line 53

def format_month(calendar)
  output = []

  # Month/Year header - centered
  header = calendar.month_year_header
  output << header.center(20)
  output << ""

  # Day headers
  output << calendar.day_headers.join(" ")

  # Calendar grid
  calendar.calendar_grid.each do |week|
    week_str = week.map {|day|
      format_day(day, calendar)
    }.join(" ")
    output << week_str
  end

  output.join("\n")
end

#format_quarter(calendars) ⇒ String

Formats three calendars side-by-side in a quarter view.

Displays three months horizontally with aligned headers and grids. Typically used for showing current month with adjacent months.

Examples:

Quarter view

calendars = [
  Calendar.new(2024, 5, country: :jp),
  Calendar.new(2024, 6, country: :jp),
  Calendar.new(2024, 7, country: :jp)
]
formatter.format_quarter(calendars)

Parameters:

  • calendars (Array<Calendar>)

    Array of exactly 3 calendars to display

Returns:

  • (String)

    Formatted quarter view string

Raises:

  • (ArgumentError)

    If not exactly 3 calendars provided



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fasti/formatter.rb', line 91

def format_quarter(calendars)
  raise ArgumentError, "Expected 3 calendars for quarter view" unless calendars.length == 3

  output = []

  # Headers for all three months
  headers = calendars.map {|cal| cal.month_year_header.center(20) }
  output << headers.join("  ")
  output << ""

  # Day headers for all three months
  day_headers = calendars.map {|cal| cal.day_headers.join(" ") }
  output << day_headers.join("  ")

  # Calendar grids side by side
  max_rows = calendars.map {|cal| cal.calendar_grid.length }.max

  (0...max_rows).each do |row_index|
    row_parts = calendars.map {|cal|
      if row_index < cal.calendar_grid.length
        week = cal.calendar_grid[row_index]
        week.map {|day| format_day(day, cal) }.join(" ")
      else
        " " * 20 # Empty space for shorter months
      end
    }
    output << row_parts.join("  ")
  end

  output.join("\n")
end

#format_year(year, country:, start_of_week: :sunday) ⇒ String

Formats a complete year view with all 12 months in quarters.

Displays the full year as 4 quarters, each containing 3 months side-by-side. Each quarter is separated by blank lines.

Examples:

Full year display

formatter.format_year(2024, start_of_week: :sunday, country: :jp)
# Displays all 12 months in 4 rows of 3 months each

Parameters:

  • year (Integer)

    The year to display

  • start_of_week (Symbol) (defaults to: :sunday)

    Week start preference (:sunday or :monday)

  • country (String)

    Country code for holiday context

Returns:

  • (String)

    Formatted year view string



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/fasti/formatter.rb', line 136

def format_year(year, country:, start_of_week: :sunday)
  output = []

  # Year header
  output << year.to_s.center(64)
  output << ""

  # Process 4 quarters (3 months each)
  quarters = []
  (1..12).each_slice(3) do |months|
    calendars = months.map {|month| Calendar.new(year, month, start_of_week:, country:) }
    quarters << format_quarter(calendars)
  end

  output << quarters.join("\n\n")
  output.join("\n")
end