Sha256: 015557eb499ea5e7cbef4c28980f59b7ab312692bee39a1403626c4049078e47

Contents?: true

Size: 1.56 KB

Versions: 80

Compression:

Stored size: 1.56 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:  'One or more directories 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

80 entries across 80 versions & 4 rubygems

Version Path
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/cli.rb
listen-3.9.0 lib/listen/cli.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/cli.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/cli.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/cli.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/cli.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/cli.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/cli.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/cli.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/cli.rb
harbr-0.2.2 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/cli.rb
harbr-0.2.1 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/cli.rb
harbr-0.2.0 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/cli.rb
harbr-0.1.99 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/cli.rb
harbr-0.1.98 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/cli.rb
harbr-0.1.97 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/cli.rb
harbr-0.1.96 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/cli.rb
harbr-0.1.95 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/cli.rb
harbr-0.1.94 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/cli.rb
harbr-0.1.93 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/cli.rb