Sha256: 81c16249a1ca5a04e5f094e80da18f77ce0ac1a7a179ed2bd07d5a3e3a0f4b62

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

# (c) Copyright 2020 Ribose Inc.
#

module Iev
  module Cli
    # @todo
    #   Make it thread-safe.  Currently, calling UI methods from different
    #   threads may result with mangled output.  At first glance it seems like
    #   something is wrong with carriage returns, but more research is needed.
    module Ui
      module_function

      def debug(*args)
        Helper.cli_out(:debug, *args)
      end

      def warn(*args)
        Helper.cli_out(:warn, *args)
      end

      # Prints progress message which will be replaced on next call.
      def progress(message)
        return unless $IEV_PROGRESS

        print "#{Helper.clear_progress}#{message} "
      end

      # Prints generic message.
      def info(message)
        print "#{Helper.clear_progress}#{message}\n"
      end

      # Sets an UI tag which will be prepended to messages printed with
      # #debug and #warn.
      def set_ui_tag(str)
        Thread.current[:iev_ui_tag] = str
      end

      module Helper
        module_function

        def clear_progress
          $IEV_PROGRESS ? "\r#{' ' * 40}\r" : ""
        end

        def cli_out(level, *args)
          topic = args[0].is_a?(Symbol) ? args.shift : nil
          message = args.map(&:to_s).join(" ").chomp
          ui_tag = Thread.current[:iev_ui_tag]

          return unless should_out?(level, topic)

          print [
            clear_progress,
            ui_tag,
            ui_tag && ": ",
            message,
            "\n",
          ].join
        end

        def should_out?(level, topic)
          topic.nil? || level == :warn || $IEV_DEBUG[topic]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
iev-0.3.5 lib/iev/cli/ui.rb