Class: Yetty::Ui

Inherits:
Object
  • Object
show all
Defined in:
lib/yetty/ui.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ self

Build new UI instance

Parameters:

  • args (Hash) (defaults to: {})

Options Hash (args):

  • :color (TrueClass, FalseClass)

    enable/disable colors

  • :output_to (IO)

    IO to write



15
16
17
18
# File 'lib/yetty/ui.rb', line 15

def initialize(args={})
  @colorize = args.fetch(:colors, true)
  @output_to = args.fetch(:output_to, $stdout)
end

Instance Attribute Details

#colorizeTruthy, Falsey

Returns:

  • (Truthy, Falsey)


7
8
9
# File 'lib/yetty/ui.rb', line 7

def colorize
  @colorize
end

Instance Method Details

#ask(question, default = nil) ⇒ String

Prompt for question and receive answer

Parameters:

  • question (String)
  • default (String) (defaults to: nil)

Returns:

  • (String)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/yetty/ui.rb', line 82

def ask(question, default=nil)
  string = question.dup
  if(default)
    string << " [#{default}]"
  end
  result = nil
  until(result)
    info "#{string}: ", :nonewline
    result = $stdin.gets.strip
    if(result.empty? && default)
      result = default
    end
    if(result.empty?)
      error 'Please provide a value'
      result = nil
    end
  end
  result
end

#color(string, *args) ⇒ String

Colorize string

Parameters:

  • string (String)
  • args (Symbol)

Returns:

  • (String)


73
74
75
# File 'lib/yetty/ui.rb', line 73

def color(string, *args)
  Paint[string, *args]
end

#error(string, *args) ⇒ String

Format error string

Parameters:

  • string (String)

Returns:

  • (String)


62
63
64
65
66
# File 'lib/yetty/ui.rb', line 62

def error(string, *args)
  output_method = args.include?(:nonewline) ? :print : :puts
  self.send(output_method, "#{color('[ERROR]:', :red, :bold)} #{string}")
  string
end

#info(string, *args) ⇒ String

Output information string

Parameters:

  • string (String)

Returns:

  • (String)


42
43
44
45
46
# File 'lib/yetty/ui.rb', line 42

def info(string, *args)
  output_method = args.include?(:nonewline) ? :print : :puts
  self.send(output_method, "#{color('[Yetty]:', :green)} #{string}")
  string
end

Output directly

Parameters:

  • string (String) (defaults to: '')

Returns:

  • (String)


33
34
35
36
# File 'lib/yetty/ui.rb', line 33

def print(string='')
  @output_to.print string
  string
end

#puts(string = '') ⇒ String

Output directly

Parameters:

  • string (String) (defaults to: '')

Returns:

  • (String)


24
25
26
27
# File 'lib/yetty/ui.rb', line 24

def puts(string='')
  @output_to.puts string
  string
end

#warn(string, *args) ⇒ String

Format warning string

Parameters:

  • string (String)

Returns:

  • (String)


52
53
54
55
56
# File 'lib/yetty/ui.rb', line 52

def warn(string, *args)
  output_method = args.include?(:nonewline) ? :print : :puts
  self.send(output_method, "#{color('[WARN]:', :yellow, :bold)} #{string}")
  string
end