Class: LgtmHD::CLI

Inherits:
Object
  • Object
show all
Includes:
Commander::Methods
Defined in:
lib/lgtm_hd/cli.rb

Constant Summary collapse

CMD_RANDOM_SYNTAX =
'lgtm_hd random [options]'.freeze
CMD_TRANSFORM_SYNTAX =
'lgtm_hd <URI|FILE> [options]'.freeze
OPTIONS_SYNTAX =
'[-i | --interactive] [-d | --destination <DIR>] [-p | --preview high|low|none]'

Instance Method Summary collapse

Instance Method Details

#help_the_noobieObject

end run def



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/lgtm_hd/cli.rb', line 104

def help_the_noobie
  say_step "\nTo add LGTM text to image:"
  say_code_block CLI::CMD_TRANSFORM_SYNTAX
  say_step "\nTo fetch random LGTM.IN image:"
  say_code_block CLI::CMD_RANDOM_SYNTAX
  say_step "\nGlobal Options:"
  say_code_block CLI::OPTIONS_SYNTAX
  say_step "\nMore Help:"
  say_code_block "lgtm_hd --help"
  say_step "\nVisit #{LgtmHD::Configuration::MORE_HELP_URL} for development purpose or more examples\n"
  exit
end

#runObject



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

def run
  program :name, LgtmHD::Configuration::PROGRAM_NAME
  program :version, LgtmHD::VERSION
  program :description, LgtmHD::Configuration::DESCRIPTION
  program :help_formatter, Commander::HelpFormatter::TerminalCompact

  default_command :transform
  global_option '-i', '--interactive', 'Turn on interactive Mode. In case you forgot all these super complexive args and options' do say "-- LGTM HD Interactive Mode --" end
  global_option '-d', '--dest DIR', String, 'Directory to export the LGTM image to. Default value is user\'s current working directory'
  global_option '-p', '--preview QUALITY', String, 'Quality of Image preview live on terminal at the end. Accepted values for QUALITY are [high, low, none]. Default value is high. Set to none for skipping'

  command :random do |c|
    c.syntax = CMD_RANDOM_SYNTAX
    c.summary = 'Fetch random images from LGTM.in'
    c.description = ''
    c.example 'Example', 'lgtm_hd random'

    c.action do |args, options|
      if options.interactive  # Interactive mode!
        options.dest ||= ask('Destination Directory (Enter to skip): ')
        options.preview ||= ask('Image Preview Quality? [high|low|none] (Enter to skip)')
      end
      dest_dir = CLI.destination_dir(options.dest)
      dest_file_prefix = CLI.destination_file_prefix
      check_uris(dest_dir)

      say_step "Fetching from lgtm.in"
      dest_uri,image_markdown = LgtmDotIn.fetch_random_image(dest_dir,dest_file_prefix) do |url, markdown|
        say_step "Loading image at #{url}"
      end

      say_ok "Exported image to #{dest_uri}"

      show_preview dest_uri, options.preview

      copy_file_to_clipboard(dest_uri)
      say "\nOr you can copy the markdown format below provided by lgtm.in"
      say_code_block "#{image_markdown}"
      say "\nIf the image does not have LGTM texts on it, run the cmd below"
      say_code_block "lgtm_hd #{dest_uri}"

    end
  end

  command :transform do |c|
    c.syntax = CMD_TRANSFORM_SYNTAX
    c.summary = 'Generate LGTM text on top of image URL or local image File'
    c.description = 'The command \e[3mtransform\e[0m is the default command hence you can skip typing it instead'
    c.example 'Example', 'lgtm_hd http://domain.com/image.png'

    c.action do |args, options|
      # ARGS validation!
      if args.length >= 1
        source_uri = args[0]
      elsif options.interactive  # Interactive mode!
        source_uri ||= ask('Source Image (URL or Path/to/file): ')
        options.dest ||= ask('Destination Directory (Enter to skip): ')
        options.preview ||= ask('Image Preview Quality [high|low|none] (Enter to skip): ')
      else
        # Since this is the default command so we will provide a little extra care for first-time user
        help_the_noobie
      end

      # Validate the inputs
      source_uri = CLI.source_uri(source_uri)
      dest_dir = CLI.destination_dir(options.dest)
      check_uris(dest_dir, source_uri)
      dest_file = File.join(dest_dir, CLI.destination_file_prefix + CLI.extname(source_uri))

      # Do stuff with our LGTM meme
      say_step "Reading and inspecting source at #{source_uri}"
      meme_generator = MemeGenerator.new(input_image_uri:source_uri, output_image_uri:dest_file)
      say_step "Transforming Image"
      meme_generator.draw

      # Export and play around with the clipboard
      say_step "Exporting to file"
      meme_generator.export do |output|
        say_ok "Exported LGTM image to #{output}."
        show_preview output, options.preview
        copy_file_to_clipboard output
      end # end of meme_generator.export block
    end # end of action
  end # end of command transform

  run!
end

#show_preview(image_path, quality) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/lgtm_hd/cli.rb', line 117

def show_preview(image_path, quality)
  quality ||= 'high'
  quality.downcase!
  if quality.eql? "none" then return end
  quality = 'high' unless quality.eql? 'low'

  say_step "\nImage Preview"
  CatpixMini::print_image image_path, {
    :limit_y => 1.0,
    :resolution => quality,
    :center_x => false,
    :center_y => true
  }
end