Class: Arcanus::UI

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

Overview

Manages all interaction with the user.

Instance Method Summary collapse

Constructor Details

#initialize(input, output) ⇒ UI

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

Parameters:


18
19
20
21
22
23
# File 'lib/arcanus/ui.rb', line 18

def initialize(input, output)
  @input = input
  @output = output
  @pastel = Pastel.new(enabled: output.tty?)
  @prompt = TTY::Prompt.new
end

Instance Method Details

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

Print output in bold face.

Parameters:

  • args (Array)
  • kwargs (Hash)

62
63
64
# File 'lib/arcanus/ui.rb', line 62

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)

78
79
80
# File 'lib/arcanus/ui.rb', line 78

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)

70
71
72
# File 'lib/arcanus/ui.rb', line 70

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)

102
103
104
# File 'lib/arcanus/ui.rb', line 102

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

#newlineObject

Print a blank line.


107
108
109
# File 'lib/arcanus/ui.rb', line 107

def newline
  print('')
end

Print the specified output.

Parameters:

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

    whether to append a newline


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

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

#secret_user_inputString?

Get user input without echoing (useful for passwords).

Does not strip extraneous whitespace (since it could be part of password).

Returns:

  • (String, nil)

41
42
43
44
45
46
47
# File 'lib/arcanus/ui.rb', line 41

def secret_user_input
  if input = @input.get(noecho: true)
    input.chomp # Remove trailing newline as it is not part of password
  end
rescue Interrupt
  exit 130 # User cancelled
end

#spinner(*args) ⇒ Object

Execute a command with a spinner animation until it completes.


112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/arcanus/ui.rb', line 112

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

  yield
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)

86
87
88
# File 'lib/arcanus/ui.rb', line 86

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

#table(options = {}) {|t| ... } ⇒ 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.

Yields:

  • (t)

131
132
133
134
135
# File 'lib/arcanus/ui.rb', line 131

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

#user_inputString?

Get user input, stripping extraneous whitespace.

Returns:

  • (String, nil)

28
29
30
31
32
33
34
# File 'lib/arcanus/ui.rb', line 28

def user_input
  if input = @input.get
    input.strip
  end
rescue Interrupt
  exit 130 # User cancelled
end

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

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

Parameters:

  • args (Array)
  • kwargs (Hash)

94
95
96
# File 'lib/arcanus/ui.rb', line 94

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