Class: Wahy::CLI

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

Class Method Summary collapse

Class Method Details

Helper method to print a single verse



82
83
84
85
86
87
# File 'lib/wahy/cli.rb', line 82

def self.print_sign(sign)
  verse_id = sign['VerseID']
  # CDATA text parsing and whitespace stripping
  text = sign.text.strip
  puts "[#{verse_id}] ".colorize(:cyan).bold + text
end

.start(args) ⇒ Object



6
7
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/wahy/cli.rb', line 6

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

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

    opts.on("-l", "--lang LANGUAGE", "Language selection ('tur' or 'eng') - Default: eng") do |l|
      options[:lang] = l
    end

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

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

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

  opt_parser.parse!(args)

  begin
    # Fetch Data
    doc = Wahy.new_data(options[:lang])
    chapters = Wahy.chapters_data(doc)
    chapter = Wahy.scripture_data(chapters, options[:scripture])

    if chapter.nil?
      puts "Error: Scripture '#{options[:scripture]}' not found.".colorize(:red)
      exit 1
    end

    # Header preparation
    chapter_id = chapter['ChapterID']
    chapter_name = chapter['ChapterName']
    title = "#{chapter_id}. #{chapter_name}"

    # Get terminal width for centering (fallback to 80 if it fails)
    term_width = `tput cols`.to_i rescue 80
    term_width = 80 if term_width == 0

    # Print centered and colored header
    puts "\n"
    puts title.center(term_width).colorize(:green).bold
    puts ("=" * title.length).center(term_width).colorize(:green)
    puts "\n"

    signs = Wahy.sign_data(chapter)

    # Print verses
    if options[:ayah].to_s.downcase == 'all'
      signs.each { |sign| print_sign(sign) }
    else
      sign = Wahy.take_specific_sign(signs, options[:ayah])
      if sign.nil?
        puts "Error: Ayah '#{options[:ayah]}' not found in this scripture.".colorize(:red)
      else
        print_sign(sign)
      end
    end
    puts "\n"
  rescue => e
    puts "An error occurred: #{e.message}".colorize(:red)
    exit 1
  end
end