From 8e576ed1002b408d9784629274de667760040aee Mon Sep 17 00:00:00 2001 From: "yuichi.imori" Date: Fri, 10 May 2024 13:29:11 +0900 Subject: [PATCH 01/13] =?UTF-8?q?=E6=9C=AC=E6=97=A5=E3=81=AE=E6=97=A5?= =?UTF-8?q?=E4=BB=98=E5=8F=96=E5=BE=97=E3=81=A8=E3=82=B3=E3=83=9E=E3=83=B3?= =?UTF-8?q?=E3=83=89=E3=83=A9=E3=82=A4=E3=83=B3=E5=BC=95=E6=95=B0=E3=81=AE?= =?UTF-8?q?=E5=80=A4=E3=82=92=E5=8F=96=E5=BE=97=E3=81=99=E3=82=8B=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.calendar/cal.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100755 02.calendar/cal.rb diff --git a/02.calendar/cal.rb b/02.calendar/cal.rb new file mode 100755 index 0000000000..66a2529c09 --- /dev/null +++ b/02.calendar/cal.rb @@ -0,0 +1,17 @@ +#!/usr/bin/env ruby + +require 'date' +require 'optparse' + +# 今日の日付を取得する +today_date = Date.today +date_params = {year: today_date.year, month: today_date.month, day: today_date.day} + +# コマンドライン引数で指定された年と月を取得する +opt = OptionParser.new +command_params = {} + +opt.on('-y [VAL]') {|v| v.to_i} +opt.on('-m [VAL]') {|v| v.to_i} + +opt.parse!(ARGV, into: command_params) \ No newline at end of file From e4a992d75b57dde00c2ba84eb09d5e55a09ffeeb Mon Sep 17 00:00:00 2001 From: "yuichi.imori" Date: Fri, 10 May 2024 13:35:24 +0900 Subject: [PATCH 02/13] =?UTF-8?q?command=5Fparams=E3=81=AE=E5=BC=95?= =?UTF-8?q?=E6=95=B0=E3=82=92cmd=5Fparams=E3=81=AB=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.calendar/cal.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/02.calendar/cal.rb b/02.calendar/cal.rb index 66a2529c09..cad6960e73 100755 --- a/02.calendar/cal.rb +++ b/02.calendar/cal.rb @@ -9,7 +9,7 @@ # コマンドライン引数で指定された年と月を取得する opt = OptionParser.new -command_params = {} +cmd_params = {} opt.on('-y [VAL]') {|v| v.to_i} opt.on('-m [VAL]') {|v| v.to_i} From 9f2a29a58e88e1a8fe94c6c4391de0a8de1bc940 Mon Sep 17 00:00:00 2001 From: "yuichi.imori" Date: Fri, 10 May 2024 13:38:23 +0900 Subject: [PATCH 03/13] =?UTF-8?q?cmd=5Fparams=E3=81=B8=E3=81=AE=E5=A4=89?= =?UTF-8?q?=E6=9B=B4=E3=82=92command=5Fparams=E3=82=92=E4=BD=BF=E3=81=A3?= =?UTF-8?q?=E3=81=A6=E3=81=84=E3=81=9F=E7=AE=87=E6=89=80=E3=81=AB=E5=8F=8D?= =?UTF-8?q?=E6=98=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.calendar/cal.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/02.calendar/cal.rb b/02.calendar/cal.rb index cad6960e73..4a4232b0b9 100755 --- a/02.calendar/cal.rb +++ b/02.calendar/cal.rb @@ -14,4 +14,4 @@ opt.on('-y [VAL]') {|v| v.to_i} opt.on('-m [VAL]') {|v| v.to_i} -opt.parse!(ARGV, into: command_params) \ No newline at end of file +opt.parse!(ARGV, into: cmd_params) \ No newline at end of file From df28d2b109b5ed09ec46428439a83cb409a2912a Mon Sep 17 00:00:00 2001 From: "yuichi.imori" Date: Fri, 10 May 2024 15:05:21 +0900 Subject: [PATCH 04/13] =?UTF-8?q?=E3=82=B3=E3=83=9E=E3=83=B3=E3=83=89?= =?UTF-8?q?=E3=83=A9=E3=82=A4=E3=83=B3=E5=BC=95=E6=95=B0=E3=81=8B=E3=82=89?= =?UTF-8?q?=E5=85=A5=E5=8A=9B=E3=81=95=E3=82=8C=E3=81=9F=E6=95=B0=E5=80=A4?= =?UTF-8?q?=E3=82=92date=5Fparams=E3=81=AB=E4=B8=8A=E6=9B=B8=E3=81=8D?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.calendar/cal.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/02.calendar/cal.rb b/02.calendar/cal.rb index 4a4232b0b9..48da88fecd 100755 --- a/02.calendar/cal.rb +++ b/02.calendar/cal.rb @@ -14,4 +14,18 @@ opt.on('-y [VAL]') {|v| v.to_i} opt.on('-m [VAL]') {|v| v.to_i} -opt.parse!(ARGV, into: cmd_params) \ No newline at end of file +opt.parse!(ARGV, into: cmd_params) + +# date_paramsにコマンドライン引数で取得した年と月を反映 +unless cmd_params[:y] == nil + if cmd_params[:y] < 1970 || 2100 < cmd_params[:y] # 年数が対応範囲内か確認する + raise OutOfRangeError, "年数が範囲外です。1970年から2100年までの年数を指定してください。" + end + date_params[:year] = cmd_params[:y] +end +unless cmd_params[:m] == nil + if cmd_params[:m] < 1 || 12 < cmd_params[:y] # 正しい月が入力されているか確認する + raise InvalidRangeError, "正しい月を指定してください。" + end + date_params[:month] = cmd_params[:m] +end From d8bc34ec1e4e453933bb5057244c999a15e1fded Mon Sep 17 00:00:00 2001 From: "yuichi.imori" Date: Fri, 10 May 2024 15:59:39 +0900 Subject: [PATCH 05/13] =?UTF-8?q?=E8=AA=AD=E3=81=BF=E8=BE=BC=E3=82=80?= =?UTF-8?q?=E5=BC=95=E6=95=B0=E3=81=AE=E9=96=93=E9=81=95=E3=81=84=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.calendar/cal.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/02.calendar/cal.rb b/02.calendar/cal.rb index 48da88fecd..9debe1d5cf 100755 --- a/02.calendar/cal.rb +++ b/02.calendar/cal.rb @@ -24,7 +24,7 @@ date_params[:year] = cmd_params[:y] end unless cmd_params[:m] == nil - if cmd_params[:m] < 1 || 12 < cmd_params[:y] # 正しい月が入力されているか確認する + if cmd_params[:m] < 1 || 12 < cmd_params[:m] # 正しい月が入力されているか確認する raise InvalidRangeError, "正しい月を指定してください。" end date_params[:month] = cmd_params[:m] From 72cfae48785576615d518976040ad66e7fcdcf09 Mon Sep 17 00:00:00 2001 From: "yuichi.imori" Date: Fri, 10 May 2024 16:00:11 +0900 Subject: [PATCH 06/13] =?UTF-8?q?=E3=82=AB=E3=83=AC=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=E3=81=AE=E8=A1=A8=E7=A4=BA=E9=83=A8=E5=88=86=E3=81=AE?= =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.calendar/cal.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/02.calendar/cal.rb b/02.calendar/cal.rb index 9debe1d5cf..ef072ca0ee 100755 --- a/02.calendar/cal.rb +++ b/02.calendar/cal.rb @@ -29,3 +29,31 @@ end date_params[:month] = cmd_params[:m] end + +# 指定された月の1日目の曜日と日数を計算する +first_date = Date.new(date_params[:year], date_params[:month], 1) +last_date = Date.new(date_params[:year], date_params[:month], -1) + +# カレンダーの表示部分を作成する +# x月 xxxxの出力、曜日の出力 +puts " #{date_params[:month]}月 #{date_params[:year]}" +puts "日 月 火 水 木 金 土" + +# カレンダー部分の出力 +# 余白部分の空白を予め出力しておく +dow_num = first_date.wday +dow_num.times { print " " } + +# 日にちの出力 +(first_date.day..last_date.day).each do |num| + print num.to_s.rjust(2) + if dow_num == 6 + print "\n" + dow_num = 0 + elsif num != last_date.day + print " " + dow_num += 1 + else + print "\n" + end +end \ No newline at end of file From eb8e1e1d84f9d00fe2de0c21067a0c767681bd29 Mon Sep 17 00:00:00 2001 From: "yuichi.imori" Date: Fri, 10 May 2024 16:18:37 +0900 Subject: [PATCH 07/13] =?UTF-8?q?=E4=BB=8A=E6=97=A5=E3=81=AE=E6=97=A5?= =?UTF-8?q?=E4=BB=98=E3=81=AE=E5=87=BA=E5=8A=9B=E7=B5=90=E6=9E=9C=E3=81=AE?= =?UTF-8?q?=E8=89=B2=E3=82=92=E3=82=92=E5=8F=8D=E8=BB=A2=E3=81=95=E3=81=9B?= =?UTF-8?q?=E3=82=8B=E3=83=97=E3=83=AD=E3=82=B0=E3=83=A9=E3=83=A0=E3=82=92?= =?UTF-8?q?=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.calendar/cal.rb | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/02.calendar/cal.rb b/02.calendar/cal.rb index ef072ca0ee..5040f4c802 100755 --- a/02.calendar/cal.rb +++ b/02.calendar/cal.rb @@ -17,17 +17,30 @@ opt.parse!(ARGV, into: cmd_params) # date_paramsにコマンドライン引数で取得した年と月を反映 +# コマンドラインで入力された月、年が同一かどうか判定する件数を追加 +same_ym = true + unless cmd_params[:y] == nil + same_ym = false if cmd_params[:y] < 1970 || 2100 < cmd_params[:y] # 年数が対応範囲内か確認する raise OutOfRangeError, "年数が範囲外です。1970年から2100年までの年数を指定してください。" end - date_params[:year] = cmd_params[:y] + if date_params[:year] == cmd_params[:y] + same_ym = true + else + date_params[:year] = cmd_params[:y] + end end unless cmd_params[:m] == nil + same_ym = false if cmd_params[:m] < 1 || 12 < cmd_params[:m] # 正しい月が入力されているか確認する raise InvalidRangeError, "正しい月を指定してください。" end - date_params[:month] = cmd_params[:m] + if date_params[:month] == cmd_params[:m] + same_ym = true + else + date_params[:month] = cmd_params[:m] + end end # 指定された月の1日目の曜日と日数を計算する @@ -39,6 +52,12 @@ puts " #{date_params[:month]}月 #{date_params[:year]}" puts "日 月 火 水 木 金 土" +# 歓迎要件をこなす +#色の反転 +def reverse_cmpcolor(text) + "\e[7m#{text}\e[0m" +end + # カレンダー部分の出力 # 余白部分の空白を予め出力しておく dow_num = first_date.wday @@ -46,7 +65,11 @@ # 日にちの出力 (first_date.day..last_date.day).each do |num| - print num.to_s.rjust(2) + if same_ym == true && num == date_params[:day] + print reverse_cmpcolor(num.to_s.rjust(2)) + else + print num.to_s.rjust(2) + end if dow_num == 6 print "\n" dow_num = 0 From f340860668db973c2a1f16882d89b6c87eb74e4e Mon Sep 17 00:00:00 2001 From: "yuichi.imori" Date: Mon, 20 May 2024 12:12:23 +0900 Subject: [PATCH 08/13] =?UTF-8?q?=E3=81=84=E3=81=9F=E3=81=A0=E3=81=84?= =?UTF-8?q?=E3=81=9F=E3=83=95=E3=82=A3=E3=83=BC=E3=83=89=E3=83=90=E3=83=83?= =?UTF-8?q?=E3=82=AF=E3=81=AE=E5=8F=8D=E6=98=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.calendar/cal.rb | 76 ++++++++++++++++------------------------------ 1 file changed, 27 insertions(+), 49 deletions(-) diff --git a/02.calendar/cal.rb b/02.calendar/cal.rb index 5040f4c802..d79d976012 100755 --- a/02.calendar/cal.rb +++ b/02.calendar/cal.rb @@ -3,80 +3,58 @@ require 'date' require 'optparse' -# 今日の日付を取得する -today_date = Date.today -date_params = {year: today_date.year, month: today_date.month, day: today_date.day} +today_date = Date.today +date_params = { year: today_date.year, month: today_date.month, day: today_date.day } -# コマンドライン引数で指定された年と月を取得する opt = OptionParser.new cmd_params = {} opt.on('-y [VAL]') {|v| v.to_i} opt.on('-m [VAL]') {|v| v.to_i} - opt.parse!(ARGV, into: cmd_params) -# date_paramsにコマンドライン引数で取得した年と月を反映 -# コマンドラインで入力された月、年が同一かどうか判定する件数を追加 -same_ym = true +target_params = { year: date_params[:year], month: date_params[:month] } -unless cmd_params[:y] == nil - same_ym = false - if cmd_params[:y] < 1970 || 2100 < cmd_params[:y] # 年数が対応範囲内か確認する +if cmd_params[:y] # 年が指定されているとき + if cmd_params[:y] < 1970 || 2100 < cmd_params[:y] raise OutOfRangeError, "年数が範囲外です。1970年から2100年までの年数を指定してください。" end - if date_params[:year] == cmd_params[:y] - same_ym = true - else - date_params[:year] = cmd_params[:y] - end + target_params[:year] = cmd_params[:y] end -unless cmd_params[:m] == nil - same_ym = false - if cmd_params[:m] < 1 || 12 < cmd_params[:m] # 正しい月が入力されているか確認する + +if cmd_params[:m] # 月が指定されているとき + if cmd_params[:m] < 1 || 12 < cmd_params[:m] raise InvalidRangeError, "正しい月を指定してください。" end - if date_params[:month] == cmd_params[:m] - same_ym = true - else - date_params[:month] = cmd_params[:m] - end + target_params[:month] = cmd_params[:m] end -# 指定された月の1日目の曜日と日数を計算する -first_date = Date.new(date_params[:year], date_params[:month], 1) -last_date = Date.new(date_params[:year], date_params[:month], -1) +first_date = Date.new(target_params[:year], target_params[:month], 1) +last_date = Date.new(target_params[:year], target_params[:month], -1) + # カレンダーの表示部分を作成する # x月 xxxxの出力、曜日の出力 -puts " #{date_params[:month]}月 #{date_params[:year]}" +puts " #{target_params[:month]}月 #{target_params[:year]}" puts "日 月 火 水 木 金 土" -# 歓迎要件をこなす -#色の反転 -def reverse_cmpcolor(text) - "\e[7m#{text}\e[0m" -end - -# カレンダー部分の出力 -# 余白部分の空白を予め出力しておく -dow_num = first_date.wday -dow_num.times { print " " } +# 1日目までの左上の空白を出力 +day_of_week_index = first_date.wday +day_of_week_index.times { print " " * 3 } # 日にちの出力 -(first_date.day..last_date.day).each do |num| - if same_ym == true && num == date_params[:day] - print reverse_cmpcolor(num.to_s.rjust(2)) +(first_date..last_date).each do |current_date| + if current_date.year == date_params[:year] && current_date.month == date_params[:month] && current_date.day == date_params[:day] + print "\e[7m#{current_date.day.to_s.rjust(2)}\e[0m" # 今日の日付の色を反転 else - print num.to_s.rjust(2) + print current_date.day.to_s.rjust(2) end - if dow_num == 6 + if day_of_week_index == 6 # 曜日リセット print "\n" - dow_num = 0 - elsif num != last_date.day - print " " - dow_num += 1 + day_of_week_index = 0 else - print "\n" + print " " + day_of_week_index += 1 end -end \ No newline at end of file +end +print "\n" unless day_of_week_index == 0 # 最後の行の改行 From fffae6173f067a0a6218c445d277974ff267cb5c Mon Sep 17 00:00:00 2001 From: "yuichi.imori" Date: Tue, 21 May 2024 11:18:34 +0900 Subject: [PATCH 09/13] =?UTF-8?q?Revert=20"=E3=81=84=E3=81=9F=E3=81=A0?= =?UTF-8?q?=E3=81=84=E3=81=9F=E3=83=95=E3=82=A3=E3=83=BC=E3=83=89=E3=83=90?= =?UTF-8?q?=E3=83=83=E3=82=AF=E3=81=AE=E5=8F=8D=E6=98=A0"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit f340860668db973c2a1f16882d89b6c87eb74e4e. --- 02.calendar/cal.rb | 76 ++++++++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 27 deletions(-) diff --git a/02.calendar/cal.rb b/02.calendar/cal.rb index d79d976012..5040f4c802 100755 --- a/02.calendar/cal.rb +++ b/02.calendar/cal.rb @@ -3,58 +3,80 @@ require 'date' require 'optparse' -today_date = Date.today -date_params = { year: today_date.year, month: today_date.month, day: today_date.day } +# 今日の日付を取得する +today_date = Date.today +date_params = {year: today_date.year, month: today_date.month, day: today_date.day} +# コマンドライン引数で指定された年と月を取得する opt = OptionParser.new cmd_params = {} opt.on('-y [VAL]') {|v| v.to_i} opt.on('-m [VAL]') {|v| v.to_i} + opt.parse!(ARGV, into: cmd_params) -target_params = { year: date_params[:year], month: date_params[:month] } +# date_paramsにコマンドライン引数で取得した年と月を反映 +# コマンドラインで入力された月、年が同一かどうか判定する件数を追加 +same_ym = true -if cmd_params[:y] # 年が指定されているとき - if cmd_params[:y] < 1970 || 2100 < cmd_params[:y] +unless cmd_params[:y] == nil + same_ym = false + if cmd_params[:y] < 1970 || 2100 < cmd_params[:y] # 年数が対応範囲内か確認する raise OutOfRangeError, "年数が範囲外です。1970年から2100年までの年数を指定してください。" end - target_params[:year] = cmd_params[:y] + if date_params[:year] == cmd_params[:y] + same_ym = true + else + date_params[:year] = cmd_params[:y] + end end - -if cmd_params[:m] # 月が指定されているとき - if cmd_params[:m] < 1 || 12 < cmd_params[:m] +unless cmd_params[:m] == nil + same_ym = false + if cmd_params[:m] < 1 || 12 < cmd_params[:m] # 正しい月が入力されているか確認する raise InvalidRangeError, "正しい月を指定してください。" end - target_params[:month] = cmd_params[:m] + if date_params[:month] == cmd_params[:m] + same_ym = true + else + date_params[:month] = cmd_params[:m] + end end -first_date = Date.new(target_params[:year], target_params[:month], 1) -last_date = Date.new(target_params[:year], target_params[:month], -1) - +# 指定された月の1日目の曜日と日数を計算する +first_date = Date.new(date_params[:year], date_params[:month], 1) +last_date = Date.new(date_params[:year], date_params[:month], -1) # カレンダーの表示部分を作成する # x月 xxxxの出力、曜日の出力 -puts " #{target_params[:month]}月 #{target_params[:year]}" +puts " #{date_params[:month]}月 #{date_params[:year]}" puts "日 月 火 水 木 金 土" -# 1日目までの左上の空白を出力 -day_of_week_index = first_date.wday -day_of_week_index.times { print " " * 3 } +# 歓迎要件をこなす +#色の反転 +def reverse_cmpcolor(text) + "\e[7m#{text}\e[0m" +end + +# カレンダー部分の出力 +# 余白部分の空白を予め出力しておく +dow_num = first_date.wday +dow_num.times { print " " } # 日にちの出力 -(first_date..last_date).each do |current_date| - if current_date.year == date_params[:year] && current_date.month == date_params[:month] && current_date.day == date_params[:day] - print "\e[7m#{current_date.day.to_s.rjust(2)}\e[0m" # 今日の日付の色を反転 +(first_date.day..last_date.day).each do |num| + if same_ym == true && num == date_params[:day] + print reverse_cmpcolor(num.to_s.rjust(2)) else - print current_date.day.to_s.rjust(2) + print num.to_s.rjust(2) end - if day_of_week_index == 6 # 曜日リセット + if dow_num == 6 print "\n" - day_of_week_index = 0 - else + dow_num = 0 + elsif num != last_date.day print " " - day_of_week_index += 1 + dow_num += 1 + else + print "\n" end -end -print "\n" unless day_of_week_index == 0 # 最後の行の改行 +end \ No newline at end of file From c96d4ef28a0159473886d5908f97391577d30e1a Mon Sep 17 00:00:00 2001 From: "yuichi.imori" Date: Tue, 21 May 2024 11:46:28 +0900 Subject: [PATCH 10/13] =?UTF-8?q?Reapply=20"=E3=81=84=E3=81=9F=E3=81=A0?= =?UTF-8?q?=E3=81=84=E3=81=9F=E3=83=95=E3=82=A3=E3=83=BC=E3=83=89=E3=83=90?= =?UTF-8?q?=E3=83=83=E3=82=AF=E3=81=AE=E5=8F=8D=E6=98=A0"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit fffae6173f067a0a6218c445d277974ff267cb5c. --- 02.calendar/cal.rb | 76 ++++++++++++++++------------------------------ 1 file changed, 27 insertions(+), 49 deletions(-) diff --git a/02.calendar/cal.rb b/02.calendar/cal.rb index 5040f4c802..d79d976012 100755 --- a/02.calendar/cal.rb +++ b/02.calendar/cal.rb @@ -3,80 +3,58 @@ require 'date' require 'optparse' -# 今日の日付を取得する -today_date = Date.today -date_params = {year: today_date.year, month: today_date.month, day: today_date.day} +today_date = Date.today +date_params = { year: today_date.year, month: today_date.month, day: today_date.day } -# コマンドライン引数で指定された年と月を取得する opt = OptionParser.new cmd_params = {} opt.on('-y [VAL]') {|v| v.to_i} opt.on('-m [VAL]') {|v| v.to_i} - opt.parse!(ARGV, into: cmd_params) -# date_paramsにコマンドライン引数で取得した年と月を反映 -# コマンドラインで入力された月、年が同一かどうか判定する件数を追加 -same_ym = true +target_params = { year: date_params[:year], month: date_params[:month] } -unless cmd_params[:y] == nil - same_ym = false - if cmd_params[:y] < 1970 || 2100 < cmd_params[:y] # 年数が対応範囲内か確認する +if cmd_params[:y] # 年が指定されているとき + if cmd_params[:y] < 1970 || 2100 < cmd_params[:y] raise OutOfRangeError, "年数が範囲外です。1970年から2100年までの年数を指定してください。" end - if date_params[:year] == cmd_params[:y] - same_ym = true - else - date_params[:year] = cmd_params[:y] - end + target_params[:year] = cmd_params[:y] end -unless cmd_params[:m] == nil - same_ym = false - if cmd_params[:m] < 1 || 12 < cmd_params[:m] # 正しい月が入力されているか確認する + +if cmd_params[:m] # 月が指定されているとき + if cmd_params[:m] < 1 || 12 < cmd_params[:m] raise InvalidRangeError, "正しい月を指定してください。" end - if date_params[:month] == cmd_params[:m] - same_ym = true - else - date_params[:month] = cmd_params[:m] - end + target_params[:month] = cmd_params[:m] end -# 指定された月の1日目の曜日と日数を計算する -first_date = Date.new(date_params[:year], date_params[:month], 1) -last_date = Date.new(date_params[:year], date_params[:month], -1) +first_date = Date.new(target_params[:year], target_params[:month], 1) +last_date = Date.new(target_params[:year], target_params[:month], -1) + # カレンダーの表示部分を作成する # x月 xxxxの出力、曜日の出力 -puts " #{date_params[:month]}月 #{date_params[:year]}" +puts " #{target_params[:month]}月 #{target_params[:year]}" puts "日 月 火 水 木 金 土" -# 歓迎要件をこなす -#色の反転 -def reverse_cmpcolor(text) - "\e[7m#{text}\e[0m" -end - -# カレンダー部分の出力 -# 余白部分の空白を予め出力しておく -dow_num = first_date.wday -dow_num.times { print " " } +# 1日目までの左上の空白を出力 +day_of_week_index = first_date.wday +day_of_week_index.times { print " " * 3 } # 日にちの出力 -(first_date.day..last_date.day).each do |num| - if same_ym == true && num == date_params[:day] - print reverse_cmpcolor(num.to_s.rjust(2)) +(first_date..last_date).each do |current_date| + if current_date.year == date_params[:year] && current_date.month == date_params[:month] && current_date.day == date_params[:day] + print "\e[7m#{current_date.day.to_s.rjust(2)}\e[0m" # 今日の日付の色を反転 else - print num.to_s.rjust(2) + print current_date.day.to_s.rjust(2) end - if dow_num == 6 + if day_of_week_index == 6 # 曜日リセット print "\n" - dow_num = 0 - elsif num != last_date.day - print " " - dow_num += 1 + day_of_week_index = 0 else - print "\n" + print " " + day_of_week_index += 1 end -end \ No newline at end of file +end +print "\n" unless day_of_week_index == 0 # 最後の行の改行 From f59cd388999b9005bb8f1d780fd33d67cbd5c2af Mon Sep 17 00:00:00 2001 From: "yuichi.imori" Date: Tue, 21 May 2024 11:58:23 +0900 Subject: [PATCH 11/13] =?UTF-8?q?date=5Fparams=E3=82=92=E5=89=8A=E9=99=A4?= =?UTF-8?q?=E3=81=97=E3=80=81=E4=BB=8A=E6=97=A5=E3=81=AE=E6=97=A5=E4=BB=98?= =?UTF-8?q?=E3=81=AE=E5=8F=96=E5=BE=97=E3=82=92today=5Fdate=E3=81=A7?= =?UTF-8?q?=E8=A1=8C=E3=81=86=E3=82=88=E3=81=86=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.calendar/cal.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/02.calendar/cal.rb b/02.calendar/cal.rb index d79d976012..b8586a8a22 100755 --- a/02.calendar/cal.rb +++ b/02.calendar/cal.rb @@ -4,7 +4,6 @@ require 'optparse' today_date = Date.today -date_params = { year: today_date.year, month: today_date.month, day: today_date.day } opt = OptionParser.new cmd_params = {} @@ -13,7 +12,7 @@ opt.on('-m [VAL]') {|v| v.to_i} opt.parse!(ARGV, into: cmd_params) -target_params = { year: date_params[:year], month: date_params[:month] } +target_params = { year: today_date.year, month: today_date.month } if cmd_params[:y] # 年が指定されているとき if cmd_params[:y] < 1970 || 2100 < cmd_params[:y] @@ -44,7 +43,7 @@ # 日にちの出力 (first_date..last_date).each do |current_date| - if current_date.year == date_params[:year] && current_date.month == date_params[:month] && current_date.day == date_params[:day] + if current_date.year == today_date.year && current_date.month == today_date.month && current_date.day == today_date.day print "\e[7m#{current_date.day.to_s.rjust(2)}\e[0m" # 今日の日付の色を反転 else print current_date.day.to_s.rjust(2) From 9f7186e03a97169bbe1cf3d5d44433721c22bb16 Mon Sep 17 00:00:00 2001 From: "yuichi.imori" Date: Tue, 21 May 2024 12:00:51 +0900 Subject: [PATCH 12/13] =?UTF-8?q?day=5Fof=5Fweek=5Findex=E3=82=92=E5=89=8A?= =?UTF-8?q?=E9=99=A4=E3=81=97=E3=80=81=E5=9C=9F=E6=9B=9C=E6=97=A5=E3=81=AE?= =?UTF-8?q?=E5=88=A4=E5=AE=9A=E3=81=ABsaturday=3F=E3=82=92=E4=BD=BF?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.calendar/cal.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/02.calendar/cal.rb b/02.calendar/cal.rb index b8586a8a22..8fb84e6fd1 100755 --- a/02.calendar/cal.rb +++ b/02.calendar/cal.rb @@ -38,8 +38,7 @@ puts "日 月 火 水 木 金 土" # 1日目までの左上の空白を出力 -day_of_week_index = first_date.wday -day_of_week_index.times { print " " * 3 } +first_date.wday.times { print " " * 3 } # 日にちの出力 (first_date..last_date).each do |current_date| @@ -48,12 +47,10 @@ else print current_date.day.to_s.rjust(2) end - if day_of_week_index == 6 # 曜日リセット + if current_date.saturday? # 曜日リセット print "\n" - day_of_week_index = 0 else print " " - day_of_week_index += 1 end end -print "\n" unless day_of_week_index == 0 # 最後の行の改行 +print "\n" unless last_date.saturday? # 最後の行の改行 From 3739d1cadf0198299fc28514a3365964c26b7eb3 Mon Sep 17 00:00:00 2001 From: "yuichi.imori" Date: Tue, 21 May 2024 15:53:31 +0900 Subject: [PATCH 13/13] =?UTF-8?q?=E4=BB=8A=E6=97=A5=E3=81=AE=E6=97=A5?= =?UTF-8?q?=E4=BB=98=E3=81=AE=E5=88=A4=E5=AE=9A=E6=96=B9=E6=B3=95=E3=82=92?= =?UTF-8?q?Date=E3=82=AF=E3=83=A9=E3=82=B9=E5=90=8C=E5=A3=AB=E3=81=A7?= =?UTF-8?q?=E5=88=A4=E5=AE=9A=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.calendar/cal.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/02.calendar/cal.rb b/02.calendar/cal.rb index 8fb84e6fd1..04e0eb1d52 100755 --- a/02.calendar/cal.rb +++ b/02.calendar/cal.rb @@ -42,7 +42,7 @@ # 日にちの出力 (first_date..last_date).each do |current_date| - if current_date.year == today_date.year && current_date.month == today_date.month && current_date.day == today_date.day + if current_date == today_date print "\e[7m#{current_date.day.to_s.rjust(2)}\e[0m" # 今日の日付の色を反転 else print current_date.day.to_s.rjust(2)