Sha256: 1e864ff6cc077bbaa3b985e9335efc63d3bf0bb75a4f2d4b0ac14043cea6644f

Contents?: true

Size: 1.55 KB

Versions: 16

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

require 'thor'
require 'listen'
require 'logger'

module Listen
  class CLI < Thor
    default_task :start

    desc 'start', 'Starts Listen'

    class_option :verbose,
                 type:    :boolean,
                 default: false,
                 aliases: '-v',
                 banner:  'Verbose'

    class_option :directory,
                 type:    :array,
                 default: '.',
                 aliases: '-d',
                 banner:  'The directory to listen to'

    class_option :relative,
                 type:    :boolean,
                 default: false,
                 aliases: '-r',
                 banner:  'Convert paths relative to current directory'

    def start
      Listen::Forwarder.new(options).start
    end
  end

  class Forwarder
    attr_reader :logger

    def initialize(options)
      @options = options
      @logger = ::Logger.new(STDOUT, level: ::Logger::INFO)
      @logger.formatter = proc { |_, _, _, msg| "#{msg}\n" }
    end

    def start
      logger.info 'Starting listen...'

      directory = @options[:directory]
      relative = @options[:relative]
      callback = proc do |modified, added, removed|
        if @options[:verbose]
          logger.info "+ #{added}" unless added.empty?
          logger.info "- #{removed}" unless removed.empty?
          logger.info "> #{modified}" unless modified.empty?
        end
      end

      listener = Listen.to(directory, relative: relative, &callback)

      listener.start

      sleep 0.5 while listener.processing?
    end
  end
end

Version data entries

16 entries across 15 versions & 3 rubygems

Version Path
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/listen-3.7.1/lib/listen/cli.rb
listen-3.7.1 lib/listen/cli.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/listen-3.7.0/lib/listen/cli.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/gems/listen-3.7.0/lib/listen/cli.rb
listen-3.7.0 lib/listen/cli.rb
listen-3.6.0 lib/listen/cli.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/3.0.0/gems/listen-3.5.1/lib/listen/cli.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/2.7.0/gems/listen-3.3.3/lib/listen/cli.rb
listen-3.5.1 lib/listen/cli.rb
listen-3.5.0 lib/listen/cli.rb
listen-3.4.1 lib/listen/cli.rb
listen-3.4.0 lib/listen/cli.rb
listen-3.3.4 lib/listen/cli.rb
vagrant-unbundled-2.2.14.0 vendor/bundle/ruby/2.7.0/gems/listen-3.3.3/lib/listen/cli.rb
listen-3.3.3 lib/listen/cli.rb
listen-3.3.2 lib/listen/cli.rb