From b5e339c51a569a7d695b8e029eb18287eb97c027 Mon Sep 17 00:00:00 2001 From: Rikuoh Date: Tue, 19 Mar 2024 07:43:50 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=AB=E3=83=AC=E3=83=B3=E3=83=80=E3=83=BC?= =?UTF-8?q?=E6=94=B9=E4=BF=AE=E9=80=94=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cal.rb | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/cal.rb b/cal.rb index 5166bf8..55dbcf3 100755 --- a/cal.rb +++ b/cal.rb @@ -1,34 +1,37 @@ #!/usr/bin/ruby +# frozen_string_literal: true + require 'date' require 'optparse' -## 年月の初期設定(現在の年月) year = Date.today.year month = Date.today.month +week = %w[日 月 火 水 木 金 土] -## コマンドラインオプション指定で年月を置き換える opt = OptionParser.new 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 -totaldate = Date.new(year, month, -1).day -week = %w[日 月 火 水 木 金 土] - -## カレンダーを出力する -puts month.to_s.rjust(7) + '月 ' + year.to_s +first_day = Date.new(year, month, 1) +last_day = Date.new(year, month, -1) +puts "#{month.to_s.rjust(7)}月\s#{year}" puts week.join(' ') -print ' ' * startwday -days = startwday -(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 +print "\s\s\s" * first_day.wday + +(first_day..last_day).each do |date| + day_string = date.day.to_s.rjust(2, ' ') + day_string = "\e[7m#{day_string}\e[0m" if date == Date.today + print day_string + puts if date.saturday? end +puts + +# print "#{date.day.to_s.rjust(2)}\s" +# date += 1 +# print "\n" if date.sunday? +# if date == Date.today && ARGV == [] # 引数なしなら現在日をハイライトする +# print "\e[7m\e[7m" +# else +# print "\e[0m" +# end