Module: Crayons
- Defined in:
- lib/rvvm/crayons.rb
Overview
Utility module providing an API to run shell commands with spinners and stdout mesage formatting.
Class Method Summary collapse
-
.command(tag, cmd, ignore_errors: false) ⇒ void
Runs a shell command with a spinner.
-
.init ⇒ void
Initializes Crayons module.
-
.log_error(message) ⇒ void
Logs a red text message to stdout.
-
.log_pass(message) ⇒ void
Logs a green text message to stdout.
-
.spinner_log(message) ⇒ void
Logs a message during spinner execution removing the need to manually pause and resume spinner.
-
.spinner_pause ⇒ void
Pauses spinner thread execution to enable uninterrupted stdout outputs.
-
.spinner_resume ⇒ void
Resumes spinner thread execution.
-
.spinner_running? ⇒ Boolean
Returns spinner status.
-
.spinner_start(message) ⇒ void
Starts a cli spinner appending the provided message.
-
.spinner_stop(message = nil, exit) ⇒ void
Stops spinner printing a formated status message.
Class Method Details
.command(tag, cmd, ignore_errors: false) ⇒ void
This method returns an undefined value.
Runs a shell command with a spinner.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/rvvm/crayons.rb', line 143 def self.command(tag, cmd, ignore_errors: false) spinner_start(tag) out = @command.run!(cmd) spinner_log("#{out.out}\n") spinner_stop(nil, out.success?) puts "" exit(1) unless out.success? && !ignore_errors rescue Errno::ENOENT spinner_stop(nil, false) log_error("\nError: '#{cmd.split.first}' command not found!\n") exit(1) unless ignore_errors rescue StandardError => e spinner_stop(nil, false) log_error("\nAn unexpected error ocurred:\n\n#{e.message}\n") exit(1) unless ignore_errors end |
.init ⇒ void
This method returns an undefined value.
Initializes Crayons module.
47 48 49 |
# File 'lib/rvvm/crayons.rb', line 47 def self.init @command = TTY::Command.new(printer: :null) end |
.log_error(message) ⇒ void
This method returns an undefined value.
Logs a red text message to stdout.
179 180 181 |
# File 'lib/rvvm/crayons.rb', line 179 def self.log_error() puts .red end |
.log_pass(message) ⇒ void
This method returns an undefined value.
Logs a green text message to stdout.
168 169 170 |
# File 'lib/rvvm/crayons.rb', line 168 def self.log_pass() puts .green end |
.spinner_log(message) ⇒ void
This method returns an undefined value.
Logs a message during spinner execution removing the need to manually pause and resume spinner.
If a spinner is not running simply prints message.
128 129 130 131 132 |
# File 'lib/rvvm/crayons.rb', line 128 def self.spinner_log() spinner_pause if @spinner_running puts spinner_resume if @spinner_running end |
.spinner_pause ⇒ void
This method returns an undefined value.
Pauses spinner thread execution to enable uninterrupted stdout outputs.
83 84 85 86 87 88 89 |
# File 'lib/rvvm/crayons.rb', line 83 def self.spinner_pause return unless @spinner_running print "\r\e[K" @thread_paused = true print "\r\e[K" end |
.spinner_resume ⇒ void
This method returns an undefined value.
Resumes spinner thread execution.
96 97 98 |
# File 'lib/rvvm/crayons.rb', line 96 def self.spinner_resume @thread_paused = false if @spinner_running end |
.spinner_running? ⇒ Boolean
Returns spinner status.
54 55 56 |
# File 'lib/rvvm/crayons.rb', line 54 def self.spinner_running? @spinner_running end |
.spinner_start(message) ⇒ void
This method returns an undefined value.
Starts a cli spinner appending the provided message.
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rvvm/crayons.rb', line 65 def self.spinner_start() print "\r\e[K" @spinner = TTY::Spinner.new("[:spinner] #{message}", interval: 5) @spinner_running = true @thread = Thread.new do @spinner.auto_spin while @thread_sleeping print "\r\e[K" sleep(0.1) end end end |
.spinner_stop(message = nil, exit) ⇒ void
This method returns an undefined value.
Stops spinner printing a formated status message.
108 109 110 111 112 113 114 115 116 |
# File 'lib/rvvm/crayons.rb', line 108 def self.spinner_stop( = nil, exit) return unless @spinner_running sleep(0.1) = || (exit ? "Done!" : "Failed!") exit ? @spinner.success(.green) : @spinner.error(.red) @thread.join @spinner_running = false end |