Sha256: 6f94c130dc28643700475879cb722ab022c780fdd4bfaea85cc16e309b81454f

Contents?: true

Size: 1.75 KB

Versions: 4

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

require_relative '../models/color_theme'
require_relative '../services/stderr_redirector_service'
require_relative '../views/shared/error'
require_relative 'color_themable'

module Dsu
  module Support
    module CommandHookable
      class << self
        def included(base)
          base.extend(ColorThemable)
          base.extend(ClassMethods)
        end
      end

      module ClassMethods
        def start(args = ARGV, options = {})
          display_dsu_header unless suspend_header?(args, options)
          stderror = Services::StderrRedirectorService.call do
            super
          end
          display_errors_if(stderror)
          display_dsu_footer
        end

        def display_dsu_header
          puts apply_theme("Dsu v#{Dsu::VERSION}", theme_color: color_theme.dsu_header)
          puts
        end

        private

        def suspend_header?(args, _options)
          return false unless args.count > 1

          # TODO: I18n?
          true if args[0] == 'theme' && %w[use delete].include?(args[1])
        end

        def display_dsu_footer
          puts apply_theme('_' * 35, theme_color: color_theme.dsu_footer)
          # TODO: I18n.
          footer = apply_theme("Theme: #{color_theme.theme_name}", theme_color: color_theme.dsu_footer)
          puts footer
        end

        def display_errors_if(stderror_string)
          stderror_string = stderror_string.strip
          return unless stderror_string.present?

          errors = stderror_string.split("\n").map(&:strip)
          Views::Shared::Error.new(messages: errors, options: options.merge({ ordered_list: false })).render
        end

        def color_theme
          Models::ColorTheme.current_or_default
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dsu-2.1.4 lib/dsu/support/command_hookable.rb
dsu-2.1.3 lib/dsu/support/command_hookable.rb
dsu-2.1.2 lib/dsu/support/command_hookable.rb
dsu-2.1.1 lib/dsu/support/command_hookable.rb