From a354bf5148dcf89706772917a81eb5cb38552b06 Mon Sep 17 00:00:00 2001 From: sanzay_mdr Date: Wed, 13 Sep 2023 10:12:55 +0545 Subject: [PATCH 1/3] feat: implement CLI calendar for english date and nepali miti --- lib/calendar.rb | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/cli.rb | 10 +++++++++ 2 files changed, 68 insertions(+) create mode 100644 lib/calendar.rb diff --git a/lib/calendar.rb b/lib/calendar.rb new file mode 100644 index 0000000..856022f --- /dev/null +++ b/lib/calendar.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +require "date" + +# Class to render English and Nepali Calendar +class Calendar + attr_reader :english_date_today, :nepali_date_today + + def initialize + @english_date_today = Date.today + @nepali_date_today = Miti.to_bs(english_date_today.to_s) + end + + def english_calendar + first_day_of_month = Date.new(english_date_today.year, english_date_today.month, 1) + last_day_of_month = Date.new(english_date_today.year, english_date_today.month, -1) + + puts "Calendar for #{english_date_today.strftime("%B %Y")}" + puts "Sun Mon Tue Wed Thu Fri Sat" + + print_calendar(first_day_of_month, last_day_of_month) + end + + def nepali_calendar + barsa = nepali_date_today.barsa + mahina = nepali_date_today.mahina + number_of_days = Miti::Data::NEPALI_YEAR_MONTH_HASH[barsa][mahina - 1] + first_day_of_month = Miti.to_ad("#{barsa}/#{mahina}/01") + last_day_of_month = Miti.to_ad("#{barsa}/#{mahina}/#{number_of_days}") + + puts "Calendar for #{Miti::NepaliDate.months_in_english[mahina - 1]} #{barsa}" + puts "Sun Mon Tue Wed Thu Fri Sat" + + print_calendar(first_day_of_month, last_day_of_month) + end + + def print_calendar(first_day_of_month, last_day_of_month) + (first_day_of_month..last_day_of_month).each_with_index do |date, idx| + idx += 1 + if date == first_day_of_month + print " " * (date.wday * 4) # Add spaces for the first week + end + + if date == english_date_today + # Highlight the current day in a different color + print "\e[1;32m#{idx.to_s.rjust(3)}\e[0m" # Green text + else + print idx.to_s.rjust(3) # Right-align day number + end + + if date.wday == 6 + puts "\n" # Start a new line for the next week on Saturday + else + print " " # Add space between days + end + end + end +end diff --git a/lib/cli.rb b/lib/cli.rb index 9a9a8fb..c7eb4a0 100644 --- a/lib/cli.rb +++ b/lib/cli.rb @@ -73,6 +73,16 @@ def next @shell.say(current_month_last_description, :cyan) end + desc "english_calendar", "show current month's english calendar" + def english_calendar + Calendar.new.english_calendar + end + + desc "nepali_calendar", "show current month's nepali calendar" + def nepali_calendar + Calendar.new.nepali_calendar + end + no_commands do def self.exit_on_failure? true From 5f8ea7cf4c6900236bfd8d560fe175ddbd86681a Mon Sep 17 00:00:00 2001 From: sanzay manandhar Date: Thu, 18 Jan 2024 09:42:48 +0545 Subject: [PATCH 2/3] require 'calendar' class on Miti::CLI --- lib/cli.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/cli.rb b/lib/cli.rb index c7eb4a0..16bf819 100644 --- a/lib/cli.rb +++ b/lib/cli.rb @@ -3,6 +3,7 @@ require "thor" require "date" require_relative "miti" +require_relative "calendar" module Miti # class to integrate CLI From eecfb1d7289cc9ba7d40e4183b1c160b611f2864 Mon Sep 17 00:00:00 2001 From: sanzay manandhar Date: Thu, 18 Jan 2024 09:44:06 +0545 Subject: [PATCH 3/3] print a new empty line after the iteration completes, such that SDOUT will print a clean calendar --- lib/calendar.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/calendar.rb b/lib/calendar.rb index 856022f..1fc801d 100644 --- a/lib/calendar.rb +++ b/lib/calendar.rb @@ -54,5 +54,6 @@ def print_calendar(first_day_of_month, last_day_of_month) print " " # Add space between days end end + puts "\n" end end