/
cal
executable file
·330 lines (273 loc) · 8.15 KB
/
cal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env ruby
$LOAD_PATH << "lib"
require 'zeller'
require 'assemble_month'
require 'print_cal'
#Calendar calls zeller for single month, or annual for a year
#annual calls zeller once for each month
#zeller finds the first day of a month and calls assemble_month (which creates an array of 8 strings for each month's calendar)
#assemble_month then either calls the single_print or annual_print method
#single_print loops through the month array and prints each line
#annual_print loops through the entire calendar and prints in the specified format (e.x. [4x3], [2x6], [3x3, 1x2, 1x1], etc.)
#Calendar is called at end of file
class Calendar
LINE_WIDTH = 20
MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
attr_accessor :month
attr_accessor :year
attr_accessor :calendar_array
attr_accessor :format_array
def initialize(*args)
input = args[0]
#current month (no input)
if input.length == 0
time = Time.new
@month = time.month
@year = time.year
zeller(@month, @year, false)
#self.zeller(@month, @year, false)
#unspecified number of rows
elsif input.length == 1
year = input[0].dup
year.chomp!
@year = year.to_i
@month = "annual"
@calendar_array = []
@format_array = [[4, 3]]
self.annual(@year)
#specified number of rows
elsif input.length > 2
if input.length == 3 && input[1] == "rows"
rows = input[2].dup
rows = rows.to_i
year = input[0].dup
year.chomp!
@month = "annual"
@year = year.to_i
@calendar_array = []
#bad row argument
if rows < 2 || rows > 12
puts "INPUT ERROR"
puts "Number of rows must be between 2 and 12"
#even number of rows
elsif rows == 2 || rows == 3 || rows ==4 || rows == 6 || rows == 12
columns = 12/rows
@format_array = [[rows, columns]]
self.annual(@year)
#5 rows
else
if rows == 5
@format_array = [[3, 3], [1, 2], [1, 1]]
self.annual(@year)
#7-11 rows
else
temp_rows = 12 % rows
extra_rows = rows - (12 % rows)
rows = temp_rows
@format_array = [[rows, 2], [extra_rows, 1]]
self.annual(@year)
end
end
#bad input
else
puts "INPUT ERROR"
puts "Accepted Format is: 2014, 01 2014, January 2014, or 2014 rows 2"
end
#single month
else
month = input[0].dup
year = input[1].dup
month.chomp!
year.chomp!
@month = month
@year = year.to_i
zeller(@month, @year, false)
#self.zeller(@month, @year, false)
end
end #of initialize
def annual(year)
month = 0
12.times do
month += 1
zeller(month, year, true)
#self.zeller(month, year, true)
end
end
#PRIVATE METHODS------------------------------------------------------------------
#---------------------------------------------------------------------------------
private
def zeller(month, year, annual)
#checks to see if the written format rather than the number format
if month.to_s.length > 3
month.downcase!
month.capitalize!
#converts to number format
month = (MONTHS.index(month)) + 1
end
q = 1
m = month.to_i
k = year%100
j = year/100
y = year.to_i
if m < 3
y -= 1
m += 12
h = ( (q + (((m+1)*26)/10) + y + (y/4) + (6*(y/100)) + (y/400)) % 7 )
else
h = ( (q + ((13*(m+1))/5) + k + (k/4) + (j/4) + (5*j) ) % 7 )
end
h -= 1
assemble_month(h, m, year, annual)
end #of zeller
#--------------------------------------------------------
def assemble_month(h, m, year, annual)
#contains array of strings to be assembled when printed
this_month = []
#sets month name and number of days
day_array = [31, "x", 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
m -= 12 if m > 12
month = MONTHS[m-1]
days = day_array[m-1]
#checks for leap year and sets days for February
if days == "x"
if year % 4 != 0
days = 28
elsif year % 100 != 0 || year % 400 == 0
days = 29
else
days = 28
end
end
#CAL HEADER-------
#total_length = 20
month_length = month.length
header = ""
if @month == "annual"
margin_space = (LINE_WIDTH - month_length) / 2
margin_space.times { header << " " }
header << month
else
margin_space = (LINE_WIDTH - (month_length + 5)) / 2
margin_space.times { header << " " }
header << month
header << " "
header << year.to_s
end
#push header to array
this_month << header
this_month << "Su Mo Tu We Th Fr Sa"
#CAL DATES-------
day = 1
count = 0
row = ""
h = 6 if h == -1
leading_space = (h*3) + 1
count += leading_space
leading_space.times do
row << " "
end
#if loops starts without printing this first the spacing will be incorrect
row << "1"
count += 1
day += 1
#finish 1st row
while count < LINE_WIDTH
#adds extra space if one digit number
row << " " if day < 10
row << " "
row << day.to_s
day += 1
count += 3
end
#push 1st row to array
this_month << row
row_count = 1
#other rows
while day <= days
count = 0
row = ""
day_count = 0
while day_count <= 7
day_count += 1
while count < LINE_WIDTH
#adds extra space if one digit number
row << " " if day < 10
row << day.to_s
day += 1
count += 3
row << " " if count < 19
end
end
#chops at last day
last_day = row.index(days.to_s)
row = row[0,last_day+2] if last_day
#push other rows to array
this_month << row
row_count += 1
end
#adds line breaks if necessary
(6 - row_count).times {this_month << ""}
if annual == true
@calendar_array << this_month
annual_print if @calendar_array.length == 12
else
single_print(this_month)
end
end #of assemble_month
#--------------------------------------------------------
def single_print(this_month)
this_month.each do |line|
puts line
end
end
def annual_print
#index keeps track of how many months have already been printed
index = 0
print_title = true
#loops through to set specified format of rows and columns
@format_array.each do |rows, columns|
# prints calendar header if nothing has been printed yet
if print_title == true
title_line = ""
title_space = ( (LINE_WIDTH*columns) + (2*(columns-1)) -4 ) / 2
(title_space - 1).times {title_line << " "}
title_line << @year.to_s
puts title_line
line_break = ""
puts line_break
end
#loop for number of rows at specified format
rows.times do #rows
print_title = false
#contains month arrays currently being printed
temp_months = []
column_index = 0
columns.times do
temp_months << @calendar_array[index + column_index]
column_index += 1
end
line_index = 0
8.times do #lines per row
temp_string = ""
inner_index = 0
columns.times do #loops through current months per row and concats them to one line
temp_month = temp_months[inner_index]
temp_line = temp_month[line_index]
temp_string << temp_line
#adjusts spacing
if inner_index != columns - 1
(LINE_WIDTH - temp_line.length).times {temp_string << " "}
end
temp_string << " " if inner_index < columns - 1
#prints current line
puts temp_string if inner_index == columns - 1
inner_index += 1
end #of inner loop
line_index += 1
end #of row loop (concats current months onto same lines)
index += columns
end #of column loop
end #of format loop
end #of annual_print
end #of class
new_cal = Calendar.new(ARGV)