Class: Fasti::Formatter
- Inherits:
-
Object
- Object
- Fasti::Formatter
- 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)
Instance Method Summary collapse
-
#format_month(calendar) ⇒ String
Formats a single month calendar with headers and color coding.
-
#format_quarter(calendars) ⇒ String
Formats three calendars side-by-side in a quarter view.
-
#format_year(year, country:, start_of_week: :sunday) ⇒ String
Formats a complete year view with all 12 months in quarters.
-
#initialize(styles: {}) ⇒ Formatter
constructor
Creates a new formatter instance.
Constructor Details
#initialize(styles: {}) ⇒ Formatter
Creates a new formatter instance.
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.
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.
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.
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 |