Module: DayOneKindle::CLI

Defined in:
lib/dayone-kindle/cli.rb

Class Method Summary collapse

Class Method Details

.dialog(value) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/dayone-kindle/cli.rb', line 3

def self.dialog(value)
  script = <<-END
  tell app "System Events"
display dialog "#{value}"
  end tell
END

  system('osascript ' + script.split(/\n/).map { |line| "-e '#{line}'" }.join(' ') + '> /dev/null 2>&1')
end

.optionsObject



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
# File 'lib/dayone-kindle/cli.rb', line 13

def self.options
  return @options if @options

  options = {
    ask_confirmation: true,
    tags: [],
    dry_run: false,
    archive: true
  }

  optparse = OptionParser.new do |opts|
    opts.banner = 'Usage: dayone-kindle [options]'

    opts.on '--dry', 'Outputs highlights instead of importing them (use for testing)' do
      options[:dry_run] = true
    end

    opts.on '-t', '--tags reading,quote', Array, 'Tags to be saved with highlights' do |t|
      options[:tags] = t
    end

    opts.on '--auto-confirm', 'Do not ask for confirmation before import' do
      options[:ask_confirmation] = false
    end

    opts.on '--no-archive', 'Do not archive imported highlights on device' do
      options[:archive] = false
    end
  end

  optparse.parse!

  @options = options
end

.runObject



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
78
79
# File 'lib/dayone-kindle/cli.rb', line 48

def self.run
  tags = options[:tags]

  DayOneKindle::Device.find_at('/Volumes').each do |kindle|
    next if kindle.highlights.empty?

    if options[:ask_confirmation]
      label = "#{kindle.name} detected. Highlights will be imported to Day One."
      next unless dialog(label)
    end

    store = DayOneKindle::DataStore.new(kindle.highlights, tags)
    puts "#{store.entries.count} highlights to import"

    puts "Tags: #{tags.empty? ? 'no tags' : tags.join(', ')}"

    if options[:dry_run]
      puts 'Dry run, no highlight imported'
    else
      entries = store.save!
      puts "#{entries.count} highlights imported with tags"

      if options[:archive]
        path = kindle.archive_highlights!
        puts "Highlights archived at #{path}"
      end

      kindle.clear_highlights!
      puts 'Highlights cleared from device'
    end
  end
end