Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions lib/calendar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# 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
puts "\n"
end
end
11 changes: 11 additions & 0 deletions lib/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "thor"
require "date"
require_relative "miti"
require_relative "calendar"
require_relative "miti/cli/next"

module Miti
Expand Down Expand Up @@ -77,6 +78,16 @@ def difference(date1, date2)
@shell.say(Miti.differentiate(date1, date2), :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
Expand Down