AndreyAntipov/iterm2-viewer

View on GitHub
bin/iterm2-viewer

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
# encoding: utf-8

# Setup patches
lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
$LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)

require 'iterm2/viewer'

module Iterm2
  # CLI interface for view images in terminal
  module Viewer
    # Common methods
    # @param [String, #arguments] routes to files
    # @todo write tests
    class << self
      def show(arguments)
        # initialize whenever application called from iTerm2 with arguments
        return unless ENV['TERM_PROGRAM'] == 'iTerm.app' || !arguments.nil?

        # catch options
        if arguments[0] =~ /^(-\w|--\w{2,}?)$/
          # @todo Refactoring is needed
          # @todo Notification, command not found
          version if arguments[0].to_s == '--version' || arguments[0].to_s == '-v'
        # switch to renders
        else
          # clean entire screen
          # @todo Optimize (fit to enitre screen)

          # prerapre  media files for render
          render arguments
        end
      end

      # Preare routes argumetns to render
      #
      # @param routes [Array] parameters with path to file
      def render(objects)
        return if objects.nil?
        print "\e[2J\e[f"
        objects.each do |o|
          begin
            Render.new(Media.new o).reveal
          rescue ArgumentError
            next
          end
        end
      end

      # Display current version of application
      #
      # @return [String] Constant of version of application
      def version
        abort VERSION
      end
    end
  end
end

# Run application with shell arguments
Iterm2::Viewer.show ARGV