Class: Wahy::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/wahy/cli.rb

Class Method Summary collapse

Class Method Details

.display_chapter_list(chapters, lang) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/wahy/cli.rb', line 129

def self.display_chapter_list(chapters, lang)
  lang_display = lang.to_s.downcase.start_with?('t') ? "TURKISH" : "ENGLISH"

  puts "=" * 50
  puts " QURAN CHAPTERS (#{lang_display}) ".center(50).cyan.bold
  puts "=" * 50

  puts sprintf("%-10s | %-35s", "ID", "CHAPTER NAME").yellow.bold
  puts "-" * 50

  chapters.each do |c|
    id = c['ChapterID']
    name = c['ChapterName']
    puts sprintf("%-10s | %-35s", id, name)
  end

  puts "=" * 50
end

.run(options) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/wahy/cli.rb', line 57

def self.run(options)
  begin
    data = Wahy.new_data(options[:lang])
  rescue => e
    puts "Error: #{e.message}".red
    exit 1
  end

  quran = Wahy.chapters_data(data)

  if options[:list_chapters]
    display_chapter_list(quran, options[:lang])
    exit
  end

  scripture_input = options[:scripture].to_s

  # Check chapter number range for numeric input
  if scripture_input =~ /^\d+$/
    chapter_num = scripture_input.to_i
    if chapter_num < 1 || chapter_num > 114
      puts "Error: Chapter number must be between 1 and 114. Got #{chapter_num}.".red
      exit 1
    end
  end

  scripture_id = options[:scripture]
  chapter_node = Wahy.scripture_data(quran, scripture_id)

  unless chapter_node
    puts "Error: Scripture '#{options[:scripture]}' could not be found.".red
    exit 1
  end

  chapter_id = chapter_node['ChapterID']
  chapter_name = chapter_node['ChapterName']
  verses = chapter_node.xpath('Verse')
  total_verses = verses.length

  selected_verses = []
  if options[:ayah].to_s.downcase == 'all'
    selected_verses = verses
  else
    target_ayah = options[:ayah].to_i
    match = verses.find { |v| v['VerseID'] == target_ayah.to_s }
    if match
      selected_verses = [match]
    else
      puts "Error: Ayah ##{target_ayah} not found in Chapter #{chapter_id} (#{chapter_name}).".red
      puts "This chapter has #{total_verses} ayah(s). Valid range: 1-#{total_verses}.".red
      exit 1
    end
  end

  terminal_width = 75
  puts "=" * terminal_width
  header_title = "Chapter #{chapter_id}: #{chapter_name}"
  puts header_title.center(terminal_width).upcase.cyan.bold
  puts "=" * terminal_width
  puts ""

  selected_verses.each do |v|
    verse_id = v['VerseID']
    verse_text = v.text.strip

    print "[#{verse_id}] ".green.bold
    puts verse_text.white
    puts ""
  end
  puts "=" * terminal_width
end

.start(args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/wahy/cli.rb', line 8

def self.start(args)
  options = {
    lang: 'eng',
    scripture: '1',
    ayah: 'all',
    list_chapters: false
  }

  opt_parser = OptionParser.new do |opts|
    opts.banner = "Usage: wahy [options]"

    opts.on("-l", "--lang LANG", "Pick language ('eng'|'tur' or 'en'|'tr') [Default: eng]") do |l|
      options[:lang] = l
    end

    opts.on("-s", "--scripture SCRIPTURE", "Pick scripture name or number (1-114) [Default: 1]") do |s|
      options[:scripture] = s
    end

    opts.on("-a", "--ayah AYAH", "Pick sign/ayah number or 'all' [Default: all]") do |a|
      options[:ayah] = a
    end

    opts.on("--list-chapters", "List all chapters in a table format for the selected language") do
      options[:list_chapters] = true
    end

    opts.on("-v", "--version", "Prints the current version") do
      puts "wahy version #{Wahy::VERSION}"
      exit
    end

    opts.on("-h", "--help", "Prints this help menu") do
      puts opts
      exit
    end
  end

  begin
    opt_parser.parse!(args)
  rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e
    puts "CLI Error: #{e.message}".red
    puts opt_parser
    exit 1
  end

  run(options)
end