-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcal.rb
More file actions
188 lines (162 loc) · 4.82 KB
/
Copy pathcal.rb
File metadata and controls
188 lines (162 loc) · 4.82 KB
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
# frozen_string_literal: true
require 'date'
class Calender
def initialize
@usage = "Usage: cal [general options] [[-y year] [-m month]]\n#{format('%48s', 'cal [general options] [-y] [[month] year]')}\n#{format('%46s', 'cal [general options] [-m month] [year]')}"
if ARGV.empty?
@year = Date.today.year
@month = Date.today.month
else
validate_argv
set_date
end
end
# カレンダーを出力
def output
calender = if @month
set_monthry_calender
else
# @month が nil の場合は年間カレンダーを出力する
set_yearly_calender
end
puts calender
end
# 正しく引数が渡されているかチェック
private
def validate_argv
# 実装しているオプションは -m と -y のみ
illegal_option_matchs = ARGV.grep(/-\S{2,}|-[^my]/)
unless illegal_option_matchs.empty?
puts "cal: illegal option -- #{illegal_option_matchs[0].gsub(/(^.*?)(-)/, '')}\n#{@usage}"
exit(1)
end
# 同じオプションは複数指定できない
duplicated = ARGV.select { |e| ARGV.count(e) > 1 }.select.grep(/-[my]/)
unless duplicated.empty?
puts "cal: Double #{duplicated[0]} specified.\n#{@usage}"
exit(1)
end
# 引数は最大で4つまで
if ARGV.length > 4
puts "cal: Too many arguments.\n#{@usage}"
exit(1)
end
# 引数が4つの場合は -y -m のオプションが必須
if ARGV.length == 4
unless ARGV.include?('-m') || ARGV.include?('-y')
puts @usage
exit(1)
end
end
# ハイフンのない文字列が引き渡されている
illegal_option_matchs = ARGV.grep(/^[^-0-9].*?/)
unless illegal_option_matchs.empty?
puts "cal: not a valid #{illegal_option_matchs[0]}"
exit(1)
end
end
# コマンドライン引数をもとにカレンダー対象日を決定する
def set_date
if ARGV.length == 1
case ARGV[0]
when '-y'
# -y オプションだけ渡された場合はその年のすべての月を表示する
@year = Date.today.year
@month = nil
when '-m'
# -m オプションだけ渡された場合は処理できずエラー
puts "cal: option requires an argument -- m\n#{@usage}"
exit(1)
else
# 数字だけ渡された場合はその年のすべての月を表示する
@year = ARGV[0].to_i
@month = nil
end
end
if ARGV.length == 2
case ARGV[0]
when '-y'
@year = ARGV[1].to_i
@month = nil
when '-m'
@year = Date.today.year
@month = ARGV[1].to_i
else
@year = ARGV[1].to_i
@month = ARGV[0].to_i
end
end
if ARGV.length == 3
case ARGV[0]
when '-y'
# -y オプションは数字を2つとれない
puts "cal: -y together a given month is not supported.\n#{@usage}"
exit(1)
when '-m'
@year = ARGV[2].to_i
@month = ARGV[1].to_i
else
# 数字だけで3つの引数はエラー
puts @usage
exit(1)
end
end
if ARGV.length == 4
ARGV.each_with_index do |arg, i|
case arg
when '-y'
@year = ARGV[i + 1].to_i
when '-m'
@month = ARGV[i + 1].to_i
end
end
end
# year は 1..9999
unless (1..9999).cover?(@year.to_i)
puts "cal: year '#{@year}' not in range 1..9999\n#{@usage}"
exit(1)
end
# month は 1..12
if @month && !(1..12).cover?(@month)
puts "cal: #{@month} is neither a month number (1..12)\n#{@usage}"
exit(1)
end
end
def set_monthry_calender
first_date = Date.new(@year, @month, 1)
last_date = Date.new(@year, @month, -1)
" #{@month}月 #{@year} " + make_calender(first_date, last_date)
end
def set_yearly_calender
calender_ary = []
(1..12).each do |n|
fotter = if n != 12
"\n\n"
else
"\n"
end
first_date = Date.new(@year, n, 1)
last_date = Date.new(@year, n, -1)
calender_ary.push(" #{n}月 " + make_calender(first_date, last_date) + fotter)
end
" #{@year} \n" + calender_ary.join('')
end
def make_calender(first_date, last_date)
calender_ary = []
d = 1
(0..41).each do |i|
if i < first_date.wday || i > (last_date.day + first_date.wday - 1)
calender_ary[i] = ' '
else
calender_ary[i] = if Date.new(@year, first_date.month, d).saturday?
"#{format('%02d', d)}\n"
else
format('%02d', d)
end
d += 1
end
end
"\n 日 月 火 水 木 金 土\n #{calender_ary.join(' ')}"
end
end
Calender.new.output