Class: Gerrit::UI

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/gerrit/ui.rb

Overview

Manages all interaction with the user.

Instance Method Summary collapse

Constructor Details

#initialize(input, output) ⇒ UI

Creates a Gerrit::UI that mediates between the given input/output streams.

Parameters:



15
16
17
18
19
20
# File 'lib/gerrit/ui.rb', line 15

def initialize(input, output)
  @input = input
  @output = output
  @pastel = Pastel.new
  @shell = TTY::Shell.new
end

Instance Method Details

#bold(*args, **kwargs) ⇒ Object

Print output in bold face.

Parameters:

  • args (Array)
  • kwargs (Hash)


44
45
46
# File 'lib/gerrit/ui.rb', line 44

def bold(*args, **kwargs)
  print(@pastel.bold(*args), **kwargs)
end

#bold_error(*args, **kwargs) ⇒ Object

Print the specified output in a bold face and color indicative of error.

Parameters:

  • args (Array)
  • kwargs (Hash)


60
61
62
# File 'lib/gerrit/ui.rb', line 60

def bold_error(*args, **kwargs)
  print(@pastel.bold.red(*args), **kwargs)
end

#error(args, **kwargs) ⇒ Object

Print the specified output in a color indicative of error.

Parameters:

  • args (Array)
  • kwargs (Hash)


52
53
54
# File 'lib/gerrit/ui.rb', line 52

def error(args, **kwargs)
  print(@pastel.red(*args), **kwargs)
end

#info(*args, **kwargs) ⇒ Object

Print the specified output in a color indicating information.

Parameters:

  • args (Array)
  • kwargs (Hash)


84
85
86
# File 'lib/gerrit/ui.rb', line 84

def info(*args, **kwargs)
  print(@pastel.cyan(*args), **kwargs)
end

#newlineObject

Print a blank line.



89
90
91
# File 'lib/gerrit/ui.rb', line 89

def newline
  print('')
end

Print the specified output.

Parameters:

  • output (String)
  • newline (Boolean) (defaults to: true)

    whether to append a newline



35
36
37
38
# File 'lib/gerrit/ui.rb', line 35

def print(output, newline: true)
  @output.print(output)
  @output.print("\n") if newline
end

#spinner(*args, &block) ⇒ Object

Execute a command with a spinner animation until it completes.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/gerrit/ui.rb', line 94

def spinner(*args, &block)
  spinner = TTY::Spinner.new(*args)
  spinner_thread = Thread.new do
    loop do
      sleep 0.1
      spinner.spin
    end
  end

  block.call
ensure
  spinner_thread.kill
  newline # Ensure next line of ouptut on separate line from spinner
end

#success(*args, **kwargs) ⇒ Object

Print the specified output in a color indicative of success.

Parameters:

  • args (Array)
  • kwargs (Hash)


68
69
70
# File 'lib/gerrit/ui.rb', line 68

def success(*args, **kwargs)
  print(@pastel.green(*args), **kwargs)
end

#table(options = {}, &block) ⇒ Object

Prints a table.

Customize the table by passing a block and operating on the table object passed to that block to add rows and customize its appearance.



113
114
115
116
117
# File 'lib/gerrit/ui.rb', line 113

def table(options = {}, &block)
  t = TTY::Table.new(options)
  block.call(t)
  print(t.render(:unicode, options))
end

#user_inputString?

Get user input, stripping extraneous whitespace.

Returns:

  • (String, nil)


25
26
27
28
29
# File 'lib/gerrit/ui.rb', line 25

def user_input
  if input = @input.get
    input.strip
  end
end

#warning(*args, **kwargs) ⇒ Object

Print the specified output in a color indicative of a warning.

Parameters:

  • args (Array)
  • kwargs (Hash)


76
77
78
# File 'lib/gerrit/ui.rb', line 76

def warning(*args, **kwargs)
  print(@pastel.yellow(*args), **kwargs)
end