Sha256: cc8ec9f575f2d2b7803c7e72e6f860d2f1dd575f6390fd50e5ccf5b35ad43034

Contents?: true

Size: 1.18 KB

Versions: 12

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true
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

12 entries across 12 versions & 1 rubygems

Version Path
licensed-1.5.2 lib/licensed/ui/shell.rb
licensed-1.5.1 lib/licensed/ui/shell.rb
licensed-1.4.0 lib/licensed/ui/shell.rb
licensed-1.3.4 lib/licensed/ui/shell.rb
licensed-1.3.3 lib/licensed/ui/shell.rb
licensed-1.3.2 lib/licensed/ui/shell.rb
licensed-1.3.1 lib/licensed/ui/shell.rb
licensed-1.3.0 lib/licensed/ui/shell.rb
licensed-1.2.0 lib/licensed/ui/shell.rb
licensed-1.1.0 lib/licensed/ui/shell.rb
licensed-1.0.1 lib/licensed/ui/shell.rb
licensed-1.0.0 lib/licensed/ui/shell.rb