From aa92ee1d420349bd32664598f9a10a7e3a82a9aa Mon Sep 17 00:00:00 2001 From: Rikuoh Date: Sat, 9 Mar 2024 09:13:53 +0900 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cal.rb | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/cal.rb b/cal.rb index e9de621..5166bf8 100755 --- a/cal.rb +++ b/cal.rb @@ -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