Sha256: 26235d910f7270d9621e7c7b9c8e688cd34cef70a10cb12385c5a08ead2411d9

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

require "thor"

module Licensed
  module UI
    class Shell
      LEVELS = %w(silent error warn confirm info debug)

      def initialize
        @shell = STDOUT.tty? ? Thor::Base.shell.new : Thor::Shell::Basic.new
        @level = ENV['DEBUG'] ? "debug" : "info"
      end

      def debug(msg, newline = true)
        @shell.say msg, nil, newline if level?("debug")
      end

      def info(msg, newline = true)
        @shell.say msg, nil, newline if level?("info")
      end

      def confirm(msg, newline = true)
        @shell.say msg, :green, newline if level?("confirm")
      end

      def warn(msg, newline = true)
        @shell.say msg, :yellow, newline if level?("warn")
      end

      def error(msg, newline = true)
        @shell.say msg, :red, newline if level?("error")
      end

      def level=(level)
        raise ArgumentError unless LEVELS.include?(level.to_s)
        @level = level
      end

      def level?(name = nil)
        name ? LEVELS.index(name) <= LEVELS.index(@level) : @level
      end

      def silence
        old_level, @level = @level, "silent"
        yield
      ensure
        @level = old_level
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
licensed-0.11.1 lib/licensed/ui/shell.rb
licensed-0.11.0 lib/licensed/ui/shell.rb
licensed-0.10.0 lib/licensed/ui/shell.rb
licensed-0.6.0 lib/licensed/ui/shell.rb