Sha256: c2983601484ebb9628a939944214cda606e4c3198c99f91c6b6a680d2bf86248

Contents?: true

Size: 1.22 KB

Versions: 69

Compression:

Stored size: 1.22 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 newline
        info ""
      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

69 entries across 69 versions & 1 rubygems

Version Path
licensed-5.0.0 lib/licensed/ui/shell.rb
licensed-4.5.0 lib/licensed/ui/shell.rb
licensed-4.4.0 lib/licensed/ui/shell.rb
licensed-4.3.1 lib/licensed/ui/shell.rb
licensed-4.3.0 lib/licensed/ui/shell.rb
licensed-4.2.0 lib/licensed/ui/shell.rb
licensed-4.1.0 lib/licensed/ui/shell.rb
licensed-4.0.4 lib/licensed/ui/shell.rb
licensed-4.0.3 lib/licensed/ui/shell.rb
licensed-4.0.2 lib/licensed/ui/shell.rb
licensed-4.0.1 lib/licensed/ui/shell.rb
licensed-4.0.0 lib/licensed/ui/shell.rb
licensed-3.9.1 lib/licensed/ui/shell.rb
licensed-3.9.0 lib/licensed/ui/shell.rb
licensed-3.8.0 lib/licensed/ui/shell.rb
licensed-3.7.5 lib/licensed/ui/shell.rb
licensed-3.7.4 lib/licensed/ui/shell.rb
licensed-3.7.3 lib/licensed/ui/shell.rb
licensed-3.7.2 lib/licensed/ui/shell.rb
licensed-3.7.1 lib/licensed/ui/shell.rb