This commit is contained in:
Rikuoh Tsujitani 2024-03-09 09:13:53 +09:00
parent fb7ee36275
commit aa92ee1d42
Signed by: riq0h
GPG key ID: 010F09DEA298C717

25
cal.rb
View file

@ -1,27 +1,34 @@
#!/usr/bin/ruby
require 'date'
require 'optparse'
## 年月の初期設定(現在の年月)
year = Date.today.year
month = Date.today.month
## コマンドラインオプション指定で年月を置き換える
opt = OptionParser.new
opt.on('-m int') { |v| month = v.to_i }
opt.on('-y int') { |v| year = v.to_i }
opt.parse!(ARGV)
opt.on('-m month') { |v| month = v.to_i }
opt.on('-y year') { |v| year = v.to_i }
opt.parse(ARGV)
startwday = Date.new(year, month, 1).wday # その月の最初の曜日
enddate = Date.new(year, month, -1).day # その月の最後の曜日
## 月初日の曜日、当該月の総日数、週の配列
startwday = Date.new(year, month, 1).wday
totaldate = Date.new(year, month, -1).day
week = %w[日 月 火 水 木 金 土]
puts month.to_s.rjust(8) + '月 ' + year.to_s
## カレンダーを出力する
puts month.to_s.rjust(7) + '月 ' + year.to_s
puts week.join(' ')
print ' ' * startwday
days = startwday
(1..enddate).each do |day|
(1..totaldate).each do |day|
print day.to_s.rjust(2) + ' '
days += 1
print "\n" if days % 7 == 0
if day == Date.today.day - 1 && ARGV == []
print "\e[30m\e[47m"
else
print "\e[0m"
end
end