Sha256: ced1014a94d0a9c886d23a20120101d2eda6ba5db5fc55610025f6f8b64c6e94

Contents?: true

Size: 1.89 KB

Versions: 139

Compression:

Stored size: 1.89 KB

Contents

# frozen-string-literal: true
require('cli/ui')

module CLI
  module UI
    module Widgets
      class Status < Widgets::Base
        ARGPARSE_PATTERN = %r{
          \A (?<succeeded> \d+)
          :  (?<failed>    \d+)
          :  (?<working>   \d+)
          :  (?<pending>   \d+) \z
        }x # e.g. "1:23:3:404"
        OPEN  = Color::RESET.code + Color::BOLD.code + '[' + Color::RESET.code
        CLOSE = Color::RESET.code + Color::BOLD.code + ']' + Color::RESET.code
        ARROW = Color::RESET.code + Color::GRAY.code + '◂' + Color::RESET.code
        COMMA = Color::RESET.code + Color::GRAY.code + ',' + Color::RESET.code

        SPINNER_STOPPED = '⠿'
        EMPTY_SET = '∅'

        def render
          if zero?(@succeeded) && zero?(@failed) && zero?(@working) && zero?(@pending)
            Color::RESET.code + Color::BOLD.code + EMPTY_SET + Color::RESET.code
          else
            #   [          0✓            ,         2✗          ◂         3⠼           ◂         4⌛︎           ]
            "#{OPEN}#{succeeded_part}#{COMMA}#{failed_part}#{ARROW}#{working_part}#{ARROW}#{pending_part}#{CLOSE}"
          end
        end

        private

        def zero?(num_str)
          num_str == '0'
        end

        def colorize_if_nonzero(num_str, rune, color)
          color = Color::GRAY if zero?(num_str)
          color.code + num_str + rune
        end

        def succeeded_part
          colorize_if_nonzero(@succeeded, Glyph::CHECK.char, Color::GREEN)
        end

        def failed_part
          colorize_if_nonzero(@failed, Glyph::X.char, Color::RED)
        end

        def working_part
          rune = zero?(@working) ? SPINNER_STOPPED : Spinner.current_rune
          colorize_if_nonzero(@working, rune, Color::BLUE)
        end

        def pending_part
          colorize_if_nonzero(@pending, Glyph::HOURGLASS.char, Color::WHITE)
        end
      end
    end
  end
end

Version data entries

139 entries across 139 versions & 3 rubygems

Version Path
shopify-cli-2.36.0 vendor/deps/cli-ui/lib/cli/ui/widgets/status.rb
shopify-cli-2.35.0 vendor/deps/cli-ui/lib/cli/ui/widgets/status.rb
shopify-cli-2.34.0 vendor/deps/cli-ui/lib/cli/ui/widgets/status.rb
shopify-cli-2.33.1 vendor/deps/cli-ui/lib/cli/ui/widgets/status.rb
shopify-cli-2.33.0 vendor/deps/cli-ui/lib/cli/ui/widgets/status.rb
shopify-cli-2.32.1 vendor/deps/cli-ui/lib/cli/ui/widgets/status.rb
shopify-cli-2.32.0 vendor/deps/cli-ui/lib/cli/ui/widgets/status.rb
shopify-cli-2.31.0 vendor/deps/cli-ui/lib/cli/ui/widgets/status.rb
shopify-cli-2.30.0 vendor/deps/cli-ui/lib/cli/ui/widgets/status.rb
shopify-cli-2.29.0 vendor/deps/cli-ui/lib/cli/ui/widgets/status.rb
shopify-cli-2.28.0 vendor/deps/cli-ui/lib/cli/ui/widgets/status.rb
shopify-cli-2.27.0 vendor/deps/cli-ui/lib/cli/ui/widgets/status.rb
shopify-cli-2.26.0 vendor/deps/cli-ui/lib/cli/ui/widgets/status.rb
shopify-cli-2.25.0 vendor/deps/cli-ui/lib/cli/ui/widgets/status.rb
shopify-cli-2.24.0 vendor/deps/cli-ui/lib/cli/ui/widgets/status.rb
shopify-cli-2.23.0 vendor/deps/cli-ui/lib/cli/ui/widgets/status.rb
shopify-cli-2.22.0 vendor/deps/cli-ui/lib/cli/ui/widgets/status.rb
shopify-cli-2.21.0 vendor/deps/cli-ui/lib/cli/ui/widgets/status.rb
shopify-cli-2.20.1 vendor/deps/cli-ui/lib/cli/ui/widgets/status.rb
shopify-cli-2.20.0 vendor/deps/cli-ui/lib/cli/ui/widgets/status.rb