# frozen_string_literal: true require_relative 'command' require_relative 'api_client' require 'dri/refinements/truncate' require "tty-config" require "pastel" require 'forwardable' module Dri class Command extend Forwardable def_delegators :command, :run def pastel(**options) Pastel.new(**options) end # Main configuration def config @config ||= begin config = TTY::Config.new config.filename = ".dri_profile" config.extname = ".yml" config.append_path Dir.pwd config.append_path Dir.home config end end def api_client ApiClient.new(config) end def profile @profile ||= config.read end def emoji @emoji ||= profile["settings"]["emoji"] end def username @username ||= profile["settings"]["user"] end def token @token ||= profile["settings"]["token"] end def timezone @timezone ||= profile["settings"]["timezone"] end def verify_config_exists return if config.exist? logger.error "Oops, could not find a configuration. Try using #{add_color('dri init', :yellow)} first." exit 1 end def add_color(str, *color) @options[:no_color] ? str : pastel.decorate(str, *color) end # Execute this command # # @api public def execute(*) raise( NotImplementedError, "#{self.class}##{__method__} must be implemented" ) end def logger require 'tty-logger' TTY::Logger.new end def spinner require 'tty-spinner' TTY::Spinner.new("[:spinner] ⏳", format: :classic) end # The external commands runner # # @see http://www.rubydoc.info/gems/tty-command # # @api public def command(**options) require 'tty-command' TTY::Command.new(options) end # The cursor movement # # @see http://www.rubydoc.info/gems/tty-cursor # # @api public def cursor require 'tty-cursor' TTY::Cursor end # Open a file or text in the user's preferred editor # # @see http://www.rubydoc.info/gems/tty-editor # # @api public def editor require 'tty-editor' TTY::Editor end # The interactive prompt # # @see http://www.rubydoc.info/gems/tty-prompt # # @api public def prompt(**options) require 'tty-prompt' TTY::Prompt.new(options) end end end