Class: Thor::Shell::HTML

Inherits:
Basic
  • Object
show all
Defined in:
lib/thor/shell/html.rb

Overview

Inherit from Thor::Shell::Basic and add set_color behavior. Check Thor::Shell::Basic to see all available methods.

Constant Summary collapse

BOLD =

The start of an HTML bold sequence.

"font-weight: bold"
BLACK =

Set the terminal’s foreground HTML color to black.

"color: black"
RED =

Set the terminal’s foreground HTML color to red.

"color: red"
GREEN =

Set the terminal’s foreground HTML color to green.

"color: green"
YELLOW =

Set the terminal’s foreground HTML color to yellow.

"color: yellow"
BLUE =

Set the terminal’s foreground HTML color to blue.

"color: blue"
MAGENTA =

Set the terminal’s foreground HTML color to magenta.

"color: magenta"
CYAN =

Set the terminal’s foreground HTML color to cyan.

"color: cyan"
WHITE =

Set the terminal’s foreground HTML color to white.

"color: white"
ON_BLACK =

Set the terminal’s background HTML color to black.

"background-color: black"
ON_RED =

Set the terminal’s background HTML color to red.

"background-color: red"
ON_GREEN =

Set the terminal’s background HTML color to green.

"background-color: green"
ON_YELLOW =

Set the terminal’s background HTML color to yellow.

"background-color: yellow"
ON_BLUE =

Set the terminal’s background HTML color to blue.

"background-color: blue"
ON_MAGENTA =

Set the terminal’s background HTML color to magenta.

"background-color: magenta"
ON_CYAN =

Set the terminal’s background HTML color to cyan.

"background-color: cyan"
ON_WHITE =

Set the terminal’s background HTML color to white.

"background-color: white"

Constants inherited from Basic

Basic::DEFAULT_TERMINAL_WIDTH

Instance Attribute Summary

Attributes inherited from Basic

#base, #padding

Instance Method Summary collapse

Methods inherited from Basic

#as_unicode, #ask_filtered, #ask_simply, #dynamic_width, #dynamic_width_stty, #dynamic_width_tput, #error, #file_collision, #file_collision_help, #indent, #initialize, #is?, #lookup_color, #mute, #mute?, #no?, #prepare_message, #print_in_columns, #print_table, #print_wrapped, #quiet?, #say, #say_status, #stderr, #stdout, #terminal_width, #truncate, #unix?, #yes?

Constructor Details

This class inherits a constructor from Thor::Shell::Basic

Instance Method Details

#ask(statement, color = nil) ⇒ Object

Ask something to the user and receives a response.

Example

ask(“What is your name?”)

TODO: Implement #ask for Thor::Shell::HTML

Raises:

  • (NotImplementedError)


70
71
72
# File 'lib/thor/shell/html.rb', line 70

def ask(statement, color = nil)
  raise NotImplementedError, "Implement #ask for Thor::Shell::HTML"
end

#can_display_colors?Boolean (protected)

Returns:

  • (Boolean)


76
77
78
# File 'lib/thor/shell/html.rb', line 76

def can_display_colors?
  true
end

#diff_lcs_loaded?Boolean (protected)

Check if Diff::LCS is loaded. If it is, use it to create pretty output for diff.

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
122
123
# File 'lib/thor/shell/html.rb', line 113

def diff_lcs_loaded? #:nodoc:
  return true if defined?(Diff::LCS)
  return @diff_lcs_loaded unless @diff_lcs_loaded.nil?

  @diff_lcs_loaded = begin
    require "diff/lcs"
    true
  rescue LoadError
    false
  end
end

#output_diff_line(diff) ⇒ Object (protected)

:nodoc:



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/thor/shell/html.rb', line 96

def output_diff_line(diff) #:nodoc:
  case diff.action
  when "-"
    say "- #{diff.old_element.chomp}", :red, true
  when "+"
    say "+ #{diff.new_element.chomp}", :green, true
  when "!"
    say "- #{diff.old_element.chomp}", :red, true
    say "+ #{diff.new_element.chomp}", :green, true
  else
    say "  #{diff.old_element.chomp}", nil, true
  end
end

#set_color(string, *colors) ⇒ Object

Set color by using a string or one of the defined constants. If a third option is set to true, it also adds bold to the string. This is based on Highline implementation and it automatically appends CLEAR to the end of the returned String.



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/thor/shell/html.rb', line 51

def set_color(string, *colors)
  if colors.all? { |color| color.is_a?(Symbol) || color.is_a?(String) }
    html_colors = colors.map { |color| lookup_color(color) }
    "<span style=\"#{html_colors.join('; ')};\">#{string}</span>"
  else
    color, bold = colors
    html_color = self.class.const_get(color.to_s.upcase) if color.is_a?(Symbol)
    styles = [html_color]
    styles << BOLD if bold
    "<span style=\"#{styles.join('; ')};\">#{string}</span>"
  end
end

#show_diff(destination, content) ⇒ Object (protected)

Overwrite show_diff to show diff with colors if Diff::LCS is available.



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/thor/shell/html.rb', line 83

def show_diff(destination, content) #:nodoc:
  if diff_lcs_loaded? && ENV["THOR_DIFF"].nil? && ENV["RAILS_DIFF"].nil?
    actual  = File.binread(destination).to_s.split("\n")
    content = content.to_s.split("\n")

    Diff::LCS.sdiff(actual, content).each do |diff|
      output_diff_line(diff)
    end
  else
    super
  end
end