Class: Honyomi::CLI

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

Instance Method Summary collapse

Instance Method Details

#add(*args) ⇒ Object



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
# File 'lib/honyomi/cli.rb', line 22

def add(*args)
  core = Core.new
  core.load_database

  if options[:title] && args.size > 1
    puts "Arguments is specified more than once, but there is --title option"
    return
  end

  args.each do |arg|
    begin
      book, status = core.add(arg, options)
    rescue Errno::ENOENT => e
      puts "Not found 'pdftotext' command (poppler, xpdf)"
      exit -1
    end

    unless book
      puts "Not exist: #{arg}"
      next
    end

    case status
    when :update
      puts "U #{book.id.to_s} #{book.title} (#{book.page_num} pages)"
    when :add
      puts "A #{book.id.to_s} #{book.title} (#{book.page_num} pages)"
    else
      raise
    end
  end
end

#edit(*args) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/honyomi/cli.rb', line 71

def edit(*args)
  core = Core.new
  core.load_database

  begin 
    book_id = args[0].to_i
    core.edit(book_id, options)
    puts core.list([book_id])
  rescue HonyomiError => e
    puts e
    exit -1
  end
end

#image(*args) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/honyomi/cli.rb', line 134

def image(*args)
  core = Core.new
  core.load_database

  args.each do |id|
    core.image(id.to_i, { verbose: true })
  end
end

#initObject



9
10
11
12
13
14
15
16
17
# File 'lib/honyomi/cli.rb', line 9

def init
  begin
    core = Core.new
    core.init_database
    puts "Create database to \"#{core.db_path}\""
  rescue Groonga::FileExists
    puts "Database already exists in \"#{core.db_path}\""
  end
end

#list(*args) ⇒ Object



115
116
117
118
119
120
# File 'lib/honyomi/cli.rb', line 115

def list(*args)
  core = Core.new
  core.load_database

  puts core.list(args.map{|v| v.to_i }, options)
end

#move(old_path, new_path) ⇒ Object



144
145
146
147
148
149
# File 'lib/honyomi/cli.rb', line 144

def move(old_path, new_path)
  core = Core.new
  core.load_database

  core.move(old_path, new_path)
end

#remove(*args) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/honyomi/cli.rb', line 86

def remove(*args)
  core = Core.new
  core.load_database

  args.each do |id|
    puts core.list([id.to_i])
    core.remove(id.to_i)
  end
end

#search(*args) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/honyomi/cli.rb', line 97

def search(*args)
  core = Core.new
  core.load_database

  results, snippet = core.search(args.join(" "))

  puts "#{results.size} matches"
  results.map do |page|
    puts "--- #{page.book.title} (#{page.page_no} page) ---"
    snippet.execute(page.text).each do |segment|
      puts segment.gsub("\n", "")
    end
  end
end

#webObject



127
128
129
130
131
# File 'lib/honyomi/cli.rb', line 127

def web
  core = Core.new
  core.load_database      
  core.web(options)
end