From 593a6cd4ac5158ca33fbacfc6eaf21db7bfdb052 Mon Sep 17 00:00:00 2001 From: seicho Date: Mon, 28 Aug 2023 22:06:37 +0900 Subject: [PATCH 1/7] =?UTF-8?q?=E3=82=AB=E3=83=AC=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=E3=83=97=E3=83=AD=E3=82=B0=E3=83=A9=E3=83=A0=E8=AA=B2?= =?UTF-8?q?=E9=A1=8C=E6=8F=90=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.calendar/my_calender.rb | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 02.calendar/my_calender.rb diff --git a/02.calendar/my_calender.rb b/02.calendar/my_calender.rb new file mode 100644 index 0000000000..5dc4d9d7cd --- /dev/null +++ b/02.calendar/my_calender.rb @@ -0,0 +1,57 @@ +require "optparse" +require "date" +opt = OptionParser.new +parameters = ARGV.getopts("y:", "m:") + +def print_calender(date,e) + counter = 0 + empty = e + today_day = 0 + + print "#{date.month}月 #{date.year}".center(21) + "\n" + + ["日","月","火","水","木","金","土"].each do |x| + print x.rjust(2) + end + print "\n" + + #1週目のインデントを作成 + empty.times do |x| + print "".rjust(3) + counter += 1 + end + + #渡されたDateオブジェクトの年・月が現在の年・月と一致しているか判定 + if date.year == Date.today.year && date.month == Date.today.month + today_day = Date.today.day + end + + (1..date.day).each do |x| + if x == today_day + print "\e[31m\e[47m#{x.to_s.rjust(3)}\e[0m" + else + print x.to_s.rjust(3) + end + counter += 1 + if counter == 7 + print "\n" + counter = 0 + end + end + puts "" + #最後に%が表示されてしまうのを防ぐために追加 +end + +#コマンドラインオプションに応じてprint_calenderに渡す引数を定義 +if parameters["y"] && parameters["m"] + date = Date.new(parameters["y"].to_i,parameters["m"].to_i,-1) + indent_for_first_week = Date.new(parameters["y"].to_i,parameters["m"].to_i,1).wday +elsif parameters["m"] + date = Date.new(Date.today.year,parameters["m"].to_i,-1) + indent_for_first_week = Date.new(Date.today.year,parameters["m"].to_i,1).wday +else + date = Date.new(Date.today.year,Date.today.month,-1) + indent_for_first_week = Date.new(Date.today.year,Date.today.month,1).wday +end + +print_calender(date, indent_for_first_week) From d5296b93f9361550ee314f855302d490eb27365e Mon Sep 17 00:00:00 2001 From: seicho Date: Mon, 28 Aug 2023 22:18:30 +0900 Subject: [PATCH 2/7] =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E5=87=A6?= =?UTF-8?q?=E7=90=86=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.calendar/my_calender.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/02.calendar/my_calender.rb b/02.calendar/my_calender.rb index 5dc4d9d7cd..b45212ba5e 100644 --- a/02.calendar/my_calender.rb +++ b/02.calendar/my_calender.rb @@ -41,6 +41,11 @@ def print_calender(date,e) puts "" #最後に%が表示されてしまうのを防ぐために追加 end +#1-12以外の月が入力された際の処理 +if parameters["m"].to_i < 1 || parameters["m"].to_i > 12 + print "cal: #{parameters["m"]}は月を示す数字ではありません" + exit +end #コマンドラインオプションに応じてprint_calenderに渡す引数を定義 if parameters["y"] && parameters["m"] From c0302ef63671519caec89f9779d121e6955a488e Mon Sep 17 00:00:00 2001 From: seicho Date: Tue, 29 Aug 2023 22:11:56 +0900 Subject: [PATCH 3/7] =?UTF-8?q?=E4=B8=8D=E9=81=A9=E5=88=87=E3=81=AA?= =?UTF-8?q?=E6=9C=88=E3=81=AE=E5=85=A5=E5=8A=9B=E3=81=AB=E5=AF=BE=E3=81=99?= =?UTF-8?q?=E3=82=8B=E5=87=A6=E7=90=86=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.calendar/my_calender.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/02.calendar/my_calender.rb b/02.calendar/my_calender.rb index b45212ba5e..983937b571 100644 --- a/02.calendar/my_calender.rb +++ b/02.calendar/my_calender.rb @@ -5,7 +5,7 @@ def print_calender(date,e) counter = 0 - empty = e + first_line_indent = e today_day = 0 print "#{date.month}月 #{date.year}".center(21) + "\n" @@ -16,7 +16,7 @@ def print_calender(date,e) print "\n" #1週目のインデントを作成 - empty.times do |x| + first_line_indent.times do |x| print "".rjust(3) counter += 1 end @@ -41,17 +41,22 @@ def print_calender(date,e) puts "" #最後に%が表示されてしまうのを防ぐために追加 end + #1-12以外の月が入力された際の処理 -if parameters["m"].to_i < 1 || parameters["m"].to_i > 12 - print "cal: #{parameters["m"]}は月を示す数字ではありません" - exit +def isMonthValueInvalid?(parameters) + if parameters["m"].to_i < 1 || parameters["m"].to_i > 12 + print "cal: #{parameters["m"]}は月を示す数字ではありません" + exit + end end #コマンドラインオプションに応じてprint_calenderに渡す引数を定義 if parameters["y"] && parameters["m"] + isMonthValueInvalid?(parameters) date = Date.new(parameters["y"].to_i,parameters["m"].to_i,-1) indent_for_first_week = Date.new(parameters["y"].to_i,parameters["m"].to_i,1).wday elsif parameters["m"] + isMonthValueInvalid?(parameters) date = Date.new(Date.today.year,parameters["m"].to_i,-1) indent_for_first_week = Date.new(Date.today.year,parameters["m"].to_i,1).wday else From 5e34433fde84b16ce7175c37e94cc634d77303af Mon Sep 17 00:00:00 2001 From: seicho Date: Sun, 10 Sep 2023 23:30:26 +0900 Subject: [PATCH 4/7] =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=E7=AE=87=E6=89=80=E3=81=AE=E4=BF=AE=E6=AD=A3=EF=BC=9A=E8=A1=A8?= =?UTF-8?q?=E8=A8=98=E3=82=92=E8=8B=B1=E8=AA=9E=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/my_calender.rb | 81 ++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 39 deletions(-) diff --git a/02.calendar/my_calender.rb b/02.calendar/my_calender.rb index 983937b571..13c7fd3b06 100644 --- a/02.calendar/my_calender.rb +++ b/02.calendar/my_calender.rb @@ -3,65 +3,68 @@ opt = OptionParser.new parameters = ARGV.getopts("y:", "m:") -def print_calender(date,e) +def print_calender(first_day, last_day) counter = 0 - first_line_indent = e - today_day = 0 + first_line_indent = first_day.wday + today = 0 - print "#{date.month}月 #{date.year}".center(21) + "\n" + print "#{first_day.strftime("%B")} #{first_day.year}".center(23) + "\n" - ["日","月","火","水","木","金","土"].each do |x| - print x.rjust(2) + ["Su","Mo","Tu","We","Th","Fr","Sa"].each do |x| + print x.rjust(3) end - print "\n" + puts - #1週目のインデントを作成 first_line_indent.times do |x| print "".rjust(3) counter += 1 end - #渡されたDateオブジェクトの年・月が現在の年・月と一致しているか判定 - if date.year == Date.today.year && date.month == Date.today.month - today_day = Date.today.day + if first_day.year == Date.today.year && first_day.month == Date.today.month + today = Date.today end - (1..date.day).each do |x| - if x == today_day - print "\e[31m\e[47m#{x.to_s.rjust(3)}\e[0m" + (first_day..last_day).each do |x| + if x == today + print "\e[7m#{x.day.to_s.rjust(3)}\e[0m" else - print x.to_s.rjust(3) - end - counter += 1 - if counter == 7 - print "\n" - counter = 0 + print x.day.to_s.rjust(3) end + puts if x.saturday? end - puts "" - #最後に%が表示されてしまうのを防ぐために追加 + puts end -#1-12以外の月が入力された際の処理 -def isMonthValueInvalid?(parameters) - if parameters["m"].to_i < 1 || parameters["m"].to_i > 12 - print "cal: #{parameters["m"]}は月を示す数字ではありません" +def validate_month(month) + if month.to_i < 1 || month.to_i > 12 + print "cal: #{month}は月を示す数字ではありません" exit end end #コマンドラインオプションに応じてprint_calenderに渡す引数を定義 -if parameters["y"] && parameters["m"] - isMonthValueInvalid?(parameters) - date = Date.new(parameters["y"].to_i,parameters["m"].to_i,-1) - indent_for_first_week = Date.new(parameters["y"].to_i,parameters["m"].to_i,1).wday -elsif parameters["m"] - isMonthValueInvalid?(parameters) - date = Date.new(Date.today.year,parameters["m"].to_i,-1) - indent_for_first_week = Date.new(Date.today.year,parameters["m"].to_i,1).wday -else - date = Date.new(Date.today.year,Date.today.month,-1) - indent_for_first_week = Date.new(Date.today.year,Date.today.month,1).wday -end +#if parameters["y"] && parameters["m"] +# validate_month(parameters) +# date = Date.new(parameters["y"].to_i,parameters["m"].to_i,-1) +# indent_for_first_week = Date.new(parameters["y"].to_i,parameters["m"].to_i,1).wday +#elsif parameters["m"] +# validate_month(parameters) +# date = Date.new(Date.today.year,parameters["m"].to_i,-1) +# indent_for_first_week = Date.new(Date.today.year,parameters["m"].to_i,1).wday +#else +# date = Date.new(Date.today.year,Date.today.month,-1) +# indent_for_first_week = Date.new(Date.today.year,Date.today.month,1).wday +#end + +input_year = parameters['y'] +input_month = parameters['m'] +validate_month(input_month) if input_month +today = Date.today + +year = input_year || today.year +month = input_month || today.month + +first_day = Date.new(year.to_i, month.to_i, 1) +last_day = Date.new(year.to_i, month.to_i, -1) -print_calender(date, indent_for_first_week) +print_calender(first_day, last_day) From d5fcf4f7a83b08008384a3307a6d7116234f97ae Mon Sep 17 00:00:00 2001 From: seicho Date: Sun, 10 Sep 2023 23:36:21 +0900 Subject: [PATCH 5/7] =?UTF-8?q?=E8=8B=B1=E8=AA=9E=E8=A1=A8=E8=A8=98?= =?UTF-8?q?=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/my_calender.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/02.calendar/my_calender.rb b/02.calendar/my_calender.rb index 13c7fd3b06..3c928b320a 100644 --- a/02.calendar/my_calender.rb +++ b/02.calendar/my_calender.rb @@ -37,7 +37,8 @@ def print_calender(first_day, last_day) def validate_month(month) if month.to_i < 1 || month.to_i > 12 - print "cal: #{month}は月を示す数字ではありません" + print "cal: #{month} is neither a month number (1..12) nor a name" + puts exit end end From 0220ba2a890c54882cd34ac562bc4433c4a798c5 Mon Sep 17 00:00:00 2001 From: seicho Date: Sun, 10 Sep 2023 23:39:17 +0900 Subject: [PATCH 6/7] =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=E3=81=AE=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.calendar/my_calender.rb | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/02.calendar/my_calender.rb b/02.calendar/my_calender.rb index 3c928b320a..92b25c50b7 100644 --- a/02.calendar/my_calender.rb +++ b/02.calendar/my_calender.rb @@ -43,20 +43,6 @@ def validate_month(month) end end -#コマンドラインオプションに応じてprint_calenderに渡す引数を定義 -#if parameters["y"] && parameters["m"] -# validate_month(parameters) -# date = Date.new(parameters["y"].to_i,parameters["m"].to_i,-1) -# indent_for_first_week = Date.new(parameters["y"].to_i,parameters["m"].to_i,1).wday -#elsif parameters["m"] -# validate_month(parameters) -# date = Date.new(Date.today.year,parameters["m"].to_i,-1) -# indent_for_first_week = Date.new(Date.today.year,parameters["m"].to_i,1).wday -#else -# date = Date.new(Date.today.year,Date.today.month,-1) -# indent_for_first_week = Date.new(Date.today.year,Date.today.month,1).wday -#end - input_year = parameters['y'] input_month = parameters['m'] validate_month(input_month) if input_month From 188768832519253e7d52dd587edc056d690b157e Mon Sep 17 00:00:00 2001 From: seicho Date: Mon, 11 Sep 2023 12:53:59 +0900 Subject: [PATCH 7/7] =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=E7=AE=87=E6=89=80=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.calendar/my_calender.rb | 43 +++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/02.calendar/my_calender.rb b/02.calendar/my_calender.rb index 92b25c50b7..8ec74a0a6d 100644 --- a/02.calendar/my_calender.rb +++ b/02.calendar/my_calender.rb @@ -1,31 +1,26 @@ -require "optparse" -require "date" -opt = OptionParser.new -parameters = ARGV.getopts("y:", "m:") +# frozen_string_literal: true + +require 'optparse' +require 'date' +parameters = ARGV.getopts('y:', 'm:') def print_calender(first_day, last_day) - counter = 0 - first_line_indent = first_day.wday - today = 0 - - print "#{first_day.strftime("%B")} #{first_day.year}".center(23) + "\n" - - ["Su","Mo","Tu","We","Th","Fr","Sa"].each do |x| + first_line_indent = first_day.wday + + print "#{first_day.strftime('%B')} #{first_day.year}".center(23) + puts + + %w[Su Mo Tu We Th Fr Sa].each do |x| print x.rjust(3) end puts - first_line_indent.times do |x| - print "".rjust(3) - counter += 1 + first_line_indent.times do + print ' ' end - if first_day.year == Date.today.year && first_day.month == Date.today.month - today = Date.today - end - (first_day..last_day).each do |x| - if x == today + if x == Date.today print "\e[7m#{x.day.to_s.rjust(3)}\e[0m" else print x.day.to_s.rjust(3) @@ -36,11 +31,11 @@ def print_calender(first_day, last_day) end def validate_month(month) - if month.to_i < 1 || month.to_i > 12 - print "cal: #{month} is neither a month number (1..12) nor a name" - puts - exit - end + return unless month.to_i < 1 || month.to_i > 12 + + print "cal: #{month} is neither a month number (1..12) nor a name" + puts + exit end input_year = parameters['y']