Class: Morpheus::Cli::ColoringCommand

Inherits:
Object
  • Object
show all
Includes:
CliCommand
Defined in:
lib/morpheus/cli/commands/standard/coloring_command.rb

Overview

This is for use in dotfile scripts It allows you to turn colors on or off globally

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from CliCommand

#apply_options, #build_common_options, #build_option_type_options, #build_standard_add_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #get_subcommand_description, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_bytes_param, #parse_id_list, #parse_list_options, #parse_list_subtitles, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands

Instance Method Details

#handle(args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/morpheus/cli/commands/standard/coloring_command.rb', line 12

def handle(args)
  append_newline = true
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = "Usage: morpheus #{command_name} [on|off]"
    #build_common_options(opts, options, [])
    opts.on('-q','--quiet', "No Output, do not print to stdout") do
      options[:quiet] = true
    end
    opts.on('-h', '--help', "Print this help" ) do
      puts opts
      exit
    end
    opts.footer = "Enable [on] or Disable [off] ANSI Colors for all output.\n" + 
                  "Use [on?] or [off?] to print the current value and exit accordingly." + "\n" +
                  "Passing no arguments is the same as `#{command_name} on`"
  end
  optparse.parse!(args)
  if args.count > 1
    raise_command_error "wrong number of arguments, expected 0-1 and got (#{args.count}) #{args.join(' ')}\n#{optparse}"
  end
  
  coloring_was_enabled = Term::ANSIColor::coloring?
  exit_code = 0

  if args.count == 0
    # no args means turn it on.
    Term::ANSIColor::coloring = true
  else
    subcmd = args[0].to_s.strip
    if subcmd == "on"
      Term::ANSIColor::coloring = true
    elsif subcmd == "off"
      Term::ANSIColor::coloring = false
      
    elsif subcmd == "on?"
      exit_code = Term::ANSIColor::coloring? ? 0 : 1
    elsif subcmd == "off?"
      exit_code = Term::ANSIColor::coloring? ? 1 : 0
    else
      puts optparse
      return 127
    end
  end
  unless options[:quiet]
    if Term::ANSIColor::coloring?
      if coloring_was_enabled == false
        Morpheus::Logging::DarkPrinter.puts "coloring enabled" if Morpheus::Logging.debug?
        recalculate_after_color_change()
      end
      puts "#{cyan}coloring: #{bold}#{green}on#{reset}"
    else
      if coloring_was_enabled == true
        Morpheus::Logging::DarkPrinter.puts "coloring disabled" if Morpheus::Logging.debug?
        recalculate_after_color_change()
      end
      puts "coloring: off"
    end
  end
  return exit_code
end