Class: Arcanus::UI
Overview
Manages all interaction with the user.
Instance Method Summary collapse
-
#bold(*args, **kwargs) ⇒ Object
Print output in bold face.
-
#bold_error(*args, **kwargs) ⇒ Object
Print the specified output in a bold face and color indicative of error.
-
#error(args, **kwargs) ⇒ Object
Print the specified output in a color indicative of error.
-
#info(*args, **kwargs) ⇒ Object
Print the specified output in a color indicating information.
-
#initialize(input, output) ⇒ UI
constructor
Creates a UI that mediates between the given input/output streams.
-
#newline ⇒ Object
Print a blank line.
-
#print(output, newline: true) ⇒ Object
Print the specified output.
-
#secret_user_input ⇒ String?
Get user input without echoing (useful for passwords).
-
#spinner(*args) ⇒ Object
Execute a command with a spinner animation until it completes.
-
#success(*args, **kwargs) ⇒ Object
Print the specified output in a color indicative of success.
-
#table(options = {}) {|t| ... } ⇒ Object
Prints a table.
-
#user_input ⇒ String?
Get user input, stripping extraneous whitespace.
-
#warning(*args, **kwargs) ⇒ Object
Print the specified output in a color indicative of a warning.
Constructor Details
#initialize(input, output) ⇒ UI
Creates a Arcanus::UI that mediates between the given input/output streams.
15 16 17 18 19 20 |
# File 'lib/arcanus/ui.rb', line 15 def initialize(input, output) @input = input @output = output @pastel = Pastel.new(enabled: output.tty?) @shell = TTY::Shell.new end |
Instance Method Details
#bold(*args, **kwargs) ⇒ Object
Print output in bold face.
59 60 61 |
# File 'lib/arcanus/ui.rb', line 59 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.
75 76 77 |
# File 'lib/arcanus/ui.rb', line 75 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.
67 68 69 |
# File 'lib/arcanus/ui.rb', line 67 def error(args, **kwargs) print(@pastel.red(*args), **kwargs) end |
#info(*args, **kwargs) ⇒ Object
Print the specified output in a color indicating information.
99 100 101 |
# File 'lib/arcanus/ui.rb', line 99 def info(*args, **kwargs) print(@pastel.cyan(*args), **kwargs) end |
#newline ⇒ Object
Print a blank line.
104 105 106 |
# File 'lib/arcanus/ui.rb', line 104 def newline print('') end |
#print(output, newline: true) ⇒ Object
Print the specified output.
50 51 52 53 |
# File 'lib/arcanus/ui.rb', line 50 def print(output, newline: true) @output.print(output) @output.print("\n") if newline end |
#secret_user_input ⇒ String?
Get user input without echoing (useful for passwords).
Does not strip extraneous whitespace (since it could be part of password).
38 39 40 41 42 43 44 |
# File 'lib/arcanus/ui.rb', line 38 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.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/arcanus/ui.rb', line 109 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.
83 84 85 |
# File 'lib/arcanus/ui.rb', line 83 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.
128 129 130 131 132 |
# File 'lib/arcanus/ui.rb', line 128 def table( = {}) t = TTY::Table.new() yield t print(t.render(:unicode, )) end |
#user_input ⇒ String?
Get user input, stripping extraneous whitespace.
25 26 27 28 29 30 31 |
# File 'lib/arcanus/ui.rb', line 25 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.
91 92 93 |
# File 'lib/arcanus/ui.rb', line 91 def warning(*args, **kwargs) print(@pastel.yellow(*args), **kwargs) end |