Class: CliMiami::S

Inherits:
Object
  • Object
show all
Defined in:
lib/cli_miami/say.rb

Class Method Summary collapse

Class Method Details

.ay(text = '', options = {}) ⇒ Object

By preset [Symbol[…

symbol

Uses defined preset name

By options [Hash]… color: => [symbol] See README for ansi color codes bgcolor: => [symbol] See README for ansi color codes style: => [symbol] See README for ansi style codes justify: => [center|ljust|rjust] The type of justification to use padding: => [integer] The maximum string size to justify text in indent: => [integer] The number of characters to indent newline: => [boolean] True if you want a newline after the output overwrite: => [boolean] True if you want the next line to overwrite the current line

Parameters:

  • options (Symbol or Hash) (defaults to: {})

    options can be preset symbol, or a hash of options.

Returns:



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cli_miami/say.rb', line 29

def self.ay text = '', options = {}
  original_text = text

  # set default options
  @options = {
    :style => [],
    :newline => true
  }

  # merge preset options
  if options.is_a? Symbol
    @options.merge! CliMiami.presets[options]
  elsif preset = options.delete(:preset)
    @options.merge! CliMiami.presets[preset]
  end

  # merge remaining options
  if options.is_a? Hash
    @options.merge! options
  end

  # convert single style into an array for processing
  if !@options[:style].is_a? Array
    @options[:style] = [@options[:style]]
  end

  # Justify/Padding options
  if @options[:justify] && @options[:padding]
    text = text.send @options[:justify], @options[:padding]
  end

  # Set foreground color
  if @options[:color]
    # if bright style is passed, use the bright color variation
    text = if @options[:style].delete :bright
      text.send("bright_#{@options[:color]}")
    else
      text.send(@options[:color])
    end
  end

  # Set background color
  if @options[:bgcolor]
    text = text.send("on_#{@options[:bgcolor]}")
  end

  # Apply all styles
  @options[:style].each do |style|
    text = text.send(style)
  end

  # Indent
  if @options[:indent]
    text = (' ' * @options[:indent]) + text
  end

  # Flag text to be overwritten by next text written
  if @options[:overwrite]
    text = "#{text}\r"
  end

  # Determine if a newline should be used
  if !@options[:newline] || @options[:overwrite]
    $stdout.print text
  else
    $stdout.puts text
  end

  # return original text
  return original_text
end